@@ -231,13 +231,19 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
231231 {
232232 case HTTP_POST:
233233 curl_easy_setopt (curl_handle, CURLOPT_POST, 1L );
234- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDS, argument.post_data .data ());
235- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data .size ());
234+ if (argument.post_data )
235+ {
236+ curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDS, argument.post_data ->data ());
237+ curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data ->size ());
238+ }
236239 break ;
237240 case HTTP_PATCH:
238241 curl_easy_setopt (curl_handle, CURLOPT_CUSTOMREQUEST, " PATCH" );
239- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDS, argument.post_data .data ());
240- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data .size ());
242+ if (argument.post_data )
243+ {
244+ curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDS, argument.post_data ->data ());
245+ curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDSIZE, argument.post_data ->size ());
246+ }
241247 break ;
242248 case HTTP_HEAD:
243249 curl_easy_setopt (curl_handle, CURLOPT_NOBODY, 1L );
@@ -249,14 +255,16 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
249255 unsigned int fail_count = 0 , max_fails = 1 ;
250256 while (true )
251257 {
252- *result. status_code = curl_easy_perform (curl_handle);
253- if (*result. status_code == CURLE_OK || max_fails >= fail_count)
258+ retVal = curl_easy_perform (curl_handle);
259+ if (retVal == CURLE_OK || max_fails >= fail_count)
254260 break ;
255261 else
256262 fail_count++;
257263 }
258264
259- curl_easy_getinfo (curl_handle, CURLINFO_HTTP_CODE, &retVal);
265+ long code = 0 ;
266+ curl_easy_getinfo (curl_handle, CURLINFO_HTTP_CODE, &code);
267+ *result.status_code = code;
260268
261269 if (result.cookies )
262270 {
@@ -277,9 +285,9 @@ static int curlGet(const FetchArgument &argument, FetchResult &result)
277285
278286 curl_easy_cleanup (curl_handle);
279287
280- if (data)
288+ if (data && !argument. keep_resp_on_fail )
281289 {
282- if (*result. status_code != CURLE_OK || retVal != 200 )
290+ if (retVal != CURLE_OK || *result. status_code != 200 )
283291 data->clear ();
284292 data->shrink_to_fit ();
285293 }
@@ -316,7 +324,7 @@ std::string webGet(const std::string &url, const std::string &proxy, unsigned in
316324 int return_code = 0 ;
317325 std::string content;
318326
319- FetchArgument argument {HTTP_GET, url, proxy, " " , request_headers, nullptr , cache_ttl};
327+ FetchArgument argument {HTTP_GET, url, proxy, nullptr , request_headers, nullptr , cache_ttl};
320328 FetchResult fetch_res {&return_code, &content, response_headers, nullptr };
321329
322330 if (startsWith (url, " data:" ))
@@ -386,138 +394,29 @@ void flushCache()
386394 operateFiles (" cache" , [](const std::string &file){ remove ((" cache/" + file).data ()); return 0 ; });
387395}
388396
389- int curlPost (const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
390- {
391- CURL *curl_handle;
392- CURLcode res;
393- struct curl_slist *list = NULL ;
394- long retVal = 0 ;
395-
396- curl_init ();
397- curl_handle = curl_easy_init ();
398- list = curl_slist_append (list, " Content-Type: application/json;charset='utf-8'" );
399- for (const std::string &x : request_headers)
400- list = curl_slist_append (list, x.data ());
401-
402- curl_progress_data limit;
403- curl_set_common_options (curl_handle, url.data (), &limit);
404- curl_easy_setopt (curl_handle, CURLOPT_POST, 1L );
405- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDS, data.data ());
406- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDSIZE, data.size ());
407- curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, writer);
408- curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, retData);
409- curl_easy_setopt (curl_handle, CURLOPT_HTTPHEADER, list);
410- if (proxy.size ())
411- curl_easy_setopt (curl_handle, CURLOPT_PROXY, proxy.data ());
412-
413- res = curl_easy_perform (curl_handle);
414- curl_slist_free_all (list);
415-
416- if (res == CURLE_OK)
417- {
418- curl_easy_getinfo (curl_handle, CURLINFO_HTTP_CODE, &retVal);
419- }
420-
421- curl_easy_cleanup (curl_handle);
422-
423- return retVal;
424- }
425-
426397int webPost (const std::string &url, const std::string &data, const std::string &proxy, const string_icase_map &request_headers, std::string *retData)
427398{
428399 // return curlPost(url, data, proxy, request_headers, retData);
429400 int return_code = 0 ;
430- FetchArgument argument {HTTP_POST, url, proxy, " " , &request_headers, nullptr , 0 };
401+ FetchArgument argument {HTTP_POST, url, proxy, &data , &request_headers, nullptr , 0 , true };
431402 FetchResult fetch_res {&return_code, retData, nullptr , nullptr };
432403 return webGet (argument, fetch_res);
433404}
434405
435- int curlPatch (const std::string &url, const std::string &data, const std::string &proxy, const string_array &request_headers, std::string *retData)
436- {
437- CURL *curl_handle;
438- CURLcode res;
439- long retVal = 0 ;
440- struct curl_slist *list = NULL ;
441-
442- curl_init ();
443-
444- curl_handle = curl_easy_init ();
445-
446- list = curl_slist_append (list, " Content-Type: application/json;charset='utf-8'" );
447- for (const std::string &x : request_headers)
448- list = curl_slist_append (list, x.data ());
449-
450- curl_progress_data limit;
451- curl_set_common_options (curl_handle, url.data (), &limit);
452- curl_easy_setopt (curl_handle, CURLOPT_CUSTOMREQUEST, " PATCH" );
453- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDS, data.data ());
454- curl_easy_setopt (curl_handle, CURLOPT_POSTFIELDSIZE, data.size ());
455- curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, writer);
456- curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, retData);
457- curl_easy_setopt (curl_handle, CURLOPT_HTTPHEADER, list);
458- if (proxy.size ())
459- curl_easy_setopt (curl_handle, CURLOPT_PROXY, proxy.data ());
460-
461- res = curl_easy_perform (curl_handle);
462- curl_slist_free_all (list);
463- if (res == CURLE_OK)
464- {
465- res = curl_easy_getinfo (curl_handle, CURLINFO_HTTP_CODE, &retVal);
466- }
467-
468- curl_easy_cleanup (curl_handle);
469-
470- return retVal;
471- }
472-
473406int webPatch (const std::string &url, const std::string &data, const std::string &proxy, const string_icase_map &request_headers, std::string *retData)
474407{
475408 // return curlPatch(url, data, proxy, request_headers, retData);
476409 int return_code = 0 ;
477- FetchArgument argument {HTTP_PATCH, url, proxy, " " , &request_headers, nullptr , 0 };
410+ FetchArgument argument {HTTP_PATCH, url, proxy, &data , &request_headers, nullptr , 0 , true };
478411 FetchResult fetch_res {&return_code, retData, nullptr , nullptr };
479412 return webGet (argument, fetch_res);
480413}
481414
482- int curlHead (const std::string &url, const std::string &proxy, const string_array &request_headers, std::string &response_headers)
483- {
484- CURL *curl_handle;
485- CURLcode res;
486- long retVal = 0 ;
487- struct curl_slist *list = NULL ;
488-
489- curl_init ();
490-
491- curl_handle = curl_easy_init ();
492-
493- list = curl_slist_append (list, " Content-Type: application/json;charset='utf-8'" );
494- for (const std::string &x : request_headers)
495- list = curl_slist_append (list, x.data ());
496-
497- curl_progress_data limit;
498- curl_set_common_options (curl_handle, url.data (), &limit);
499- curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, writer);
500- curl_easy_setopt (curl_handle, CURLOPT_HEADERDATA, &response_headers);
501- curl_easy_setopt (curl_handle, CURLOPT_NOBODY, 1L );
502- curl_easy_setopt (curl_handle, CURLOPT_HTTPHEADER, list);
503- if (proxy.size ())
504- curl_easy_setopt (curl_handle, CURLOPT_PROXY, proxy.data ());
505-
506- res = curl_easy_perform (curl_handle);
507- curl_slist_free_all (list);
508- if (res == CURLE_OK)
509- res = curl_easy_getinfo (curl_handle, CURLINFO_HTTP_CODE, &retVal);
510-
511- curl_easy_cleanup (curl_handle);
512-
513- return retVal;
514- }
515-
516415int webHead (const std::string &url, const std::string &proxy, const string_icase_map &request_headers, std::string &response_headers)
517416{
518417 // return curlHead(url, proxy, request_headers, response_headers);
519418 int return_code = 0 ;
520- FetchArgument argument {HTTP_HEAD, url, proxy, " " , &request_headers, nullptr , 0 };
419+ FetchArgument argument {HTTP_HEAD, url, proxy, nullptr , &request_headers, nullptr , 0 };
521420 FetchResult fetch_res {&return_code, nullptr , &response_headers, nullptr };
522421 return webGet (argument, fetch_res);
523422}
@@ -533,39 +432,4 @@ string_array headers_map_to_array(const string_map &headers)
533432int webGet (const FetchArgument& argument, FetchResult &result)
534433{
535434 return curlGet (argument, result);
536- /*
537- switch(argument.method)
538- {
539- case HTTP_GET:
540- return curlGet(argument, result);
541- case HTTP_POST:
542- {
543- string_array request_header;
544- if(argument.request_headers != nullptr)
545- request_header = headers_map_to_array(*argument.request_headers);
546- int res = curlPost(argument.url, argument.post_data, argument.proxy, request_header, result.content);
547- *result.status_code = res;
548- return res;
549- }
550- case HTTP_PATCH:
551- {
552- string_array request_header;
553- if(argument.request_headers != nullptr)
554- request_header = headers_map_to_array(*argument.request_headers);
555- int res = curlPatch(argument.url, argument.post_data, argument.proxy, request_header, result.content);
556- *result.status_code = res;
557- return res;
558- }
559- case HTTP_HEAD:
560- {
561- string_array request_header;
562- if(argument.request_headers != nullptr)
563- request_header = headers_map_to_array(*argument.request_headers);
564- int res = curlHead(argument.url, argument.proxy, request_header, *result.response_headers);
565- *result.status_code = res;
566- return res;
567- }
568- }
569- return -1;
570- */
571435}
0 commit comments