/* Called by: zxid_soap_call_raw */ struct zx_str* zxid_http_post_raw(zxid_conf* cf, int url_len, const char* url, int len, const char* data, const char* SOAPaction) { #ifdef USE_CURL struct zx_str* ret; CURLcode res; struct zxid_curl_ctx rc; struct zxid_curl_ctx wc; struct curl_slist content_type; struct curl_slist SOAPaction_curl; char* urli; rc.buf = rc.p = ZX_ALLOC(cf->ctx, ZXID_INIT_SOAP_BUF+1); rc.lim = rc.buf + ZXID_INIT_SOAP_BUF; #if 0 cf->curl = curl_easy_init(); curl_easy_reset(cf->curl); LOCK_INIT(cf->curl_mx); LOCK(cf->curl_mx, "curl-soap"); #else LOCK(cf->curl_mx, "curl-soap"); curl_easy_reset(cf->curl); #endif curl_easy_setopt(cf->curl, CURLOPT_WRITEDATA, &rc); curl_easy_setopt(cf->curl, CURLOPT_WRITEFUNCTION, zxid_curl_write_data); curl_easy_setopt(cf->curl, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(cf->curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(cf->curl, CURLOPT_MAXREDIRS, 110); curl_easy_setopt(cf->curl, CURLOPT_SSL_VERIFYPEER, 0); /* *** arrange verification */ curl_easy_setopt(cf->curl, CURLOPT_SSL_VERIFYHOST, 0); /* *** arrange verification */ //curl_easy_setopt(cf->curl, CURLOPT_CERTINFO, 1); if (url_len == -1) url_len = strlen(url); urli = ZX_ALLOC(cf->ctx, url_len+1); memcpy(urli, url, url_len); urli[url_len] = 0; DD("urli(%s) len=%d", urli, len); curl_easy_setopt(cf->curl, CURLOPT_URL, urli); if (len == -1) len = strlen(data); wc.buf = wc.p = (char*)data; wc.lim = (char*)data + len; curl_easy_setopt(cf->curl, CURLOPT_POST, 1); curl_easy_setopt(cf->curl, CURLOPT_POSTFIELDSIZE, len); curl_easy_setopt(cf->curl, CURLOPT_READDATA, &wc); curl_easy_setopt(cf->curl, CURLOPT_READFUNCTION, zxid_curl_read_data); ZERO(&content_type, sizeof(content_type)); content_type.data = cf->wsc_soap_content_type; /* SOAP11: "Content-Type: text/xml" */ if (SOAPaction) { ZERO(&SOAPaction_curl, sizeof(SOAPaction_curl)); SOAPaction_curl.data = (char*)SOAPaction; SOAPaction_curl.next = &content_type; //curl_slist_append(3) curl_easy_setopt(cf->curl, CURLOPT_HTTPHEADER, &SOAPaction_curl); } else { curl_easy_setopt(cf->curl, CURLOPT_HTTPHEADER, &content_type); } INFO("----------- call(%s) -----------", urli); DD("SOAP_CALL post(%.*s) len=%d\n", len, data, len); D_XML_BLOB(cf, "SOAPCALL POST", len, data); res = curl_easy_perform(cf->curl); /* <========= Actual call, blocks. */ switch (res) { case 0: break; case CURLE_SSL_CONNECT_ERROR: ERR("Is the URL(%s) really an https url? Check that certificate of the server is valid and that certification authority is known to the client. CURLcode(%d) CURLerr(%s)", urli, res, CURL_EASY_STRERR(res)); DD("buf(%.*s)", rc.lim-rc.buf, rc.buf); #if 0 struct curl_certinfo* ci; res = curl_easy_getinfo(cf->curl, CURLINFO_CERTINFO, &ci); /* CURLINFO_SSL_VERIFYRESULT */ if (!res && ci) { int i; struct curl_slist *slist; D("%d certs", ci->num_of_certs); for (i = 0; i < ci->num_of_certs; ++i) for (slist = ci->certinfo[i]; slist; slist = slist->next) D("%d: %s", i, slist->data); } #endif break; default: ERR("Failed post to url(%s) CURLcode(%d) CURLerr(%s)", urli, res, CURL_EASY_STRERR(res)); DD("buf(%.*s)", rc.lim-rc.buf, rc.buf); } /*curl_easy_getinfo(cf->curl, CURLINFO_CONTENT_TYPE, char*);*/ UNLOCK(cf->curl_mx, "curl-soap"); ZX_FREE(cf->ctx, urli); rc.lim = rc.p; rc.p[0] = 0; DD("SOAP_CALL got(%s)", rc.buf); D_XML_BLOB(cf, "SOAPCALL GOT", rc.lim - rc.buf, rc.buf); ret = zx_ref_len_str(cf->ctx, rc.lim - rc.buf, rc.buf); return ret; #else ERR("This copy of zxid was compiled to NOT use libcurl. SOAP calls (such as Artifact profile and WSC) are not supported. Add -DUSE_CURL (make ENA_CURL=1) and recompile. %d", 0); return 0; #endif }
bool CurlHttpClient::execute(const internal::HttpRequest& request, internal::HttpResponse& response) { CURLStatus_ = CURLE_FAILED_INIT; errorBuf_[0] = '\0'; bool retry(false); unsigned retryCount(0); response.clear(); CURL* curl = curl_easy_init(); if (NULL == curl) { return (internal::HttpResponse::HTTP_OK == response.getHttpStatus()); } std::string URI(getRequestString(request)); FREDCPP_LOG_DEBUG("CURL:URI:" << URI); if (!CACertFile_.empty()) { std::ifstream ifs(CACertFile_.c_str()); FREDCPP_LOG_DEBUG("CURL:CACertFile:" << CACertFile_ << " found:" << ifs.good()); } if (CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent_.c_str())) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeDataCallback_)) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L)) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_FILE, &response.getContentStream())) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeoutSecs_)) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_URL, URI.c_str())) && CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuf_)) && (!request.isHttps() || CURLE_OK == (CURLStatus_ = curl_easy_setopt(curl, CURLOPT_CAINFO, CACertFile_.c_str()))) ) { do { CURLStatus_ = curl_easy_perform(curl); if (CURLE_OK == CURLStatus_) { // on successfull call get http-status and content-type long code(0L); char *strInfo(NULL); if (CURLE_OK == curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code) && CURLE_OK == curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &strInfo)) { response.setHttpStatus(httpStatusFromCode(code)); response.setContentType(strInfo); } } else { FREDCPP_LOG_DEBUG("CURL:Request failed CURLStatus:" << CURLStatus_ << "|" << errorBuf_); } retry = (CURLE_OPERATION_TIMEDOUT == CURLStatus_ || CURLE_COULDNT_RESOLVE_HOST == CURLStatus_ || CURLE_COULDNT_RESOLVE_PROXY == CURLStatus_ || CURLE_COULDNT_CONNECT == CURLStatus_); if (retry) { if (retryCount < retryMaxCount_) { ++retryCount; FREDCPP_LOG_DEBUG("CURL:Waiting " << retryWaitSecs_ << "ms" << " before request retry " << retryCount << " ..."); internal::sleep(retryWaitSecs_); } else { retry = false; FREDCPP_LOG_DEBUG("CURL:Retry maximum count " << retryMaxCount_ << " reached - giving up."); } } } while (retry); } if (CURLE_OK == CURLStatus_) { FREDCPP_LOG_DEBUG("CURL:http-response:" << response.getHttpStatus() << " " << "content-type:" << response.getContentType()); } curl_easy_cleanup(curl); return (internal::HttpResponse::HTTP_OK == response.getHttpStatus()); }
const char *wswcurl_get_content_type( wswcurl_req *req ) { char *content_type = NULL; curl_easy_getinfo( req->curl, CURLINFO_CONTENT_TYPE, &content_type ); return content_type; }
///==============================return the current url=============================/// std::string Browser::geturl() { char * current_url; curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL, ¤t_url); return current_url; }
static int curl_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { BDRVCURLState *s = bs->opaque; CURLState *state = NULL; QemuOpts *opts; Error *local_err = NULL; const char *file; double d; static int inited = 0; if (flags & BDRV_O_RDWR) { qerror_report(ERROR_CLASS_GENERIC_ERROR, "curl block device does not support writes"); return -EROFS; } opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); qemu_opts_absorb_qdict(opts, options, &local_err); if (error_is_set(&local_err)) { qerror_report_err(local_err); error_free(local_err); goto out_noclean; } s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE); if ((s->readahead_size & 0x1ff) != 0) { fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n", s->readahead_size); goto out_noclean; } file = qemu_opt_get(opts, "url"); if (file == NULL) { qerror_report(ERROR_CLASS_GENERIC_ERROR, "curl block driver requires " "an 'url' option"); goto out_noclean; } if (!inited) { curl_global_init(CURL_GLOBAL_ALL); inited = 1; } DPRINTF("CURL: Opening %s\n", file); s->url = g_strdup(file); state = curl_init_state(s); if (!state) goto out_noclean; // Get file size s->accept_range = false; curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1); curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb); curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s); if (curl_easy_perform(state->curl)) goto out; curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); if (d) s->len = (size_t)d; else if(!s->len) goto out; if ((!strncasecmp(s->url, "http://", strlen("http://")) || !strncasecmp(s->url, "https://", strlen("https://"))) && !s->accept_range) { pstrcpy(state->errmsg, CURL_ERROR_SIZE, "Server does not support 'range' (byte ranges)."); goto out; } DPRINTF("CURL: Size = %zd\n", s->len); curl_clean_state(state); curl_easy_cleanup(state->curl); state->curl = NULL; // Now we know the file exists and its size, so let's // initialize the multi interface! s->multi = curl_multi_init(); curl_multi_setopt(s->multi, CURLMOPT_SOCKETDATA, s); curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); curl_multi_do(s); qemu_opts_del(opts); return 0; out: fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg); curl_easy_cleanup(state->curl); state->curl = NULL; out_noclean: g_free(s->url); qemu_opts_del(opts); return -EINVAL; }
int ocfetchurl(CURL* curl, const char* url, OCbytes* buf, long* filetime, struct OCcredentials* creds) { int stat = OC_NOERR; CURLcode cstat = CURLE_OK; size_t len; long httpcode = 0; char tbuf[1024]; /* Set the URL */ cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url); if (cstat != CURLE_OK) goto fail; if(creds != NULL && creds->password != NULL && creds->username != NULL) { /* Set user and password */ #if defined (HAVE_CURLOPT_USERNAME) && defined (HAVE_CURLOPT_PASSWORD) cstat = curl_easy_setopt(curl, CURLOPT_USERNAME, creds->username); if (cstat != CURLE_OK) goto fail; cstat = curl_easy_setopt(curl, CURLOPT_PASSWORD, creds->password); if (cstat != CURLE_OK) goto fail; #else snprintf(tbuf,1023,"%s:%s",creds->username,creds->password); cstat = curl_easy_setopt(curl, CURLOPT_USERPWD, tbuf); if (cstat != CURLE_OK) goto fail; #endif } /* send all data to this function */ cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); if (cstat != CURLE_OK) goto fail; /* we pass our file to the callback function */ cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buf); if (cstat != CURLE_OK) goto fail; /* One last thing; always try to get the last modified time */ cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1); cstat = curl_easy_perform(curl); if(cstat == CURLE_PARTIAL_FILE) { /* Log it but otherwise ignore */ oclog(OCLOGWARN, "curl error: %s; ignored", curl_easy_strerror(cstat)); cstat = CURLE_OK; } httpcode = ocfetchhttpcode(curl); if(cstat != CURLE_OK) goto fail; /* Get the last modified time */ if(filetime != NULL) cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime); if(cstat != CURLE_OK) goto fail; /* Null terminate the buffer*/ len = ocbyteslength(buf); ocbytesappend(buf, '\0'); ocbytessetlength(buf, len); /* dont count null in buffer size*/ #ifdef OCDEBUG oclog(OCLOGNOTE,"buffersize: %lu bytes",(off_t)ocbyteslength(buf)); #endif return OCTHROW(stat); fail: oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat)); switch (httpcode) { case 401: stat = OC_EAUTH; break; case 404: stat = OC_ENOFILE; break; case 500: stat = OC_EDAPSVC; break; case 200: break; default: stat = OC_ECURL; break; } return OCTHROW(stat); }
void spider(void *pack,char *line,char * pathtable) { struct MemoryStruct chunk; FILE *fp=NULL; bool match_string=false,save_response=false,test_tamper=false; long status=0,length=0; int old=0,res=0,counter=0,counter_cookie=0,counter_agent=0,POST=0,timeout=0,debug_host=3; char *make=NULL,*make_cookie=NULL,*make_agent=NULL,*tamper=NULL,*responsetemplate=NULL,*tmp_response=NULL,*tmp_make=NULL,*tmp_make_cookie=NULL,*tmp_make_agent=NULL,*tmp_line=NULL,*tmp_line2=NULL; char **pack_ptr=(char **)pack,**arg = pack_ptr; char randname[16],line2[1024],log[2048],tabledata[4086],pathsource[1024]; if(arg[12]!=NULL) save_response=true; if(arg[8]!=NULL) timeout=atoi(arg[8]); // payload tamper if(arg[20]!=NULL) { tamper=arg[20]; if(strstr(tamper,"encode64")) { line=encode64(line,strlen(line)-1); test_tamper=true; } if(strstr(tamper,"randcase")) { line=rand_case(line); test_tamper=true; } if(strstr(tamper,"urlencode")) { line=urlencode(line); test_tamper=true; } if(strstr(tamper,"double_urlencode")) { line=double_urlencode(line); test_tamper=true; } if(strstr(tamper,"spaces2comment")) { line=spaces2comment(line); test_tamper=true; } if(strstr(tamper,"unmagicquote")) { line=unmagicquote(line); test_tamper=true; } if(strstr(tamper,"apostrophe2nullencode")) { line=apostrophe2nullencode(line); test_tamper=true; } if(strstr(tamper,"rand_comment")) { line=rand_comment(line); test_tamper=true; } if(strstr(tamper,"rand_space")) { line=rand_space(line); test_tamper=true; } if(test_tamper==false) { DEBUG("error at tamper argument\n"); exit(0); } } memset(pathsource,0,sizeof(char)*1023); if(save_response==false) { strcat(pathsource,"0"); } // brute POST/GET/COOKIES/UserAgent if(arg[21]==NULL) { POST=(arg[4]==NULL)?0:1; counter=char_type_counter(POST?arg[4]:arg[0],'^'); counter_cookie=char_type_counter(arg[13]!=NULL?arg[13]:"",'^'); counter_agent=char_type_counter(arg[19]!=NULL?arg[19]:"",'^'); old=counter; } else { char *file_request=readLine(arg[21]); counter=char_type_counter(file_request,'^'); old=counter; xfree((void**)&file_request); } chomp(line); // goto to fix signal stop if user do ctrl+c try_again: while ( old > 0 || counter_cookie > 0 || counter_agent > 0 ) { CURL *curl; // curl_global_init(CURL_GLOBAL_ALL); chunk.memory=NULL; chunk.size = 0; curl_socket_t sockfd; /* socket */ long sockextr; size_t iolen; curl = curl_easy_init(); // DEBUG("counts ^ : %d \n",old); if(arg[21]==NULL) { make=payload_injector( (POST?arg[4]:arg[0]),line,old); if(arg[13]!=NULL) make_cookie=payload_injector( arg[13],line,counter_cookie); if(arg[19]!=NULL) make_agent=payload_injector( arg[19],line,counter_agent); curl_easy_setopt(curl, CURLOPT_URL, POST?arg[0]:make); } else { // if is custom request char *request_file=readLine(arg[21]); make=payload_injector( request_file,line,old); curl_easy_setopt(curl, CURLOPT_URL, arg[0]); xfree((void**)&request_file); } if ( POST ) curl_easy_setopt(curl, CURLOPT_POSTFIELDS, make); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); // load user agent if ( arg[6]!=NULL ) { curl_easy_setopt(curl, CURLOPT_USERAGENT, arg[6]); } else { curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (0d1n v0.1) "); } // json headers to use JSON if(arg[14]!=NULL) { struct curl_slist *headers = NULL; curl_slist_append(headers, arg[14]); if(arg[16]!=NULL) { curl_slist_append(headers, "Accept: application/json"); curl_slist_append(headers, "Content-Type: application/json"); } curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_slist_free_all(headers); } else { if(arg[16] != NULL) { struct curl_slist *headers = NULL; curl_slist_append(headers, "Accept: application/json"); curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_slist_free_all(headers); } } //use custom method PUT,DELETE... if(arg[15]!=NULL) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, arg[15]); } curl_easy_setopt(curl, CURLOPT_ENCODING,"gzip,deflate"); // load cookie jar if ( arg[3] != NULL ) { curl_easy_setopt(curl,CURLOPT_COOKIEFILE,arg[3]); curl_easy_setopt(curl,CURLOPT_COOKIEJAR,arg[3]); } else { curl_easy_setopt(curl,CURLOPT_COOKIEJAR,"odin_cookiejar.txt"); } // LOAD cookie fuzz if(arg[13]!=NULL) { curl_easy_setopt(curl,CURLOPT_COOKIE,make_cookie); } // LOAD UserAgent FUZZ if(arg[19]!=NULL) { curl_easy_setopt(curl,CURLOPT_USERAGENT,make_agent); } curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); // Load cacert if ( arg[7] != NULL ) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2); curl_easy_setopt(curl, CURLOPT_CAINFO, arg[7]); } else { curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L); curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L); } if(timeout) curl_easy_setopt(curl,CURLOPT_TIMEOUT,timeout); // load single proxy if(arg[17] != NULL) { curl_easy_setopt(curl, CURLOPT_PROXY, arg[17]); // curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1); } // load random proxy in list if(arg[18] != NULL) { char *randproxy=Random_linefile(arg[18]); // printf("PROXY LOAD: %s\n",randproxy); curl_easy_setopt(curl, CURLOPT_PROXY, randproxy); // curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1); } if ( arg[9] != NULL ) curl_easy_setopt(curl,CURLOPT_SSLVERSION,(long)atoi(arg[9])); curl_easy_setopt(curl,CURLOPT_VERBOSE,0); curl_easy_setopt(curl,CURLOPT_HEADER,1); if(arg[21]!=NULL) { curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); } res=curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,&status); // custom http request if(arg[21]!=NULL) { curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr); sockfd = sockextr; if(!wait_on_socket(sockfd, 0, 60000L)) { DEBUG("error in socket at custom http request"); } res=curl_easy_send(curl, make, strlen(make), &iolen); // recv data while(1) { wait_on_socket(sockfd, 1, 60000L); chunk.memory=xmalloc(sizeof(char)*3024); res = curl_easy_recv(curl, chunk.memory, 3023, &iolen); chunk.size=strlen(chunk.memory); if(strlen(chunk.memory) > 8) break; if(CURLE_OK != res) break; } status=(long)parse_http_status(chunk.memory); //status=404; } // length of response if(chunk.size<=0) length=0.0; else length=chunk.size; if(status==0) { debug_host--; DEBUG("Problem in Host: \n %s",chunk.memory); if(debug_host<0) exit(0); goto try_again; } // arg[10] list to find with regex , arg[2] list without regex if( (arg[2]) || (arg[10]) ) { if(save_response==true) { memset(pathsource,0,sizeof(char)*1023); } fp = fopen((arg[2]!=NULL)?arg[2]:arg[10], "r"); if ( !fp ) { DEBUG("error to open response list"); exit(1); } while ( fgets(line2,1023,fp) != NULL) { chomp(line2); // find a string in response if(status != 0) { if ( arg[2] != NULL ) match_string=strstr(chunk.memory,line2)?true:false; if ( arg[10] != NULL ) match_string=strstr_regex(chunk.memory,line2)?true:false; } if(chunk.memory && (match_string == true) ) { if(make_cookie!=NULL) { fprintf(stdout,"%s [ %s %ld %s ] Payload: %s %s %s Grep: %s %s %s Params: %s \nCookie: %s %s\n",YELLOW,CYAN,status,YELLOW,GREEN,line,YELLOW,CYAN,line2,YELLOW,make,make_cookie,LAST); } if(make_agent!=NULL) { fprintf(stdout,"%s [ %s %ld %s ] Payload: %s %s %s Grep: %s %s %s Params: %s \nCookie: %s %s\n",YELLOW,CYAN,status,YELLOW,GREEN,line,YELLOW,CYAN,line2,YELLOW,make,make_agent,LAST); } else { fprintf(stdout,"%s [ %s %ld %s ] Payload: %s %s %s Grep: %s %s %s Params: %s %s\n",YELLOW,CYAN,status,YELLOW,GREEN,line,YELLOW,CYAN,line2,YELLOW,make,LAST); } if(save_response==true) { // create responses path memset(pathsource,0,sizeof(char)*1023); strncat(pathsource,"response_sources/",18); strncat(pathsource,arg[5], 15); mkdir(pathsource,S_IRWXU|S_IRWXG|S_IRWXO); snprintf(pathsource,986,"response_sources/%s/%s.html",arg[5],rand_str(randname, sizeof randname)); } // write log file snprintf(log,2047,"[ %ld ] Payload: %s Grep: %s Params: %s cookie: %s UserAgent: %s \n Path Response Source: %s\n",status,line,line2,make,(make_cookie!=NULL)?make_cookie:" ",(make_agent!=NULL)?make_agent:" ",pathsource); WriteFile(arg[5],log); memset(log,0,2047); if(save_response==true) { // write highlights response responsetemplate=NULL; responsetemplate=readLine(TEMPLATE); WriteFile(pathsource,responsetemplate); memset(responsetemplate,0,strlen(responsetemplate)-1); tmp_response=NULL; tmp_response=html_entities(chunk.memory); WriteFile(pathsource,tmp_response); memset(tmp_response,0,strlen(tmp_response)-1); WriteFile(pathsource,"</pre></html>"); } // create datatables tmp_make=html_entities(make); tmp_line2=html_entities(line2); tmp_line=html_entities(line); if(make_cookie!=NULL) { tmp_make_cookie=html_entities(make_cookie); snprintf(tabledata,4085,"[\"<a class=\\\"fancybox fancybox.iframe\\\" href=\\\"../%s\\\">%ld </a>\",\"%ld\",\"%s cookie: %s\",\"%s\",\"%s\"],\n",pathsource,status,length,tmp_make,tmp_make_cookie,tmp_line2,tmp_line); memset(tmp_make_cookie,0,strlen(tmp_make_cookie)-1); } if(make_agent!=NULL) { tmp_make_agent=html_entities(make_agent); snprintf(tabledata,4085,"[\"<a class=\\\"fancybox fancybox.iframe\\\" href=\\\"../%s\\\">%ld </a>\",\"%ld\",\"%s UserAgent: %s\",\"%s\",\"%s\"],\n",pathsource,status,length,tmp_make,tmp_make_agent,tmp_line2,tmp_line); memset(tmp_make_agent,0,strlen(tmp_make_agent)-1); } else { snprintf(tabledata,4085,"[\"<a class=\\\"fancybox fancybox.iframe\\\" href=\\\"../%s\\\">%ld </a>\",\"%ld\",\"%s\",\"%s\",\"%s\"],\n",pathsource,status,length,tmp_make,tmp_line2,tmp_line); } WriteFile(pathtable,tabledata); // memset(tmp_make,0,strlen(tmp_make)-1); // memset(tmp_make_cookie,0,strlen(tmp_make_cookie)-1); // memset(tmp_make_agent,0,strlen(tmp_make_agent)-1); memset(tmp_line,0,strlen(tmp_line)-1); memset(tmp_line2,0,strlen(tmp_line2)-1); memset(tabledata,0,4085); memset(pathsource,0,strlen(pathsource)-1); } } if( fclose(fp) == EOF ) { DEBUG("Error in close()"); exit(1); } fp=NULL; } else { if(counter_cookie) { fprintf(stdout,"%s [ %s %ld %s ] Payload: %s %s %s Params: %s %s\n Cookie: %s %s\n",YELLOW,CYAN,status,YELLOW,GREEN,line,YELLOW,CYAN,make,make_cookie,LAST); } if(counter_agent) { fprintf(stdout,"%s [ %s %ld %s ] Payload: %s %s %s Params: %s %s\n UserAgent: %s %s\n",YELLOW,CYAN,status,YELLOW,GREEN,line,YELLOW,CYAN,make,make_agent,LAST); } else { fprintf(stdout,"%s [ %s %ld %s ] Payload: %s %s %s Params: %s %s %s\n",YELLOW,CYAN,status,YELLOW,GREEN,line,YELLOW,CYAN,make,LAST); } if(save_response==true) { // memset(pathsource,0,sizeof(char)*1023); strncat(pathsource,"response_sources/",18); strncat(pathsource,arg[5], 15); mkdir(pathsource,S_IRWXU|S_IRWXG|S_IRWXO); snprintf(pathsource,986,"response_sources/%s/%s.html",arg[5],rand_str(randname, sizeof randname)); } //write logs snprintf(log,2047,"[%ld Payload: %s Params: %s Cookie: %s UserAgent: %s \n Path Response Source: %s\n",status,line,make,(make_cookie!=NULL)?make_cookie:" ",(make_agent!=NULL)?make_agent:" ",pathsource); WriteFile(arg[5],log); memset(log,0,2047); if(save_response==true) { // write response source with highlights responsetemplate=readLine(TEMPLATE); WriteFile(pathsource,responsetemplate); //memset(responsetemplate,0,strlen(responsetemplate)-1); tmp_response=html_entities(chunk.memory); WriteFile(pathsource,tmp_response); //memset(tmp_response,0,strlen(tmp_response)-1); WriteFile(pathsource,"</pre></html>"); } // create datatables tmp_make=html_entities(make); tmp_line=html_entities(line); if(counter_cookie) { tmp_make_cookie=html_entities(make_cookie); snprintf(tabledata,4085,"[\"<a class=\\\"fancybox fancybox.iframe\\\" href=\\\"../%s\\\">%ld </a>\",\"%ld\",\"%s cookie: %s\",\"\",\"%s\"],\n",pathsource,status,length,tmp_make,tmp_make_cookie,tmp_line); // memset(tmp_make_cookie,0,strlen(tmp_make_cookie)-1); } if(counter_agent) { tmp_make_agent=html_entities(make_agent); snprintf(tabledata,4085,"[\"<a class=\\\"fancybox fancybox.iframe\\\" href=\\\"../%s\\\">%ld </a>\",\"%ld\",\"%s UserAgent: %s\",\"\",\"%s\"],\n",pathsource,status,length,tmp_make,tmp_make_agent,tmp_line); } else { snprintf(tabledata,4047,"[\"<a class=\\\"fancybox fancybox.iframe\\\" href=\\\"../%s\\\">%ld </a>\",\"%ld\",\"%s\",\"\",\"%s\"],\n",pathsource,status,length,tmp_make,tmp_line); } WriteFile(pathtable,tabledata); memset(tmp_make,0,strlen(tmp_make)-1); memset(tmp_line,0,strlen(tmp_line)-1); memset(tabledata,0,4085); memset(pathsource,0,strlen(pathsource)-1); //DEBUG("part B"); } //DEBUG("END PARTS"); // memset(make,0,strlen(make)-1); // memset(make_cookie,0,strlen(make_cookie)-1); // memset(make_agent,0,strlen(make_agent)-1); // memset(pathsource,0,strlen(pathsource)-1); xfree((void **)&chunk.memory); // curl_easy_cleanup(curl); // curl_global_cleanup(); if(old>0) old--; if(counter_cookie > 0) counter_cookie--; if(counter_agent > 0) counter_agent--; debug_host=3; } xfree((void **)&make_agent); xfree((void **)&make_cookie); xfree((void **)&make); xfree((void **)&tmp_make); xfree((void **)&tmp_make_cookie); xfree((void **)&tmp_make_agent); xfree((void **)&tmp_line); xfree((void **)&tmp_line2); xfree((void **)&responsetemplate); xfree((void **)&tmp_response); if(arg[20] != NULL) xfree((void **)&line); // DEBUG("GOOO3"); }
void ResourceHandleManager::downloadTimerCallback(Timer<ResourceHandleManager>* timer) { fd_set fdread; fd_set fdwrite; fd_set fdexcep; int maxfd = 0; FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); curl_multi_fdset(m_curlMultiHandle, &fdread, &fdwrite, &fdexcep, &maxfd); struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = selectTimeoutMS * 1000; // select waits microseconds int rc = ::select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); if (-1 == rc) { #ifndef NDEBUG printf("bad: select() returned -1\n"); #endif return; } int runningHandles = 0; CURLMcode curlCode = CURLM_CALL_MULTI_PERFORM; while (CURLM_CALL_MULTI_PERFORM == curlCode) { curlCode = curl_multi_perform(m_curlMultiHandle, &runningHandles); } // check the curl messages indicating completed transfers // and free their resources while (true) { int messagesInQueue; CURLMsg* msg = curl_multi_info_read(m_curlMultiHandle, &messagesInQueue); if (!msg) break; if (CURLMSG_DONE != msg->msg) continue; // find the node which has same d->m_handle as completed transfer CURL* handle = msg->easy_handle; ASSERT(handle); ResourceHandle* job; curl_easy_getinfo(handle, CURLINFO_PRIVATE, &job); ASSERT(job); if (!job) continue; ResourceHandleInternal* d = job->getInternal(); if (CURLE_OK == msg->data.result) { if (d->client()) d->client()->didFinishLoading(job); } else { #ifndef NDEBUG char* url = 0; curl_easy_getinfo(d->m_handle, CURLINFO_EFFECTIVE_URL, &url); printf("Curl ERROR for url='%s', error: '%s'\n", url, curl_easy_strerror(msg->data.result)); free(url); #endif if (d->client()) d->client()->didFail(job, ResourceError()); } removeFromCurl(job); } if (!m_downloadTimer.isActive() && (runningHandles > 0)) m_downloadTimer.startOneShot(pollTimeSeconds); }
void CCHttpRequest_impl::onRequest(void) { if (m_postFields.size() > 0) { curl_easy_setopt(m_curl, CURLOPT_POST, 1L); std::stringbuf buf; PostFieldsIterator it = m_postFields.begin(); while (it != m_postFields.end()) { char* part = curl_easy_escape(m_curl, it->first.c_str(), 0); buf.sputn(part, strlen(part)); buf.sputc('='); curl_free(part); part = curl_easy_escape(m_curl, it->second.c_str(), 0); buf.sputn(part, strlen(part)); curl_free(part); ++it; if (it != m_postFields.end()) buf.sputc('&'); } curl_easy_setopt(m_curl, CURLOPT_COPYPOSTFIELDS, buf.str().c_str()); } else if (m_postdata.length() > 0) { curl_easy_setopt(m_curl, CURLOPT_POST, 1L); curl_easy_setopt(m_curl, CURLOPT_COPYPOSTFIELDS, m_postdata.c_str()); } else if (m_isPost) { curl_easy_setopt(m_curl, CURLOPT_COPYPOSTFIELDS, ""); } struct curl_slist* chunk = NULL; for (HeadersIterator it = m_headers.begin(); it != m_headers.end(); ++it) { chunk = curl_slist_append(chunk, (*it).c_str()); } curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, chunk); curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, 0); curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1); CURLcode code = curl_easy_perform(m_curl); curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &m_responseCode); curl_easy_cleanup(m_curl); m_curl = NULL; curl_slist_free_all(chunk); m_errorCode = (code == CURLE_OK) ? CCHttpRequestErrorNone : CCHttpRequestErrorUnknown; m_errorMessage = (code == CURLE_OK) ? "" : curl_easy_strerror(code); m_responseData = (unsigned char*)malloc(m_rawResponseBuffLength + 1); m_responseData[m_rawResponseBuffLength] = '\0'; m_responseDataLength = 0; for (RawResponseDataBuffIterator it = m_rawResponseBuff.begin(); it != m_rawResponseBuff.end(); ++it) { CCHttpRequest_impl::Chunk* chunk = *it; size_t bytes = chunk->getBytes(); memcpy(m_responseData + m_responseDataLength, chunk->getChunk(), bytes); m_responseDataLength += bytes; } cleanupRawResponseBuff(); m_responseString = std::string(reinterpret_cast<char*>(m_responseData)); m_state = STATE_COMPLETED; }
void* cURLCamera::thread_func() { long tRet; double dSize; c = curl_easy_init(); if(c == NULL) { Fatal("Failed getting easy handle from libcurl"); } /* Set URL */ cRet = curl_easy_setopt(c, CURLOPT_URL, mPath.c_str()); if(cRet != CURLE_OK) Fatal("Failed setting libcurl URL: %s", curl_easy_strerror(cRet)); /* Header callback */ cRet = curl_easy_setopt(c, CURLOPT_HEADERFUNCTION, &header_callback_dispatcher); if(cRet != CURLE_OK) Fatal("Failed setting libcurl header callback function: %s", curl_easy_strerror(cRet)); cRet = curl_easy_setopt(c, CURLOPT_HEADERDATA, this); if(cRet != CURLE_OK) Fatal("Failed setting libcurl header callback object: %s", curl_easy_strerror(cRet)); /* Data callback */ cRet = curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, &data_callback_dispatcher); if(cRet != CURLE_OK) Fatal("Failed setting libcurl data callback function: %s", curl_easy_strerror(cRet)); cRet = curl_easy_setopt(c, CURLOPT_WRITEDATA, this); if(cRet != CURLE_OK) Fatal("Failed setting libcurl data callback object: %s", curl_easy_strerror(cRet)); /* Progress callback */ cRet = curl_easy_setopt(c, CURLOPT_NOPROGRESS, 0); if(cRet != CURLE_OK) Fatal("Failed enabling libcurl progress callback function: %s", curl_easy_strerror(cRet)); cRet = curl_easy_setopt(c, CURLOPT_PROGRESSFUNCTION, &progress_callback_dispatcher); if(cRet != CURLE_OK) Fatal("Failed setting libcurl progress callback function: %s", curl_easy_strerror(cRet)); cRet = curl_easy_setopt(c, CURLOPT_PROGRESSDATA, this); if(cRet != CURLE_OK) Fatal("Failed setting libcurl progress callback object: %s", curl_easy_strerror(cRet)); /* Set username and password */ if(!mUser.empty()) { cRet = curl_easy_setopt(c, CURLOPT_USERNAME, mUser.c_str()); if(cRet != CURLE_OK) Error("Failed setting username: %s", curl_easy_strerror(cRet)); } if(!mPass.empty()) { cRet = curl_easy_setopt(c, CURLOPT_PASSWORD, mPass.c_str()); if(cRet != CURLE_OK) Error("Failed setting password: %s", curl_easy_strerror(cRet)); } /* Authenication preference */ cRet = curl_easy_setopt(c, CURLOPT_HTTPAUTH, CURLAUTH_ANY); if(cRet != CURLE_OK) Warning("Failed setting libcurl acceptable http authenication methods: %s", curl_easy_strerror(cRet)); /* Work loop */ for(int attempt=1;attempt<=CURL_MAXRETRY;attempt++) { tRet = 0; while(!bTerminate) { /* Do the work */ cRet = curl_easy_perform(c); if(mode == MODE_SINGLE) { if(cRet != CURLE_OK) { break; } /* Attempt to get the size of the file */ cRet = curl_easy_getinfo(c, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dSize); if(cRet != CURLE_OK) { break; } /* We need to lock for the offsets array and the condition variable */ lock(); /* Push the size into our offsets array */ if(dSize > 0) { single_offsets.push_back(dSize); } else { Fatal("Unable to get the size of the image"); } /* Signal the request complete condition variable */ tRet = pthread_cond_signal(&request_complete_cond); if(tRet != 0) { Error("Failed signaling request completed condition variable: %s",strerror(tRet)); } /* Unlock */ unlock(); } else if (mode == MODE_STREAM) { break; } } /* Return value checking */ if(cRet == CURLE_ABORTED_BY_CALLBACK || bTerminate) { /* Aborted */ break; } else if (cRet != CURLE_OK) { /* Some error */ Error("cURL Request failed: %s",curl_easy_strerror(cRet)); if(attempt < CURL_MAXRETRY) { Error("Retrying.. Attempt %d of %d",attempt,CURL_MAXRETRY); /* Do a reset */ lock(); databuffer.clear(); single_offsets.clear(); mode = MODE_UNSET; bReset = true; unlock(); } tRet = -50; } } /* Cleanup */ curl_easy_cleanup(c); c = NULL; return (void*)tRet; }
char *request(const char *url) { CURL *curl = NULL; CURLcode status; struct curl_slist *headers = NULL; char *data = NULL; long code; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(!curl) goto error; data = malloc(BUFFER_SIZE); if(!data) goto error; struct write_result write_result = { .data = data, .pos = 0 }; curl_easy_setopt(curl, CURLOPT_URL, url); headers = curl_slist_append(headers, "User-Agent: sea/0.1"); headers = curl_slist_append(headers, "Content-Type: application/json"); size_t token_size = 255; char *token = (char *)calloc(sizeof(char), token_size); get_auth_token(token, token_size); headers = curl_slist_append(headers, token); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // headers = curl_slist_append(headers, sprintf("Authorization: Bearer %s", token)); // curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result); status = curl_easy_perform(curl); if(status != 0) { fprintf(stderr, "error: unable to request data from %s:\n", url); fprintf(stderr, "%s\n", curl_easy_strerror(status)); goto error; } curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); if(code != 200) { fprintf(stderr, "error: server responded with code %ld\n", code); goto error; } curl_easy_cleanup(curl); curl_slist_free_all(headers); curl_global_cleanup(); /* zero-terminate the result */ data[write_result.pos] = '\0'; return data; error: if(data) free(data); if(curl) curl_easy_cleanup(curl); if(headers) curl_slist_free_all(headers); if(token) free(token); curl_global_cleanup(); return NULL; }
static int st_http_record_sync_info(u1db_sync_target *st, const char *source_replica_uid, int source_gen, const char *trans_id) { struct _http_state *state; struct _http_request req = {0}; char *url = NULL; int status; long http_code; json_object *json = NULL; const char *raw_body = NULL; int raw_len; struct curl_slist *headers = NULL; if (st == NULL || source_replica_uid == NULL || st->implementation == NULL) { return U1DB_INVALID_PARAMETER; } status = impl_as_http_state(st->implementation, &state); if (status != U1DB_OK) { return status; } status = u1db__format_sync_url(st, source_replica_uid, &url); if (status != U1DB_OK) { goto finish; } json = json_object_new_object(); if (json == NULL) { status = U1DB_NOMEM; goto finish; } json_object_object_add(json, "generation", json_object_new_int(source_gen)); json_object_object_add(json, "transaction_id", json_object_new_string(trans_id)); raw_body = json_object_to_json_string(json); raw_len = strlen(raw_body); req.state = state; req.put_buffer = raw_body; req.num_put_bytes = raw_len; headers = curl_slist_append(headers, "Content-Type: application/json"); // We know the message is going to be short, no reason to wait for server // confirmation of the post. headers = curl_slist_append(headers, "Expect:"); status = curl_easy_setopt(state->curl, CURLOPT_URL, url); if (status != CURLE_OK) { goto finish; } status = curl_easy_setopt(state->curl, CURLOPT_HTTPHEADER, headers); if (status != CURLE_OK) { goto finish; } status = curl_easy_setopt(state->curl, CURLOPT_UPLOAD, 1L); if (status != CURLE_OK) { goto finish; } status = curl_easy_setopt(state->curl, CURLOPT_PUT, 1L); if (status != CURLE_OK) { goto finish; } status = simple_set_curl_data(state->curl, &req, &req, &req); if (status != CURLE_OK) { goto finish; } status = curl_easy_setopt(state->curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)req.num_put_bytes); if (status != CURLE_OK) { goto finish; } status = maybe_sign_url(st, "PUT", url, &headers); if (status != U1DB_OK) { goto finish; } // Now actually send the data status = curl_easy_perform(state->curl); if (status != CURLE_OK) { goto finish; } status = curl_easy_getinfo(state->curl, CURLINFO_RESPONSE_CODE, &http_code); if (status != CURLE_OK) { goto finish; } if (http_code != 200 && http_code != 201) { status = http_code; goto finish; } finish: if (req.header_buffer != NULL) { free(req.header_buffer); } if (req.body_buffer != NULL) { free(req.body_buffer); } if (json != NULL) { json_object_put(json); } if (url != NULL) { free(url); } if (headers != NULL) { curl_slist_free_all(headers); } return status; }
static int st_http_get_sync_info(u1db_sync_target *st, const char *source_replica_uid, const char **st_replica_uid, int *st_gen, int *source_gen, char **trans_id) { struct _http_state *state; struct _http_request req = {0}; char *url = NULL; const char *tmp = NULL; int status; long http_code; struct curl_slist *headers = NULL; json_object *json = NULL, *obj = NULL; if (st == NULL || source_replica_uid == NULL || st_replica_uid == NULL || st_gen == NULL || source_gen == NULL || st->implementation == NULL) { return U1DB_INVALID_PARAMETER; } status = impl_as_http_state(st->implementation, &state); if (status != U1DB_OK) { return status; } headers = curl_slist_append(NULL, "Content-Type: application/json"); if (headers == NULL) { status = U1DB_NOMEM; goto finish; } req.state = state; status = u1db__format_sync_url(st, source_replica_uid, &url); if (status != U1DB_OK) { goto finish; } status = curl_easy_setopt(state->curl, CURLOPT_HTTPGET, 1L); if (status != CURLE_OK) { goto finish; } // status = curl_easy_setopt(state->curl, CURLOPT_USERAGENT, "..."); status = curl_easy_setopt(state->curl, CURLOPT_URL, url); if (status != CURLE_OK) { goto finish; } req.body_buffer = req.header_buffer = NULL; status = simple_set_curl_data(state->curl, &req, &req, NULL); if (status != CURLE_OK) { goto finish; } status = maybe_sign_url(st, "GET", url, &headers); if (status != U1DB_OK) { goto finish; } status = curl_easy_setopt(state->curl, CURLOPT_HTTPHEADER, headers); if (status != CURLE_OK) { goto finish; } // Now do the GET status = curl_easy_perform(state->curl); if (status != CURLE_OK) { goto finish; } status = curl_easy_getinfo(state->curl, CURLINFO_RESPONSE_CODE, &http_code); if (status != CURLE_OK) { goto finish; } if (http_code != 200) { // 201 for created? shouldn't happen on GET status = http_code; goto finish; } if (req.body_buffer == NULL) { status = U1DB_INVALID_HTTP_RESPONSE; goto finish; } json = json_tokener_parse(req.body_buffer); if (json == NULL) { status = U1DB_NOMEM; goto finish; } obj = json_object_object_get(json, "target_replica_uid"); if (obj == NULL) { status = U1DB_INVALID_HTTP_RESPONSE; goto finish; } if (state->replica_uid == NULL) { // we cache this on the state object, because the api for get_sync_info // asserts that callers do not have to free the returned string. // This isn't a functional problem, because if the sync target ever // changed its replica uid we'd be seriously broken anyway. state->replica_uid = strdup(json_object_get_string(obj)); } else { if (strcmp(state->replica_uid, json_object_get_string(obj)) != 0) { // Our http target changed replica_uid, this would be a really // strange bug status = U1DB_INVALID_HTTP_RESPONSE; goto finish; } } *st_replica_uid = state->replica_uid; if (*st_replica_uid == NULL) { status = U1DB_NOMEM; goto finish; } obj = json_object_object_get(json, "target_replica_generation"); if (obj == NULL) { status = U1DB_INVALID_HTTP_RESPONSE; goto finish; } *st_gen = json_object_get_int(obj); obj = json_object_object_get(json, "source_replica_generation"); if (obj == NULL) { status = U1DB_INVALID_HTTP_RESPONSE; goto finish; } *source_gen = json_object_get_int(obj); obj = json_object_object_get(json, "source_transaction_id"); if (obj == NULL) { *trans_id = NULL; } else { tmp = json_object_get_string(obj); if (tmp == NULL) { *trans_id = NULL; } else { *trans_id = strdup(tmp); if (*trans_id == NULL) { status = U1DB_NOMEM; } } } finish: if (req.header_buffer != NULL) { free(req.header_buffer); } if (req.body_buffer != NULL) { free(req.body_buffer); } if (json != NULL) { json_object_put(json); } if (url != NULL) { free(url); } curl_slist_free_all(headers); return status; }
const char* zxid_get_last_content_type(zxid_conf* cf) { char* ct; curl_easy_getinfo(cf->curl, CURLINFO_CONTENT_TYPE, &ct); return ct; }
int GTHTTP_sendRequest(const char *url, const unsigned char *request, size_t request_length, unsigned char **response, size_t *response_length, char **error) { int res = GT_UNKNOWN_ERROR; CURL *curl = NULL; char *err = NULL; internal_curl_receive_buffer receive_buffer = { NULL, 0 }; long http_res; if (url == NULL || response == NULL || response_length == NULL) { res = GT_INVALID_ARGUMENT; goto cleanup; } curl = curl_easy_init(); if (curl == NULL) { res = map_impl(CURLE_FAILED_INIT); goto cleanup; } if (error != NULL) { err = GT_malloc(CURL_ERROR_SIZE + 1); if (err == NULL) { res = GT_OUT_OF_MEMORY; goto cleanup; } } curl_easy_setopt(curl, CURLOPT_USERAGENT, agent); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); if (request != NULL) { curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (const void *) request); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request_length); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receiveDataFromLibCurl); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &receive_buffer); if (connect_timeout >= 0) { curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connect_timeout); } if (response_timeout >= 0) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, response_timeout); } curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); if (err != NULL) { curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, err); } res = curl_easy_perform(curl); if (res != CURLE_OK && error != NULL) { // we had some error and client code wanted the message *error = err; err = NULL; } if (res == CURLE_HTTP_RETURNED_ERROR) { if (curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_res) == CURLE_OK) { // now we have the actual HTTP response code, so we replace // the generic 'HTTP error' with the more accurate one res = map_http(http_res); goto cleanup; } } res = map_impl(res); if (res != GT_OK) { goto cleanup; } *response = receive_buffer.buffer; receive_buffer.buffer = NULL; *response_length = receive_buffer.buf_sz; cleanup: curl_easy_cleanup(curl); GT_free(receive_buffer.buffer); GT_free(err); return res; }
/* Http响应码 */ long HttpClient_ResponseCode(HttpClient *client) { //{{{ long httpCode; curl_easy_getinfo(client->curl, CURLINFO_RESPONSE_CODE, &httpCode); return httpCode; }
void mapcache_http_do_request(mapcache_context *ctx, mapcache_http *req, mapcache_buffer *data, apr_table_t *headers, long *http_code) { CURL *curl_handle; char error_msg[CURL_ERROR_SIZE]; int ret; struct curl_slist *curl_headers=NULL; curl_handle = curl_easy_init(); /* specify URL to get */ curl_easy_setopt(curl_handle, CURLOPT_URL, req->url); #ifdef DEBUG ctx->log(ctx, MAPCACHE_DEBUG, "curl requesting url %s",req->url); #endif /* send all data to this function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, _mapcache_curl_memory_callback); /* we pass our mapcache_buffer struct to the callback function */ curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)data); if(headers != NULL) { /* intercept headers */ struct _header_struct h; h.headers = headers; h.ctx=ctx; curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, _mapcache_curl_header_callback); curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, (void*)(&h)); } curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_msg); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, req->connection_timeout); curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, req->timeout); curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1); if(req->headers) { const apr_array_header_t *array = apr_table_elts(req->headers); apr_table_entry_t *elts = (apr_table_entry_t *) array->elts; int i; for (i = 0; i < array->nelts; i++) { char *val = elts[i].val; if(strchr(val,'{') && ctx->headers_in) { _header_replace_str(ctx,ctx->headers_in,&val); } curl_headers = curl_slist_append(curl_headers, apr_pstrcat(ctx->pool,elts[i].key,": ",val,NULL)); } } if(!req->headers || !apr_table_get(req->headers,"User-Agent")) { curl_headers = curl_slist_append(curl_headers, "User-Agent: "MAPCACHE_USERAGENT); } curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, curl_headers); if(req->post_body && req->post_len>0) { curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, req->post_body); } /* get it! */ ret = curl_easy_perform(curl_handle); if(http_code) curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, http_code); else curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1); if(ret != CURLE_OK) { ctx->set_error(ctx, 502, "curl failed to request url %s : %s", req->url, error_msg); } /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); }
// A download finished, find out what it was, whether there were any errors and // if so, how severe. If none, rename file and other such stuff. static qboolean finish_download (void) { int msgs_in_queue; CURLMsg *msg; CURLcode result; dlhandle_t *dl; CURL *curl; long response; double sec, bytes; char size[16], speed[16]; char temp[MAX_OSPATH]; qboolean fatal_error = qfalse; const char *err; print_type_t level; do { msg = curl_multi_info_read (curl_multi, &msgs_in_queue); if (!msg) break; if (msg->msg != CURLMSG_DONE) continue; curl = msg->easy_handle; dl = find_handle (curl); cls.download.current = NULL; cls.download.percent = 0; //filelist processing is done on read if (dl->file) { fclose (dl->file); dl->file = NULL; } curl_handles--; result = msg->data.result; switch (result) { //for some reason curl returns CURLE_OK for a 404... case CURLE_HTTP_RETURNED_ERROR: case CURLE_OK: curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &response); if (result == CURLE_OK && response == 200) { //success break; } err = http_strerror (response); //404 is non-fatal if (response == 404) { level = PRINT_ALL; goto fail1; } //every other code is treated as fatal //not marking download as done since //we are falling back to UDP level = PRINT_ERROR; fatal_error = qtrue; goto fail2; case CURLE_COULDNT_RESOLVE_HOST: case CURLE_COULDNT_CONNECT: case CURLE_COULDNT_RESOLVE_PROXY: //connection problems are fatal err = curl_easy_strerror (result); level = PRINT_ERROR; fatal_error = qtrue; goto fail2; default: err = curl_easy_strerror (result); level = PRINT_WARNING; fail1: //we mark download as done even if it errored //to prevent multiple attempts. CL_FinishDownload (dl->queue); fail2: Com_LPrintf (level, "[HTTP] %s [%s] [%d remaining file%s]\n", dl->queue->path, err, cls.download.pending, cls.download.pending == 1 ? "" : "s"); if (dl->path[0]) { remove (dl->path); dl->path[0] = 0; } if (dl->buffer) { Z_Free (dl->buffer); dl->buffer = NULL; } curl_multi_remove_handle (curl_multi, curl); continue; } //mark as done CL_FinishDownload (dl->queue); //show some stats curl_easy_getinfo (curl, CURLINFO_TOTAL_TIME, &sec); curl_easy_getinfo (curl, CURLINFO_SIZE_DOWNLOAD, &bytes); if (sec < 0.001) sec = 0.001; Com_FormatSizeLong (size, sizeof (size), bytes); Com_FormatSizeLong (speed, sizeof (speed), bytes / sec); //FIXME: //technically i shouldn't need to do this as curl will auto reuse the //existing handle when you change the url. however, the curl_handles goes //all weird when reusing a download slot in this way. if you can figure //out why, please let me know. curl_multi_remove_handle (curl_multi, curl); Com_Printf ("[HTTP] %s [%s, %s/sec] [%d remaining file%s]\n", dl->queue->path, size, speed, cls.download.pending, cls.download.pending == 1 ? "" : "s"); if (dl->path[0]) { //rename the temp file Q_snprintf (temp, sizeof(temp), "%s/%s", fs_gamedir, dl->queue->path); if (rename (dl->path, temp)) Com_EPrintf ("[HTTP] Failed to rename '%s' to '%s': %s\n", dl->path, dl->queue->path, strerror (errno)); dl->path[0] = 0; //a pak file is very special... if (dl->queue->type == DL_PAK) { CL_RestartFilesystem (qfalse); rescan_queue (); } } else if (!fatal_error) { parse_file_list (dl); } } while (msgs_in_queue > 0); //fatal error occured, disable HTTP if (fatal_error) { abort_downloads(); return qfalse; } // see if we have more to dl CL_RequestNextDownload (); return qtrue; }
void HTTPServerAgent::ThreadMain() { while (1) { // look for requests SDL_LockMutex(m_requestQueueLock); // if there's no requests, wait until the main thread wakes us if (m_requestQueue.empty()) SDL_CondWait(m_requestQueueCond, m_requestQueueLock); // woken up but nothing on the queue means we're being destroyed if (m_requestQueue.empty()) { // main thread is waiting for this lock, and will start // cleanup as soon as it has it SDL_UnlockMutex(m_requestQueueLock); // might be cleaned up already, so don't do anything else, // just get out of here return; } // grab a request Request req = m_requestQueue.front(); m_requestQueue.pop(); // done with the queue SDL_UnlockMutex(m_requestQueueLock); Json::FastWriter writer; req.buffer = writer.write(req.data); Response resp(req.onSuccess, req.onFail, req.userdata); curl_easy_setopt(m_curl, CURLOPT_URL, std::string(m_endpoint+"/"+req.method).c_str()); curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, req.buffer.size()); curl_easy_setopt(m_curl, CURLOPT_READDATA, &req); curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &resp); CURLcode rc = curl_easy_perform(m_curl); resp.success = rc == CURLE_OK; if (!resp.success) resp.buffer = std::string("call failed: " + std::string(curl_easy_strerror(rc))); if (resp.success) { uint32_t code; curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &code); if (code != 200) { resp.success = false; resp.buffer = stringf("call returned HTTP status: %0{d}", code); } } if (resp.success) { Json::Reader reader; resp.success = reader.parse(resp.buffer, resp.data, false); if (!resp.success) resp.buffer = std::string("JSON parse error: " + reader.getFormattedErrorMessages()); } SDL_LockMutex(m_responseQueueLock); m_responseQueue.push(resp); SDL_UnlockMutex(m_responseQueueLock); } }
int main(void) { CURLM *cm; CURLMsg *msg; long L; unsigned int C=0; int M, Q, U = -1; fd_set R, W, E; struct timeval T; curl_global_init(CURL_GLOBAL_ALL); cm = curl_multi_init(); /* we can optionally limit the total amount of connections this multi handle uses */ curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX); for (C = 0; C < MAX; ++C) { init(cm, C); } while (U) { while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U)); if (U) { FD_ZERO(&R); FD_ZERO(&W); FD_ZERO(&E); if (curl_multi_fdset(cm, &R, &W, &E, &M)) { fprintf(stderr, "E: curl_multi_fdset\n"); return EXIT_FAILURE; } if (curl_multi_timeout(cm, &L)) { fprintf(stderr, "E: curl_multi_timeout\n"); return EXIT_FAILURE; } if (L == -1) L = 100; if (M == -1) { #ifdef WIN32 Sleep(L); #else sleep(L / 1000); #endif } else { T.tv_sec = L/1000; T.tv_usec = (L%1000)*1000; if (0 > select(M+1, &R, &W, &E, &T)) { fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n", M+1, L, errno, strerror(errno)); return EXIT_FAILURE; } } } while ((msg = curl_multi_info_read(cm, &Q))) { if (msg->msg == CURLMSG_DONE) { char *url; CURL *e = msg->easy_handle; curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); fprintf(stderr, "R: %d - %s <%s>\n", msg->data.result, curl_easy_strerror(msg->data.result), url); curl_multi_remove_handle(cm, e); curl_easy_cleanup(e); } else { fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg); } if (C < CNT) { init(cm, C++); U++; /* just to prevent it from remaining at 0 if there are more URLs to get */ } } } curl_multi_cleanup(cm); curl_global_cleanup(); return EXIT_SUCCESS; }
static const char *url_execute(const char *p_url, MCUrlExecuteCallback p_callback, void *p_state) { const char *t_error; t_error = NULL; bool t_is_http, t_is_https; t_is_http = strncmp(p_url, "http", 4) == 0; t_is_https = strncmp(p_url, "https", 5) == 0; curl_slist *t_headers; t_headers = NULL; if (t_error == NULL && MChttpheaders != NULL && t_is_http) { if (!url_build_header_list(MChttpheaders, t_headers)) t_error = "couldn't build header list"; } CURL *t_url_handle; t_url_handle = NULL; if (t_error == NULL) { t_url_handle = curl_easy_init(); if (t_url_handle == NULL) t_error = "couldn't create handle"; } if (t_error == NULL) { if (curl_easy_setopt(t_url_handle, CURLOPT_URL, p_url) != CURLE_OK) t_error = "couldn't set url"; } if (t_error == NULL) { if (curl_easy_setopt(t_url_handle, CURLOPT_WRITEFUNCTION, url_write_callback) != CURLE_OK) t_error = "couldn't set callback"; } if (t_error == NULL && t_headers != NULL) { if (curl_easy_setopt(t_url_handle, CURLOPT_HTTPHEADER, t_headers) != CURLE_OK) t_error = "couldn't set headers"; } if (t_error == NULL && t_is_https) { if (curl_easy_setopt(t_url_handle, CURLOPT_SSL_VERIFYPEER, 1) != CURLE_OK || curl_easy_setopt(t_url_handle, CURLOPT_SSL_VERIFYHOST, 2) != CURLE_OK || (MCsslcertificates != NULL && curl_easy_setopt(t_url_handle, CURLOPT_CAINFO, MCsslcertificates) != CURLE_OK)) t_error = "couldn't configure ssl"; } if (t_error == NULL) { #if LIBCURL_VERSION_MINOR >= 19 if (curl_easy_setopt(t_url_handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS) != CURLE_OK || curl_easy_setopt(t_url_handle, CURLOPT_FOLLOWLOCATION, 1) != CURLE_OK) t_error = "couldn't configure follow"; #endif } // MM-2011-07-07: Added support for specifying which network interface to use. if (t_error == NULL) { if (MCdefaultnetworkinterface != NULL) if (curl_easy_setopt(t_url_handle, CURLOPT_INTERFACE, MCdefaultnetworkinterface) != CURLE_OK) t_error = "couldn't set network interface"; } if (t_error == NULL && p_callback != NULL) t_error = p_callback(p_state, t_url_handle); if (t_error == NULL) { char t_error_buffer[CURL_ERROR_SIZE]; curl_easy_setopt(t_url_handle, CURLOPT_ERRORBUFFER, t_error_buffer); MCurlresult -> clear(); MCresult -> clear(); if (curl_easy_perform(t_url_handle) == CURLE_OK) { if (t_is_http) { long t_code; if (curl_easy_getinfo(t_url_handle, CURLINFO_RESPONSE_CODE, &t_code) == CURLE_OK) { if (t_code != 200) { static char t_error_str[64]; sprintf(t_error_str, "error %ld", t_code); MCresult -> copysvalue(t_error_str); } } else t_error = "couldn't fetch response code"; } } else { static char t_error_str[CURL_ERROR_SIZE + 64]; sprintf(t_error_str, "error %s", t_error_buffer); MCresult -> copysvalue(t_error_str); } } if (t_error == NULL && t_is_http) { } if (t_url_handle != NULL) curl_easy_cleanup(t_url_handle); if (t_headers != NULL) curl_slist_free_all(t_headers); return t_error; }
http_response_t *http_request(const http_request_t *request) { CURL *curl; CURLcode curlResult; http_response_t *response; read_buffer readBuffer = { 0 }; write_buffer writeBuffer; curl = curl_easy_init(); if (curl == NULL) return NULL; if (request->type == HTTP_DATA_JSON && request->root != NULL) { readBuffer.ptr = json_dumps(request->root, JSON_COMPACT); readBuffer.length = strlen(readBuffer.ptr); readBuffer.position = 0; } else if (request->type == HTTP_DATA_RAW && request->body != NULL) { readBuffer.ptr = request->body; readBuffer.length = request->size; readBuffer.position = 0; } writeBuffer.ptr = NULL; writeBuffer.length = 0; writeBuffer.capacity = 0; curl_slist *headers = NULL; if (request->type == HTTP_DATA_JSON) { headers = curl_slist_append(headers, "Accept: " MIME_TYPE_APPLICATION_JSON); if (request->root != NULL) { headers = curl_slist_append(headers, "Content-Type: " MIME_TYPE_APPLICATION_JSON); } } if (readBuffer.ptr != NULL) { char contentLengthHeaderValue[64]; snprintf(contentLengthHeaderValue, sizeof(contentLengthHeaderValue), "Content-Length: %zu", readBuffer.length); headers = curl_slist_append(headers, contentLengthHeaderValue); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, readBuffer.ptr); } curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request->method); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true); curl_easy_setopt(curl, CURLOPT_USERAGENT, OPENRCT2_USER_AGENT); curl_easy_setopt(curl, CURLOPT_URL, request->url); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeBuffer); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_request_write_func); curlResult = curl_easy_perform(curl); if (request->type == HTTP_DATA_JSON && request->root != NULL) { free(readBuffer.ptr); } if (curlResult != CURLE_OK) { log_error("HTTP request failed: %s.", curl_easy_strerror(curlResult)); if (writeBuffer.ptr != NULL) free(writeBuffer.ptr); return NULL; } long httpStatusCode; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpStatusCode); char* contentType; curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &contentType); // Null terminate the response buffer writeBuffer.length++; writeBuffer.ptr = (char*)realloc(writeBuffer.ptr, writeBuffer.length); writeBuffer.capacity = writeBuffer.length; writeBuffer.ptr[writeBuffer.length - 1] = 0; response = NULL; // Parse as JSON if response is JSON if (contentType != NULL && strstr(contentType, "json") != NULL) { json_t *root; json_error_t error; root = json_loads(writeBuffer.ptr, 0, &error); if (root != NULL) { response = (http_response_t*) malloc(sizeof(http_response_t)); response->tag = request->tag; response->status_code = (sint32) httpStatusCode; response->root = root; response->type = HTTP_DATA_JSON; response->size = writeBuffer.length; } free(writeBuffer.ptr); } else { response = (http_response_t*) malloc(sizeof(http_response_t)); response->tag = request->tag; response->status_code = (sint32) httpStatusCode; response->body = writeBuffer.ptr; response->type = HTTP_DATA_RAW; response->size = writeBuffer.length; } curl_easy_cleanup(curl); return response; }
char *DL_GetString(const char *url) { CURL *curl = NULL; CURLcode status; char *data = NULL; long code; write_result_t write_result = { NULL, 0 }; if (!url) { Com_Printf("Empty download URL\n"); return NULL; } DL_InitDownload(); curl = curl_easy_init(); data = (char *)malloc(GET_BUFFER_SIZE); if (!data) { goto error_get; } else { write_result.data = data; } curl_easy_setopt(curl, CURLOPT_USERAGENT, va("%s %s", APP_NAME "/" APP_VERSION, curl_version())); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, DL_write_function); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result); status = curl_easy_perform(curl); if (status != 0) { Com_Printf("DL_GetString unable to request data from %s\n", url); Com_Printf("DL_GetString %s\n", curl_easy_strerror(status)); goto error_get; } curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); if (code != 200) { Com_Printf("DL_GetString server responded with code %ld\n", code); goto error_get; } curl_easy_cleanup(curl); data[write_result.pos] = '\0'; return data; error_get: if (curl) { curl_easy_cleanup(curl); } if (data) { free(data); } return NULL; }
/* =============== CL_FinishHTTPDownload A download finished, find out what it was, whether there were any errors and if so, how severe. If none, rename file and other such stuff. =============== */ static void HTTP_FinishDownload (void) { int msgs_in_queue; CURLMsg *msg; CURLcode result; dlhandle_t *dl; CURL *curl; long responseCode; double timeTaken; double fileSize; unsigned i; do { msg = curl_multi_info_read (multi, &msgs_in_queue); if (!msg) { gi.dprintf ("HTTP_FinishDownload: Odd, no message for us...\n"); return; } if (msg->msg != CURLMSG_DONE) { gi.dprintf ("HTTP_FinishDownload: Got some weird message...\n"); continue; } curl = msg->easy_handle; for (i = 0; i < MAX_DOWNLOADS; i++) { if (downloads[i].curl == curl) break; } if (i == MAX_DOWNLOADS) TDM_Error ("HTTP_FinishDownload: Handle not found!"); dl = &downloads[i]; result = msg->data.result; switch (result) { //for some reason curl returns CURLE_OK for a 404... case CURLE_HTTP_RETURNED_ERROR: case CURLE_OK: curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &responseCode); if (responseCode == 404) { TDM_HandleDownload (dl->tdm_handle, NULL, 0, responseCode); gi.dprintf ("HTTP: %s: 404 File Not Found\n", dl->URL); curl_multi_remove_handle (multi, dl->curl); dl->inuse = false; continue; } else if (responseCode == 200) { TDM_HandleDownload (dl->tdm_handle, dl->tempBuffer, dl->position, responseCode); gi.TagFree (dl->tempBuffer); } else { TDM_HandleDownload (dl->tdm_handle, NULL, 0, responseCode); if (dl->tempBuffer) gi.TagFree (dl->tempBuffer); } break; //fatal error default: TDM_HandleDownload (dl->tdm_handle, NULL, 0, 0); gi.dprintf ("HTTP Error: %s: %s\n", dl->URL, curl_easy_strerror (result)); curl_multi_remove_handle (multi, dl->curl); dl->inuse = false; continue; } //show some stats curl_easy_getinfo (curl, CURLINFO_TOTAL_TIME, &timeTaken); curl_easy_getinfo (curl, CURLINFO_SIZE_DOWNLOAD, &fileSize); //FIXME: //technically i shouldn't need to do this as curl will auto reuse the //existing handle when you change the URL. however, the handleCount goes //all weird when reusing a download slot in this way. if you can figure //out why, please let me know. curl_multi_remove_handle (multi, dl->curl); dl->inuse = false; gi.dprintf ("HTTP: Finished %s: %.f bytes, %.2fkB/sec\n", dl->URL, fileSize, (fileSize / 1024.0) / timeTaken); } while (msgs_in_queue > 0); }
long Connection::GetResponseCode() const { long response_code = 0; curl_easy_getinfo(handle_, CURLINFO_RESPONSE_CODE, &response_code); return response_code; }
ntv_t * libsvc_http_json_get(const char *url, const char *auth, char *errbuf, size_t errlen) { cache_entry_t *ce; static cache_entry_t *skel; time_t now = time(NULL); pthread_mutex_lock(&cache_mutex); if(skel == NULL) skel = calloc(1, sizeof(cache_entry_t)); skel->ce_url = (char *)url; skel->ce_auth = (char *)auth; ce = RB_INSERT_SORTED(&cache_entries, skel, ce_link, cache_entry_cmp); if(ce == NULL) { // Nothing found -> New item 'skel' was inserted ce = skel; skel = NULL; ce->ce_url = strdup(url); ce->ce_auth = auth ? strdup(auth) : NULL; } while(ce->ce_status == -1) pthread_cond_wait(&cache_cond, &cache_mutex); if(ce->ce_expire > now) { if(ce->ce_status == 200) { ntv_t *m = ntv_json_deserialize(ce->ce_response, errbuf, errlen); pthread_mutex_unlock(&cache_mutex); return m; } if(ce->ce_status >= 400) { snprintf(errbuf, errlen, "HTTP Error %d", ce->ce_status); pthread_mutex_unlock(&cache_mutex); return NULL; } } ce->ce_status = -1; char *out; size_t outlen; FILE *f = open_buffer(&out, &outlen); struct curl_slist *slist = NULL; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, f); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libsvc"); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, hdrfunc); curl_easy_setopt(curl, CURLOPT_HEADERDATA, ce); slist = curl_slist_append(slist, "Accept: application/json"); if(auth != NULL) slist = curl_slist_append(slist, tsprintf("Authorization: %s", auth)); if(ce->ce_etag != NULL) slist = curl_slist_append(slist, tsprintf("If-None-Match: %s", ce->ce_etag)); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); ce->ce_expire = 0; free(ce->ce_etag); ce->ce_etag = NULL; pthread_mutex_unlock(&cache_mutex); CURLcode result = curl_easy_perform(curl); curl_slist_free_all(slist); pthread_mutex_lock(&cache_mutex); pthread_cond_broadcast(&cache_cond); fwrite("", 1, 1, f); fclose(f); if(result) { snprintf(errbuf, errlen, "%s", curl_easy_strerror(result)); curl_easy_cleanup(curl); ce->ce_expire = 0; ce->ce_status = 0; free(ce->ce_response); ce->ce_response = NULL; pthread_mutex_unlock(&cache_mutex); free(out); return NULL; } long http_code = 0; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); ce->ce_status = http_code; if(http_code == 304) { ce->ce_status = 200; } else if(http_code == 200) { free(ce->ce_response); ce->ce_response = out; out = NULL; } else { snprintf(errbuf, errlen, "HTTP Error %lu", http_code); free(ce->ce_response); ce->ce_response = NULL; } free(out); curl_easy_cleanup(curl); ntv_t *m = NULL; if(ce->ce_response != NULL) m = ntv_json_deserialize(ce->ce_response, errbuf, errlen); pthread_mutex_unlock(&cache_mutex); return m; }
bool CurlCore::VerifyUser(const string& sAuthUri, const string &sUserName, const string &sUserRepo, string &sTTL, string sCertificatePath) { CURLcode res; string sUri = sAuthUri + "/" + sUserName; if(sUserRepo != "") sUri += "/"+sUserRepo+"/releases"; //printf("Req URI: "<<sUri<<endl; if(m_pCurl) { curl_easy_setopt(m_pCurl, CURLOPT_URL, sUri.c_str()); /* example.com is redirected, so we tell libcurl to follow redirection */ curl_easy_setopt(m_pCurl, CURLOPT_FOLLOWLOCATION, 1L); // User-agent header string sUserAgent = "CeasyCurl (sharma) v1.0"; curl_easy_setopt(m_pCurl, CURLOPT_USERAGENT, sUserAgent.c_str()); if(sCertificatePath == "") { #ifdef ENABLE_HTTPS curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L); #endif } else { curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 1); curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 2); curl_easy_setopt(m_pCurl, CURLOPT_CAINFO, sCertificatePath.c_str()); } curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteCallback); /* Perform the request, res will get the return code */ res = curl_easy_perform(m_pCurl); //printf("Curl req performed"<<endl; char * url; long respcode=0; curl_easy_getinfo(m_pCurl, CURLINFO_RESPONSE_CODE, &respcode); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); switch(respcode) { case 200: { // Parse the static string buffer readBuffer for username vector<string> sValues; printf("Your querry Succeeded :)\n"); ParseJsonData(readBuffer, sValues); //for(int i=0;i<sValues.size();i++) { //printf("Response Values = %s\n", sValues[i].c_str()); } readBuffer.clear(); return true; break; } case 401: printf("Un-Authorize\n"); break; case 504: printf("Server Time-out\n"); break; case 400: printf("Page not found\n"); break; default: break; } readBuffer.clear(); } return false; }
static void* ov_http_client_complete_task(void* data) { CURLM* handle; CURLMsg* message; VALUE error_class; VALUE error_instance; VALUE transfer; long code; ov_http_client_object* client_ptr; ov_http_request_object* request_ptr; ov_http_response_object* response_ptr; ov_http_transfer_object* transfer_ptr; /* The passed pointer is the libcurl message describing the completed transfer: */ message = (CURLMsg*) data; handle = message->easy_handle; /* The transfer is stored as the private data of the libcurl easy handle: */ curl_easy_getinfo(handle, CURLINFO_PRIVATE, &transfer); /* Get the pointers to the transfer, client and response: */ ov_http_transfer_ptr(transfer, transfer_ptr); ov_http_client_ptr(transfer_ptr->client, client_ptr); ov_http_request_ptr(transfer_ptr->request, request_ptr); ov_http_response_ptr(transfer_ptr->response, response_ptr); /* Remove the transfer from the pending hash: */ rb_hash_delete(client_ptr->pending, transfer_ptr->request); if (message->data.result == CURLE_OK) { /* Copy the response code and the response body to the response object: */ curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &code); response_ptr->code = LONG2NUM(code); response_ptr->body = rb_funcall(transfer_ptr->out, STRING_ID, 0); /* Put the request and the response in the completed transfers hash: */ rb_hash_aset(client_ptr->completed, transfer_ptr->request, transfer_ptr->response); /* Send a summary of the response to the log: */ ov_http_client_log_info( client_ptr->log, "Received response code %"PRIsVALUE" for %"PRIsVALUE" request to URL '%"PRIsVALUE"'.", response_ptr->code, request_ptr->method, request_ptr->url ); } else { /* Select the error class according to the kind of error returned by libcurl: */ switch (message->data.result) { case CURLE_COULDNT_CONNECT: case CURLE_COULDNT_RESOLVE_HOST: case CURLE_COULDNT_RESOLVE_PROXY: error_class = ov_connection_error_class; break; case CURLE_OPERATION_TIMEDOUT: error_class = ov_timeout_error_class; break; default: error_class = ov_error_class; } /* Put the request and error in the completed transfers hash: */ error_instance = rb_sprintf("Can't send request: %s", curl_easy_strerror(message->data.result)); error_instance = rb_class_new_instance(1, &error_instance, error_class); rb_hash_aset(client_ptr->completed, transfer_ptr->request, error_instance); } /* Now that the libcurl easy handle is released, we can release the headers as well: */ curl_slist_free_all(transfer_ptr->headers); return NULL; }
const char *wswcurl_getip(wswcurl_req *req) { char *ipstr = NULL; curl_easy_getinfo( req->curl, CURLINFO_PRIMARY_IP, &ipstr ); return ipstr; }
static gboolean fetch_tweets(gpointer GOL_UNUSED_ARG(data)) { if (!enable) return FALSE; CURL* curl = NULL; CURLcode res = CURLE_OK; long http_status = 0; MEMFILE* mbody = NULL; char* body = NULL; xmlDocPtr doc = NULL; xmlNodeSetPtr nodes = NULL; xmlXPathContextPtr ctx = NULL; xmlXPathObjectPtr path = NULL; mbody = memfopen(); curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_URL, "https://api.twitter.com/1/statuses/public_timeline.xml"); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, REQUEST_TIMEOUT); curl_easy_setopt(curl, CURLOPT_TIMEOUT, REQUEST_TIMEOUT); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, mbody); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); res = curl_easy_perform(curl); if (res == CURLE_OK) curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_status); curl_easy_cleanup(curl); body = memfstrdup(mbody); memfclose(mbody); if (res != CURLE_OK || http_status != 200) goto leave; doc = body ? xmlParseDoc((xmlChar*) body) : NULL; if (!doc) goto leave; ctx = xmlXPathNewContext(doc); if (!ctx) goto leave; path = xmlXPathEvalExpression((xmlChar*)"/statuses/status", ctx); if (!path || xmlXPathNodeSetIsEmpty(path->nodesetval)) goto leave; nodes = path->nodesetval; const size_t length = xmlXPathNodeSetGetLength(nodes); gchar* first_id = NULL; for (size_t n = 0; n < length; n++) { char* id = NULL; char* user_id = NULL; char* icon = NULL; char* user_name = NULL; char* text = NULL; xmlNodePtr status = nodes->nodeTab[n]; if (status->type != XML_ATTRIBUTE_NODE && status->type != XML_ELEMENT_NODE && status->type != XML_CDATA_SECTION_NODE) continue; status = status->children; while(status) { if (!strcmp("id", (char*) status->name)) id = (char*) status->children->content; if (!strcmp("text", (char*) status->name)) { if (status->children) text = (char*) status->children->content; } /* user nodes */ if (!strcmp("user", (char*) status->name)) { xmlNodePtr user = status->children; while(user) { if (!strcmp("id", (char*) user->name)) user_id = XML_CONTENT(user); if (!strcmp("screen_name", (char*) user->name)) user_name = XML_CONTENT(user); if (!strcmp("profile_image_url", (char*) user->name)) { icon = (char*) g_strchug(g_strchomp((gchar*) XML_CONTENT(user))); } user = user->next; } } status = status->next; } if (!first_id) first_id = id; if (id && last_id && !strcmp(id, last_id)) break; if (text && user_id) { NOTIFICATION_INFO* ni = g_new0(NOTIFICATION_INFO, 1); ni->title = g_strdup(user_name); ni->text = g_strdup(text); ni->icon = g_strdup(icon); g_timeout_add(1000 * (n+1), delay_show, ni); } } if (last_id) g_free(last_id); if (first_id) last_id = g_strdup(first_id); leave: free(body); if (path) xmlXPathFreeObject(path); if (ctx) xmlXPathFreeContext(ctx); if (doc) xmlFreeDoc(doc); g_timeout_add(1000 * length, fetch_tweets, NULL); return FALSE; }