static void process_curl_messages(void) { int num_messages; struct active_request_slot *slot; CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages); while (curl_message != NULL) { if (curl_message->msg == CURLMSG_DONE) { int curl_result = curl_message->data.result; slot = active_queue_head; while (slot != NULL && slot->curl != curl_message->easy_handle) slot = slot->next; if (slot != NULL) { curl_multi_remove_handle(curlm, slot->curl); slot->curl_result = curl_result; finish_active_slot(slot); } else { fprintf(stderr, "Received DONE message for unknown request!\n"); } } else { fprintf(stderr, "Unknown CURL message received: %d\n", (int)curl_message->msg); } curl_message = curl_multi_info_read(curlm, &num_messages); } }
static int run_slot(struct active_request_slot *slot) { int err = 0; struct slot_results results; slot->results = &results; slot->curl_result = curl_easy_perform(slot->curl); finish_active_slot(slot); if (results.curl_result != CURLE_OK) { err |= error("RPC failed; result=%d, HTTP code = %ld", results.curl_result, results.http_code); } return err; }
static int run_slot(struct active_request_slot *slot) { int err; struct slot_results results; slot->results = &results; slot->curl_result = curl_easy_perform(slot->curl); finish_active_slot(slot); err = handle_curl_result(&results); if (err != HTTP_OK && err != HTTP_REAUTH) { error("RPC failed; result=%d, HTTP code = %ld", results.curl_result, results.http_code); } return err; }
static int post_rpc(struct rpc_state *rpc) { struct active_request_slot *slot; struct slot_results results; struct curl_slist *headers = NULL; int use_gzip = rpc->gzip_request; char *gzip_body = NULL; int err = 0, large_request = 0; /* Try to load the entire request, if we can fit it into the * allocated buffer space we can use HTTP/1.0 and avoid the * chunked encoding mess. */ while (1) { size_t left = rpc->alloc - rpc->len; char *buf = rpc->buf + rpc->len; int n; if (left < LARGE_PACKET_MAX) { large_request = 1; use_gzip = 0; break; } n = packet_read_line(rpc->out, buf, left); if (!n) break; rpc->len += n; } slot = get_active_slot(); slot->results = &results; curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); curl_easy_setopt(slot->curl, CURLOPT_POST, 1); curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); curl_easy_setopt(slot->curl, CURLOPT_ENCODING, ""); headers = curl_slist_append(headers, rpc->hdr_content_type); headers = curl_slist_append(headers, rpc->hdr_accept); if (large_request) { /* The request body is large and the size cannot be predicted. * We must use chunked encoding to send it. */ headers = curl_slist_append(headers, "Expect: 100-continue"); headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); rpc->initial_buffer = 1; curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); #ifndef NO_CURL_IOCTL curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl); curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc); #endif if (options.verbosity > 1) { fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); fflush(stderr); } } else if (use_gzip && 1024 < rpc->len) { /* The client backend isn't giving us compressed data so * we can try to deflate it ourselves, this may save on. * the transfer time. */ size_t size; z_stream stream; int ret; memset(&stream, 0, sizeof(stream)); ret = deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED, (15 + 16), 8, Z_DEFAULT_STRATEGY); if (ret != Z_OK) die("cannot deflate request; zlib init error %d", ret); size = deflateBound(&stream, rpc->len); gzip_body = xmalloc(size); stream.next_in = (unsigned char *)rpc->buf; stream.avail_in = rpc->len; stream.next_out = (unsigned char *)gzip_body; stream.avail_out = size; ret = deflate(&stream, Z_FINISH); if (ret != Z_STREAM_END) die("cannot deflate request; zlib deflate error %d", ret); ret = deflateEnd(&stream); if (ret != Z_OK) die("cannot deflate request; zlib end error %d", ret); size = stream.total_out; headers = curl_slist_append(headers, "Content-Encoding: gzip"); curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size); if (options.verbosity > 1) { fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", rpc->service_name, (unsigned long)rpc->len, (unsigned long)size); fflush(stderr); } } else { /* We know the complete request size in advance, use the * more normal Content-Length approach. */ curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf); curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len); if (options.verbosity > 1) { fprintf(stderr, "POST %s (%lu bytes)\n", rpc->service_name, (unsigned long)rpc->len); fflush(stderr); } } curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in); curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc); slot->curl_result = curl_easy_perform(slot->curl); finish_active_slot(slot); if (results.curl_result != CURLE_OK) { err |= error("RPC failed; result=%d, HTTP code = %ld", results.curl_result, results.http_code); } curl_slist_free_all(headers); free(gzip_body); return err; }