static void die_if_curl_error(CURLcode err) { if (err) { char *msg = check_curl_error(err, "curl"); error_msg_and_die("%s", msg); } }
static void xcurl_easy_setopt_ptr(CURL *handle, CURLoption option, const void *parameter) { CURLcode err = curl_easy_setopt(handle, option, parameter); if (err) { char *msg = check_curl_error(err, "curl"); error_msg_and_die("%s", msg); } }
static void xcurl_easy_setopt_off_t(CURL *handle, CURLoption option, curl_off_t parameter) { /* Can't reuse xcurl_easy_setopt_ptr: paramter is too wide */ CURLcode err = curl_easy_setopt(handle, option, parameter); if (err) { char *msg = check_curl_error(err, "curl"); error_msg_and_die("%s", msg); } }
int abrt_post(abrt_post_state_t *state, const char *url, const char *content_type, const char *data, off_t data_size) { CURLcode curl_err; long response_code; abrt_post_state_t localstate; VERB3 log("abrt_post('%s','%s')", url, data); if (!state) { memset(&localstate, 0, sizeof(localstate)); state = &localstate; } 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 : "")); } // Do a regular 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); // Supply POST data... struct curl_httppost* post = NULL; struct curl_httppost* last = NULL; FILE* data_file = NULL; if (data_size == ABRT_POST_DATA_FROMFILE) { // ...from a file data_file = fopen(data, "r"); if (!data_file) //FIXME: perror_msg_and_die("can't open '%s'", data); 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); } else if (data_size == ABRT_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) //FIXME: perror_msg_and_die("can't open '%s'", data); // 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 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 { // .. from a blob in memory xcurl_easy_setopt_ptr(handle, CURLOPT_POSTFIELDS, data); // note1: if data_size == ABRT_POST_DATA_STRING == -1, curl will use strlen(data) xcurl_easy_setopt_long(handle, CURLOPT_POSTFIELDSIZE, data_size); // note2: CURLOPT_POSTFIELDSIZE_LARGE can't be used: xcurl_easy_setopt_long() // truncates data_size on 32-bit arch. Need xcurl_easy_setopt_long_long()? // Also, I'm not sure CURLOPT_POSTFIELDSIZE_LARGE special-cases -1. } struct curl_slist *httpheader_list = NULL; // Override "Content-Type:" if (data_size != ABRT_POST_DATA_FROMFILE_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); xcurl_easy_setopt_ptr(handle, CURLOPT_HTTPHEADER, httpheader_list); } // Override "Accept: text/plain": helps convince server to send plain-text // error messages in the body of HTTP error responses [not verified to work] httpheader_list = curl_slist_append(httpheader_list, "Accept: text/plain"); 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"); // 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 & ABRT_POST_WANT_HEADERS) { xcurl_easy_setopt_ptr(handle, CURLOPT_HEADERFUNCTION, (void*)save_headers); xcurl_easy_setopt_ptr(handle, CURLOPT_WRITEHEADER, state); } FILE* body_stream = NULL; if (state->flags & ABRT_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 & ABRT_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. curl_err = curl_easy_perform(handle); if (curl_err) { VERB2 log("curl_easy_perform: error %d", (int)curl_err); if (state->flags & ABRT_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: http 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; }