예제 #1
0
파일: pcs_http.c 프로젝트: imzjy/baidupcs
static inline void pcs_http_prepare(struct pcs_http *http, enum HttpMethod method, const char *url, PcsBool follow_location,
							 PcsHttpWriteFunction write_func, void *state)
{
	pcs_http_reset_response(http);
	curl_easy_setopt(http->curl, CURLOPT_URL, url);
	switch(method)
	{
	case HTTP_METHOD_GET:
		curl_easy_setopt(http->curl, CURLOPT_HTTPGET, 1L);
		break;
	case HTTP_METHOD_POST:
		curl_easy_setopt(http->curl, CURLOPT_POST, 1L);
		break;
	}
	curl_easy_setopt(http->curl, CURLOPT_HEADER, 1L);
	curl_easy_setopt(http->curl, CURLOPT_WRITEFUNCTION, write_func);
	curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, state);

	if (follow_location)
		curl_easy_setopt(http->curl, CURLOPT_FOLLOWLOCATION, 1L);
	else
		curl_easy_setopt(http->curl, CURLOPT_FOLLOWLOCATION, 0L);

	if (http->progress) {
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSFUNCTION, http->progress_func);
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSDATA, http->progress_data);
		curl_easy_setopt(http->curl, CURLOPT_NOPROGRESS, (long)0);
	}
	else {
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSFUNCTION, NULL);
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSDATA, NULL);
		curl_easy_setopt(http->curl, CURLOPT_NOPROGRESS, (long)1);
	}
}
예제 #2
0
static inline void pcs_http_prepare(struct pcs_http *http, enum HttpMethod method, const char *url, PcsBool follow_location,
	PcsHttpWriteFunction write_func, void *state,
	curl_off_t max_recv_speed, curl_off_t max_send_speed,
	curl_off_t resume_from, curl_off_t max_length)
{
	pcs_http_reset_response(http);
	curl_easy_setopt(http->curl, CURLOPT_USERAGENT, http->usage ? http->usage : USAGE);
	curl_easy_setopt(http->curl, CURLOPT_URL, url);
	switch(method)
	{
	case HTTP_METHOD_GET:
		curl_easy_setopt(http->curl, CURLOPT_HTTPGET, 1L);
		break;
	case HTTP_METHOD_POST:
		curl_easy_setopt(http->curl, CURLOPT_POST, 1L);
		break;
	}
	curl_easy_setopt(http->curl, CURLOPT_HEADER, 1L);
	curl_easy_setopt(http->curl, CURLOPT_CONNECTTIMEOUT, (long)http->connect_timeout);
	curl_easy_setopt(http->curl, CURLOPT_TIMEOUT, (long)http->timeout);
	curl_easy_setopt(http->curl, CURLOPT_NOSIGNAL, 1L);
	curl_easy_setopt(http->curl, CURLOPT_WRITEFUNCTION, write_func);
	curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, state);

	// 设置文件续传的位置给libcurl
	if (resume_from && max_length) {
		char *range = pcs_utils_sprintf("%" PRId64 "-%" PRId64, resume_from, resume_from + max_length - 1);
		curl_easy_setopt(http->curl, CURLOPT_RANGE, range);
		pcs_free(range);
	}
	else if (resume_from) {
		curl_easy_setopt(http->curl, CURLOPT_RESUME_FROM_LARGE, resume_from);
	}
	else {
		curl_easy_setopt(http->curl, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)0);
	}

	curl_easy_setopt(http->curl, CURLOPT_MAX_RECV_SPEED_LARGE, max_recv_speed ? max_recv_speed : (curl_off_t)0);
	curl_easy_setopt(http->curl, CURLOPT_MAX_SEND_SPEED_LARGE, max_send_speed ? max_send_speed : (curl_off_t)0);

	if (follow_location)
		curl_easy_setopt(http->curl, CURLOPT_FOLLOWLOCATION, 1L);
	else
		curl_easy_setopt(http->curl, CURLOPT_FOLLOWLOCATION, 0L);

	if (http->progress) {
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSFUNCTION, http->progress_func);
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSDATA, http->progress_data);
		curl_easy_setopt(http->curl, CURLOPT_NOPROGRESS, (long)0);
	}
	else {
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSFUNCTION, NULL);
		curl_easy_setopt(http->curl, CURLOPT_PROGRESSDATA, NULL);
		curl_easy_setopt(http->curl, CURLOPT_NOPROGRESS, (long)1);
	}
}