• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ curl_easy_init函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中curl_easy_init函数的典型用法代码示例。如果您正苦于以下问题:C++ curl_easy_init函数的具体用法?C++ curl_easy_init怎么用?C++ curl_easy_init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了curl_easy_init函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: janus_mutex_lock

janus_turnrest_response *janus_turnrest_request(void) {
    janus_mutex_lock(&api_mutex);
    if(api_server == NULL) {
        janus_mutex_unlock(&api_mutex);
        return NULL;
    }
    /* Prepare the request URI */
    char query_string[512];
    g_snprintf(query_string, 512, "service=turn");
    if(api_key != NULL) {
        char buffer[256];
        g_snprintf(buffer, 256, "&api=%s", api_key);
        g_strlcat(query_string, buffer, 512);
    }
    char request_uri[1024];
    g_snprintf(request_uri, 1024, "%s?%s", api_server, query_string);
    JANUS_LOG(LOG_VERB, "Sending request: %s\n", request_uri);
    janus_mutex_unlock(&api_mutex);
    /* Prepare the libcurl context */
    CURLcode res;
    CURL *curl = curl_easy_init();
    if(curl == NULL) {
        JANUS_LOG(LOG_ERR, "libcurl error\n");
        return NULL;
    }
    curl_easy_setopt(curl, CURLOPT_URL, request_uri);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    /* FIXME Some servers don't like a POST with no data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);	/* FIXME Max 10 seconds */
    /* For getting data, we use an helper struct and the libcurl callback */
    janus_turnrest_buffer data;
    data.buffer = malloc(1);
    data.size = 0;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, janus_turnrest_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Janus/1.0");
    /* Send the request */
    res = curl_easy_perform(curl);
    if(res != CURLE_OK) {
        JANUS_LOG(LOG_ERR, "Couldn't send the request: %s\n", curl_easy_strerror(res));
        free(data.buffer);
        curl_easy_cleanup(curl);
        return NULL;
    }
    /* Cleanup the libcurl context */
    curl_easy_cleanup(curl);
    /* Process the response */
    JANUS_LOG(LOG_VERB, "Got %zu bytes from the TURN REST API server\n", data.size);
    JANUS_LOG(LOG_VERB, "%s\n", data.buffer);
    json_error_t error;
    json_t *root = json_loads(data.buffer, 0, &error);
    if(!root) {
        JANUS_LOG(LOG_ERR, "Couldn't parse response: error on line %d: %s", error.line, error.text);
        free(data.buffer);
        return NULL;
    }
    free(data.buffer);
    json_t *username = json_object_get(root, "username");
    if(!username) {
        JANUS_LOG(LOG_ERR, "Invalid response: missing username\n");
        return NULL;
    }
    if(!json_is_string(username)) {
        JANUS_LOG(LOG_ERR, "Invalid response: username should be a string\n");
        return NULL;
    }
    json_t *password = json_object_get(root, "password");
    if(!password) {
        JANUS_LOG(LOG_ERR, "Invalid response: missing password\n");
        return NULL;
    }
    if(!json_is_string(password)) {
        JANUS_LOG(LOG_ERR, "Invalid response: password should be a string\n");
        return NULL;
    }
    json_t *ttl = json_object_get(root, "ttl");
    if(ttl && (!json_is_integer(ttl) || json_integer_value(ttl) < 0)) {
        JANUS_LOG(LOG_ERR, "Invalid response: ttl should be a positive integer\n");
        return NULL;
    }
    json_t *uris = json_object_get(root, "uris");
    if(!uris) {
        JANUS_LOG(LOG_ERR, "Invalid response: missing uris\n");
        return NULL;
    }
    if(!json_is_array(uris) || json_array_size(uris) == 0) {
        JANUS_LOG(LOG_ERR, "Invalid response: uris should be a non-empty array\n");
        return NULL;
    }
    /* Turn the response into a janus_turnrest_response object we can use */
    janus_turnrest_response *response = calloc(1, sizeof(janus_turnrest_response));
    response->username = g_strdup(json_string_value(username));
    response->password = g_strdup(json_string_value(password));
    response->ttl = ttl ? json_integer_value(ttl) : 0;
    response->servers = NULL;
    size_t i = 0;
    for(i=0; i<json_array_size(uris); i++) {
        json_t *uri = json_array_get(uris, i);
        if(uri == NULL || !json_is_string(uri)) {
//.........这里部分代码省略.........
开发者ID:JonasBryum,项目名称:janus-gateway,代码行数:101,代码来源:turnrest.c


示例2: check

CURLcode check(char *response, size_t length, const char *username, const char* password, const char* proxy, int stype) {

	CURL *curl = curl_easy_init();
	if(!curl) {
		fdo_log(GENLOG, "cURL Init Error: (?!)");
		return CURLE_FAILED_INIT; //?!?!?!?!
	}

	struct memstruct CurlStruct;

	CurlStruct.memory = malloc(1);
	*CurlStruct.memory = 0;
	CurlStruct.size = 0;

	//Prepare custom headers.
	char *userenc = curl_easy_escape(curl, username, 0);
	if(!userenc) {
		free(CurlStruct.memory);
		return CURLE_FAILED_INIT;
	}
	char *passenc = curl_easy_escape(curl, password, 0);
	if(!passenc) {
		free(CurlStruct.memory);
		curl_free(passenc); passenc = NULL;
		return CURLE_FAILED_INIT;
	}
	size_t plen = snprintf(NULL, 0, "rem=on&username=%s&password=%s&submit=Log+In&mod=www&ssl=1&dest=account_settings.ws", userenc, passenc);
	plen += 1;
	char *post = malloc(plen);
	if(!post) {
		free(CurlStruct.memory);
		curl_free(userenc); userenc = NULL;
		curl_free(passenc); passenc = NULL;
		return CURLE_FAILED_INIT;
	}
	snprintf(post, plen, "rem=on&username=%s&password=%s&submit=Log+In&mod=www&ssl=1&dest=account_settings.ws", userenc, passenc);

	curl_free(userenc); userenc = NULL;
	curl_free(passenc); passenc = NULL;

	curl_easy_setopt(curl, CURLOPT_URL, "https://secure.runescape.com/m=weblogin/login.ws"); //This may change in the future.
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0"); //Possibly change this in the future. They block useragents when they're old(Probably my fault)
	curl_easy_setopt(curl, CURLOPT_REFERER, "https://secure.runescape.com/m=weblogin/loginform.ws?mod=www&ssl=1&reauth=1&dest=account_settings.ws"); //Likewise, may be needed to change.
	curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); //When followlocation takes place.
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
	curl_easy_setopt(curl, CURLOPT_HEADER, 0);
	curl_easy_setopt(curl, CURLOPT_ENCODING, "identity");
	curl_easy_setopt(curl, CURLOPT_POST, 1);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);
	curl_easy_setopt(curl, CURLOPT_PROXYTYPE, stype);
	curl_easy_setopt(curl, CURLOPT_PROXY, proxy);

	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, StoreCurl);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&CurlStruct);

	CURLcode res = curl_easy_perform(curl);

	if(res != CURLE_OK) {
		fdo_log(DBGLOG, "cURL Error: %s, Proxy: %s.(type: %d)", curl_easy_strerror(res), proxy, stype);
	} else {
		if(CurlStruct.memory != NULL) {
			strncpy(response, CurlStruct.memory, length);
			response[length-1] = '\0';
		} else {
			response = NULL;
		}
	}

	curl_easy_cleanup(curl);
	free(CurlStruct.memory); CurlStruct.memory = NULL;
	free(post); post = NULL;

	return res;
}
开发者ID:MegaManSec,项目名称:RS-Account-Checker,代码行数:80,代码来源:curl.c


示例3: curl_post_form

char* curl_post_form(const char *url,\
	                 const char *postdata,\
	                 const char *proxy,\
	                 const char *cookie,\
	                 int flag_cookie)


{
   long timeout = 10800;
	long connect_timeout = 15;
	long low_speed_limit = 1024;
	long low_speed_time = 60;

	struct mem_node *mem_head = NULL;
	long response_code;
   CURL *curl = curl_easy_init();
   curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); // for thread safe
   curl_easy_setopt(curl,CURLOPT_URL,url); //url地址  
   curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postdata); //post参数  
   curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connect_timeout);
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
	curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, low_speed_limit);
	curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, low_speed_time);
   curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,&curl_read); //对返回的数据进行操作的函数地址  
   curl_easy_setopt(curl,CURLOPT_WRITEDATA,&mem_head); //这是write_data的第四个参数值  
   curl_easy_setopt(curl,CURLOPT_POST,1); //设置非0表示本次操作为post   
   curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); //设置为非0,响应头信息location  
   if(flag_cookie)
   {
    curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"./cookie.txt");
     curl_easy_setopt(curl,CURLOPT_COOKIEJAR,"./cookie.txt");
 }
  // curl_easy_setopt(easy_handle, CURLOPT_PROXY,proxy);
   CURLcode rc = curl_easy_perform(curl); 
  	curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
	curl_easy_cleanup(curl);
	if (rc!=CURLE_OK) {
		return NULL;
	}else if (response_code!=200 && response_code!=206){
		struct mem_node *p = mem_head;
		while(p){
			struct mem_node *q = p;
			p = p->next;
			free(q->buffer);
			free(q);
		}
		return NULL;
	}else{
		struct mem_node *p = mem_head;
		size_t size = 0;
		while(p){
			size += p->size;
			p = p->next;
		}
		char *content = (char*)malloc(size+1);
		p = mem_head;
		size = 0;
		while(p){
			memcpy(content+size, p->buffer, p->size);
			size += p->size;
			struct mem_node *q = p;
			p = p->next;
			free(q->buffer);
			free(q);
		}
		content[size] = 0;
		return content;
	}
  
}
开发者ID:qqmcc,项目名称:webscan,代码行数:70,代码来源:http.c


示例4: stratum_connect

bool stratum_connect(struct stratum_ctx *sctx, const char *url)
{
	CURL *curl;
	int rc;

	pthread_mutex_lock(&sctx->sock_lock);
	if (sctx->curl)
		curl_easy_cleanup(sctx->curl);
	sctx->curl = curl_easy_init();
	if (!sctx->curl) {
		applog(LOG_ERR, "CURL initialization failed");
		pthread_mutex_unlock(&sctx->sock_lock);
		return false;
	}
	curl = sctx->curl;
	if (!sctx->sockbuf) {
		sctx->sockbuf = (char*)calloc(RBUFSIZE, 1);
		sctx->sockbuf_size = RBUFSIZE;
	}
	sctx->sockbuf[0] = '\0';
	pthread_mutex_unlock(&sctx->sock_lock);

	if (url != sctx->url) {
		free(sctx->url);
		sctx->url = strdup(url);
	}
	free(sctx->curl_url);
	sctx->curl_url = (char*)malloc(strlen(url));
	sprintf(sctx->curl_url, "http%s", strstr(url, "://"));

	if (opt_protocol)
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
	curl_easy_setopt(curl, CURLOPT_URL, sctx->curl_url);
	curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, sctx->curl_err_str);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
	if (opt_proxy && opt_proxy_type != CURLPROXY_HTTP) {
		curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
		curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
	} else if (getenv("http_proxy")) {
		if (getenv("all_proxy"))
			curl_easy_setopt(curl, CURLOPT_PROXY, getenv("all_proxy"));
		else if (getenv("ALL_PROXY"))
			curl_easy_setopt(curl, CURLOPT_PROXY, getenv("ALL_PROXY"));
		else
			curl_easy_setopt(curl, CURLOPT_PROXY, "");
	}
#if LIBCURL_VERSION_NUM >= 0x070f06
	curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
#endif
#if LIBCURL_VERSION_NUM >= 0x071101
	curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket_grab_cb);
	curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sctx->sock);
#endif
	curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);

	rc = curl_easy_perform(curl);
	if (rc) {
		applog(LOG_ERR, "Stratum connection failed: %s", sctx->curl_err_str);
		curl_easy_cleanup(curl);
		sctx->curl = NULL;
		return false;
	}

#if LIBCURL_VERSION_NUM < 0x071101
	/* CURLINFO_LASTSOCKET is broken on Win64; only use it as a last resort */
	curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, (long *)&sctx->sock);
#endif

	return true;
}
开发者ID:lstrchm,项目名称:ccminer,代码行数:73,代码来源:util.c


示例5: http_query

/* 
 * Performs http_query and saves possible result (first body line of reply)
 * to pvar.
 */
int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post)
{
    CURL *curl;
    CURLcode res;  
    str value, post_value;
    char *url, *at, *post;
    char* stream;
    long stat;
    pv_spec_t *dst;
    pv_value_t val;
    double download_size;

    if (fixup_get_svalue(_m, (gparam_p)_url, &value) != 0) {
	LM_ERR("cannot get page value\n");
	return -1;
    }

    curl = curl_easy_init();
    if (curl == NULL) {
	LM_ERR("failed to initialize curl\n");
	return -1;
    }

    url = pkg_malloc(value.len + 1);
    if (url == NULL) {
	curl_easy_cleanup(curl);
	LM_ERR("cannot allocate pkg memory for url\n");
	return -1;
    }
    memcpy(url, value.s, value.len);
    *(url + value.len) = (char)0;
    curl_easy_setopt(curl, CURLOPT_URL, url);

    if (_post) {
        /* Now specify we want to POST data */ 
	curl_easy_setopt(curl, CURLOPT_POST, 1L);

    	if (fixup_get_svalue(_m, (gparam_p)_post, &post_value) != 0) {
		LM_ERR("cannot get post value\n");
		pkg_free(url);
		return -1;
    	}
        post = pkg_malloc(post_value.len + 1);
        if (post == NULL) {
		curl_easy_cleanup(curl);
		pkg_free(url);
        	LM_ERR("cannot allocate pkg memory for post\n");
        	return -1;
	}
	memcpy(post, post_value.s, post_value.len);
	*(post + post_value.len) = (char)0;
 	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
    }
       

    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)http_query_timeout);

    stream = NULL;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);

    res = curl_easy_perform(curl);  
    pkg_free(url);
    if (_post) {
	pkg_free(post);
    }
    curl_easy_cleanup(curl);

    if (res != CURLE_OK) {
	LM_ERR("failed to perform curl\n");
	return -1;
    }

    curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat);
    if ((stat >= 200) && (stat < 400)) {
	curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
	LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
	/* search for line feed */
	at = memchr(stream, (char)10, download_size);
	if (at == NULL) {
	    /* not found: use whole stream */
	    at = stream + (unsigned int)download_size;
	}
	val.rs.s = stream;
	val.rs.len = at - stream;
	LM_DBG("http)query result: %.*s\n", val.rs.len, val.rs.s);
	val.flags = PV_VAL_STR;
	dst = (pv_spec_t *)_dst;
	dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
    }
	
    return stat;
}
开发者ID:aallamaa,项目名称:kamailio,代码行数:98,代码来源:functions.c


示例6: main

int main(void)
{
  CURL *curl;
  CURLM *mcurl;
  int still_running = 1;
  struct timeval mp_start;
  struct curl_slist *recipients = NULL;
  struct upload_status upload_ctx;

  upload_ctx.lines_read = 0;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(!curl)
    return 1;

  mcurl = curl_multi_init();
  if(!mcurl)
    return 2;

  /* This is the URL for your mailserver */
  curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");

  /* Note that this option isn't strictly required, omitting it will result in
   * libcurl sending the MAIL FROM command with empty sender data. All
   * autoresponses should have an empty reverse-path, and should be directed
   * to the address in the reverse-path which triggered them. Otherwise, they
   * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
   */
  curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);

  /* Add two recipients, in this particular case they correspond to the
   * To: and Cc: addressees in the header, but they could be any kind of
   * recipient. */
  recipients = curl_slist_append(recipients, TO);
  recipients = curl_slist_append(recipients, CC);
  curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

  /* We're using a callback function to specify the payload (the headers and
   * body of the message). You could just use the CURLOPT_READDATA option to
   * specify a FILE pointer to read from. */
  curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
  curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
  curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

  /* Tell the multi stack about our easy handle */
  curl_multi_add_handle(mcurl, curl);

  /* Record the start time which we can use later */
  mp_start = tvnow();

  /* We start some action by calling perform right away */
  curl_multi_perform(mcurl, &still_running);

  while(still_running) {
    struct timeval timeout;
    fd_set fdread;
    fd_set fdwrite;
    fd_set fdexcep;
    int maxfd = -1;
    int rc;

    long curl_timeo = -1;

    /* Initialise the file descriptors */
    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(mcurl, &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 */
    curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);

    /* In a real-world program you OF COURSE check the return code of the
       function calls.  On success, the value of maxfd is guaranteed to be
       greater or equal than -1.  We call select(maxfd + 1, ...), specially in
       case of (maxfd == -1), we call select(0, ...), which is basically equal
       to sleep. */
    rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);

    if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
      fprintf(stderr,
              "ABORTING: Since it seems that we would have run forever.\n");
      break;
    }

    switch(rc) {
//.........这里部分代码省略.........
开发者ID:08142008,项目名称:curl,代码行数:101,代码来源:smtp-multi.c


示例7: test

int test(char *URL)
{
  int res = 0;
  CURL *curl[NUM_HANDLES];
  int running;
  char done=FALSE;
  CURLM *m;
  int i, j;
  struct timeval ml_start;
  struct timeval mp_start;
  char ml_timedout = FALSE;
  char mp_timedout = FALSE;
  char target_url[256];

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

  if ((m = curl_multi_init()) == NULL) {
    fprintf(stderr, "curl_multi_init() failed\n");
    curl_global_cleanup();
    return TEST_ERR_MAJOR_BAD;
  }

  /* get NUM_HANDLES easy handles */
  for(i=0; i < NUM_HANDLES; i++) {
    curl[i] = curl_easy_init();
    if(!curl[i]) {
      fprintf(stderr, "curl_easy_init() failed "
              "on handle #%d\n", i);
      for (j=i-1; j >= 0; j--) {
        curl_multi_remove_handle(m, curl[j]);
        curl_easy_cleanup(curl[j]);
      }
      curl_multi_cleanup(m);
      curl_global_cleanup();
      return TEST_ERR_MAJOR_BAD + i;
    }
    sprintf(target_url, "%s%04i", URL, i + 1);
    target_url[sizeof(target_url) - 1] = '\0';
    curl_easy_setopt(curl[i], CURLOPT_URL, target_url);

    /* go verbose */
    curl_easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);

    /* include headers */
    curl_easy_setopt(curl[i], CURLOPT_HEADER, 1L);

    /* add handle to multi */
    if ((res = (int)curl_multi_add_handle(m, curl[i])) != CURLM_OK) {
      fprintf(stderr, "curl_multi_add_handle() failed, "
              "on handle #%d with code %d\n", i, res);
      curl_easy_cleanup(curl[i]);
      for (j=i-1; j >= 0; j--) {
        curl_multi_remove_handle(m, curl[j]);
        curl_easy_cleanup(curl[j]);
      }
      curl_multi_cleanup(m);
      curl_global_cleanup();
      return TEST_ERR_MAJOR_BAD + i;
    }
  }

  curl_multi_setopt(m, CURLMOPT_PIPELINING, 1L);

  ml_timedout = FALSE;
  ml_start = tutil_tvnow();

  fprintf(stderr, "Start at URL 0\n");

  while (!done) {
    fd_set rd, wr, exc;
    int max_fd;
    struct timeval interval;

    interval.tv_sec = 1;
    interval.tv_usec = 0;

    if (tutil_tvdiff(tutil_tvnow(), ml_start) > 
        MAIN_LOOP_HANG_TIMEOUT) {
      ml_timedout = TRUE;
      break;
    }
    mp_timedout = FALSE;
    mp_start = tutil_tvnow();

    while (res == CURLM_CALL_MULTI_PERFORM) {
      res = (int)curl_multi_perform(m, &running);
      if (tutil_tvdiff(tutil_tvnow(), mp_start) > 
          MULTI_PERFORM_HANG_TIMEOUT) {
        mp_timedout = TRUE;
        break;
      }
      if (running <= 0) {
        done = TRUE; /* bail out */
        break;
      }
    }
    if (mp_timedout || done)
//.........这里部分代码省略.........
开发者ID:JulianSpillane,项目名称:moai-dev,代码行数:101,代码来源:lib530.c


示例8: PostToWS

// ------------------------------------------------------------------
// Funcion que realiza un post al WS
// ------------------------------------------------------------------
int PostToWS(const char *url, const char *postData, char result[2048])
{
	char tmp[1024];
	char tmpb[1024];
	int exitStatus = 0;
	CURL* curl;
	CURLcode res;	
	struct WritePOST wtext;
	struct ReadPOST rtext;
   init_struct(&rtext);	
	
	/* Headers */ 
	struct curl_slist *headers = NULL;
	headers = curl_slist_append(headers, "Accept: application/json");
	headers = curl_slist_append(headers, "Content-Type: application/json");
	headers = curl_slist_append(headers, "charsets: utf-8");
	// Parameters for call 
	wtext.ptr = postData;
	wtext.sizeleft = strlen(postData);
	/* Initialise libcurl */
	curl_global_init(CURL_GLOBAL_ALL);	
	/* Get a curl handle */
	curl = curl_easy_init(); 
	if(curl) 
	{
		//printf("%s", url);
		/* First set the URL that is about to receive our POST. */ 
		curl_easy_setopt(curl, CURLOPT_URL, url);
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
		/* Now specify we want to POST data */ 
		curl_easy_setopt(curl, CURLOPT_POST, 1L);
		/* we want to use our own read function */ 
		curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
		/* pointer to pass to our read function */ 
		curl_easy_setopt(curl, CURLOPT_READDATA, &wtext);
		/* get verbose debug output please */ 
		//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
		/* Set the expected POST size. If you want to POST large amounts of data,
			consider CURLOPT_POSTFIELDSIZE_LARGE */ 
		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)wtext.sizeleft);
		/* we want to use our own write function */ 
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
		/* pointer to pass to our write function */ 
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rtext);
		/* Perform the request, res will get the return code */ 
		res = curl_easy_perform(curl);
		if(res) 
		{
			sprintf(tmp, "ERROR: %s", curl_easy_strerror(res));
			printf("%s\n", tmp);
			exitStatus = 0;
		}
		else
		{
			if(rtext.ptr != NULL)
			{
				if(strlen(rtext.ptr) > 0)
				{		
					sprintf(result, "%s", rtext.ptr);
					// Imprimir resultados del WS
					printf("\nA: %s\n", result);
				}
				else
				{
					printf("\nA: if(strlen(rtext.ptr) > 0) es falso");
				}
				free(rtext.ptr);				
			}
			exitStatus = 1;
		}
		/* always cleanup */ 
		curl_easy_cleanup(curl);
	}
	else 
	{
		exitStatus = 0;
	}
	/* Return the exit status */
	return exitStatus;
}
开发者ID:yosoyelmilton,项目名称:Pinzuar,代码行数:83,代码来源:ws_consumer.c


示例9: printf

void email::send() {
	if( ! activated ) {
		printf( "Send mail attempted: %s", content.c_str() );
		exit(0);
	} else {
		// Check maximum sent count
		if( sent_count > 3 ) {
			dump( "Too much email sent. Exiting for safety...\n" );
			exit(0);
		} else {
			dump( "Sending an email...\n" );
			dump( "%s\n" , subject.c_str());
			dump( "Content: %s\n", content.c_str() );
		}
		
		// Correct return code
		for( uint n=0; n<content.size(); n++ ) {
			if( content[n] == '\n' && content[n+1] == 0 ) content[n] = 0;
		}
		// Set message
		payload_text[2] = subject.c_str();
		payload_text[4] = content.c_str();
	}
#if ENABLE_LCURL
	CURL *curl;
	CURLcode res;
	struct curl_slist *recipients = NULL;
	struct upload_status upload_ctx;
	upload_ctx.lines_read = 0;
	
	curl = curl_easy_init();
	if (curl) {
		/* This is the URL for your mailserver. Note the use of port 587 here,
		 * instead of the normal SMTP port (25). Port 587 is commonly used for
		 * secure mail submission (see RFC4403), but you should use whatever
		 * matches your server configuration. */
		curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.yoursever.com:587");
		
		/* In this example, we'll start with a plain text connection, and upgrade
		 * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
		 * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
		 * will continue anyway - see the security discussion in the libcurl
		 * tutorial for more details. */
		curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
		
		/* If your server doesn't have a valid certificate, then you can disable
		 * part of the Transport Layer Security protection by setting the
		 * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
		 *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
		 *   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
		 * That is, in general, a bad idea. It is still better than sending your
		 * authentication details in plain text though.
		 * Instead, you should get the issuer certificate (or the host certificate
		 * if the certificate is self-signed) and add it to the set of certificates
		 * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
		 * docs/SSLCERTS for more information.
		 */
		// curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
		
		/* A common reason for requiring transport security is to protect
		 * authentication details (user names and passwords) from being "snooped"
		 * on the network. Here is how the user name and password are provided: */
		curl_easy_setopt(curl, CURLOPT_USERNAME, "[email protected]");
		curl_easy_setopt(curl, CURLOPT_PASSWORD, "yourpassword");
		
		/* value for envelope reverse-path */
		curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
		/* Add two recipients, in this particular case they correspond to the
		 * To: and Cc: addressees in the header, but they could be any kind of
		 * recipient. */
		recipients = curl_slist_append(recipients, TO);
		curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
		
		/* In this case, we're using a callback function to specify the data. You
		 * could just use the CURLOPT_READDATA option to specify a FILE pointer to
		 * read from.
		 */
		curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
		curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
		
		/* Since the traffic will be encrypted, it is very useful to turn on debug
		 * information within libcurl to see what is happening during the transfer.
		 */
		// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
		
		/* send the message (including headers) */
		res = curl_easy_perform(curl);
		/* Check for errors */
		if(res != CURLE_OK) {
			fprintf(stderr, "curl_easy_perform() failed: %s\n",
					curl_easy_strerror(res));
			exit(0);
		}
		/* free the list of recipients and clean up */
		curl_slist_free_all(recipients);
		curl_easy_cleanup(curl);
		
		// Clear message body
		content.clear();
//.........这里部分代码省略.........
开发者ID:wenderen,项目名称:btp,代码行数:101,代码来源:email.cpp


示例10: query_tool_set_powered

int query_tool_set_powered(const char* tool_id, bool powered) {
  CURL* handle;
  CURLcode error_code;
  char buf[1024];
  long response = 0;
  struct curl_slist *headers = NULL;

#ifdef DEBUG_EVENT_RESPONSE
  FILE *fdebug;
  fdebug = fopen("debug.html", "w");
#endif

  handle = curl_easy_init();
  if (handle == NULL)
    return 1;

  headers = curl_slist_append(headers, "Accept: application/json");
  headers = curl_slist_append(headers, "Content-Type: application/json");

  sprintf(buf, "PUBLIC_KEY: %s", public_key);
  headers = curl_slist_append(headers, buf);

  sprintf(buf, "PRIVATE_KEY: %s", private_key);
  headers = curl_slist_append(headers, buf);

  // For new ModWSGI version.  Delete previous
  // authentication headers when Roboclub8 fully migrated
  sprintf(buf, "PUBLIC-KEY: %s", public_key);
  headers = curl_slist_append(headers, buf);

  sprintf(buf, "PRIVATE-KEY: %s", private_key);
  headers = curl_slist_append(headers, buf);

  json_object* json = json_object_new_object();
  json_object_object_add(json, "powered", json_object_new_boolean(powered));

  sprintf(buf, "%s/api/machines/%s/", server, tool_id);
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
  if (error_code) goto error;

  /* TODO disabling host and peer verification should theoretically be removed
   * eventually */
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
  if (error_code) goto error;

#ifdef DEBUG_EVENT_RESPONSE
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug);
#else
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
#endif
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "PUT");
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_POSTFIELDS, json_object_to_json_string(json));
  if (error_code) goto error;


  error_code = curl_easy_perform(handle);
  if (error_code) goto error;

  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
  if (error_code) goto error;
  if (response >= 400)
    log_print("ERROR: response %ld from %s", response, buf);
  else if (response > 200)
    log_print("WARNING: response %ld from %s", response, buf);

  curl_easy_cleanup(handle);
  curl_slist_free_all(headers);
#ifdef DEBUG_EVENT_RESPONSE
  fclose(fdebug);
#endif

  // Free json object
  json_object_put(json);

  // return error if it's not a 200-level response
  return response >= 300;

error:
  log_print("ERROR: curl: %s", curl_easy_strerror(error_code));
  curl_easy_cleanup(handle);
  curl_slist_free_all(headers);
#ifdef DEBUG_EVENT_RESPONSE
  fclose(fdebug);
#endif

  // Free json object
  json_object_put(json);

  return 1;
}
开发者ID:CMU-Robotics-Club,项目名称:Tooltron,代码行数:100,代码来源:query.c


示例11: query_tools

int query_tools(struct tool_t*** tools) {
  log_print("Requesting machines from server");
  
  CURL* handle;
  CURLcode error_code;
  char url[1024];
  long response = 0;

  handle = curl_easy_init();
  if (handle == NULL)
    return 0;

  // only retrieve machines that have Tooltron boxes on them
  sprintf(url, "%s/api/machines/?toolbox_id__isnull=False", server);

  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
  if (error_code) goto error;

  buffer_idx = 0;
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_buffer);
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
  if (error_code) goto error;

  error_code = curl_easy_perform(handle);
  if (error_code) goto error;

  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
  if (error_code) goto error;
  if (response >= 400)
    log_print("ERROR: response %ld from %s", response, url);
  else if (response > 200)
    log_print("WARNING: response %ld from %s", response, url);

  struct json_object *json_tools = json_tokener_parse((char*)buffer);

  int num_tools = json_object_array_length(json_tools);

  log_print("Read %d tools from server", num_tools);

  (*tools) = (struct tool_t**)malloc(num_tools * sizeof(struct tool_t*));

  if(!tools) {
    log_print("Out of memory mallocing tool array");
    return -1;
  }

  int i;
  for(i = 0; i < num_tools; i++) {
    json_object* json_tool = json_object_array_get_idx(json_tools, i);

    json_object* json_tool_id = json_object_object_get(json_tool, "id");
    json_object* json_tool_toolbox_id = json_object_object_get(json_tool, "toolbox_id");
    json_object* json_tool_name = json_object_object_get(json_tool, "type");

    const char* tool_id = json_object_get_string(json_tool_id);
    const char* tool_name = json_object_get_string(json_tool_name);
    int tool_toolbox_id = json_object_get_int(json_tool_toolbox_id);

    log_print("Tool ID: %s, Tool Name: %s, Toolbox ID: %d", tool_id, tool_name, tool_toolbox_id);

    struct tool_t* tool = malloc(sizeof(struct tool_t));

    if(!tool) {
      log_print("Out of memory mallocing tool");
      return -1;
    }

    tool->id = malloc(strlen(tool_id) + 1);

    if(!tool->id) {
      log_print("Out of memory mallocing tool id");
      return -1;
    }

    strcpy(tool->id, tool_id);


    tool->name = malloc(strlen(tool_name) + 1);

    if(!tool->name) {
      log_print("Out of memory mallocing tool name");
      return -1;
    }

    strcpy(tool->name, tool_name);

  
    tool->address = tool_toolbox_id;
    tool->connected = 1;
    tool->state = TS_INIT;
    tool->user = 0;
    tool->event = NULL;
    tool->powered = false;

    (*tools)[i] = tool;
  }

  curl_easy_cleanup(handle);
//.........这里部分代码省略.........
开发者ID:CMU-Robotics-Club,项目名称:Tooltron,代码行数:101,代码来源:query.c


示例12: query_add_event

/*
 * query_add_event
 *
 * Makes an HTTPS POST request to add an event to the CRM server, including
 * user, tool, start time, and stop time. Reads the password from password.txt.
 * Returns 0 if successful, or 1 if there was an error and the caller should
 * try the same event again later.
 *
 * Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD
 * HH:MM:SS" with 24-hour time
 */
int query_add_event(struct event_t *event) {
  CURL* handle;
  CURLcode error_code;
  struct curl_httppost *formpost = NULL, *lastptr = NULL;
  char buf[1024];
  struct tm *timeinfo;
  long response = 0;

#ifdef DEBUG_EVENT_RESPONSE
  FILE *fdebug;
  fdebug = fopen("debug.html", "w");
#endif

  handle = curl_easy_init();
  if (handle == NULL)
    return 1;

  timeinfo = localtime(&event->tstart);
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
  curl_formadd(&formpost, &lastptr,
      CURLFORM_COPYNAME, "tstart",
      CURLFORM_COPYCONTENTS, buf,
      CURLFORM_END);

  timeinfo = localtime(&event->tend);
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
  curl_formadd(&formpost, &lastptr,
      CURLFORM_COPYNAME, "tend",
      CURLFORM_COPYCONTENTS, buf,
      CURLFORM_END);

  sprintf(buf, "%08x", event->user);
  curl_formadd(&formpost, &lastptr,
      CURLFORM_COPYNAME, "user_id",
      CURLFORM_COPYCONTENTS, buf,
      CURLFORM_END);

  sprintf(buf, "%d", event->tool_id);
  curl_formadd(&formpost, &lastptr,
      CURLFORM_COPYNAME, "machine_id",
      CURLFORM_COPYCONTENTS, buf,
      CURLFORM_END);

  curl_formadd(&formpost, &lastptr,
      CURLFORM_COPYNAME, "succ",
      CURLFORM_COPYCONTENTS, event->succ? "1" : "0",
      CURLFORM_END);

  sprintf(buf, "%s/crm/add_card_event/", server);
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
  if (error_code) goto error;

  /* TODO disabling host and peer verification should theoretically be removed
   * eventually */
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
  if (error_code) goto error;

#ifdef DEBUG_EVENT_RESPONSE
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug);
#else
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
#endif
  if (error_code) goto error;

  error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
  if (error_code) goto error;

  error_code = curl_easy_perform(handle);
  if (error_code) goto error;

  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
  if (error_code) goto error;
  if (response >= 400)
    log_print("ERROR: response %ld from %s", response, buf);
  else if (response > 200)
    log_print("WARNING: response %ld from %s", response, buf);

  curl_easy_cleanup(handle);
  curl_formfree(formpost);
#ifdef DEBUG_EVENT_RESPONSE
  fclose(fdebug);
#endif
  // return error if it's not a 200-level response
  return response >= 300;

error:
//.........这里部分代码省略.........
开发者ID:CMU-Robotics-Club,项目名称:Tooltron,代码行数:101,代码来源:query.c


示例13: newsfeed_update

/* feed_update()
 * Takes initialized feed with url set, fetches the feed from this url,
 * updates rest of Feed struct members and returns HTTP response code
 * we got from url's server. */
int newsfeed_update(struct newsfeed * feed, time_t last_update)
{
#if (defined(HAVE_LIBCURL) && defined(HAVE_EXPAT))
  CURL * eh;
  CURLcode curl_res;
  struct newsfeed_parser_context * feed_ctx;
  unsigned int res;
  unsigned int timeout_value;
  long response_code;
  
  if (feed->feed_url == NULL) {
    res = NEWSFEED_ERROR_BADURL;
    goto err;
  }
  
  /* Init curl before anything else. */
  eh = curl_easy_init();
  if (eh == NULL) {
    res = NEWSFEED_ERROR_MEMORY;
    goto err;
  }
  
  /* Curl initialized, create parser context now. */
  feed_ctx = malloc(sizeof(* feed_ctx));
  if (feed_ctx == NULL) {
    res = NEWSFEED_ERROR_MEMORY;
    goto free_eh;
  }
  
  feed_ctx->parser = XML_ParserCreate(NULL);
  if (feed_ctx->parser == NULL) {
    res = NEWSFEED_ERROR_MEMORY;
    goto free_ctx;
  }
  feed_ctx->depth = 0;
  feed_ctx->str = mmap_string_sized_new(256);
  if (feed_ctx->str == NULL) {
    res = NEWSFEED_ERROR_MEMORY;
    goto free_praser;
  }
  feed_ctx->feed = feed;
  feed_ctx->location = 0;
  feed_ctx->curitem = NULL;
  feed_ctx->error = NEWSFEED_NO_ERROR;
  
  /* Set initial expat handlers, which will take care of choosing
   * correct parser later. */
  newsfeed_parser_set_expat_handlers(feed_ctx);
  
  if (feed->feed_timeout != 0)
    timeout_value = feed->feed_timeout;
  else
    timeout_value = mailstream_network_delay.tv_sec;
  
  curl_easy_setopt(eh, CURLOPT_URL, feed->feed_url);
  curl_easy_setopt(eh, CURLOPT_NOPROGRESS, 1);
#ifdef CURLOPT_MUTE
  curl_easy_setopt(eh, CURLOPT_MUTE, 1);
#endif
  curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, newsfeed_writefunc);
  curl_easy_setopt(eh, CURLOPT_WRITEDATA, feed_ctx);
  curl_easy_setopt(eh, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt(eh, CURLOPT_MAXREDIRS, 3);
  curl_easy_setopt(eh, CURLOPT_TIMEOUT, timeout_value);
  curl_easy_setopt(eh, CURLOPT_NOSIGNAL, 1);
  curl_easy_setopt(eh, CURLOPT_USERAGENT, "libEtPan!");
  
  /* Use HTTP's If-Modified-Since feature, if application provided
   * the timestamp of last update. */
  if (last_update != -1) {
    curl_easy_setopt(eh, CURLOPT_TIMECONDITION,
        CURL_TIMECOND_IFMODSINCE);
    curl_easy_setopt(eh, CURLOPT_TIMEVALUE, last_update);
  }
        
#if LIBCURL_VERSION_NUM >= 0x070a00
  curl_easy_setopt(eh, CURLOPT_SSL_VERIFYPEER, 0);
  curl_easy_setopt(eh, CURLOPT_SSL_VERIFYHOST, 0);
#endif

  curl_res = curl_easy_perform(eh);
  if (curl_res != 0) {
    res = curl_error_convert(curl_res);
    goto free_str;
  }
  
  curl_easy_getinfo(eh, CURLINFO_RESPONSE_CODE, &response_code);
  
  curl_easy_cleanup(eh);
  
  if (feed_ctx->error != NEWSFEED_NO_ERROR) {
    res = feed_ctx->error;
    goto free_str;
  }
  
  /* Cleanup, we should be done. */
//.........这里部分代码省略.........
开发者ID:timfel,项目名称:libetpan,代码行数:101,代码来源:newsfeed.c


示例14: main

int main(int argc, char *argv[])
{
  CURL    *curl;
  conf_t  conf[1];
  int     OptionIndex;
  struct  tm *lt;
  struct  tm *gmt;
  time_t  tt;
  time_t  tt_local;
  time_t  tt_gmt;
  double  tzonediffFloat;
  int     tzonediffWord;
  char    timeBuf[61];
  char    tzoneBuf[16];
  int     RetValue;

  OptionIndex     = 0;
  ShowAllHeader   = 0;    /* Do not show HTTP Header */
  AutoSyncTime    = 0;    /* Do not synchronise computer clock */
  RetValue        = 0;    /* Successful Exit */
  conf_init(conf);

  if (argc > 1) {
    while (OptionIndex < argc) {
      if (strncmp(argv[OptionIndex], "--server=", 9) == 0)
        snprintf(conf->timeserver, MAX_STRING, "%s", &argv[OptionIndex][9]);

      if (strcmp(argv[OptionIndex], "--showall") == 0)
        ShowAllHeader = 1;

      if (strcmp(argv[OptionIndex], "--synctime") == 0)
        AutoSyncTime = 1;

      if (strncmp(argv[OptionIndex], "--proxy-user=", 13) == 0)
        snprintf(conf->proxy_user, MAX_STRING, "%s", &argv[OptionIndex][13]);

      if (strncmp(argv[OptionIndex], "--proxy=", 8) == 0)
        snprintf(conf->http_proxy, MAX_STRING, "%s", &argv[OptionIndex][8]);

      if ((strcmp(argv[OptionIndex], "--help") == 0) ||
          (strcmp(argv[OptionIndex], "/?") == 0)) {
        showUsage();
        return 0;
      }
      OptionIndex++;
    }
  }

  if (*conf->timeserver == 0)     /* Use default server for time information */
    snprintf(conf->timeserver, MAX_STRING, "%s", DefaultTimeServer[0]);

  /* Init CURL before usage */
  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  if (curl) {
    SyncTime_CURL_Init(curl, conf->http_proxy, conf->proxy_user);

    /* Calculating time diff between GMT and localtime */
    tt       = time(0);
    lt       = localtime(&tt);
    tt_local = mktime(lt);
    gmt      = gmtime(&tt);
    tt_gmt   = mktime(gmt);
    tzonediffFloat = difftime(tt_local, tt_gmt);
    tzonediffWord  = (int)(tzonediffFloat/3600.0);

    if ((double)(tzonediffWord * 3600) == tzonediffFloat)
      snprintf(tzoneBuf, 15, "%+03d'00'", tzonediffWord);
    else
      snprintf(tzoneBuf, 15, "%+03d'30'", tzonediffWord);

    /* Get current system time and local time */
    GetSystemTime(&SYSTime);
    GetLocalTime(&LOCALTime);
    snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
             DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
             MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
             LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
             LOCALTime.wMilliseconds);

    fprintf(stderr, "Fetch: %s\n\n", conf->timeserver);
    fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf);

    /* HTTP HEAD command to the Webserver */
    SyncTime_CURL_Fetch(curl, conf->timeserver, "index.htm",
                        HTTP_COMMAND_HEAD);

    GetLocalTime(&LOCALTime);
    snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
             DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
             MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
             LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
             LOCALTime.wMilliseconds);
    fprintf(stderr, "\nAfter  HTTP. Date: %s%s\n", timeBuf, tzoneBuf);

    if (AutoSyncTime == 3) {
      /* Synchronising computer clock */
      if (!SetSystemTime(&SYSTime)) {  /* Set system time */
        fprintf(stderr, "ERROR: Unable to set system time.\n");
        RetValue = 1;
//.........这里部分代码省略.........
开发者ID:499940913,项目名称:moon,代码行数:101,代码来源:synctime.c


示例15: fetch_curl_register


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ curl_easy_perform函数代码示例发布时间:2022-05-30
下一篇:
C++ curl_easy_getinfo函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap