Esempio n. 1
0
File: curl.c Progetto: diegonc/burp
long aur_login(void) {
  long code, ret = 0;
  CURLcode status;
  struct curl_httppost *post, *last;
  struct curl_slist *headers;
  static struct write_result response;

  post = last = NULL;
  headers = NULL;
  response.memory = NULL;
  response.size = 0;

  curl_formadd(&post, &last,
    CURLFORM_COPYNAME, AUR_LOGIN_FIELD,
    CURLFORM_COPYCONTENTS, config->user, CURLFORM_END);
  curl_formadd(&post, &last,
    CURLFORM_COPYNAME, AUR_PASSWD_FIELD,
    CURLFORM_COPYCONTENTS, config->password, CURLFORM_END);

  if (config->persist) {
    curl_formadd(&post, &last,
      CURLFORM_COPYNAME, AUR_REMEMBERME_FIELD,
      CURLFORM_COPYCONTENTS, "on", CURLFORM_END);
  }

  headers = curl_slist_append(headers, "Expect:");

  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  curl_easy_setopt(curl, CURLOPT_URL, AUR_LOGIN_URL);

  if (config->verbose > 0) {
    printf("Logging in to AUR as user %s\n", config->user);
  }

  status = curl_easy_perform(curl);
  if(status != 0) {
    fprintf(stderr, "curl error: unable to send data to %s\n", AUR_LOGIN_URL);
    fprintf(stderr, "%s\n", curl_easy_strerror(status));
    ret = status;
    goto cleanup;
  }

  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
  if(code != 200) {
    fprintf(stderr, "curl error: server responded with code %ld\n", code);
    ret = code;
    goto cleanup;
  }

  if (config->verbose > 1) {
    printf("%s\n", response.memory);
  }

  if (strstr(response.memory, AUR_LOGIN_FAIL_MSG) != NULL) {
    fprintf(stderr, "Error: %s\n", AUR_LOGIN_FAIL_MSG);
    ret = 1L; /* Reuse an uncommon curl error */
  }

cleanup:
  free(response.memory);
  curl_slist_free_all(headers);
  curl_formfree(post);

  /* We're done using the password. Overwrite its memory */
  config->password = memset(config->password, 42, strlen(config->password));

  return(ret);
}
Esempio n. 2
0
/* test function */
int test(char *URL)
{
    int res;
    CURLSHcode scode;
    char *url;
    struct Tdata tdata;
    CURL *curl;
    CURLSH *share;
    struct curl_slist *headers;
    int i;
    struct userdata user;

    user.text = (char *)"Pigs in space";
    user.counter = 0;

    printf( "GLOBAL_INIT\n" );
    curl_global_init( CURL_GLOBAL_ALL );

    /* prepare share */
    printf( "SHARE_INIT\n" );
    share = curl_share_init();
    curl_share_setopt( share, CURLSHOPT_LOCKFUNC,   lock);
    curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, unlock);
    curl_share_setopt( share, CURLSHOPT_USERDATA,   &user);
    printf( "CURL_LOCK_DATA_COOKIE\n" );
    curl_share_setopt( share, CURLSHOPT_SHARE,      CURL_LOCK_DATA_COOKIE);
    printf( "CURL_LOCK_DATA_DNS\n" );
    curl_share_setopt( share, CURLSHOPT_SHARE,      CURL_LOCK_DATA_DNS);

    res = 0;

    /* start treads */
    for (i=1; i<=THREADS; i++ )
    {

        /* set thread data */
        tdata.url   = suburl( URL, i ); /* must be freed */
        tdata.share = share;

        /* simulate thread, direct call of "thread" function */
        printf( "*** run %d\n",i );
        fire( &tdata );

        free( tdata.url );

    }


    /* fetch a another one and save cookies */
    printf( "*** run %d\n", i );
    curl = curl_easy_init();

    url = suburl( URL, i );
    headers = sethost( NULL );
    curl_easy_setopt( curl, CURLOPT_HTTPHEADER, (void*)headers );
    curl_easy_setopt( curl, CURLOPT_URL,        url );
    printf( "CURLOPT_SHARE\n" );
    curl_easy_setopt( curl, CURLOPT_SHARE,      share );
    printf( "CURLOPT_COOKIEJAR\n" );
    curl_easy_setopt( curl, CURLOPT_COOKIEJAR,  JAR );

    printf( "PERFORM\n" );
    curl_easy_perform( curl );

    /* try to free share, expect to fail because share is in use*/
    printf( "try SHARE_CLEANUP...\n" );
    scode = curl_share_cleanup( share );
    if ( scode==CURLSHE_OK )
    {
        fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
        share = NULL;
    }
    else
    {
        printf( "SHARE_CLEANUP failed, correct\n" );
    }

    /* clean up last handle */
    printf( "CLEANUP\n" );
    curl_easy_cleanup( curl );
    curl_slist_free_all( headers );
    free(url);


    /* free share */
    printf( "SHARE_CLEANUP\n" );
    scode = curl_share_cleanup( share );
    if ( scode!=CURLSHE_OK )
    {
        fprintf(stderr, "curl_share_cleanup failed, code errno %d\n", scode);
    }

    printf( "GLOBAL_CLEANUP\n" );
    curl_global_cleanup();

    return res;
}
Esempio n. 3
0
int main(int argc, char **argv)
{
  CURL *curl;
  CURLcode res;
  FILE *hd_src;
  struct stat file_info;
  curl_off_t fsize;

  struct curl_slist *headerlist=NULL;
  static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  static const char buf_2 [] = "RNTO " RENAME_FILE_TO;

  /* get the file size of the local file */
  if(stat(LOCAL_FILE, &file_info)) {
    printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
    return 1;
  }
  fsize = (curl_off_t)file_info.st_size;

  printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);

  /* get a FILE * of the same file */
  hd_src = fopen(LOCAL_FILE, "rb");

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if(curl) {
    /* build a list of commands to pass to libcurl */
    headerlist = curl_slist_append(headerlist, buf_1);
    headerlist = curl_slist_append(headerlist, buf_2);

    /* we want to use our own read function */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

    /* enable uploading */
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

    /* specify target */
    curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);

    /* pass in that last of FTP commands to run after the transfer */
    curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);

    /* now specify which file to upload */
    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

    /* Set the size of the file to upload (optional).  If you give a *_LARGE
       option you MUST make sure that the type of the passed-in argument is a
       curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
       make sure that to pass in a type 'long' argument. */
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                     (curl_off_t)fsize);

    /* Now run off and do what you've been told! */
    res = curl_easy_perform(curl);

    /* clean up the FTP commands list */
    curl_slist_free_all (headerlist);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  fclose(hd_src); /* close the local file */

  curl_global_cleanup();
  return 0;
}
Esempio n. 4
0
/* test function */
int test(char *URL)
{
  int res;
  CURLSHcode scode = CURLSHE_OK;
  char *url = NULL;
  struct Tdata tdata;
  CURL *curl;
  CURLSH *share;
  struct curl_slist *headers = NULL;
  int i;
  struct userdata user;

  user.text = (char *)"Pigs in space";
  user.counter = 0;

  printf( "GLOBAL_INIT\n" );
  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
    fprintf(stderr, "curl_global_init() failed\n");
    return TEST_ERR_MAJOR_BAD;
  }

  /* prepare share */
  printf( "SHARE_INIT\n" );
  if ((share = curl_share_init()) == NULL) {
    fprintf(stderr, "curl_share_init() failed\n");
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  if ( CURLSHE_OK == scode ) {
    printf( "CURLSHOPT_LOCKFUNC\n" );
    scode = curl_share_setopt( share, CURLSHOPT_LOCKFUNC, my_lock);
  }
  if ( CURLSHE_OK == scode ) {
    printf( "CURLSHOPT_UNLOCKFUNC\n" );
    scode = curl_share_setopt( share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  }
  if ( CURLSHE_OK == scode ) {
    printf( "CURLSHOPT_USERDATA\n" );
    scode = curl_share_setopt( share, CURLSHOPT_USERDATA, &user);
  }
  if ( CURLSHE_OK == scode ) {
    printf( "CURL_LOCK_DATA_COOKIE\n" );
    scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  }
  if ( CURLSHE_OK == scode ) {
    printf( "CURL_LOCK_DATA_DNS\n" );
    scode = curl_share_setopt( share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  }

  if ( CURLSHE_OK != scode ) {
    fprintf(stderr, "curl_share_setopt() failed\n");
    curl_share_cleanup(share);
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  /* initial cookie manipulation */
  if ((curl = curl_easy_init()) == NULL) {
    fprintf(stderr, "curl_easy_init() failed\n");
    curl_share_cleanup(share);
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }
  printf( "CURLOPT_SHARE\n" );
  test_setopt( curl, CURLOPT_SHARE,      share );
  printf( "CURLOPT_COOKIELIST injected_and_clobbered\n" );
  test_setopt( curl, CURLOPT_COOKIELIST,
               "Set-Cookie: injected_and_clobbered=yes; "
               "domain=host.foo.com; expires=Sat Feb 2 11:56:27 GMT 2030" );
  printf( "CURLOPT_COOKIELIST ALL\n" );
  test_setopt( curl, CURLOPT_COOKIELIST, "ALL" );
  printf( "CURLOPT_COOKIELIST session\n" );
  test_setopt( curl, CURLOPT_COOKIELIST, "Set-Cookie: session=elephants" );
  printf( "CURLOPT_COOKIELIST injected\n" );
  test_setopt( curl, CURLOPT_COOKIELIST,
               "Set-Cookie: injected=yes; domain=host.foo.com; "
               "expires=Sat Feb 2 11:56:27 GMT 2030" );
  printf( "CURLOPT_COOKIELIST SESS\n" );
  test_setopt( curl, CURLOPT_COOKIELIST, "SESS" );
  printf( "CLEANUP\n" );
  curl_easy_cleanup( curl );


  res = 0;

  /* start treads */
  for (i=1; i<=THREADS; i++ ) {

    /* set thread data */
    tdata.url   = suburl( URL, i ); /* must be curl_free()d */
    tdata.share = share;

    /* simulate thread, direct call of "thread" function */
    printf( "*** run %d\n",i );
    fire( &tdata );

    curl_free( tdata.url );

  }


  /* fetch a another one and save cookies */
  printf( "*** run %d\n", i );
  if ((curl = curl_easy_init()) == NULL) {
    fprintf(stderr, "curl_easy_init() failed\n");
    curl_share_cleanup(share);
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  url = suburl( URL, i );
  headers = sethost( NULL );
  test_setopt( curl, CURLOPT_HTTPHEADER, headers );
  test_setopt( curl, CURLOPT_URL,        url );
  printf( "CURLOPT_SHARE\n" );
  test_setopt( curl, CURLOPT_SHARE,      share );
  printf( "CURLOPT_COOKIEJAR\n" );
  test_setopt( curl, CURLOPT_COOKIEJAR,  JAR );
  printf( "CURLOPT_COOKIELIST FLUSH\n" );
  test_setopt( curl, CURLOPT_COOKIELIST, "FLUSH" );

  printf( "PERFORM\n" );
  curl_easy_perform( curl );

  /* try to free share, expect to fail because share is in use*/
  printf( "try SHARE_CLEANUP...\n" );
  scode = curl_share_cleanup( share );
  if ( scode==CURLSHE_OK )
  {
    fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
    share = NULL;
  } else {
    printf( "SHARE_CLEANUP failed, correct\n" );
  }

test_cleanup:

  /* clean up last handle */
  printf( "CLEANUP\n" );
  curl_easy_cleanup( curl );

  if ( headers )
    curl_slist_free_all( headers );

  if ( url )
    curl_free(url);

  /* free share */
  printf( "SHARE_CLEANUP\n" );
  scode = curl_share_cleanup( share );
  if ( scode!=CURLSHE_OK )
    fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
            (int)scode);

  printf( "GLOBAL_CLEANUP\n" );
  curl_global_cleanup();

  return res;
}
Esempio n. 5
0
json_t* authRequest(Parameters& params, std::string url, std::string request, std::string options) {
  // nonce
  struct timeval tv;
  gettimeofday(&tv, NULL);
  unsigned long long nonce = (tv.tv_sec * 1000.0) + (tv.tv_usec * 0.001) + 0.5;

  // check if options parameter is empty
  std::ostringstream oss;
  if (options.empty()) {
    oss << "{\"request\":\"/v1/" << request << "\",\"nonce\":\"" << nonce << "\"}";
  }
  else {
    oss << "{\"request\":\"/v1/" << request << "\",\"nonce\":\"" << nonce << "\", " << options << "}";
  }
  std::string tmpPayload = base64_encode(reinterpret_cast<const unsigned char*>(oss.str().c_str()), oss.str().length());

  oss.clear();
  oss.str("");

  oss << "X-GEMINI-PAYLOAD:" << tmpPayload;
  std::string payload;
  payload = oss.str();

  oss.clear();
  oss.str("");

  // build the signature
  unsigned char* digest;

  // Using sha384 hash engine
  digest = HMAC(EVP_sha384(), params.geminiSecret.c_str(), strlen(params.geminiSecret.c_str()), (unsigned char*)tmpPayload.c_str(), strlen(tmpPayload.c_str()), NULL, NULL);

  char mdString[SHA384_DIGEST_LENGTH+100];   // FIXME +100
  for (int i = 0; i < SHA384_DIGEST_LENGTH; ++i) {
    sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
  }
  oss.clear();
  oss.str("");
  oss << "X-GEMINI-SIGNATURE:" << mdString;

  // cURL headers
  struct curl_slist *headers = NULL;
  std::string api = "X-GEMINI-APIKEY:" + std::string(params.geminiApi);
  headers = curl_slist_append(headers, api.c_str());
  headers = curl_slist_append(headers, payload.c_str());
  headers = curl_slist_append(headers, oss.str().c_str());

  // cURL request
  CURLcode resCurl;
//  curl = curl_easy_init();
  if (params.curl) {
    std::string readBuffer;
    curl_easy_setopt(params.curl, CURLOPT_POST, 1L);
    // curl_easy_setopt(params.curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(params.curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(params.curl, CURLOPT_POSTFIELDS, "");
    curl_easy_setopt(params.curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(params.curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(params.curl, CURLOPT_WRITEDATA, &readBuffer);
    curl_easy_setopt(params.curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(params.curl, CURLOPT_CONNECTTIMEOUT, 10L);
    resCurl = curl_easy_perform(params.curl);
    json_t *root;
    json_error_t error;

    while (resCurl != CURLE_OK) {
      *params.logFile << "<Gemini> Error with cURL. Retry in 2 sec..." << std::endl;
      sleep(2.0);
      readBuffer = "";
      resCurl = curl_easy_perform(params.curl);
    }
    root = json_loads(readBuffer.c_str(), 0, &error);

    while (!root) {
      *params.logFile << "<Gemini> Error with JSON:\n" << error.text << std::endl;
      *params.logFile << "<Gemini> Buffer:\n" << readBuffer.c_str() << std::endl;
      *params.logFile << "<Gemini> Retrying..." << std::endl;
      sleep(2.0);
      readBuffer = "";
      resCurl = curl_easy_perform(params.curl);
      while (resCurl != CURLE_OK) {
        *params.logFile << "<Gemini> Error with cURL. Retry in 2 sec..." << std::endl;
        sleep(2.0);
        readBuffer = "";
        resCurl = curl_easy_perform(params.curl);
      }
      root = json_loads(readBuffer.c_str(), 0, &error);
    }
    curl_slist_free_all(headers);
    curl_easy_reset(params.curl);
    return root;
  }
  else {
    *params.logFile << "<Gemini> Error with cURL init." << std::endl;
    return NULL;
  }
}
Esempio n. 6
0
GString* http_post_stream_upload(http* h, const gchar* url, goffset len, http_data_fn read_cb, gpointer user_data, GError** err)
{
  struct curl_slist* headers = NULL;
  glong http_status = 0;
  GString* response;
  CURLcode res;
  struct _stream_data data;

  g_return_val_if_fail(h != NULL, NULL);
  g_return_val_if_fail(url != NULL, NULL);
  g_return_val_if_fail(err == NULL || *err == NULL, NULL);

  http_no_expect(h);

  // setup post headers and url
  curl_easy_setopt(h->curl, CURLOPT_POST, 1L);
  curl_easy_setopt(h->curl, CURLOPT_URL, url);

  // setup request post body writer
  http_set_content_length(h, len);
  curl_easy_setopt(h->curl, CURLOPT_POSTFIELDSIZE_LARGE, len);

  data.cb = read_cb;
  data.user_data = user_data;
  curl_easy_setopt(h->curl, CURLOPT_READFUNCTION, (curl_read_callback)curl_read);
  curl_easy_setopt(h->curl, CURLOPT_READDATA, &data);

  // prepare buffer for the response body
  response = g_string_sized_new(512);
  curl_easy_setopt(h->curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)append_gstring);
  curl_easy_setopt(h->curl, CURLOPT_WRITEDATA, response);

  g_hash_table_foreach(h->headers, (GHFunc)add_header, &headers);
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, headers);

  // perform HTTP request
  res = curl_easy_perform(h->curl);

  // check the result
  if (res == CURLE_OK)
  {
    if (curl_easy_getinfo(h->curl, CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK)
    {
      if (http_status == 200)
      {
        goto out;
      }
      else
      {
        g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Server returned %ld", http_status);
      }
    }
    else
    {
      g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Can't get http status code");
    }
  }
  else if (res == CURLE_GOT_NOTHING)
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_NO_RESPONSE, "CURL error: %s", curl_easy_strerror(res));
  }
  else
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "CURL error: %s", curl_easy_strerror(res));
  }

  g_string_free(response, TRUE);
  response = NULL;

out:
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, NULL);
  curl_slist_free_all(headers);
  if (response)
    return response;

  return NULL;
}
Esempio n. 7
0
File: util.c Progetto: 1gh/cpuminer
json_t *json_rpc_call(CURL *curl, const char *url,
		      const char *userpass, const char *rpc_req,
		      bool longpoll_scan, bool longpoll, int *curl_err)
{
	json_t *val, *err_val, *res_val;
	int rc;
	struct data_buffer all_data = {0};
	struct upload_buffer upload_data;
	json_error_t err;
	struct curl_slist *headers = NULL;
	char len_hdr[64];
	char curl_err_str[CURL_ERROR_SIZE];
	long timeout = longpoll ? opt_timeout : 30;
	struct header_info hi = {0};
	bool lp_scanning = longpoll_scan && !have_longpoll;

	/* it is assumed that 'curl' is freshly [re]initialized at this pt */

	if (opt_protocol)
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
	curl_easy_setopt(curl, CURLOPT_URL, url);
	if (opt_cert)
		curl_easy_setopt(curl, CURLOPT_CAINFO, opt_cert);
	curl_easy_setopt(curl, CURLOPT_ENCODING, "");
	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
	curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
#if LIBCURL_VERSION_NUM >= 0x071200
	curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, &seek_data_cb);
	curl_easy_setopt(curl, CURLOPT_SEEKDATA, &upload_data);
#endif
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
	curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
	if (opt_proxy) {
		curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
		curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
	}
	if (userpass) {
		curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
		curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	}
#if LIBCURL_VERSION_NUM >= 0x070f06
	if (longpoll)
		curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
#endif
	curl_easy_setopt(curl, CURLOPT_POST, 1);

	if (opt_protocol)
		applog(LOG_DEBUG, "JSON protocol request:\n%s\n", rpc_req);

	upload_data.buf = rpc_req;
	upload_data.len = strlen(rpc_req);
	upload_data.pos = 0;
	sprintf(len_hdr, "Content-Length: %lu",
		(unsigned long) upload_data.len);

	headers = curl_slist_append(headers, "Content-Type: application/json");
	headers = curl_slist_append(headers, len_hdr);
	headers = curl_slist_append(headers, "User-Agent: " USER_AGENT);
	headers = curl_slist_append(headers, "X-Mining-Extensions: midstate");
	headers = curl_slist_append(headers, "Accept:"); /* disable Accept hdr*/
	headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/

	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

	rc = curl_easy_perform(curl);
	if (curl_err != NULL)
		*curl_err = rc;
	if (rc) {
		if (!(longpoll && rc == CURLE_OPERATION_TIMEDOUT))
			applog(LOG_ERR, "HTTP request failed: %s", curl_err_str);
		goto err_out;
	}

	/* If X-Stratum was found, activate Stratum */
	if (want_stratum && hi.stratum_url &&
	    !strncasecmp(hi.stratum_url, "stratum+tcp://", 14)) {
		have_stratum = true;
		tq_push(thr_info[stratum_thr_id].q, hi.stratum_url);
		hi.stratum_url = NULL;
	}

	/* If X-Long-Polling was found, activate long polling */
	if (lp_scanning && hi.lp_path && !have_stratum) {
		have_longpoll = true;
		tq_push(thr_info[longpoll_thr_id].q, hi.lp_path);
		hi.lp_path = NULL;
	}

	if (!all_data.buf) {
		applog(LOG_ERR, "Empty data received in json_rpc_call.");
		goto err_out;
	}

	val = JSON_LOADS(all_data.buf, &err);
	if (!val) {
		applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
		goto err_out;
	}

	if (opt_protocol) {
		char *s = json_dumps(val, JSON_INDENT(3));
		applog(LOG_DEBUG, "JSON protocol response:\n%s", s);
		free(s);
	}

	/* JSON-RPC valid response returns a non-null 'result',
	 * and a null 'error'. */
	res_val = json_object_get(val, "result");
	err_val = json_object_get(val, "error");

	if (!res_val || json_is_null(res_val) ||
	    (err_val && !json_is_null(err_val))) {
		char *s;

		if (err_val)
			s = json_dumps(err_val, JSON_INDENT(3));
		else
			s = strdup("(unknown reason)");

		applog(LOG_ERR, "JSON-RPC call failed: %s", s);

		free(s);

		goto err_out;
	}

	if (hi.reason)
		json_object_set_new(val, "reject-reason", json_string(hi.reason));

	databuf_free(&all_data);
	curl_slist_free_all(headers);
	curl_easy_reset(curl);
	return val;

err_out:
	free(hi.lp_path);
	free(hi.reason);
	free(hi.stratum_url);
	databuf_free(&all_data);
	curl_slist_free_all(headers);
	curl_easy_reset(curl);
	return NULL;
}
Esempio n. 8
0
int DownLoad(char *url, DownloadInfo *downloadInfo) {
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	char* get_url;
	if (curl) {
		downloadInfo->SetCurl(curl);
		struct curl_slist *chunk = NULL;
		int src_url_length = strlen(url);
		if (downloadInfo->check_is_tempfile_exits()) {
			char *bytes = NULL;
			bytes = FileLengthToString(downloadInfo->GetDownloadedSize());
			char *header = new char[12 + strlen(bytes) + 2];
			strcpy_s(header, 13, "Range:bytes=");
			strcat_s(header, strlen(header) + strlen(bytes) + 1, bytes);
			strcat_s(header, strlen(header) + 2, "-");
			chunk = curl_slist_append(chunk, header);
			delete[] bytes;
			curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
			get_url = new char[src_url_length + 17];
			strcpy_s(get_url, src_url_length + 1, url);
			strcat_s(get_url, src_url_length + 17, "&resume_flg=true");
			Log(_T("开始断点下载 %d"), downloadInfo->GetDownloadedSize());
		} else {
			downloadInfo->create_temp_file();
			get_url = new char[src_url_length + 1];
			strcpy_s(get_url, src_url_length + 1, url);
		}

		curl_easy_setopt(curl, CURLOPT_URL, get_url);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, downloadInfo);
		curl_easy_setopt(curl, CURLOPT_HEADERDATA, downloadInfo);
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
		curl_easy_setopt(curl, CURLOPT_NOBODY, FALSE);
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);//连接超时10S
		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);

		if (downloadInfo->downloadType == 0) {
			curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
			curl_easy_setopt(curl, CURLOPT_XFERINFODATA, downloadInfo);
		} else {
			curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, xferinfo);
			curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, downloadInfo);
		}
		is_get_header = false;
		res = curl_easy_perform(curl);
		if (res == CURLE_OK) {
			curl_easy_cleanup(curl);
			if (chunk)
				curl_slist_free_all(chunk);
			delete get_url;
			return 1;
		} else {
			TCHAR * error = CharToWchar_New(curl_easy_strerror(res));
			Log(_T("下载出错,error:%s %d"), error, res);
			delete[] error;
			if (res == CURLE_RECV_ERROR)
				is_need_reload = true;
		}
		curl_easy_cleanup(curl);
		if (chunk)
			curl_slist_free_all(chunk);
		delete get_url;
		return 0;
	}
	return 0;
}
Esempio n. 9
0
int
post(post_state_t *state,
                const char *url,
                const char *content_type,
                const char **additional_headers,
                const char *data,
                off_t data_size)
{
    CURLcode curl_err;
    long response_code;
    post_state_t localstate;

    VERB3 log("%s('%s','%s')", __func__, url, data);

    if (!state)
    {
        memset(&localstate, 0, sizeof(localstate));
        state = &localstate;
    }

    state->curl_result = state->http_resp_code = response_code = -1;

    CURL *handle = xcurl_easy_init();

    // Buffer[CURL_ERROR_SIZE] curl stores human readable error messages in.
    // This may be more helpful than just return code from curl_easy_perform.
    // curl will need it until curl_easy_cleanup.
    state->errmsg[0] = '\0';
    xcurl_easy_setopt_ptr(handle, CURLOPT_ERRORBUFFER, state->errmsg);
    // Shut off the built-in progress meter completely
    xcurl_easy_setopt_long(handle, CURLOPT_NOPROGRESS, 1);

    if (g_verbose >= 2)
    {
        // "Display a lot of verbose information about its operations.
        // Very useful for libcurl and/or protocol debugging and understanding.
        // The verbose information will be sent to stderr, or the stream set
        // with CURLOPT_STDERR"
        xcurl_easy_setopt_long(handle, CURLOPT_VERBOSE, 1);
        xcurl_easy_setopt_ptr(handle, CURLOPT_DEBUGFUNCTION, curl_debug);
    }

    // TODO: do we need to check for CURLE_URL_MALFORMAT error *here*,
    // not in curl_easy_perform?
    xcurl_easy_setopt_ptr(handle, CURLOPT_URL, url);

    // Auth if configured
    if (state->username)
    {
        // bitmask of allowed auth methods
        xcurl_easy_setopt_long(handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        xcurl_easy_setopt_ptr(handle, CURLOPT_USERNAME, state->username);
        xcurl_easy_setopt_ptr(handle, CURLOPT_PASSWORD, (state->password ? state->password : ""));
    }

    if (data_size != POST_DATA_FROMFILE_PUT)
    {
        // Do a HTTP POST. This also makes curl use
        // a "Content-Type: application/x-www-form-urlencoded" header.
        // (This is by far the most commonly used POST method).
        xcurl_easy_setopt_long(handle, CURLOPT_POST, 1);
    }
    // else (only POST_DATA_FROMFILE_PUT): do HTTP PUT.

    struct curl_httppost *post = NULL;
    struct curl_httppost *last = NULL;
    FILE *data_file = NULL;
    FILE *body_stream = NULL;
    struct curl_slist *httpheader_list = NULL;

    // Supply data...
    if (data_size == POST_DATA_FROMFILE
     || data_size == POST_DATA_FROMFILE_PUT
    ) {
        // ...from a file
        data_file = fopen(data, "r");
        if (!data_file)
        {
            perror_msg("Can't open '%s'", data);
            goto ret; // return -1
        }

        xcurl_easy_setopt_ptr(handle, CURLOPT_READDATA, data_file);
        // Want to use custom read function
        xcurl_easy_setopt_ptr(handle, CURLOPT_READFUNCTION, (const void*)fread_with_reporting);
        fseeko(data_file, 0, SEEK_END);
        off_t sz = ftello(data_file);
        fseeko(data_file, 0, SEEK_SET);
        if (data_size == POST_DATA_FROMFILE)
        {
            // Without this, curl would send "Content-Length: -1"
            // servers don't like that: "413 Request Entity Too Large"
            xcurl_easy_setopt_off_t(handle, CURLOPT_POSTFIELDSIZE_LARGE, sz);
        }
        else
        {
            xcurl_easy_setopt_long(handle, CURLOPT_UPLOAD, 1);
            xcurl_easy_setopt_off_t(handle, CURLOPT_INFILESIZE_LARGE, sz);
        }
    }
    else if (data_size == POST_DATA_FROMFILE_AS_FORM_DATA)
    {
        // ...from a file, in multipart/formdata format
        const char *basename = strrchr(data, '/');
        if (basename) basename++;
        else basename = data;
#if 0
        // Simple way, without custom reader function
        CURLFORMcode curlform_err = curl_formadd(&post, &last,
                        CURLFORM_PTRNAME, "file", // element name
                        CURLFORM_FILE, data, // filename to read from
                        CURLFORM_CONTENTTYPE, content_type,
                        CURLFORM_FILENAME, basename, // filename to put in the form
                        CURLFORM_END);
#else
        data_file = fopen(data, "r");
        if (!data_file)
        {
            perror_msg("Can't open '%s'", data);
            goto ret; // return -1
        }
        // Want to use custom read function
        xcurl_easy_setopt_ptr(handle, CURLOPT_READFUNCTION, (const void*)fread_with_reporting);
        // Need to know file size
        fseeko(data_file, 0, SEEK_END);
        off_t sz = ftello(data_file);
        fseeko(data_file, 0, SEEK_SET);
        // Create formdata
        CURLFORMcode curlform_err = curl_formadd(&post, &last,
                        CURLFORM_PTRNAME, "file", // element name
                        // use CURLOPT_READFUNCTION for reading, pass data_file as its last param:
                        CURLFORM_STREAM, data_file,
                        CURLFORM_CONTENTSLENGTH, (long)sz, // a must if we use CURLFORM_STREAM option
//FIXME: what if file size doesn't fit in long?
                        CURLFORM_CONTENTTYPE, content_type,
                        CURLFORM_FILENAME, basename, // filename to put in the form
                        CURLFORM_END);
#endif
        if (curlform_err != 0)
//FIXME:
            error_msg_and_die("out of memory or read error (curl_formadd error code: %d)", (int)curlform_err);
        xcurl_easy_setopt_ptr(handle, CURLOPT_HTTPPOST, post);
    }
    else if (data_size == POST_DATA_STRING_AS_FORM_DATA)
    {
        CURLFORMcode curlform_err = curl_formadd(&post, &last,
                        CURLFORM_PTRNAME, "file", // element name
                        // curl bug - missing filename
                        // http://curl.haxx.se/mail/lib-2011-07/0176.html
                        // https://github.com/bagder/curl/commit/45d883d
                        // fixed in curl-7.22.0~144
                        // tested with curl-7.24.0-3
                        // should be working on F17
                        CURLFORM_BUFFER, "*buffer*", // provides filename
                        CURLFORM_BUFFERPTR, data,
                        CURLFORM_BUFFERLENGTH, (long)strlen(data),
//FIXME: what if file size doesn't fit in long?
                        CURLFORM_CONTENTTYPE, content_type,
                        CURLFORM_END);
        if (curlform_err != 0)
            error_msg_and_die("out of memory or read error (curl_formadd error code: %d)", (int)curlform_err);
        xcurl_easy_setopt_ptr(handle, CURLOPT_HTTPPOST, post);
    }
    else
    {
        // ...from a blob in memory
        xcurl_easy_setopt_ptr(handle, CURLOPT_POSTFIELDS, data);
        // note1: if data_size == POST_DATA_STRING == -1, curl will use strlen(data)
        xcurl_easy_setopt_long(handle, CURLOPT_POSTFIELDSIZE, data_size);
        // We don't use CURLOPT_POSTFIELDSIZE_LARGE because
        // I'm not sure CURLOPT_POSTFIELDSIZE_LARGE special-cases -1.
        // Not a big problem: memory blobs >4GB are very unlikely.
    }

    // Override "Content-Type:"
    if (data_size != POST_DATA_FROMFILE_AS_FORM_DATA
        && data_size != POST_DATA_STRING_AS_FORM_DATA)
    {
        char *content_type_header = xasprintf("Content-Type: %s", content_type);
        // Note: curl_slist_append() copies content_type_header
        httpheader_list = curl_slist_append(httpheader_list, content_type_header);
        if (!httpheader_list)
            error_msg_and_die("out of memory");
        free(content_type_header);
    }

    for (; additional_headers && *additional_headers; additional_headers++)
    {
        httpheader_list = curl_slist_append(httpheader_list, *additional_headers);
        if (!httpheader_list)
            error_msg_and_die("out of memory");
    }

    // Add User-Agent: ABRT/N.M
    httpheader_list = curl_slist_append(httpheader_list, "User-Agent: ABRT/"VERSION);
    if (!httpheader_list)
        error_msg_and_die("out of memory");

    if (httpheader_list)
        xcurl_easy_setopt_ptr(handle, CURLOPT_HTTPHEADER, httpheader_list);

// Disabled: was observed to also handle "305 Use proxy" redirect,
// apparently with POST->GET remapping - which server didn't like at all.
// Attempted to suppress remapping on 305 using CURLOPT_POSTREDIR of -1,
// but it still did not work.
#if 0
    // Please handle 301/302 redirects for me
    xcurl_easy_setopt_long(handle, CURLOPT_FOLLOWLOCATION, 1);
    xcurl_easy_setopt_long(handle, CURLOPT_MAXREDIRS, 10);
    // Bitmask to control how libcurl acts on redirects after POSTs.
    // Bit 0 set (value CURL_REDIR_POST_301) makes libcurl
    // not convert POST requests into GET requests when following
    // a 301 redirection. Bit 1 (value CURL_REDIR_POST_302) makes libcurl
    // maintain the request method after a 302 redirect.
    // CURL_REDIR_POST_ALL is a convenience define that sets both bits.
    // The non-RFC behaviour is ubiquitous in web browsers, so the library
    // does the conversion by default to maintain consistency.
    // However, a server may require a POST to remain a POST.
    xcurl_easy_setopt_long(handle, CURLOPT_POSTREDIR, -1L /*CURL_REDIR_POST_ALL*/ );
#endif

    // Prepare for saving information
    if (state->flags & POST_WANT_HEADERS)
    {
        xcurl_easy_setopt_ptr(handle, CURLOPT_HEADERFUNCTION, (void*)save_headers);
        xcurl_easy_setopt_ptr(handle, CURLOPT_WRITEHEADER, state);
    }
    if (state->flags & POST_WANT_BODY)
    {
        body_stream = open_memstream(&state->body, &state->body_size);
        if (!body_stream)
            error_msg_and_die("out of memory");
        xcurl_easy_setopt_ptr(handle, CURLOPT_WRITEDATA, body_stream);
    }
    if (!(state->flags & POST_WANT_SSL_VERIFY))
    {
        xcurl_easy_setopt_long(handle, CURLOPT_SSL_VERIFYPEER, 0);
        xcurl_easy_setopt_long(handle, CURLOPT_SSL_VERIFYHOST, 0);
    }

    // This is the place where everything happens.
    // Here errors are not limited to "out of memory", can't just die.
    state->curl_result = curl_err = curl_easy_perform_with_proxy(handle, url);
    if (curl_err)
    {
        VERB2 log("curl_easy_perform: error %d", (int)curl_err);
        if (state->flags & POST_WANT_ERROR_MSG)
        {
            state->curl_error_msg = check_curl_error(curl_err, "curl_easy_perform");
            VERB3 log("curl_easy_perform: error_msg: %s", state->curl_error_msg);
        }
        goto ret;
    }

    // curl-7.20.1 doesn't do it, we get NULL body in the log message below
    // unless we fflush the body memstream ourself
    if (body_stream)
        fflush(body_stream);

    // Headers/body are already saved (if requested), extract more info
    curl_err = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response_code);
    die_if_curl_error(curl_err);
    state->http_resp_code = response_code;
    VERB3 log("after curl_easy_perform: response_code:%ld body:'%s'", response_code, state->body);

 ret:
    curl_easy_cleanup(handle);
    if (httpheader_list)
        curl_slist_free_all(httpheader_list);
    if (body_stream)
        fclose(body_stream);
    if (data_file)
        fclose(data_file);
    if (post)
        curl_formfree(post);

    return response_code;
}
Esempio n. 10
0
int gretl_curl (const char *url, const char *header, 
		const char *postdata, int include,
		char **output, char **errmsg)
{
    CURL *curl;
    struct curl_slist *hlist = NULL;
    struct GetBuf getbuf = {
	output, /* pointer to buffer */
	0       /* bytes written */
    };
    CURLcode res;
    int err = 0;

    err = gretl_curl_toggle(1);
    if (err) {
	return err;
    }
  
    curl = curl_easy_init();
    if (curl == NULL) {
	gretl_errmsg_set("curl_easy_init failed");
	return 1;
    }

    if (header != NULL) {
	hlist = curl_slist_append(hlist, header);
    }

    if (getenv("GRETL_WWW_VERBOSE") != NULL) {
	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    }

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &getbuf);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_bufwrite);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

    if (include) {
	curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    }  

    if (hlist != NULL) {
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hlist);
    }

    if (postdata != NULL) {
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *) postdata);
    }

    if (wproxy && *proxyhost != '\0') {
	curl_easy_setopt(curl, CURLOPT_PROXY, proxyhost);
    }

    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
	const char *cmsg = curl_easy_strerror(res);

	gretl_errmsg_sprintf("cURL error %d (%s)", res, cmsg);
	if (*output != NULL) {
	    free(*output);
	    *output = NULL;
	}
	if (errmsg != NULL) {
	    *errmsg = gretl_strdup(cmsg);
	}
	err = E_DATA;
    }

    if (hlist != NULL) {
	curl_slist_free_all(hlist);
    }    
    curl_easy_cleanup(curl);

    return err;
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
  CURL *curl;
  CURLcode res;

  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  static const char buf[] = "Expect:";

  curl_global_init(CURL_GLOBAL_ALL);

  /* Fill in the file upload field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "sendfile",
               CURLFORM_FILE, "postit2.c",
               CURLFORM_END);

  /* Fill in the filename field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "filename",
               CURLFORM_COPYCONTENTS, "postit2.c",
               CURLFORM_END);


  /* Fill in the submit field too, even if this is rarely needed */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "send",
               CURLFORM_END);

  curl = curl_easy_init();
  /* initalize custom header list (stating that Expect: 100-continue is not
     wanted */
  headerlist = curl_slist_append(headerlist, buf);
  if(curl) {
    /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
    if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
      /* only disable 100-continue header if explicitly requested */
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */
    curl_formfree(formpost);
    /* free slist */
    curl_slist_free_all (headerlist);
  }
  return 0;
}
Esempio n. 12
0
int8_t
http_send_message(char *msg_out, char **msg_in)
{
    CURLcode res;

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg_out);
    http_c.header_list = NULL;
    http_c.header_list = curl_slist_append(http_c.header_list, "Accept:");
    if (!http_c.header_list) return -1;
    http_c.header_list = curl_slist_append(http_c.header_list, "User-Agent: easycwmp");
    if (!http_c.header_list) return -1;
    http_c.header_list = curl_slist_append(http_c.header_list, "Content-Type: text/html; charset=utf-8");
    if (!http_c.header_list) return -1;
    if (config->acs->http100continue_disable) {
        http_c.header_list = curl_slist_append(http_c.header_list, "Expect:");
        if (!http_c.header_list) return -1;
    }
    if (msg_out) {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(msg_out));
        http_c.header_list = curl_slist_append(http_c.header_list, "SOAPAction;");
        if (!http_c.header_list) return -1;
    }
    else {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
    }
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_c.header_list);

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, msg_in);

    *msg_in = (char *) calloc (1, sizeof(char));

    res = curl_easy_perform(curl);

    if (http_c.header_list) {
        curl_slist_free_all(http_c.header_list);
        http_c.header_list = NULL;
    }

    if (!strlen(*msg_in)) {
        FREE(*msg_in);
    }

    long httpCode = 0;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);

    if (res || (httpCode != 200 && httpCode != 204)) {
        log_message(NAME, L_NOTICE, "sending http message failed\n");
        return -1;
    }


    if (*msg_in) {
        DDF("+++ RECEIVED HTTP RESPONSE +++\n");
        DDF("%s", *msg_in);
        DDF("--- RECEIVED HTTP RESPONSE ---\n");
    } else {
        DDF("+++ RECEIVED EMPTY HTTP RESPONSE +++\n");
    }

    return 0;
}
Esempio n. 13
0
//Process POST Request
int processPostTask(CCHttpRequest *request, write_callback callback, void *stream, int32_t *responseCode)
{
    CURLcode code = CURL_LAST;
    CURL *curl = curl_easy_init();
    
    do {
        if (!configureCURL(curl)) {
            break;
        }
        
        /* handle custom header data */
        /* create curl linked list */
        struct curl_slist *cHeaders=NULL;
        /* get custom header data (if set) */
      		std::vector<std::string> headers=request->getHeaders();
      		if(!headers.empty())
      		{      			
        			for(std::vector<std::string>::iterator it=headers.begin();it!=headers.end();it++)
        			{
              /* append custom headers one by one */
          				cHeaders=curl_slist_append(cHeaders,it->c_str());
        			}
           /* set custom headers for curl */
        			code = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, cHeaders);
        			if (code != CURLE_OK) {
          				break;
        			}
      		}
              
        code = curl_easy_setopt(curl, CURLOPT_URL, request->getUrl());
        if (code != CURLE_OK) {
            break;
        }
        code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
        if (code != CURLE_OK) {
            break;
        }
        code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, stream);
        if (code != CURLE_OK) {
            break;
        }
        code = curl_easy_setopt(curl, CURLOPT_POST, 1);
        if (code != CURLE_OK) {
            break;
        }
        code = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request->getRequestData());
        if (code != CURLE_OK) {
            break;
        }
        code = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request->getRequestDataSize());
        if (code != CURLE_OK) {
            break;
        }
        code = curl_easy_perform(curl);
        if (code != CURLE_OK) {
            break;
        }
        
        /* free the linked list for header data */
        curl_slist_free_all(cHeaders);

        code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, responseCode); 
        if (code != CURLE_OK || *responseCode != 200) {
            code = CURLE_HTTP_RETURNED_ERROR;
        }
    } while (0);
    if (curl) {
        curl_easy_cleanup(curl);
    }
    
    return (code == CURLE_OK ? 0 : 1);    
}
Esempio n. 14
0
int Service::HttpRequest( const string http_method,
                          const string url,
                          const HTTPDATA* lpHttpData,
                          const vector<string> custom_headers,
                          Service* lpService )
{
    //  string resp_buffer;  // body of the response from the server
    char *memblock = NULL;  // file size of POST body
 //   curl_global_init( CURL_GLOBAL_ALL ); //不可放线程内
    CURL *curl = curl_easy_init();
	this->Set_share_handle(curl);
    char errorBuffer[CURL_ERROR_SIZE] = {0};
    if ( curl )
    {
		if( !m_proxyIp.empty())
		{
			//curl_easy_setopt(curl,CURLOPT_HTTPPROXYTUNNEL, TRUE); 
			//curl_easy_setopt(curl, CURLOPT_PROXY, "https://183.141.72.153:3128");
			curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
			curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,60);
			curl_easy_setopt(curl, CURLOPT_PROXY, m_proxyIp.c_str() );
			
		}
		//curl_easy_setopt(curl, CURLOPT_PROXY, "localhost:8888");
		//curl_easy_setopt(curl, CURLOPT_PROXY, "10.142.50.140:806");
		 //curl_easy_setopt(curl, CURLOPT_PROXY, "127.0.0.1:8888");
		//curl_easy_setopt(curl, CURLOPT_PROXY, "202.138.240.159:8080");
		//邮件账号密码
		if ( !m_username.empty() && !m_password.empty() )
		{
			curl_easy_setopt( curl, CURLOPT_USERNAME, m_username.c_str() );
			curl_easy_setopt( curl, CURLOPT_PASSWORD, m_password.c_str() );
		}
		//邮件账号面
		 //curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "UIDL");
		//curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TOP 1 0");
        curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, errorBuffer );
        curl_easy_setopt( curl, CURLOPT_URL, url.c_str() );
        //curl_easy_setopt( curl, CURLOPT_HEADER, 1 );
        //curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1 );
		if (!m_cookie_file.empty())
		{
			curl_easy_setopt( curl, CURLOPT_COOKIEFILE, (char*)m_cookie_file.c_str());
			curl_easy_setopt( curl, CURLOPT_COOKIEJAR, (char*)m_cookie_file.c_str());
		}
		
// 		curl_easy_setopt( curl, CURLOPT_COOKIEFILE, "C:\\1.txt");
// 		curl_easy_setopt( curl, CURLOPT_COOKIEJAR, "C:\\1.txt");
        curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteCallback );
        curl_easy_setopt( curl, CURLOPT_WRITEDATA, lpService );
        curl_easy_setopt( curl, CURLOPT_HEADERFUNCTION, WriteHeaderCallback ); // our static function
        curl_easy_setopt( curl, CURLOPT_WRITEHEADER, lpService ); //"headers" is a member variable referencing HttpHeaders
        curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, 0L );
        curl_easy_setopt( curl, CURLOPT_SSL_VERIFYHOST, 0L );
        curl_easy_setopt( curl, CURLOPT_VERBOSE , 0 );
		curl_easy_setopt( curl, CURLOPT_NOSIGNAL, 1L );
		curl_easy_setopt( curl, CURLOPT_TIMEOUT,60 );
        //下载进度条
        curl_easy_setopt( curl, CURLOPT_NOPROGRESS, FALSE );
        curl_easy_setopt( curl, CURLOPT_PROGRESSFUNCTION, ProgressCallback );
        curl_easy_setopt( curl, CURLOPT_PROGRESSDATA, lpService );

        //curl_easy_setopt(curl,CURLOPT_SSLVERSION,3);

        struct curl_slist *headers = NULL;
        // Add standard headers
		
        for ( unsigned int i = 0; i < request_headers_.size(); ++i )
        {
            headers = curl_slist_append( headers, request_headers_[i].c_str() );
        }

        // Add any custom headers
        for ( unsigned int i = 0; i < custom_headers.size(); ++i )
        {
            headers = curl_slist_append( headers, custom_headers[i].c_str() );
        }
		//int nranIndex = myRandom(0,m_header_user_agent.size()-1);
		//headers = curl_slist_append( headers, m_header_user_agent[nranIndex].c_str() );
		headers = curl_slist_append( headers, m_header_user_agent[0].c_str() );
        if ( http_method == "GET" )
        {
            curl_easy_setopt( curl, CURLOPT_HTTPGET, 1L );
//          headers =
//              curl_slist_append(headers, "Content-Type: application/atom+xml");

        }
        else if ( http_method == "POST" )
        {
            curl_easy_setopt( curl, CURLOPT_POST, 1 );
            if( lpHttpData != NULL && lpHttpData->data != NULL && lpHttpData->datalen >= 0 )
            {
                //  curl_easy_setopt(curl, CURLOPT_BINARYTRANSFER, 1);

				curl_easy_setopt( curl, CURLOPT_READFUNCTION, UploadCallback );
				lpService->m_lpHttpData = lpHttpData;
				curl_easy_setopt( curl, CURLOPT_READDATA, lpService );
				curl_easy_setopt( curl, CURLOPT_INFILESIZE, lpService->m_lpHttpData->datalen );

//                 curl_easy_setopt( curl, CURLOPT_POSTFIELDS, lpHttpData->data );
                 curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, lpHttpData->datalen );
            }
        }
        else if ( http_method == "PUT" )
        {
            curl_easy_setopt( curl, CURLOPT_PUT, 1L );

            if( lpHttpData != NULL && lpHttpData->data != NULL )
            {
                curl_easy_setopt( curl, CURLOPT_UPLOAD, 1L );

                /* we want to use our own read function */
                curl_easy_setopt( curl, CURLOPT_READFUNCTION, UploadCallback );
                lpService->m_lpHttpData = lpHttpData;
                /* now specify which file to upload */
                /*curl_easy_setopt(curl, CURLOPT_READDATA, lpPutData);
                curl_easy_setopt(curl, CURLOPT_INFILESIZE, lpPutData->datalen);*/
                curl_easy_setopt( curl, CURLOPT_READDATA, lpService );
                curl_easy_setopt( curl, CURLOPT_INFILESIZE, lpService->m_lpHttpData->datalen );
            }

        }
        else if ( http_method == "DELETE" )
        {
            curl_easy_setopt( curl, CURLOPT_CUSTOMREQUEST, "DELETE" );

        }
        else
        {
            //cerr << "Error: Unknown http method - " << http_method << endl;

            error_str = "Error: Unknown http method - " + http_method;
            //return AVERR_HTTP_METHOD;
            return CURLE_NOT_BUILT_IN;
        }

        // attach headers to this request
        curl_easy_setopt( curl, CURLOPT_HTTPHEADER, headers );

        CURLcode curl_code = curl_easy_perform( curl );

        int http_code = 0;
        curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &http_code );

        // clean up
        curl_easy_cleanup( curl );
        curl_slist_free_all( headers );
    //    curl_global_cleanup(); //不可放线程内
        if ( memblock != NULL )
        {
            delete[] memblock;
        }

        if ( curl_code != CURLE_OK )
        {

            //cout << "\nError: [" << curl_code << "] - " << errorBuffer;
            //DebugPrintA(2, "\nError--%s\n",errorBuffer);
            error_str = errorBuffer;
            //linux modify callback error info????
            //::MessageBoxA(NULL,error_str.c_str(),"HttpReq-ER",0);
            return curl_code;

        }
        else if ( 400 <= http_code )
        {
            //DebugPrintA(3, "Http-code=%d",http_code);
            return http_code;
        }
        else
            error_str = "";
    }
    return CURLE_OK;
}
Esempio n. 15
0
static ham_status_t
_perform_request(ham_env_t *env, CURL *handle, proto_wrapper_t *request,
                proto_wrapper_t **reply)
{
    CURLcode cc;
    long response=0;
    char header[128];
    curl_buffer_t rbuf={0};
    curl_buffer_t wbuf={0};
    struct curl_slist *slist=0;

    wbuf.alloc=env_get_allocator(env);

    *reply=0;

    if (!proto_pack(request, wbuf.alloc, &rbuf.packed_data, &rbuf.packed_size))
        return (HAM_INTERNAL_ERROR);

    sprintf(header, "Content-Length: %u", rbuf.packed_size);
    slist=curl_slist_append(slist, header);
    slist=curl_slist_append(slist, "Transfer-Encoding:");
    slist=curl_slist_append(slist, "Expect:");

#ifdef HAM_DEBUG
    SETOPT(handle, CURLOPT_VERBOSE, 1);
#endif
    SETOPT(handle, CURLOPT_URL, env_get_filename(env));
    SETOPT(handle, CURLOPT_READFUNCTION, __readfunc);
    SETOPT(handle, CURLOPT_READDATA, &rbuf);
    SETOPT(handle, CURLOPT_UPLOAD, 1);
    SETOPT(handle, CURLOPT_PUT, 1);
    SETOPT(handle, CURLOPT_WRITEFUNCTION, __writefunc);
    SETOPT(handle, CURLOPT_WRITEDATA, &wbuf);
    SETOPT(handle, CURLOPT_HTTPHEADER, slist);

    cc=curl_easy_perform(handle);

    if (rbuf.packed_data)
        allocator_free(env_get_allocator(env), rbuf.packed_data);
    curl_slist_free_all(slist);

    if (cc) {
        ham_trace(("network transmission failed: %s", curl_easy_strerror(cc)));
        return (HAM_NETWORK_ERROR);
    }

    cc=curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
    if (cc) {
        ham_trace(("network transmission failed: %s", curl_easy_strerror(cc)));
        return (HAM_NETWORK_ERROR);
    }

    if (response!=200) {
        ham_trace(("server returned error %u", response));
        return (HAM_NETWORK_ERROR);
    }

    *reply=wbuf.wrapper;

    return (0);
}
Esempio n. 16
0
int Service::HttpRequest( const string http_method,
						 const string url,
						 const vector<string>& custom_headers,
						 const PostData* lpPost_data,
						 const PutData* lpPutData,
						 Service* lpService)
{
	//	string resp_buffer;  // body of the response from the server
	char *memblock = NULL;  // file size of POST body
	CURL *curl = curl_easy_init();
	int timeout=5;
	//const char *progress_data = "* ";
	if ( curl ) 
	{
		char errorBuffer[CURL_ERROR_SIZE]={0};
		if(systemset.checkProxy)
			curl_easy_setopt(curl, CURLOPT_PROXY, systemset.Proxy);

		curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, lpService);
		//设置下载速度=0时 N次退出
		//	SD_curl_easy_setopt_EXT(curl, CURLOPT_TIMEOUT, timeout);绝对不能出现。不然只连接timeout秒
		//设置URL地址错误 重连N次后推出
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
		//设置最低速度。为了中途拔网线
		curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 32);
		curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout);

		if ( http_method == "PUT" )
		{
			curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,WriteHeaderCallback); // our static function
			curl_easy_setopt(curl, CURLOPT_WRITEHEADER, lpService); //"headers" is a member variable referencing HttpHeaders 
		}

		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
		curl_easy_setopt(curl, CURLOPT_VERBOSE , 1);

		//下载进度条
		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
		curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, ProgressCallback);
		curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, lpService);


		//curl_easy_setopt(curl,CURLOPT_SSLVERSION,3);

		struct curl_slist *headers = NULL;

		unsigned int  i=0;
		// Add standard headers
		for (i = 0; i < request_headers_.size(); ++i) 
		{
			headers = curl_slist_append(headers, request_headers_[i].c_str());
		}

		// Add any custom headers
		for (i = 0; i < custom_headers.size(); ++i) 
		{
			headers = curl_slist_append(headers, custom_headers[i].c_str());
		}

		if ( http_method == "GET" )
		{
			curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
			headers =
				curl_slist_append(headers, "Content-Type: application/atom+xml");

		} 
		else if ( http_method == "POST" )
		{
			curl_easy_setopt(curl, CURLOPT_POST, 1);
			// Configure post for plain text or binary data
			if( lpPost_data != NULL && lpPost_data->data != NULL )
			{
				//  curl_easy_setopt(curl, CURLOPT_BINARYTRANSFER, 1);
		
				curl_easy_setopt(curl, CURLOPT_POSTFIELDS, lpPost_data->data);
				curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, lpPost_data->datalen);
			}
		}
		else if ( http_method == "PUT" )
		{
			curl_easy_setopt(curl, CURLOPT_PUT, 1L);

			if( lpPutData != NULL && lpPutData->data != NULL )
			{
				curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

				/* we want to use our own read function */
				curl_easy_setopt(curl, CURLOPT_READFUNCTION, UploadCallback);
				lpService->m_lpPutData = lpPutData;
				/* now specify which file to upload */
				/*curl_easy_setopt(curl, CURLOPT_READDATA, lpPutData);
				curl_easy_setopt(curl, CURLOPT_INFILESIZE, lpPutData->datalen);*/
				curl_easy_setopt(curl, CURLOPT_READDATA, lpService);
				curl_easy_setopt(curl, CURLOPT_INFILESIZE, lpService->m_lpPutData->datalen);
			}

		} 
		else if ( http_method == "DELETE" )
		{
			curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");

		}
		else
		{
		//	cerr << "Error: Unknown http method - " << http_method << endl;
		//	error_str = "Error: Unknown http method - " + http_method;
			//return AVERR_HTTP_METHOD;
			return CURLE_NOT_BUILT_IN;
		}

		// attach headers to this request
		//curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/*
		headers = curl_slist_append(headers, "Content-Type: text/xml; charset=utf-8");  

		curl_easy_setopt(curl, CURLOPT_HEADER, 1);
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
*/		

		CURLcode curl_code = curl_easy_perform(curl);

		int http_code = 0;
		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

		// clean up
		curl_easy_cleanup(curl);
		curl_slist_free_all(headers);
		if ( memblock != NULL )
		{
			delete[] memblock;
		}

		if ( curl_code != CURLE_OK ) 
		{

//			cout << "\nError: [" << curl_code << "] - " << errorBuffer;
			//DebugPrintA(2, "\nError--%s\n",errorBuffer);
			error_str = errorBuffer;
			//linux modify callback error info????
			//::MessageBoxA(NULL,error_str.c_str(),"HttpReq-ER",0);
			return curl_code;

		}
		else if ( 400 <= http_code )
		{
			//DebugPrintA(3, "Http-code=%d",http_code);
			int start, end;
			start = lpService->m_resp_buffer.find("<internalReason>");
			end = lpService->m_resp_buffer.find("</internalReason>");
			if( start>=0 && end>=0 && end>start )
			{
				start += 16;
				error_str = lpService->m_resp_buffer.substr(start, end-start);
			}
			else
				error_str = lpService->m_resp_buffer;

			return http_code;
		}
		else
			error_str = "";
	}

	return CURLE_OK;
}
Esempio n. 17
0
GString* http_post(http* h, const gchar* url, const gchar* body, gssize body_len, GError** err)
{
  struct curl_slist* headers = NULL;
  glong http_status = 0;
  GString* response;
  CURLcode res;

  g_return_val_if_fail(h != NULL, NULL);
  g_return_val_if_fail(url != NULL, NULL);
  g_return_val_if_fail(err == NULL || *err == NULL, NULL);

  // setup post headers and url
  curl_easy_setopt(h->curl, CURLOPT_POST, 1L);
  curl_easy_setopt(h->curl, CURLOPT_URL, url);
  g_hash_table_foreach(h->headers, (GHFunc)add_header, &headers);
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, headers);

  // pass request body
  if (body)
  {
    curl_easy_setopt(h->curl, CURLOPT_NOBODY, 0L);
    curl_easy_setopt(h->curl, CURLOPT_POSTFIELDS, body);
    curl_easy_setopt(h->curl, CURLOPT_POSTFIELDSIZE, body_len);
  }
  else
  {
    curl_easy_setopt(h->curl, CURLOPT_NOBODY, 1L);
    curl_easy_setopt(h->curl, CURLOPT_POSTFIELDS, NULL);
    curl_easy_setopt(h->curl, CURLOPT_POSTFIELDSIZE, 0L);
  }

  // prepare buffer for the response body
  response = g_string_sized_new(1024);
  curl_easy_setopt(h->curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)append_gstring);
  curl_easy_setopt(h->curl, CURLOPT_WRITEDATA, response);

  // perform HTTP request
  res = curl_easy_perform(h->curl);

  // check the result
  if (res == CURLE_OK)
  {
    if (curl_easy_getinfo(h->curl, CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK)
    {
      if (http_status == 200)
      {
        goto out;
      }
      else
      {
        g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Server returned %ld", http_status);
      }
    }
    else
    {
      g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Can't get http status code");
    }
  }
  else if (res == CURLE_GOT_NOTHING)
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_NO_RESPONSE, "CURL error: %s", curl_easy_strerror(res));
  }
  else
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "CURL error: %s", curl_easy_strerror(res));
  }

  g_string_free(response, TRUE);
  response = NULL;

out:
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, NULL);
  curl_slist_free_all(headers);
  if (response)
    return response;

  return NULL;
}
  int main(int argc, char **argv)
  {
    CURL *curl;
    CURLcode res;
    FILE *ftpfile;
    FILE * hd_src ;
    int hd ;
    struct stat file_info;
  
    struct curl_slist *headerlist=NULL;
    char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
    char buf_2 [] = "RNTO " RENAME_FILE_TO;
  
    /* get the file size of the local file */
    hd = open(LOCAL_FILE, O_RDONLY) ;
    fstat(hd, &file_info);
    close(hd) ;
  
    /* get a FILE * of the same file, could also be made with
       fdopen() from the previous descriptor, but hey this is just 
       an example! */
    hd_src = fopen(LOCAL_FILE, "rb");
  
    /* In windows, this will init the winsock stuff */
    curl_global_init(CURL_GLOBAL_ALL);
  
    /* get a curl handle */
    curl = curl_easy_init();
    if(curl) {
      /* build a list of commands to pass to libcurl */
//      headerlist = curl_slist_append(headerlist, buf_1);
//      headerlist = curl_slist_append(headerlist, buf_2);
  
      /* enable uploading */
      curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;

      /* set user name and password */
      curl_easy_setopt(curl, CURLOPT_USERPWD, "rutul:topspin");

      /* specify target */
      curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
  
      /* pass in that last of FTP commands to run after the transfer */
//      curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  
      /* now specify which file to upload */
      curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
  
      /* and give the size of the upload (optional) */
      curl_easy_setopt(curl, CURLOPT_INFILESIZE, file_info.st_size);
  
      /* Switch on full protocol/debug output */
      curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);

      /* Now run off and do what you've been told! */
      res = curl_easy_perform(curl);
  
      /* clean up the FTP commands list */
      curl_slist_free_all (headerlist);
  
      /* always cleanup */
      curl_easy_cleanup(curl);
    }
    fclose(hd_src); /* close the local file */
  
   curl_global_cleanup();
   return 0;
 }
Esempio n. 19
0
gboolean http_post_stream_download(http* h, const gchar* url, http_data_fn write_cb, gpointer user_data, GError** err)
{
  struct curl_slist* headers = NULL;
  glong http_status = 0;
  CURLcode res;
  struct _stream_data data;
  gboolean status = FALSE;

  g_return_val_if_fail(h != NULL, FALSE);
  g_return_val_if_fail(url != NULL, FALSE);
  g_return_val_if_fail(err == NULL || *err == NULL, FALSE);

  http_no_expect(h);

  // setup post headers and url
  curl_easy_setopt(h->curl, CURLOPT_POST, 1L);
  curl_easy_setopt(h->curl, CURLOPT_URL, url);

  // request is empty
  curl_easy_setopt(h->curl, CURLOPT_POSTFIELDSIZE, 0);

  // setup response writer
  data.cb = write_cb;
  data.user_data = user_data;
  curl_easy_setopt(h->curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curl_write);
  curl_easy_setopt(h->curl, CURLOPT_WRITEDATA, &data);

  g_hash_table_foreach(h->headers, (GHFunc)add_header, &headers);
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, headers);

  // perform HTTP request
  res = curl_easy_perform_retry_empty(h->curl);
  // check the result
  if (res == CURLE_OK)
  {
    if (curl_easy_getinfo(h->curl, CURLINFO_RESPONSE_CODE, &http_status) == CURLE_OK)
    {
      if (http_status == 200)
      {
        status = TRUE;
        goto out;
      }
      else
      {
        g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Server returned %ld", http_status);
      }
    }
    else
    {
      g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "Can't get http status code");
    }
  }
  else
  {
    g_set_error(err, HTTP_ERROR, HTTP_ERROR_OTHER, "CURL error: %s", curl_easy_strerror(res));
  }

out:
  curl_easy_setopt(h->curl, CURLOPT_HTTPHEADER, NULL);
  curl_slist_free_all(headers);
  return status;
}
Esempio n. 20
0
/* ****************************************************************************
*
* httpRequestSend -
*/
std::string httpRequestSend
(
   const std::string&     _ip,
   unsigned short         port,
   const std::string&     protocol,
   const std::string&     verb,
   const std::string&     tenant,
   const std::string&     servicePath,
   const std::string&     xauthToken,
   const std::string&     resource,
   const std::string&     content_type,
   const std::string&     content,
   bool                   useRush,
   bool                   waitForResponse,
   const std::string&     acceptFormat,
   long                   timeoutInMilliseconds
)
{
  char                       portAsString[16];
  static unsigned long long  callNo             = 0;
  std::string                result;
  std::string                ip                 = _ip;
  struct curl_slist*         headers            = NULL;
  MemoryStruct*              httpResponse       = NULL;
  CURLcode                   res;
  int                        outgoingMsgSize       = 0;
  CURL*                      curl;

  ++callNo;

  if (timeoutInMilliseconds == -1)
  {
    timeoutInMilliseconds = defaultTimeout;
  }

  LM_TRANSACTION_START("to", ip.c_str(), port, resource.c_str());

  // Preconditions check
  if (port == 0)
  {
    LM_E(("Runtime Error (port is ZERO)"));
    LM_TRANSACTION_END();
    return "error";
  }

  if (ip.empty())
  {
    LM_E(("Runtime Error (ip is empty)"));
    LM_TRANSACTION_END();
    return "error";
  }

  if (verb.empty())
  {
    LM_E(("Runtime Error (verb is empty)"));
    LM_TRANSACTION_END();
    return "error";
  }

  if (resource.empty())
  {
    LM_E(("Runtime Error (resource is empty)"));
    LM_TRANSACTION_END();
    return "error";
  }

  if ((content_type.empty()) && (!content.empty()))
  {
    LM_E(("Runtime Error (Content-Type is empty but there is actual content)"));
    LM_TRANSACTION_END();
    return "error";
  }

  if ((!content_type.empty()) && (content.empty()))
  {
    LM_E(("Runtime Error (Content-Type non-empty but there is no content)"));
    LM_TRANSACTION_END();
    return "error";
  }

  if ((curl = curl_easy_init()) == NULL)
  {
    LM_E(("Runtime Error (could not init libcurl)"));
    LM_TRANSACTION_END();
    return "error";
  }


  // Allocate to hold HTTP response
  httpResponse = new MemoryStruct;
  httpResponse->memory = (char*) malloc(1); // will grow as needed
  httpResponse->size = 0; // no data at this point

  //
  // Rush
  // Every call to httpRequestSend specifies whether RUSH should be used or not.
  // But, this depends also on how the broker was started, so here the 'useRush'
  // parameter is cancelled in case the broker was started without rush.
  //
  // If rush is to be used, the IP/port is stored in rushHeaderIP/rushHeaderPort and
  // after that, the host and port of rush is set as ip/port for the message, that is
  // now sent to rush instead of to its final destination.
  // Also, a few HTTP headers for rush must be setup.
  //
  if ((rushPort == 0) || (rushHost == ""))
    useRush = false;

  if (useRush)
  {
    char         rushHeaderPortAsString[16];
    uint16_t     rushHeaderPort     = 0;
    std::string  rushHeaderIP       = "";
    std::string  headerRushHttp     = "";

    rushHeaderIP   = ip;
    rushHeaderPort = port;
    ip             = rushHost;
    port           = rushPort;

    snprintf(rushHeaderPortAsString, sizeof(rushHeaderPortAsString), "%d", rushHeaderPort);
    headerRushHttp = "X-relayer-host: " + rushHeaderIP + ":" + rushHeaderPortAsString;
    LM_T(LmtHttpHeaders, ("HTTP-HEADERS: '%s'", headerRushHttp.c_str()));
    headers = curl_slist_append(headers, headerRushHttp.c_str());
    outgoingMsgSize += headerRushHttp.size();

    if (protocol == "https:")
    {
      headerRushHttp = "X-relayer-protocol: https";
      LM_T(LmtHttpHeaders, ("HTTP-HEADERS: '%s'", headerRushHttp.c_str()));
      headers = curl_slist_append(headers, headerRushHttp.c_str());
      outgoingMsgSize += headerRushHttp.size();
    }
  }

  snprintf(portAsString, sizeof(portAsString), "%d", port);

  // ----- User Agent
  char cvBuf[CURL_VERSION_MAX_LENGTH];
  char headerUserAgent[HTTP_HEADER_USER_AGENT_MAX_LENGTH];

  snprintf(headerUserAgent, sizeof(headerUserAgent), "User-Agent: orion/%s libcurl/%s", versionGet(), curlVersionGet(cvBuf, sizeof(cvBuf)));
  LM_T(LmtHttpHeaders, ("HTTP-HEADERS: '%s'", headerUserAgent));
  headers = curl_slist_append(headers, headerUserAgent);
  outgoingMsgSize += strlen(headerUserAgent) + 1;

  // ----- Host
  char headerHost[HTTP_HEADER_HOST_MAX_LENGTH];

  snprintf(headerHost, sizeof(headerHost), "Host: %s:%d", ip.c_str(), (int) port);
  LM_T(LmtHttpHeaders, ("HTTP-HEADERS: '%s'", headerHost));
  headers = curl_slist_append(headers, headerHost);
  outgoingMsgSize += strlen(headerHost) + 1;

  // ----- Tenant
  if (tenant != "")
  {
    headers = curl_slist_append(headers, ("fiware-service: " + tenant).c_str());
    outgoingMsgSize += tenant.size() + 16; // "fiware-service: "
  }

  // ----- Service-Path
  if (servicePath != "")
  {
    headers = curl_slist_append(headers, ("Fiware-ServicePath: " + servicePath).c_str());
    outgoingMsgSize += servicePath.size() + strlen("Fiware-ServicePath: ");
  }

  // ----- X-Auth-Token
  if (xauthToken != "")
  {
    headers = curl_slist_append(headers, ("X-Auth-Token: " + xauthToken).c_str());
    outgoingMsgSize += xauthToken.size() + strlen("X-Auth-Token: ");
  }

  // ----- Accept
  std::string acceptedFormats = "application/xml, application/json";
  if (acceptFormat != "")
  {
    acceptedFormats = acceptFormat;
  }

  std::string acceptString = "Accept: " + acceptedFormats;
  headers = curl_slist_append(headers, acceptString.c_str());
  outgoingMsgSize += acceptString.size();

  // ----- Expect
  headers = curl_slist_append(headers, "Expect: ");
  outgoingMsgSize += 8; // from "Expect: "

  // ----- Content-length
  std::stringstream contentLengthStringStream;
  contentLengthStringStream << content.size();
  std::string headerContentLength = "Content-length: " + contentLengthStringStream.str();
  LM_T(LmtHttpHeaders, ("HTTP-HEADERS: '%s'", headerContentLength.c_str()));
  headers = curl_slist_append(headers, headerContentLength.c_str());
  outgoingMsgSize += contentLengthStringStream.str().size() + 16; // from "Content-length: "
  outgoingMsgSize += content.size();

  // ----- Content-type
  headers = curl_slist_append(headers, ("Content-type: " + content_type).c_str());
  outgoingMsgSize += content_type.size() + 14; // from "Content-type: "


  // Check if total outgoing message size is too big
  if (outgoingMsgSize > MAX_DYN_MSG_SIZE)
  {
    LM_E(("Runtime Error (HTTP request to send is too large: %d bytes)", outgoingMsgSize));

    // Cleanup curl environment
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);

    free(httpResponse->memory);
    delete httpResponse;

    LM_TRANSACTION_END();
    return "error";
  }

  // Contents
  const char* payload = content.c_str();
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (u_int8_t*) payload);

  // Set up URL
  std::string url;
  if (isIPv6(ip))
    url = "[" + ip + "]";
  else
    url = ip;
  url = url + ":" + portAsString + (resource.at(0) == '/'? "" : "/") + resource;

  // Prepare CURL handle with obtained options
  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, verb.c_str()); // Set HTTP verb
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Allow redirection (?)
  curl_easy_setopt(curl, CURLOPT_HEADER, 1); // Activate include the header in the body output
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // Put headers in place
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeMemoryCallback); // Send data here
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) httpResponse); // Custom data for response handling

  //
  // Timeout 
  //
  // The parameter timeoutInMilliseconds holds the timeout time in milliseconds.
  // If the timeout time requested is 0, then no timeuot is used.
  //
  if (timeoutInMilliseconds != 0) 
  {
    curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeoutInMilliseconds);
  }


  // Synchronous HTTP request
  LM_T(LmtClientOutputPayload, ("Sending message %lu to HTTP server: sending message of %d bytes to HTTP server", callNo, outgoingMsgSize));
  res = curl_easy_perform(curl);

  if (res != CURLE_OK)
  {
    //
    // NOTE: This log line is used by the functional tests in cases/880_timeout_for_forward_and_notifications/
    //       So, this line should not be removed/altered, at least not without also modifying the functests.
    //
    LM_W(("Notification failure for %s:%s (curl_easy_perform failed: %s)", ip.c_str(), portAsString, curl_easy_strerror(res)));
    result = "";
  }
  else
  {
    // The Response is here
    LM_I(("Notification Successfully Sent to %s", url.c_str()));
    result.assign(httpResponse->memory, httpResponse->size);
  }

  // Cleanup curl environment
  curl_slist_free_all(headers);
  curl_easy_cleanup(curl);

  free(httpResponse->memory);
  delete httpResponse;

  LM_TRANSACTION_END();

  return result;
}
Esempio n. 21
0
/* perform a HTTP GET request via SSL/TLS */
static int
do_get (const char *url)
{
  CURL *c;
  struct CBC cbc;
  CURLcode errornum;
  size_t len;
  struct curl_slist *dns_info;

  len = strlen (test_data);
  if (NULL == (cbc.buf = malloc (sizeof (char) * len)))
    {
      fprintf (stderr, MHD_E_MEM);
      return -1;
    }
  cbc.size = len;
  cbc.pos = 0;

  c = curl_easy_init ();
#if DEBUG_HTTPS_TEST
  curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
#endif
  curl_easy_setopt (c, CURLOPT_URL, url);
  curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L);
  curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L);
  curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  curl_easy_setopt (c, CURLOPT_FILE, &cbc);

  /* perform peer authentication */
  /* TODO merge into send_curl_req */
  curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
  curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 2);
  dns_info = curl_slist_append (NULL, "host1:4233:127.0.0.1");
  dns_info = curl_slist_append (dns_info, "host2:4233:127.0.0.1");
  curl_easy_setopt (c, CURLOPT_RESOLVE, dns_info);
  curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);

  /* NOTE: use of CONNECTTIMEOUT without also
     setting NOSIGNAL results in really weird
     crashes on my system! */
  curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
  if (CURLE_OK != (errornum = curl_easy_perform (c)))
    {
      fprintf (stderr, "curl_easy_perform failed: `%s'\n",
               curl_easy_strerror (errornum));
      curl_easy_cleanup (c);
      free (cbc.buf);
      curl_slist_free_all (dns_info);
      return errornum;
    }

  curl_easy_cleanup (c);
  curl_slist_free_all (dns_info);
  if (memcmp (cbc.buf, test_data, len) != 0)
    {
      fprintf (stderr, "Error: local file & received file differ.\n");
      free (cbc.buf);
      return -1;
    }

  free (cbc.buf);
  return 0;
}
Esempio n. 22
0
                std::string request(http::client &client, http::method method, const std::string &userPath)
                {
                    CURLcode code;

                    char buf[http::MAX_URL_LEN + 1] = {0};

                    struct curl_slist *headers = NULL;

                    CURL *curl = NULL;

                    std::string path = userPath;

                    net::uri uri = client.uri();

                    std::string content = client.content();

                    std::string response;

                    curl = curl_easy_init();

                    if (curl == NULL) {
                        throw socket_exception("unable to initialize curl request");
                    }

                    // check if a path was specified
                    if (path.empty()) {
                        path = uri.full_path();
                    }

                    if (path.empty()) {
                        snprintf(buf, http::MAX_URL_LEN, "%s://%s", uri.scheme().c_str(), uri.host_with_port().c_str());
                    } else if (path[0] == '/') {
                        snprintf(buf, http::MAX_URL_LEN, "%s://%s%s", uri.scheme().c_str(),
                                 uri.host_with_port().c_str(), path.c_str());
                    } else {
                        snprintf(buf, http::MAX_URL_LEN, "%s://%s/%s", uri.scheme().c_str(),
                                 uri.host_with_port().c_str(), path.c_str());
                    }

                    helper::curl_set_opt(curl, CURLOPT_URL, buf);

                    helper::curl_set_opt_fun(curl, CURLOPT_WRITEFUNCTION, helper::curl_append_response_callback);

                    helper::curl_set_opt_num(curl, CURLOPT_HEADER, 1L);

#ifdef DEBUG
                    helper::curl_set_opt_num(curl, CURLOPT_VERBOSE, 1L);
#endif

                    switch (method) {
                        case http::GET:
                            helper::curl_set_opt_num(curl, CURLOPT_HTTPGET, 1L);
                            break;
                        case http::POST:
                            helper::curl_set_opt_num(curl, CURLOPT_POST, 1L);
                            if (!content.empty()) {
                                helper::curl_set_opt(curl, CURLOPT_POSTFIELDS, content.c_str());
                                helper::curl_set_opt_num(curl, CURLOPT_POSTFIELDSIZE, content.size());
                            }
                            break;
                        case http::PUT:
                            helper::curl_set_opt_num(curl, CURLOPT_PUT, 1L);
                            if (!content.empty()) {
                                helper::curl_set_opt(curl, CURLOPT_POSTFIELDS, content.c_str());
                                helper::curl_set_opt_num(curl, CURLOPT_POSTFIELDSIZE, content.size());
                            }
                            break;
                        default:
                            helper::curl_set_opt(curl, CURLOPT_CUSTOMREQUEST, http::method_names[method]);
                            break;
                    }

                    for (auto &h : client.headers()) {
                        snprintf(buf, http::MAX_URL_LEN, "%s: %s", h.first.c_str(), h.second.c_str());
                        headers = curl_slist_append(headers, buf);
                    }

                    helper::curl_set_opt(curl, CURLOPT_HTTPHEADER, headers);

                    helper::curl_set_opt_num(curl, CURLOPT_TIMEOUT, client.timeout());

                    helper::curl_set_opt(curl, CURLOPT_WRITEDATA, &response);

                    CURLcode res = curl_easy_perform(curl);

                    curl_slist_free_all(headers);

                    if (res != CURLE_OK && res != CURLE_PARTIAL_FILE) {
                        curl_easy_cleanup(curl);

                        throw socket_exception(curl_easy_strerror(res));
                    }

                    curl_easy_cleanup(curl);

                    return response;
                }
/**
* Uploads the save file to CloudyWeb.
*
* @param Filename               Filename of the save game file.
* @param PlayerControllerId     PlayerControllerId of the player who is saving the game.
*
* @return Returns true if the file upload was successful. Else, returns false.
*
*/
bool CloudyWebConnectorImpl::UploadFile(FString Filename, int32 PlayerControllerId)
{
    bool RequestSuccess = false;

    CURL *curl;
    CURLcode res;
    std::string readBuffer;
    
    FString Url = BaseUrl + SaveDataUrl;
    std::string UrlCString(TCHAR_TO_UTF8(*Url));
    
    // Filepath of .sav file
    FString Filepath = FPaths::GameDir();
    Filepath += "Saved/SaveGames/" + Filename + ".sav";
    std::string filePath(TCHAR_TO_UTF8(*Filepath));
    
    // Get game name
    FString GameName = FApp::GetGameName();
    std::string gameName(TCHAR_TO_UTF8(*GameName));

    // Get game ID
    FString GameID = FString::FromInt(GetGameId());
    std::string GameIDCString(TCHAR_TO_UTF8(*GameID));

    // Get username
    FString Username = GetUsername(PlayerControllerId);
    std::string UsernameCString(TCHAR_TO_UTF8(*Username));
    
    // Convert PlayerControllerId
    FString playerControllerIdFString = FString::FromInt(PlayerControllerId);
    std::string playerControllerId(TCHAR_TO_UTF8(*playerControllerIdFString));

    if (GetGameId() == -1 || Username.Equals("") || PlayerControllerId < 0)
    {
        UE_LOG(CloudyWebConnectorLog, Error, TEXT("The game ID, username, or player controller ID is invalid"));
        return false;
    }
    
    struct curl_httppost *formpost = NULL;
    struct curl_httppost *lastptr = NULL;
    struct curl_slist *headerlist = NULL;
    FString AuthHeader = "Authorization: Token " + Token;
    std::string AuthHeaderCString(TCHAR_TO_UTF8(*AuthHeader));
    
    curl_global_init(CURL_GLOBAL_ALL);
    
    /* Fill in the file upload field */
    curl_formadd(&formpost, &lastptr,
        CURLFORM_COPYNAME, "saved_file",
        CURLFORM_FILE, filePath.c_str(),
        CURLFORM_END);
    
    /* Fill in the player controller ID field */
    curl_formadd(&formpost, &lastptr,
        CURLFORM_COPYNAME, "user",
        CURLFORM_COPYCONTENTS, UsernameCString.c_str(),
        CURLFORM_END);
    
    /* Fill in the game name field */
    curl_formadd(&formpost, &lastptr,
        CURLFORM_COPYNAME, "game",
        CURLFORM_COPYCONTENTS, GameIDCString.c_str(),
        CURLFORM_END);
    
    curl = curl_easy_init();
    /* initialize custom header list (stating that Expect: 100-continue is not
    wanted */
    headerlist = curl_slist_append(headerlist, AuthHeaderCString.c_str());
    if (curl) {
        /* what URL that receives this POST */
        curl_easy_setopt(curl, CURLOPT_URL, UrlCString.c_str());

        /* only disable 100-continue header if explicitly requested */
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

        /* What form to send */
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    
        /* Set up string to write response into */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        /* Check if the request was successful */
        if (res == CURLE_OK)
        {
            RequestSuccess = true;
        }
    
        /* always cleanup */
        curl_easy_cleanup(curl);
    
        /* then cleanup the formpost chain */
        curl_formfree(formpost);
        /* free slist */
        curl_slist_free_all(headerlist);
    
        UE_LOG(CloudyWebConnectorLog, Warning, TEXT("Response data: %s"), UTF8_TO_TCHAR(readBuffer.c_str()));
        ReadAndStoreSaveFileURL(readBuffer.c_str(), PlayerControllerId);
    }

    return RequestSuccess;
}
Esempio n. 24
0
  void* pull_one_url(void *arg_cbget)
  {
    if (!arg_cbget)
      return NULL;

    cbget *arg = static_cast<cbget*>(arg_cbget);

    CURL *curl = NULL;

    if (!arg->_handler)
      {
        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(curl, CURLOPT_MAXREDIRS,5);
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); // do not check on SSL certificate.
      }
    else curl = arg->_handler;

    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, arg->_connect_timeout_sec);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, arg->_transfer_timeout_sec);
    curl_easy_setopt(curl, CURLOPT_URL, arg->_url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, arg);

    if (!arg->_cookies.empty())
      curl_easy_setopt(curl, CURLOPT_COOKIE, arg->_cookies.c_str());

    if (!arg->_proxy_addr.empty())
      {
        std::string proxy_str = arg->_proxy_addr + ":" + miscutil::to_string(arg->_proxy_port);
        curl_easy_setopt(curl, CURLOPT_PROXY, proxy_str.c_str());
      }

    if (arg->_http_method == "POST") // POST request.
      {
        if (arg->_content)
          {
            curl_easy_setopt(curl, CURLOPT_POST, 1);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void*)arg->_content->c_str());
            if (arg->_content_size >= 0)
              curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, arg->_content_size);
            /*std::cerr << "curl_mget POST size: " << arg->_content_size << std::endl
              << "content: " << *arg->_content << std::endl;*/
          }
        else curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
      }
    else if (arg->_http_method == "PUT" || arg->_http_method == "DELETE")
      {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, arg->_http_method.c_str());
      }

    struct curl_slist *slist=NULL;

    // useful headers.
    if (arg->_headers)
      {
        std::list<const char*>::const_iterator sit = arg->_headers->begin();
        while (sit!=arg->_headers->end())
          {
            slist = curl_slist_append(slist,(*sit));
            ++sit;
          }
        if (arg->_content)
          {
            slist = curl_slist_append(slist,strdup(arg->_content_type.c_str()));
            slist = curl_slist_append(slist,strdup("Expect:"));
          }
      }
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);

    char errorbuffer[CURL_ERROR_SIZE];
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &errorbuffer);

    try
      {
        int status = curl_easy_perform(curl);
        if (status != 0)  // an error occurred.
          {
            arg->_status = status;
            errlog::log_error(LOG_LEVEL_ERROR, "curl error on url %s: %s",arg->_url,errorbuffer);

            if (arg->_output)
              {
                delete arg->_output;
                arg->_output = NULL;
              }
          }
      }
    catch (std::exception &e)
      {
        arg->_status = -1;
        errlog::log_error(LOG_LEVEL_ERROR, "Error %s in fetching remote data with curl.", e.what());
        if (arg->_output)
          {
            delete arg->_output;
            arg->_output = NULL;
          }
      }
    catch (...)
      {
        arg->_status = -2;
        if (arg->_output)
          {
            delete arg->_output;
            arg->_output = NULL;
          }
      }

    if (!arg->_handler)
      curl_easy_cleanup(curl);

    if (slist)
      curl_slist_free_all(slist);

    return NULL;
  }
Esempio n. 25
0
void download_elevation_data(TTBIN_FILE *ttbin)
{
    CURL *curl;
    struct curl_slist *headers;
    char *post_data;
    char *response_data;
    char *str;
    uint32_t i;
    ELEV_DATA_INFO info = {0};
    int result;

    /* only download elevation data if we have GPS records */
    if (!ttbin || !ttbin->gps_record_count || !ttbin->gps_records)
        return;

    curl = curl_easy_init();
    if (!curl)
    {
        fprintf(stderr, "Unable to initialise libcurl\n");
        return;
    }

    /* create the post string to send to the server */
    post_data = malloc(ttbin->gps_record_count * 52 + 10);
    str = post_data;
    str += sprintf(str, "[\n");
    for (i = 0; i < ttbin->gps_record_count; ++i)
    {
        if (i != (ttbin->gps_record_count - 1))
        {
            str += sprintf(str, "   [ %f, %f ],\n",
                ttbin->gps_records[i].latitude,
                ttbin->gps_records[i].longitude);
        }
        else
        {
            str += sprintf(str, "   [ %f, %f ]\n",
                ttbin->gps_records[i].latitude,
                ttbin->gps_records[i].longitude);
        }
    }
    str += sprintf(str, "]\n");

    headers = curl_slist_append(NULL, "Content-Type:text/plain");

    /* setup the callback function data structure */
    info.mult = 1.0;
    info.elev = 0.0;
    info.data = ttbin->gps_records;
    info.max_count = ttbin->gps_record_count;
    info.current_count = 0;

    /* setup the transaction */
    curl_easy_setopt(curl, CURLOPT_URL, "https://mysports.tomtom.com/tyne/dem/fixmodel");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, str - post_data);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "TomTom");
    curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50);
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &info);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data);

    /* perform the transaction */
    result = curl_easy_perform(curl);
    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);
    if (result != CURLE_OK)
        fprintf(stderr, "Unable to download elevation data: %d\n", result);
}
Esempio n. 26
0
int main(void)
{
  CURL *curl;

  CURLM *multi_handle;
  int still_running;

  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  static const char buf[] = "Expect:";

  /* Fill in the file upload field. This makes libcurl load data from
     the given file name when curl_easy_perform() is called. */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "sendfile",
               CURLFORM_FILE, "postit2.c",
               CURLFORM_END);

  /* Fill in the filename field */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "filename",
               CURLFORM_COPYCONTENTS, "postit2.c",
               CURLFORM_END);

  /* Fill in the submit field too, even if this is rarely needed */
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "send",
               CURLFORM_END);

  curl = curl_easy_init();
  multi_handle = curl_multi_init();

  /* initialize custom header list (stating that Expect: 100-continue is not
     wanted */
  headerlist = curl_slist_append(headerlist, buf);
  if(curl && multi_handle) {

    /* what URL that receives this POST */
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

    curl_multi_add_handle(multi_handle, curl);

    curl_multi_perform(multi_handle, &still_running);

    do {
      struct timeval timeout;
      int rc; /* select() return code */
      CURLMcode mc; /* curl_multi_fdset() return code */

      fd_set fdread;
      fd_set fdwrite;
      fd_set fdexcep;
      int maxfd = -1;

      long curl_timeo = -1;

      FD_ZERO(&fdread);
      FD_ZERO(&fdwrite);
      FD_ZERO(&fdexcep);

      /* set a suitable timeout to play around with */
      timeout.tv_sec = 1;
      timeout.tv_usec = 0;

      curl_multi_timeout(multi_handle, &curl_timeo);
      if(curl_timeo >= 0) {
        timeout.tv_sec = curl_timeo / 1000;
        if(timeout.tv_sec > 1)
          timeout.tv_sec = 1;
        else
          timeout.tv_usec = (curl_timeo % 1000) * 1000;
      }

      /* get file descriptors from the transfers */
      mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);

      if(mc != CURLM_OK)
      {
        fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
        break;
      }

      /* On success the value of maxfd is guaranteed to be >= -1. We call
         select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
         no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
         to sleep 100ms, which is the minimum suggested value in the
         curl_multi_fdset() doc. */

      if(maxfd == -1) {
#ifdef _WIN32
        Sleep(100);
        rc = 0;
#else
        /* Portable sleep for platforms other than Windows. */
        struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
        rc = select(0, NULL, NULL, NULL, &wait);
#endif
      }
      else {
        /* Note that on some platforms 'timeout' may be modified by select().
           If you need access to the original value save a copy beforehand. */
        rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
      }

      switch(rc) {
      case -1:
        /* select error */
        break;
      case 0:
      default:
        /* timeout or readable/writable sockets */
        printf("perform!\n");
        curl_multi_perform(multi_handle, &still_running);
        printf("running: %d!\n", still_running);
        break;
      }
    } while(still_running);

    curl_multi_cleanup(multi_handle);

    /* always cleanup */
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */
    curl_formfree(formpost);

    /* free slist */
    curl_slist_free_all (headerlist);
  }
  return 0;
}
Esempio n. 27
0
static char *__curl_do_operation(curl_context *cc, struct curl_slist* headers, int curl_opt,
				 char *url, char *fields, char *data, curl_response **ret_data) {
  
  CURL *curl = NULL;
  CURLcode res;
  char *endpoint;
  char *tmpptr;
  long tmplong;
  long send_len;
  
  *ret_data = init_curl_response();
  if (!(*ret_data)) {
    goto error_exit;
  }
  
  if (data) {
    send_len = strlen(data);
    
  } else {
    send_len = 0;
  }
  
  if (url) {
    endpoint = url;
  } else {
    endpoint = cc->url;
  }
  
  struct curl_http_data send_data = {
    .ptr = data,
    .len = send_len
  };
  
  struct curl_http_data recv_data = {
    .ptr = NULL,
    .len = 0
  };
  
  if (cc->curl_persist) {
    curl = cc->curl;
  } else {
    curl = curl_easy_init();
    if (!curl) {
      dbg_info(ERROR, "Could not initialize CURL\n");
      goto error_exit;
    }
    if (cc->use_ssl) {
      __init_curl_ssl(curl, 0);
      __set_curl_ssl_certificates(curl, cc->cacerts, cc->certfile, cc->keypass, cc->keyfile);
    }
  }
  
  //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  
  curl_easy_setopt(curl, CURLOPT_URL, endpoint);
  curl_easy_setopt(curl, curl_opt, 1L);
  curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
  curl_easy_setopt(curl, CURLOPT_READDATA, &send_data);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &recv_data);
  
  if (cc->follow_redirect) {
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  }
  
  if (cc->use_cookies) {
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cc->cookiejar);
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cc->cookiejar);
  }
  
  if (headers) {
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  }
  
  if (fields && (curl_opt & CURLOPT_HTTPPOST)) {
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields);
  }
  else if (curl_opt & CURLOPT_HTTPPOST) {
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, send_len);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
  }
  
  res = curl_easy_perform(curl);
  if (res != CURLE_OK) {
    dbg_info(ERROR, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    goto error_exit;
  }
  
  if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &tmplong) == CURLE_OK)
    (*ret_data)->status = tmplong;
  
  if (curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &tmpptr) == CURLE_OK){
    if(tmpptr != NULL){
      asprintf(&((*ret_data)->redirect_url), "%s", tmpptr);
    }
  }
  
  if (curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &tmpptr) == CURLE_OK){
    if(tmpptr != NULL){
      (*ret_data)->content_type = malloc(strlen(tmpptr) + 1);
      strncpy((*ret_data)->content_type, tmpptr, strlen(tmpptr) + 1);
      //asprintf(&((*ret_data)->content_type), "%s", tmpptr);
    }
  }
  
  if (recv_data.len) {
    (*ret_data)->data = malloc(recv_data.len+1 * sizeof(char));
    memcpy((*ret_data)->data, recv_data.ptr, recv_data.len);
    (*ret_data)->data[recv_data.len] = '\0';
  }
  
  if (headers) {
    curl_slist_free_all(headers);
  }
  
  if (!(cc->curl_persist)) {
    curl_easy_cleanup(curl);
  }
  
  return (*ret_data)->data;
  
 error_exit:
  if (headers)
    curl_slist_free_all(headers);
  if (!(cc->curl_persist))
    curl_easy_cleanup(curl);
  free_curl_response(*ret_data);
  *ret_data = NULL;
  return NULL;
}

static size_t read_cb(void *ptr, size_t size, size_t nmemb, void *userp) {
  struct curl_http_data *data = (struct curl_http_data *) userp;
  
  if (size * nmemb < 1) {
    return 0;
  }
  if (data->len) {
    *(char *) ptr = data->ptr[0];
    data->ptr++;
    data->len--;
    return 1;
  }
  
  return 0;
}

static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *userp) {
  struct curl_http_data *s = (struct curl_http_data *) userp;
  size_t new_len = s->len + size*nmemb;
  s->ptr = realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
    exit(EXIT_FAILURE);
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);
  s->ptr[new_len] = '\0';
  s->len = new_len;
  
  return size*nmemb;
}
ResourceHandleInternal::~ResourceHandleInternal()
{
    fastFree(m_url);
    if (m_customHeaders)
        curl_slist_free_all(m_customHeaders);
}
Esempio n. 29
0
static void moloch_http_curlm_check_multi_info(MolochHttpServer_t *server)
{
    char *eff_url;
    CURLMsg *msg;
    int msgs_left;
    MolochHttpRequest_t *request;
    CURL *easy;

    while ((msg = curl_multi_info_read(server->multi, &msgs_left))) {
        if (msg->msg == CURLMSG_DONE) {
            easy = msg->easy_handle;
            curl_easy_getinfo(easy, CURLINFO_PRIVATE, (void*)&request);
            curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);

            long   responseCode;
            curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &responseCode);

            if (config.logESRequests) {
                double totalTime;
                double connectTime;
                double uploadSize;
                double downloadSize;

                curl_easy_getinfo(easy, CURLINFO_TOTAL_TIME, &totalTime);
                curl_easy_getinfo(easy, CURLINFO_CONNECT_TIME, &connectTime);
                curl_easy_getinfo(easy, CURLINFO_SIZE_UPLOAD, &uploadSize);
                curl_easy_getinfo(easy, CURLINFO_SIZE_DOWNLOAD, &downloadSize);

                LOG("%d/%d ASYNC %ld %s %.0lf/%.0lf %.0lfms %.0lfms",
                   request->server->outstanding,
                   request->server->connections,
                   responseCode,
                   request->url,
                   uploadSize,
                   downloadSize,
                   connectTime*1000,
                   totalTime*1000);
            }

#ifdef MOLOCH_HTTP_DEBUG
            LOG("HTTPDEBUG DECR %s %p %d %s", server->names[0], request, server->outstanding, request->url);
#endif


            if (request->func) {
                if (request->dataIn)
                    request->dataIn[request->used] = 0;
                request->func(responseCode, request->dataIn, request->used, request->uw);
            }

            if (request->dataIn) {
                free(request->dataIn);
                request->dataIn = 0;
            }
            if (request->dataOut) {
                MOLOCH_SIZE_FREE(buffer, request->dataOut);
            }
            if (request->headerList) {
                curl_slist_free_all(request->headerList);
            }
            MOLOCH_TYPE_FREE(MolochHttpRequest_t, request);

            curl_multi_remove_handle(server->multi, easy);
            curl_easy_cleanup(easy);
            MOLOCH_LOCK(requests);
            server->outstanding--;
            MOLOCH_UNLOCK(requests);
        }
    }
}
Esempio n. 30
0
File: curl.c Progetto: diegonc/burp
long aur_upload(const char *taurball) {
  char *ptr, *fullpath;
  char missing_var[10], category[3];
  long httpcode, ret = 0;
  CURLcode status;
  struct curl_httppost *post, *last;
  struct curl_slist *headers;
  static struct write_result response;

  fullpath = realpath(taurball, NULL);
  if (fullpath == NULL) {
    fprintf(stderr, "Error uploading file '%s': ", taurball);
    perror("");
    return(1L);
  }

  post = last = NULL;
  headers = NULL;
  response.memory = NULL;
  response.size = 0;

  snprintf(category, 3, "%d", config->catnum);

  curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "pkgsubmit",
    CURLFORM_COPYCONTENTS, "1", CURLFORM_END);
  curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "category",
    CURLFORM_COPYCONTENTS, category, CURLFORM_END);
  curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "pfile",
    CURLFORM_FILE, fullpath, CURLFORM_END);

  headers = curl_slist_append(headers, "Expect:");

  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  curl_easy_setopt(curl, CURLOPT_URL, AUR_SUBMIT_URL);

  if (config->verbose > 0) {
    printf("Uploading taurball: %s\n", config->verbose > 1 ? fullpath : taurball);
  }

  status = curl_easy_perform(curl);
  if (status != CURLE_OK) {
    fprintf(stderr, "curl error: unable to send data to %s\n", AUR_SUBMIT_URL);
    fprintf(stderr, "%s\n", curl_easy_strerror(status));
    ret = status;
    goto cleanup;
  }

  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
  if(httpcode != 200) {
    fprintf(stderr, "curl error: server responded with code %ld\n", httpcode);
    ret = httpcode;
    goto cleanup;
  }

  if (config->verbose > 1) {
    printf("%s\n", response.memory);
  }

  if (strstr(response.memory, AUR_NO_LOGIN)) {
    fprintf(stderr, "Error: Authentication failed on upload.\n");
    ret = 1;
  } else if (strstr(response.memory, AUR_NO_OVERWRITE)) {
    fprintf(stderr, "Error: You don't have permission to overwrite this file.\n");
    ret = 1;
  } else if (strstr(response.memory, AUR_UNKNOWN_FORMAT)) {
    fprintf(stderr, "Error: Incorrect file format. Upload must conform to AUR "
                    "packaging guidelines.\n");
    ret = 1;
  } else if (strstr(response.memory, AUR_INVALID_NAME)) {
    fprintf(stderr, "Error: Invalid package name. Only lowercase letters are "
                    "allowed. Make sure this isn't a split package.\n");
    ret = 1;
  } else if (strstr(response.memory, AUR_NO_PKGBUILD)) {
    fprintf(stderr, "Error: PKGBUILD does not exist in uploaded source.\n");
    ret = 1;
  } else if (strstr(response.memory, AUR_NO_BUILD_FUNC)) {
    fprintf(stderr, "Error: PKGBUILD is missing build function.\n");
    ret = 1;
  } else if (strstr(response.memory, AUR_MISSING_PROTO)) { 
    fprintf(stderr, "Error: Package URL is missing a protocol\n");
    ret = 1;
  } else if ((ptr = strstr(response.memory, "Missing")) && 
              sscanf(ptr, AUR_MISSING_VAR, missing_var)) {
    fprintf(stderr, "Error: Package is missing %s variable\n", missing_var);
    ret = 1;
  } else {
    printf("%s has been uploaded successfully.\n", basename(taurball));
  }

cleanup:
  free(fullpath);
  free(response.memory);
  curl_slist_free_all(headers);
  curl_formfree(post);

  return(ret);
}