| 2222 | #define HTTP_REQUEST_FILE 1 |
| 2223 | |
| 2224 | static int http_request(const char *url, |
| 2225 | void *result, int target, |
| 2226 | struct http_get_options *options) |
| 2227 | { |
| 2228 | struct active_request_slot *slot; |
| 2229 | struct slot_results results = { .retry_after = -1 }; |
| 2230 | struct curl_slist *headers = http_copy_default_headers(); |
| 2231 | struct strbuf buf = STRBUF_INIT; |
| 2232 | const char *accept_language; |
| 2233 | int ret; |
| 2234 | |
| 2235 | slot = get_active_slot(); |
| 2236 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1L); |
| 2237 | |
| 2238 | if (!result) { |
| 2239 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1L); |
| 2240 | } else { |
| 2241 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0L); |
| 2242 | curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, result); |
| 2243 | |
| 2244 | if (target == HTTP_REQUEST_FILE) { |
| 2245 | off_t posn = ftello(result); |
| 2246 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, |
| 2247 | fwrite); |
| 2248 | if (posn > 0) |
| 2249 | http_opt_request_remainder(slot->curl, posn); |
| 2250 | } else |
| 2251 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, |
| 2252 | fwrite_buffer); |
| 2253 | } |
| 2254 | |
| 2255 | curl_easy_setopt(slot->curl, CURLOPT_HEADERFUNCTION, fwrite_wwwauth); |
| 2256 | |
| 2257 | accept_language = http_get_accept_language_header(); |
| 2258 | |
| 2259 | if (accept_language) |
| 2260 | headers = curl_slist_append(headers, accept_language); |
| 2261 | |
| 2262 | strbuf_addstr(&buf, "Pragma:"); |
| 2263 | if (options->no_cache) |
| 2264 | strbuf_addstr(&buf, " no-cache"); |
| 2265 | if (options->initial_request && |
| 2266 | http_follow_config == HTTP_FOLLOW_INITIAL) |
| 2267 | curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 2268 | |
| 2269 | headers = curl_slist_append(headers, buf.buf); |
| 2270 | |
| 2271 | /* Add additional headers here */ |
| 2272 | if (options->extra_headers) { |
| 2273 | const struct string_list_item *item; |
| 2274 | for_each_string_list_item(item, options->extra_headers) |
| 2275 | headers = curl_slist_append(headers, item->string); |
| 2276 | } |
| 2277 | |
| 2278 | headers = http_append_auth_header(&http_auth, headers); |
| 2279 | |
| 2280 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
| 2281 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); |
no test coverage detected