From: Stephan Bosch Date: Tue, 20 Mar 2018 19:59:32 +0000 (+0100) Subject: lib-http: Reformat http-client-request.c. X-Git-Tag: 2.3.11.2~163 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ca2cb217d21db6fe33b2091c74aafab64bebba7b;p=thirdparty%2Fdovecot%2Fcore.git lib-http: Reformat http-client-request.c. --- diff --git a/src/lib-http/http-client-request.c b/src/lib-http/http-client-request.c index a5e77ebac9..353043c6f7 100644 --- a/src/lib-http/http-client-request.c +++ b/src/lib-http/http-client-request.c @@ -42,8 +42,8 @@ const char * http_client_request_label(struct http_client_request *req) { if (req->label == NULL) { - req->label = p_strdup_printf(req->pool, - "[Req%u: %s %s%s]", req->id, req->method, + req->label = p_strdup_printf( + req->pool, "[Req%u: %s %s%s]", req->id, req->method, http_url_create_host(&req->origin_url), req->target); } return req->label; @@ -54,11 +54,13 @@ http_client_request_update_event(struct http_client_request *req) { event_add_str(req->event, "method", req->method); event_add_str(req->event, "dest_host", req->origin_url.host.name); - event_add_int(req->event, "dest_port", http_url_get_port(&req->origin_url)); + event_add_int(req->event, "dest_port", + http_url_get_port(&req->origin_url)); if (req->target != NULL) event_add_str(req->event, "target", req->target); - event_set_append_log_prefix(req->event, t_strdup_printf( - "request %s: ", http_client_request_label(req))); + event_set_append_log_prefix( + req->event, t_strdup_printf("request %s: ", + http_client_request_label(req))); } static struct event_passthrough * @@ -75,8 +77,8 @@ http_client_request_result_event(struct http_client_request *req) req->request_offset; } if (conn->incoming_payload != NULL && - (req->state == HTTP_REQUEST_STATE_GOT_RESPONSE || - req->state == HTTP_REQUEST_STATE_PAYLOAD_IN)) { + (req->state == HTTP_REQUEST_STATE_GOT_RESPONSE || + req->state == HTTP_REQUEST_STATE_PAYLOAD_IN)) { /* got here prematurely; use bytes read so far */ i_assert(conn->in_req_callback || conn->pending_request == req); @@ -101,7 +103,7 @@ http_client_request_result_event(struct http_client_request *req) static struct http_client_request * http_client_request_new(struct http_client *client, const char *method, - http_client_request_callback_t *callback, void *context) + http_client_request_callback_t *callback, void *context) { static unsigned int id_counter = 0; pool_t pool; @@ -145,8 +147,8 @@ http_client_request(struct http_client *client, #undef http_client_request_url struct http_client_request * http_client_request_url(struct http_client *client, - const char *method, const struct http_url *target_url, - http_client_request_callback_t *callback, void *context) + const char *method, const struct http_url *target_url, + http_client_request_callback_t *callback, void *context) { struct http_client_request *req; @@ -154,7 +156,7 @@ http_client_request_url(struct http_client *client, http_url_copy_authority(req->pool, &req->origin_url, target_url); req->target = p_strdup(req->pool, http_url_create_target(target_url)); if (target_url->user != NULL && *target_url->user != '\0' && - target_url->password != NULL) { + target_url->password != NULL) { req->username = p_strdup(req->pool, target_url->user); req->password = p_strdup(req->pool, target_url->password); } @@ -165,18 +167,19 @@ http_client_request_url(struct http_client *client, #undef http_client_request_url_str struct http_client_request * http_client_request_url_str(struct http_client *client, - const char *method, const char *url_str, - http_client_request_callback_t *callback, void *context) + const char *method, const char *url_str, + http_client_request_callback_t *callback, + void *context) { struct http_client_request *req, *tmpreq; struct http_url *target_url; const char *error; - req = tmpreq = http_client_request_new - (client, method, callback, context); + req = tmpreq = http_client_request_new(client, method, + callback, context); if (http_url_parse(url_str, NULL, HTTP_URL_ALLOW_USERINFO_PART, - req->pool, &target_url, &error) < 0) { + req->pool, &target_url, &error) < 0) { req->label = p_strdup_printf(req->pool, "[Req%u: %s %s]", req->id, req->method, url_str); http_client_request_error(&tmpreq, @@ -189,7 +192,7 @@ http_client_request_url_str(struct http_client *client, req->origin_url = *target_url; req->target = p_strdup(req->pool, http_url_create_target(target_url)); if (target_url->user != NULL && *target_url->user != '\0' && - target_url->password != NULL) { + target_url->password != NULL) { req->username = p_strdup(req->pool, target_url->user); req->password = p_strdup(req->pool, target_url->password); } @@ -200,9 +203,9 @@ http_client_request_url_str(struct http_client *client, #undef http_client_request_connect struct http_client_request * http_client_request_connect(struct http_client *client, - const char *host, in_port_t port, - http_client_request_callback_t *callback, - void *context) + const char *host, in_port_t port, + http_client_request_callback_t *callback, + void *context) { struct http_client_request *req; @@ -218,9 +221,9 @@ http_client_request_connect(struct http_client *client, #undef http_client_request_connect_ip struct http_client_request * http_client_request_connect_ip(struct http_client *client, - const struct ip_addr *ip, in_port_t port, - http_client_request_callback_t *callback, - void *context) + const struct ip_addr *ip, in_port_t port, + http_client_request_callback_t *callback, + void *context) { struct http_client_request *req; const char *hostname; @@ -228,8 +231,8 @@ http_client_request_connect_ip(struct http_client *client, i_assert(ip->family != 0); hostname = net_ip2addr(ip); - req = http_client_request_connect - (client, hostname, port, callback, context); + req = http_client_request_connect(client, hostname, port, + callback, context); req->origin_url.host.ip = *ip; return req; } @@ -243,8 +246,7 @@ void http_client_request_set_event(struct http_client_request *req, http_client_request_update_event(req); } -static void -http_client_request_add(struct http_client_request *req) +static void http_client_request_add(struct http_client_request *req) { struct http_client *client = req->client; @@ -253,8 +255,7 @@ http_client_request_add(struct http_client_request *req) req->listed = TRUE; } -static void -http_client_request_remove(struct http_client_request *req) +static void http_client_request_remove(struct http_client_request *req) { struct http_client *client = req->client; @@ -263,7 +264,8 @@ http_client_request_remove(struct http_client_request *req) return; } if (req->listed) { - /* only decrease pending request counter if this request was submitted */ + /* Only decrease pending request counter if this request was + submitted */ DLLIST_REMOVE(&client->requests_list, req); client->requests_count--; } @@ -346,7 +348,7 @@ void http_client_request_destroy(struct http_client_request **_req) if (req->queue != NULL) http_client_queue_drop_request(req->queue, req); - + if (client != NULL && req->delayed_error != NULL) http_client_remove_request_error(req->client, req); req->delayed_error = NULL; @@ -368,15 +370,14 @@ void http_client_request_destroy(struct http_client_request **_req) } void http_client_request_set_port(struct http_client_request *req, - in_port_t port) + in_port_t port) { i_assert(req->state == HTTP_REQUEST_STATE_NEW); req->origin_url.port = port; event_add_int(req->event, "port", port); } -void http_client_request_set_ssl(struct http_client_request *req, - bool ssl) +void http_client_request_set_ssl(struct http_client_request *req, bool ssl) { i_assert(req->state == HTTP_REQUEST_STATE_NEW); req->origin_url.have_ssl = ssl; @@ -388,7 +389,8 @@ void http_client_request_set_urgent(struct http_client_request *req) req->urgent = TRUE; } -void http_client_request_set_preserve_exact_reason(struct http_client_request *req) +void http_client_request_set_preserve_exact_reason( + struct http_client_request *req) { req->preserve_exact_reason = TRUE; } @@ -448,7 +450,7 @@ http_client_request_add_header_full(struct http_client_request *req, case 'c': case 'C': if (strcasecmp(key, "Connection") == 0) req->have_hdr_connection = TRUE; - else if (strcasecmp(key, "Content-Length") == 0) + else if (strcasecmp(key, "Content-Length") == 0) req->have_hdr_body_spec = TRUE; break; case 'd': case 'D': @@ -528,8 +530,7 @@ const char *http_client_request_lookup_header(struct http_client_request *req, next_pos - value_pos - 2); } -void http_client_request_set_date(struct http_client_request *req, - time_t date) +void http_client_request_set_date(struct http_client_request *req, time_t date) { i_assert(req->state == HTTP_REQUEST_STATE_NEW); req->date = date; @@ -556,13 +557,14 @@ void http_client_request_set_payload(struct http_client_request *req, } req->payload_offset = input->v_offset; - /* prepare request payload sync using 100 Continue response from server */ + /* prepare request payload sync using 100 Continue response from server + */ if ((req->payload_chunked || req->payload_size > 0) && sync) req->payload_sync = TRUE; } void http_client_request_set_payload_data(struct http_client_request *req, - const unsigned char *data, size_t size) + const unsigned char *data, size_t size) { struct istream *input; unsigned char *payload_data; @@ -584,38 +586,38 @@ void http_client_request_set_payload_empty(struct http_client_request *req) } void http_client_request_set_timeout_msecs(struct http_client_request *req, - unsigned int msecs) + unsigned int msecs) { i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); req->timeout_msecs = msecs; } void http_client_request_set_timeout(struct http_client_request *req, - const struct timeval *time) + const struct timeval *time) { i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); req->timeout_time = *time; req->timeout_msecs = 0; } -void http_client_request_set_attempt_timeout_msecs(struct http_client_request *req, - unsigned int msecs) +void http_client_request_set_attempt_timeout_msecs( + struct http_client_request *req, unsigned int msecs) { i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); req->attempt_timeout_msecs = msecs; } void http_client_request_set_max_attempts(struct http_client_request *req, - unsigned int max_attempts) + unsigned int max_attempts) { i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); req->max_attempts = max_attempts; } @@ -627,55 +629,55 @@ void http_client_request_set_event_headers(struct http_client_request *req, } void http_client_request_set_auth_simple(struct http_client_request *req, - const char *username, const char *password) + const char *username, + const char *password) { req->username = p_strdup(req->pool, username); req->password = p_strdup(req->pool, password); } void http_client_request_set_proxy_url(struct http_client_request *req, - const struct http_url *proxy_url) + const struct http_url *proxy_url) { i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); req->host_url = http_url_clone_authority(req->pool, proxy_url); req->host_socket = NULL; } void http_client_request_set_proxy_socket(struct http_client_request *req, - const char *proxy_socket) + const char *proxy_socket) { i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); req->host_socket = p_strdup(req->pool, proxy_socket); req->host_url = NULL; } void http_client_request_delay_until(struct http_client_request *req, - time_t time) + time_t time) { req->release_time.tv_sec = time; req->release_time.tv_usec = 0; } -void http_client_request_delay(struct http_client_request *req, - time_t seconds) +void http_client_request_delay(struct http_client_request *req, time_t seconds) { req->release_time = ioloop_timeval; req->release_time.tv_sec += seconds; } void http_client_request_delay_msecs(struct http_client_request *req, - unsigned int msecs) + unsigned int msecs) { req->release_time = ioloop_timeval; timeval_add_msecs(&req->release_time, msecs); } int http_client_request_delay_from_response(struct http_client_request *req, - const struct http_response *response) + const struct http_response *response) { time_t retry_after = response->retry_after; unsigned int max; @@ -687,13 +689,13 @@ int http_client_request_delay_from_response(struct http_client_request *req, if (retry_after < ioloop_time) return 0; /* delay already expired */ max = (req->client->set.max_auto_retry_delay == 0 ? - req->attempt_timeout_msecs / 1000 : - req->client->set.max_auto_retry_delay); + req->attempt_timeout_msecs / 1000 : + req->client->set.max_auto_retry_delay); if ((unsigned int)(retry_after - ioloop_time) > max) return -1; /* delay too long */ req->release_time.tv_sec = retry_after; req->release_time.tv_usec = 0; - return 1; /* valid delay */ + return 1; /* valid delay */ } const char * @@ -727,7 +729,7 @@ http_client_request_get_attempts(const struct http_client_request *req) } void http_client_request_get_stats(struct http_client_request *req, - struct http_client_request_stats *stats_r) + struct http_client_request_stats *stats_r) { struct http_client *client = req->client; int diff_msecs; @@ -743,21 +745,25 @@ void http_client_request_get_stats(struct http_client_request *req, /* elapsed time since message was first sent */ if (req->first_sent_time.tv_sec > 0) { - diff_msecs = timeval_diff_msecs(&ioloop_timeval, &req->first_sent_time); + diff_msecs = timeval_diff_msecs(&ioloop_timeval, + &req->first_sent_time); stats_r->first_sent_msecs = (unsigned int)I_MAX(diff_msecs, 0); } /* elapsed time since message was last sent */ if (req->sent_time.tv_sec > 0) { - diff_msecs = timeval_diff_msecs(&ioloop_timeval, &req->sent_time); + diff_msecs = timeval_diff_msecs(&ioloop_timeval, + &req->sent_time); stats_r->last_sent_msecs = (unsigned int)I_MAX(diff_msecs, 0); } if (req->conn != NULL) { /* time spent in other ioloops */ - i_assert(ioloop_global_wait_usecs >= req->sent_global_ioloop_usecs); + i_assert(ioloop_global_wait_usecs >= + req->sent_global_ioloop_usecs); stats_r->other_ioloop_msecs = (unsigned int) - (ioloop_global_wait_usecs - req->sent_global_ioloop_usecs + 999) / 1000; + (ioloop_global_wait_usecs - + req->sent_global_ioloop_usecs + 999) / 1000; /* time spent in the http-client's own ioloop */ if (client != NULL && client->waiting) { @@ -784,7 +790,7 @@ void http_client_request_get_stats(struct http_client_request *req, } void http_client_request_append_stats_text(struct http_client_request *req, - string_t *str) + string_t *str) { struct http_client_request_stats stats; @@ -804,8 +810,8 @@ void http_client_request_append_stats_text(struct http_client_request *req, str_append(str, ", not yet sent"); } else { str_printfa(str, ", %u send attempts in %u.%03u secs", - stats.send_attempts, - stats.first_sent_msecs/1000, stats.first_sent_msecs%1000); + stats.send_attempts, stats.first_sent_msecs/1000, + stats.first_sent_msecs%1000); if (stats.send_attempts > 1) { str_printfa(str, ", %u.%03u in last attempt", stats.last_sent_msecs/1000, @@ -819,7 +825,8 @@ void http_client_request_append_stats_text(struct http_client_request *req, stats.http_ioloop_msecs%1000); } str_printfa(str, ", %u.%03u in other ioloops", - stats.other_ioloop_msecs/1000, stats.other_ioloop_msecs%1000); + stats.other_ioloop_msecs/1000, + stats.other_ioloop_msecs%1000); if (stats.lock_msecs > 0) { str_printfa(str, ", %u.%03u in locks", @@ -832,16 +839,16 @@ http_client_request_get_payload_type(struct http_client_request *req) { /* RFC 7230, Section 3.3: - The presence of a message body in a response depends on both the - request method to which it is responding and the response status code - (Section 3.1.2 of [RFC7230]). Responses to the HEAD request method + The presence of a message body in a response depends on both the + request method to which it is responding and the response status code + (Section 3.1.2 of [RFC7230]). Responses to the HEAD request method (Section 4.3.2 of [RFC7231]) never include a message body because the associated response header fields (e.g., Transfer-Encoding, Content-Length, etc.), if present, indicate only what their values would have been if the request method had been GET (Section 4.3.1 of [RFC7231]). 2xx (Successful) responses to a CONNECT request method - (Section 4.3.6 of [RFC7231]) switch to tunnel mode instead of having a - message body. + (Section 4.3.6 of [RFC7231]) switch to tunnel mode instead of having + a message body. */ if (strcmp(req->method, "HEAD") == 0) return HTTP_RESPONSE_PAYLOAD_TYPE_NOT_PRESENT; @@ -871,48 +878,56 @@ static void http_client_request_do_submit(struct http_client_request *req) target = authority; } else { /* absolute target url */ - target = t_strconcat - (http_url_create_host(&req->origin_url), req->target, NULL); + target = t_strconcat(http_url_create_host(&req->origin_url), + req->target, NULL); } /* determine what host to contact to submit this request */ if (have_proxy) { - if (req->host_socket != NULL) { /* specific socket proxy */ + if (req->host_socket != NULL) { + /* specific socket proxy */ req->host_url = NULL; - } else if (req->host_url != NULL) { /* specific normal proxy */ + } else if (req->host_url != NULL) { + /* specific normal proxy */ req->host_socket = NULL; } else if (req->origin_url.have_ssl && - !client->set.no_ssl_tunnel && !req->connect_tunnel) { - req->host_url = &req->origin_url; /* tunnel to origin server */ + !client->set.no_ssl_tunnel && + !req->connect_tunnel) { + /* tunnel to origin server */ + req->host_url = &req->origin_url; req->ssl_tunnel = TRUE; } else if (proxy_socket_path != NULL) { - req->host_socket = proxy_socket_path; /* proxy on unix socket */ + /* proxy on unix socket */ + req->host_socket = proxy_socket_path; req->host_url = NULL; } else { - req->host_url = proxy_url; /* normal proxy server */ + /* normal proxy server */ + req->host_url = proxy_url; req->host_socket = NULL; } } else { - req->host_url = &req->origin_url; /* origin server */ + /* origin server */ + req->host_url = &req->origin_url; } /* use submission date if no date is set explicitly */ if (req->date == (time_t)-1) req->date = ioloop_time; - + /* prepare value for Host header */ req->authority = p_strdup(req->pool, authority); /* debug label */ - req->label = p_strdup_printf(req->pool, "[Req%u: %s %s]", req->id, req->method, target); + req->label = p_strdup_printf(req->pool, "[Req%u: %s %s]", + req->id, req->method, target); /* update request target */ if (req->connect_tunnel || have_proxy) req->target = p_strdup(req->pool, target); if (!have_proxy) { - /* if we don't have a proxy, CONNECT requests are handled by creating - the requested connection directly */ + /* if we don't have a proxy, CONNECT requests are handled by + creating the requested connection directly */ req->connect_direct = req->connect_tunnel; if (req->connect_direct) req->urgent = TRUE; @@ -921,10 +936,12 @@ static void http_client_request_do_submit(struct http_client_request *req) if (req->timeout_time.tv_sec == 0) { if (req->timeout_msecs > 0) { req->timeout_time = ioloop_timeval; - timeval_add_msecs(&req->timeout_time, req->timeout_msecs); - } else if ( client->set.request_absolute_timeout_msecs > 0) { + timeval_add_msecs(&req->timeout_time, + req->timeout_msecs); + } else if (client->set.request_absolute_timeout_msecs > 0) { req->timeout_time = ioloop_timeval; - timeval_add_msecs(&req->timeout_time, client->set.request_absolute_timeout_msecs); + timeval_add_msecs(&req->timeout_time, + client->set.request_absolute_timeout_msecs); } } @@ -951,20 +968,19 @@ void http_client_request_submit(struct http_client_request *req) req->client->requests_count); } -void -http_client_request_get_peer_addr(const struct http_client_request *req, - struct http_client_peer_addr *addr) +void http_client_request_get_peer_addr(const struct http_client_request *req, + struct http_client_peer_addr *addr) { const char *host_socket = req->host_socket; const struct http_url *host_url = req->host_url; - + /* the IP address may be unassigned in the returned peer address, since that is only available at this stage when the target URL has an explicit IP address. */ i_zero(addr); if (host_socket != NULL) { addr->type = HTTP_CLIENT_PEER_ADDR_UNIX; - addr->a.un.path = host_socket; + addr->a.un.path = host_socket; } else if (req->connect_direct) { addr->type = HTTP_CLIENT_PEER_ADDR_RAW; addr->a.tcp.ip = host_url->host.ip; @@ -985,8 +1001,7 @@ http_client_request_get_peer_addr(const struct http_client_request *req, } } -static int -http_client_request_flush_payload(struct http_client_request *req) +static int http_client_request_flush_payload(struct http_client_request *req) { struct http_client_connection *conn = req->conn; int ret; @@ -1027,7 +1042,8 @@ http_client_request_finish_payload_out(struct http_client_request *req) i_assert(req->request_offset < conn->conn.output->offset); req->bytes_out = conn->conn.output->offset - req->request_offset; - /* advance state only when request didn't get aborted in the mean time */ + /* advance state only when request didn't get aborted in the mean time + */ if (req->state != HTTP_REQUEST_STATE_ABORTED) { i_assert(req->state == HTTP_REQUEST_STATE_PAYLOAD_OUT); @@ -1046,7 +1062,7 @@ http_client_request_finish_payload_out(struct http_client_request *req) static int http_client_request_continue_payload(struct http_client_request **_req, - const unsigned char *data, size_t size) + const unsigned char *data, size_t size) { struct ioloop *prev_ioloop, *client_ioloop, *prev_client_ioloop; struct http_client_request *req = *_req; @@ -1056,7 +1072,7 @@ http_client_request_continue_payload(struct http_client_request **_req, i_assert(client != NULL); i_assert(req->state == HTTP_REQUEST_STATE_NEW || - req->state == HTTP_REQUEST_STATE_PAYLOAD_OUT); + req->state == HTTP_REQUEST_STATE_PAYLOAD_OUT); i_assert(req->payload_input == NULL); if (conn != NULL) @@ -1082,11 +1098,12 @@ http_client_request_continue_payload(struct http_client_request **_req, if (req->delayed_error != NULL) { struct http_client_request *tmpreq = req; - /* Handle delayed error outside ioloop; the caller expects callbacks - occurring, so there is no need for delay. Also, it is very - important that any error triggers a callback before - http_client_request_send_payload() finishes, since its return - value is not always checked. + /* Handle delayed error outside ioloop; the caller + expects callbacks occurring, so there is no need for + delay. Also, it is very important that any error + triggers a callback before + http_client_request_send_payload() finishes, since + its return value is not always checked. */ http_client_remove_request_error(client, req); http_client_request_error_delayed(&tmpreq); @@ -1103,7 +1120,7 @@ http_client_request_continue_payload(struct http_client_request **_req, client->waiting = TRUE; while (req->state < HTTP_REQUEST_STATE_PAYLOAD_IN) { e_debug(req->event, "Waiting for request to finish"); - + if (req->state == HTTP_REQUEST_STATE_PAYLOAD_OUT) o_stream_set_flush_pending(req->payload_output, TRUE); @@ -1115,7 +1132,7 @@ http_client_request_continue_payload(struct http_client_request **_req, req->payload_input = NULL; break; } - } + } client->waiting = FALSE; if (prev_client_ioloop != NULL) @@ -1144,20 +1161,19 @@ http_client_request_continue_payload(struct http_client_request **_req, req->payload_wait = FALSE; - /* callback may have messed with our pointer, - so unref using local variable */ + /* callback may have messed with our pointer, so unref using local + variable */ if (!http_client_request_unref(&req)) *_req = NULL; if (conn != NULL) http_client_connection_unref(&conn); - /* Return status */ return ret; } int http_client_request_send_payload(struct http_client_request **_req, - const unsigned char *data, size_t size) + const unsigned char *data, size_t size) { struct http_client_request *req = *_req; int ret; @@ -1192,7 +1208,7 @@ int http_client_request_finish_payload(struct http_client_request **_req) } static void http_client_request_payload_input(struct http_client_request *req) -{ +{ struct http_client_connection *conn = req->conn; io_remove(&conn->io_req_payload); @@ -1233,11 +1249,14 @@ int http_client_request_send_more(struct http_client_request *req, case OSTREAM_SEND_ISTREAM_RESULT_FINISHED: /* finished sending */ if (!req->payload_chunked && - req->payload_input->v_offset - req->payload_offset != req->payload_size) { - error = t_strdup_printf("BUG: stream '%s' input size changed: " + (req->payload_input->v_offset - req->payload_offset) != + req->payload_size) { + error = t_strdup_printf( + "BUG: stream '%s' input size changed: " "%"PRIuUOFF_T"-%"PRIuUOFF_T" != %"PRIuUOFF_T, i_stream_get_name(req->payload_input), - req->payload_input->v_offset, req->payload_offset, req->payload_size); + req->payload_input->v_offset, + req->payload_offset, req->payload_size); i_error("%s", error); //FIXME: remove? http_client_connection_lost(&conn, error); return -1; @@ -1259,7 +1278,7 @@ int http_client_request_send_more(struct http_client_request *req, return 0; case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT: /* input is blocking (client needs to act; disable timeout) */ - conn->output_locked = TRUE; + conn->output_locked = TRUE; if (!pipelined) http_client_connection_stop_request_timeout(conn); conn->io_req_payload = io_add_istream_to( @@ -1280,8 +1299,8 @@ int http_client_request_send_more(struct http_client_request *req, i_stream_get_name(req->payload_input), i_stream_get_error(req->payload_input)); - /* the payload stream assigned to this request is broken, - fail this the request immediately */ + /* the payload stream assigned to this request is broken, fail + this the request immediately */ http_client_request_error(&req, HTTP_CLIENT_REQUEST_ERROR_BROKEN_PAYLOAD, "Broken payload stream"); @@ -1296,8 +1315,8 @@ int http_client_request_send_more(struct http_client_request *req, i_unreached(); } -static int http_client_request_send_real(struct http_client_request *req, - bool pipelined) +static int +http_client_request_send_real(struct http_client_request *req, bool pipelined) { const struct http_client_settings *set = &req->client->set; struct http_client_connection *conn = req->conn; @@ -1366,8 +1385,8 @@ static int http_client_request_send_real(struct http_client_request *req, strcasecmp(req->method, "POST") == 0 || strcasecmp(req->method, "PUT") == 0) { - /* send Content-Length if we have specified a payload - or when one is normally expected, even if it's 0 bytes. */ + /* send Content-Length if we have specified a payload or when + one is normally expected, even if it's 0 bytes. */ i_assert(req->payload_input != NULL || req->payload_size == 0); if (!req->have_hdr_body_spec) { str_printfa(rtext, "Content-Length: %"PRIuUOFF_T"\r\n", @@ -1380,19 +1399,18 @@ static int http_client_request_send_real(struct http_client_request *req, } if (!req->have_hdr_connection && !http_client_request_to_proxy(req)) { - /* https://tools.ietf.org/html/rfc2068 - Section 19.7.1: + /* RFC 2068, Section 19.7.1: - A client MUST NOT send the Keep-Alive connection token to a proxy - server as HTTP/1.0 proxy servers do not obey the rules of HTTP/1.1 - for parsing the Connection header field. + A client MUST NOT send the Keep-Alive connection token to a + proxy server as HTTP/1.0 proxy servers do not obey the rules + of HTTP/1.1 for parsing the Connection header field. */ str_append(rtext, "Connection: Keep-Alive\r\n"); } /* request line + implicit headers */ iov[0].iov_base = str_data(rtext); - iov[0].iov_len = str_len(rtext); + iov[0].iov_len = str_len(rtext); /* explicit headers */ if (req->headers != NULL) { iov[1].iov_base = str_data(req->headers); @@ -1448,8 +1466,7 @@ static int http_client_request_send_real(struct http_client_request *req, return 0; } -int http_client_request_send(struct http_client_request *req, - bool pipelined) +int http_client_request_send(struct http_client_request *req, bool pipelined) { int ret; @@ -1461,7 +1478,7 @@ int http_client_request_send(struct http_client_request *req, } bool http_client_request_callback(struct http_client_request *req, - struct http_response *response) + struct http_response *response) { http_client_request_callback_t *callback = req->callback; unsigned int orig_attempts = req->attempts; @@ -1475,7 +1492,8 @@ bool http_client_request_callback(struct http_client_request *req, if (req->attempts > 0 && !req->preserve_exact_reason) { unsigned int total_msecs = - timeval_diff_msecs(&ioloop_timeval, &req->submit_time); + timeval_diff_msecs(&ioloop_timeval, + &req->submit_time); response_copy.reason = t_strdup_printf( "%s (%u retries in %u.%03u secs)", response_copy.reason, req->attempts, @@ -1489,7 +1507,8 @@ bool http_client_request_callback(struct http_client_request *req, http_client_request_resubmit(req); return FALSE; } else { - /* release payload early (prevents server/client deadlock in proxy) */ + /* release payload early + (prevents server/client deadlock in proxy) */ i_stream_unref(&req->payload_input); } } @@ -1520,7 +1539,8 @@ http_client_request_send_error(struct http_client_request *req, http_client_request_resubmit(req); return FALSE; } else { - /* release payload early (prevents server/client deadlock in proxy) */ + /* release payload early + (prevents server/client deadlock in proxy) */ if (!sending && req->payload_input != NULL) i_stream_unref(&req->payload_input); } @@ -1554,7 +1574,7 @@ void http_client_request_error_delayed(struct http_client_request **_req) } void http_client_request_error(struct http_client_request **_req, - unsigned int status, const char *error) + unsigned int status, const char *error) { struct http_client_request *req = *_req; @@ -1573,11 +1593,13 @@ void http_client_request_error(struct http_client_request **_req, if (req->queue != NULL) http_client_queue_drop_request(req->queue, req); - if (req->client != NULL && (!req->submitted || - req->state == HTTP_REQUEST_STATE_GOT_RESPONSE)) { - /* we're still in http_client_request_submit() or in the callback - during a retry attempt. delay reporting the error, so the caller - doesn't have to handle immediate or nested callbacks. */ + if (req->client != NULL && + (!req->submitted || + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE)) { + /* we're still in http_client_request_submit() or in the + callback during a retry attempt. delay reporting the error, + so the caller doesn't have to handle immediate or nested + callbacks. */ req->delayed_error = p_strdup(req->pool, error); req->delayed_error_status = status; http_client_delay_request_error(req->client, req); @@ -1600,7 +1622,7 @@ void http_client_request_abort(struct http_client_request **_req) *_req = NULL; if (req->state >= HTTP_REQUEST_STATE_FINISHED && - req->delayed_error_status == 0) + req->delayed_error_status == 0) return; req->callback = NULL; @@ -1654,7 +1676,7 @@ void http_client_request_finish(struct http_client_request *req) } void http_client_request_redirect(struct http_client_request *req, - unsigned int status, const char *location) + unsigned int status, const char *location) { struct http_url *url; const char *error, *target, *origin_url; @@ -1667,8 +1689,10 @@ void http_client_request_redirect(struct http_client_request *req, /* parse URL */ if (http_url_parse(location, NULL, 0, pool_datastack_create(), &url, &error) < 0) { - http_client_request_error(&req, HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT, - t_strdup_printf("Invalid redirect location: %s", error)); + http_client_request_error(&req, + HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT, + t_strdup_printf("Invalid redirect location: %s", + error)); return; } @@ -1677,23 +1701,27 @@ void http_client_request_redirect(struct http_client_request *req, if (req->client->set.max_redirects > 0) { http_client_request_error(&req, HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT, - t_strdup_printf("Redirected more than %d times", + t_strdup_printf( + "Redirected more than %d times", req->client->set.max_redirects)); } else { http_client_request_error(&req, HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT, - "Redirect refused"); + "Redirect refused"); } return; } /* rewind payload stream */ - if (req->payload_input != NULL && req->payload_size > 0 && status != 303) { + if (req->payload_input != NULL && req->payload_size > 0 && + status != 303) { if (req->payload_input->v_offset != req->payload_offset && !req->payload_input->seekable) { - http_client_request_error(&req, - HTTP_CLIENT_REQUEST_ERROR_ABORTED, - "Redirect failed: Cannot resend payload; stream is not seekable"); + http_client_request_error( + &req, HTTP_CLIENT_REQUEST_ERROR_ABORTED, + "Redirect failed: " + "Cannot resend payload; " + "stream is not seekable"); return; } else { i_stream_seek(req->payload_input, req->payload_offset); @@ -1707,7 +1735,7 @@ void http_client_request_redirect(struct http_client_request *req, http_url_copy(req->pool, &req->origin_url, url); req->target = p_strdup(req->pool, target); - + req->host = NULL; origin_url = http_url_create(&req->origin_url); @@ -1718,18 +1746,18 @@ void http_client_request_redirect(struct http_client_request *req, origin_url, target, req->redirects); req->label = p_strdup_printf(req->pool, "[%s %s%s]", - req->method, origin_url, req->target); + req->method, origin_url, req->target); /* RFC 7231, Section 6.4.4: - - -> A 303 `See Other' redirect status response is handled a bit differently. - Basically, the response content is located elsewhere, but the original - (POST) request is handled already. + + -> A 303 `See Other' redirect status response is handled a bit + differently. Basically, the response content is located elsewhere, + but the original (POST) request is handled already. */ if (status == 303 && strcasecmp(req->method, "HEAD") != 0 && - strcasecmp(req->method, "GET") != 0) { - // FIXME: should we provide the means to skip this step? The original - // request was already handled at this point. + strcasecmp(req->method, "GET") != 0) { + // FIXME: should we provide the means to skip this step? The + // original request was already handled at this point. req->method = p_strdup(req->pool, "GET"); /* drop payload */ @@ -1752,10 +1780,12 @@ void http_client_request_resubmit(struct http_client_request *req) /* rewind payload stream */ if (req->payload_input != NULL && req->payload_size > 0) { if (req->payload_input->v_offset != req->payload_offset && - !req->payload_input->seekable) { + !req->payload_input->seekable) { http_client_request_error(&req, HTTP_CLIENT_REQUEST_ERROR_ABORTED, - "Resubmission failed: Cannot resend payload; stream is not seekable"); + "Resubmission failed: " + "Cannot resend payload; " + "stream is not seekable"); return; } else { i_stream_seek(req->payload_input, req->payload_offset); @@ -1773,7 +1803,7 @@ void http_client_request_resubmit(struct http_client_request *req) } void http_client_request_retry(struct http_client_request *req, - unsigned int status, const char *error) + unsigned int status, const char *error) { if (req->client == NULL || req->client->set.no_auto_retry || !http_client_request_try_retry(req)) @@ -1787,7 +1817,7 @@ bool http_client_request_try_retry(struct http_client_request *req) 100 continue (there's no way to rewind the payload for a retry) */ if (req->payload_wait && - (!req->payload_sync || req->payload_sync_continue)) + (!req->payload_sync || req->payload_sync_continue)) return FALSE; /* limit the number of attempts for each request */ if (req->attempts+1 >= req->max_attempts) @@ -1813,7 +1843,7 @@ void http_client_request_set_destroy_callback(struct http_client_request *req, } void http_client_request_start_tunnel(struct http_client_request *req, - struct http_client_tunnel *tunnel) + struct http_client_tunnel *tunnel) { struct http_client_connection *conn = req->conn;