Skip to content

Commit

Permalink
lib-http: http-client-request - Put shared code from http_client_requ…
Browse files Browse the repository at this point in the history
…est_resubmit/redirect() in new function.
  • Loading branch information
stephanbosch authored and cmouse committed Apr 27, 2020
1 parent de98545 commit d1a0880
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/lib-http/http-client-request.c
Expand Up @@ -1681,6 +1681,26 @@ void http_client_request_finish(struct http_client_request *req)
http_client_request_unref(&req);
}

static int
http_client_request_reset(struct http_client_request *req, bool rewind,
const char **error_r)
{
/* Rewind payload stream */
if (rewind && req->payload_input != NULL && req->payload_size > 0) {
if (req->payload_input->v_offset != req->payload_offset &&
!req->payload_input->seekable) {
*error_r = "Cannot resend payload; "
"stream is not seekable";
return -1;
}
i_stream_seek(req->payload_input, req->payload_offset);
}

/* Drop payload output stream from previous attempt */
o_stream_unref(&req->payload_output);
return 0;
}

void http_client_request_redirect(struct http_client_request *req,
unsigned int status, const char *location)
{
Expand Down Expand Up @@ -1718,25 +1738,13 @@ void http_client_request_redirect(struct http_client_request *req,
return;
}

/* rewind payload stream */
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");
return;
} else {
i_stream_seek(req->payload_input, req->payload_offset);
}
if (http_client_request_reset(req, (status != 303), &error) < 0) {
http_client_request_error(
&req, HTTP_CLIENT_REQUEST_ERROR_ABORTED,
t_strdup_printf("Redirect failed: %s", error));
return;
}

/* drop payload output stream from previous attempt */
o_stream_unref(&req->payload_output);

target = http_url_create_target(url);

http_url_copy(req->pool, &req->origin_url, url);
Expand Down Expand Up @@ -1779,28 +1787,19 @@ void http_client_request_redirect(struct http_client_request *req,

void http_client_request_resubmit(struct http_client_request *req)
{
const char *error;

i_assert(!req->payload_wait);

e_debug(req->event, "Resubmitting request");

/* 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) {
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Resubmission failed: "
"Cannot resend payload; "
"stream is not seekable");
return;
} else {
i_stream_seek(req->payload_input, req->payload_offset);
}
if (http_client_request_reset(req, TRUE, &error) < 0) {
http_client_request_error(
&req, HTTP_CLIENT_REQUEST_ERROR_ABORTED,
t_strdup_printf("Resubmission failed: %s", error));
return;
}

/* drop payload output stream from previous attempt */
o_stream_unref(&req->payload_output);

req->peer = NULL;
req->state = HTTP_REQUEST_STATE_QUEUED;
req->redirects = 0;
Expand Down

0 comments on commit d1a0880

Please sign in to comment.