void CurlHttpRequest::NotifyCompletion()
{
	if (memory_) {
		if (!content_callback_)
			return;
		ContentCallback cb = content_callback_;
		content_callback_ = nullptr;
		if (callback_message_loop_ != nullptr) {
			callback_message_loop_->PostTask(nbase::Bind(cb, content_, download_ok(), response_code_));
		} else {
			// Run the callback on current thread
			cb(content_, download_ok(), response_code_);
		}
	} else {
		if (!file_callback_)
			return;
		CompletionCallback cb = file_callback_;
		file_callback_ = nullptr;
		if (callback_message_loop_ != nullptr) {
			callback_message_loop_->PostTask(nbase::Bind(cb, download_ok(), response_code_));
		} else {
			cb(download_ok(), response_code_);
		}
	}
	progress_callback_ = nullptr;
}
示例#2
0
int download(char* file) {
	CURL* curl;
	CURLcode result;
	struct data config;
	FILE* fp;
	char bufferError[CURL_ERROR_SIZE];
	char url[URL_MAX];
	char tmp[PATH_MAX];
	int returnval = 0;
	int retries = 0;
	int success = 0;
	char fullpath[PATH_MAX];
	config.trace_ascii = 1;

	while (!success && retries < upstream_count) {
		sprintf(url, "http://%s/%s", getupstream(), file);
		sprintf(tmp, "%s/%s.tmp", cfg_get_str("cache_store"), file);
		sprintf(fullpath, "%s/%s", cfg_get_str("cache_store"), file);
		curl = curl_easy_init();
		if (curl) {
			fp = fopen(tmp, "wb");
			curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, bufferError);
			curl_easy_setopt(curl, CURLOPT_URL, url);
			curl_easy_setopt(curl, CURLOPT_HEADER, 0);
			curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
			curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
			curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,
					curl_progress_func);
			curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
			curl_easy_setopt(curl, CURLOPT_TIMEOUT,
					cfg_get_int("curl_timeout"));
			curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,
					cfg_get_int("curl_connect_timeout"));
			if (cfg_get_int("log_level") >= 3) {
				curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_trace);
				curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
				curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
			}
			result = curl_easy_perform(curl);
			curl_easy_cleanup(curl);
			fclose(fp);
		}
		if (result == CURLE_OK) {
			if (rename(tmp, fullpath) == 0) {
				download_ok(file, cfg_get_int("fe_id"));
				error_log(WARN, "Successfully downloaded: %s", fullpath);
				returnval = 0;
				success = 1;
			} else {
				remove(tmp);
				error_log(ERROR, "Rename failure: %s to %s", tmp, fullpath);
				returnval = -1;
			}
		} else {
			retries++;
			remove(tmp);
			if (curl_handle_error(result, bufferError)) {
				returnval = -1;
				error_log(WARN, "Curl error: %s", bufferError);
			} else {
				returnval = -1;
				success = 1;
				error_log(WARN, "Curl error: %s", bufferError);
			}
		}
	}
	return returnval;
}