Skip to content

Commit

Permalink
lib-http: Changed http_client_request_error to set request to NULL
Browse files Browse the repository at this point in the history
It's going to internally unreference it, so the caller should be aware of it
also.

I also changed request state check to be an assert, since I don't think
there's any safe way this could work otherwise.
  • Loading branch information
sirainen committed Jun 20, 2016
1 parent e7a0878 commit 4a4f676
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/lib-http/http-client-connection.c
Expand Up @@ -114,7 +114,7 @@ http_client_connection_abort_error(struct http_client_connection **_conn,

array_foreach_modifiable(&conn->request_wait_list, req) {
i_assert((*req)->submitted);
http_client_request_error(*req, status, error);
http_client_request_error(req, status, error);
http_client_request_unref(req);
}
array_clear(&conn->request_wait_list);
Expand All @@ -129,7 +129,7 @@ http_client_connection_abort_any_requests(struct http_client_connection *conn)
if (array_is_created(&conn->request_wait_list)) {
array_foreach_modifiable(&conn->request_wait_list, req) {
i_assert((*req)->submitted);
http_client_request_error(*req,
http_client_request_error(req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Aborting");
http_client_request_unref(req);
Expand All @@ -139,7 +139,7 @@ http_client_connection_abort_any_requests(struct http_client_connection *conn)
if (conn->pending_request != NULL) {
struct http_client_request *pending_req = conn->pending_request;
conn->pending_request = NULL;
http_client_request_error(pending_req,
http_client_request_error(&pending_req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Aborting");
http_client_request_unref(&pending_req);
Expand Down Expand Up @@ -819,7 +819,7 @@ static void http_client_connection_input(struct connection *_conn)
/* response cannot be 2xx if request payload was not completely sent
*/
if (early && response.status / 100 == 2) {
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_BAD_RESPONSE,
"Server responded with success response "
"before all payload was sent");
Expand Down
2 changes: 1 addition & 1 deletion src/lib-http/http-client-host.c
Expand Up @@ -210,7 +210,7 @@ void http_client_host_submit_request(struct http_client_host *host,
if (http_client_peer_addr_is_https(&addr) &&
host->client->ssl_ctx == NULL) {
if (http_client_init_ssl_ctx(host->client, &error) < 0) {
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED, error);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib-http/http-client-private.h
Expand Up @@ -296,7 +296,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);
void http_client_request_error_delayed(struct http_client_request **_req);
void http_client_request_error(struct http_client_request *req,
void http_client_request_error(struct http_client_request **req,
unsigned int status, const char *error);
void http_client_request_redirect(struct http_client_request *req,
unsigned int status, const char *location);
Expand Down
4 changes: 2 additions & 2 deletions src/lib-http/http-client-queue.c
Expand Up @@ -154,7 +154,7 @@ void http_client_queue_fail(struct http_client_queue *queue,
t_array_init(&treqs, array_count(req_arr));
array_copy(&treqs.arr, 0, &req_arr->arr, 0, array_count(req_arr));
array_foreach_modifiable(&treqs, req_idx) {
http_client_request_error(*req_idx, status, error);
http_client_request_error(req_idx, status, error);
}

/* all queues should be empty now... unless new requests were submitted
Expand Down Expand Up @@ -536,7 +536,7 @@ http_client_queue_request_timeout(struct http_client_queue *queue)

http_client_queue_debug(queue,
"Request %s timed out", http_client_request_label(req));
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_TIMED_OUT,
"Timed out");
}
Expand Down
22 changes: 12 additions & 10 deletions src/lib-http/http-client-request.c
Expand Up @@ -1088,11 +1088,12 @@ void http_client_request_error_delayed(struct http_client_request **_req)
http_client_request_destroy(&req);
}

void http_client_request_error(struct http_client_request *req,
void http_client_request_error(struct http_client_request **_req,
unsigned int status, const char *error)
{
if (req->state >= HTTP_REQUEST_STATE_FINISHED)
return;
struct http_client_request *req = *_req;

i_assert(req->state < HTTP_REQUEST_STATE_FINISHED);
req->state = HTTP_REQUEST_STATE_ABORTED;

if (req->queue != NULL)
Expand All @@ -1111,6 +1112,7 @@ void http_client_request_error(struct http_client_request *req,
http_client_request_send_error(req, status, error);
http_client_request_destroy(&req);
}
*_req = NULL;
}

void http_client_request_abort(struct http_client_request **_req)
Expand Down Expand Up @@ -1166,19 +1168,19 @@ 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,
http_client_request_error(&req, HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT,
t_strdup_printf("Invalid redirect location: %s", error));
return;
}

if (++req->redirects > req->client->set.max_redirects) {
if (req->client->set.max_redirects > 0) {
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT,
t_strdup_printf("Redirected more than %d times",
req->client->set.max_redirects));
} else {
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_INVALID_REDIRECT,
"Redirect refused");
}
Expand All @@ -1189,7 +1191,7 @@ void http_client_request_redirect(struct http_client_request *req,
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(&req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Redirect failed: Cannot resend payload; stream is not seekable");
return;
Expand Down Expand Up @@ -1252,7 +1254,7 @@ void http_client_request_resubmit(struct http_client_request *req)
if (req->payload_input != NULL && req->payload_size > 0) {
if (req->payload_input->v_offset != req->payload_offset &&
!req->payload_input->seekable) {
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Resubmission failed: Cannot resend payload; stream is not seekable");
return;
Expand All @@ -1265,7 +1267,7 @@ void http_client_request_resubmit(struct http_client_request *req)
if (req->payload_input != NULL && req->payload_size > 0) {
if (req->payload_input->v_offset != req->payload_offset &&
!req->payload_input->seekable) {
http_client_request_error(req,
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Resubmission failed: Cannot resend payload; stream is not seekable");
return;
Expand All @@ -1288,7 +1290,7 @@ void http_client_request_retry(struct http_client_request *req,
unsigned int status, const char *error)
{
if (!http_client_request_try_retry(req))
http_client_request_error(req, status, error);
http_client_request_error(&req, status, error);
}

bool http_client_request_try_retry(struct http_client_request *req)
Expand Down

0 comments on commit 4a4f676

Please sign in to comment.