Skip to content

Commit

Permalink
lib-http: server: Make sure output is used only when valid in http_se…
Browse files Browse the repository at this point in the history
…rver_response_send_real().

This avoids the need to hold a reference to it.
  • Loading branch information
stephanbosch committed Feb 17, 2018
1 parent 3cda61e commit 2d5e289
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/lib-http/http-server-response.c
Expand Up @@ -560,7 +560,6 @@ static int http_server_response_send_real(struct http_server_response *resp)
struct http_server_request *req = resp->request;
struct http_server_connection *conn = req->conn;
struct http_server *server = req->server;
struct ostream *output = conn->conn.output;
string_t *rtext = t_str_new(256);
struct const_iovec iov[3];
bool is_head = http_request_method_is(&req->req, "HEAD");
Expand Down Expand Up @@ -594,8 +593,8 @@ static int http_server_response_send_real(struct http_server_response *resp)
if (http_server_request_version_equals(req, 1, 0)) {
if (!is_head) {
/* cannot use Transfer-Encoding */
resp->payload_output = output;
o_stream_ref(output);
resp->payload_output = conn->conn.output;
o_stream_ref(conn->conn.output);
/* connection close marks end of payload */
close = TRUE;
}
Expand All @@ -604,7 +603,7 @@ static int http_server_response_send_real(struct http_server_response *resp)
str_append(rtext, "Transfer-Encoding: chunked\r\n");
if (!is_head) {
resp->payload_output =
http_transfer_chunked_ostream_create(output);
http_transfer_chunked_ostream_create(conn->conn.output);
}
}
} else {
Expand All @@ -615,8 +614,8 @@ static int http_server_response_send_real(struct http_server_response *resp)
resp->payload_size);
}
if (!is_head) {
resp->payload_output = output;
o_stream_ref(output);
resp->payload_output = conn->conn.output;
o_stream_ref(conn->conn.output);
}
}
} else if (resp->tunnel_callback == NULL && resp->status / 100 != 1
Expand Down Expand Up @@ -664,9 +663,8 @@ static int http_server_response_send_real(struct http_server_response *resp)
iov[2].iov_len = 2;

req->state = HTTP_SERVER_REQUEST_STATE_PAYLOAD_OUT;
o_stream_ref(output);
o_stream_cork(output);
if (o_stream_sendv(output, iov, N_ELEMENTS(iov)) < 0) {
o_stream_cork(conn->conn.output);
if (o_stream_sendv(conn->conn.output, iov, N_ELEMENTS(iov)) < 0) {
http_server_connection_handle_output_error(conn);
ret = -1;
}
Expand All @@ -689,12 +687,12 @@ static int http_server_response_send_real(struct http_server_response *resp)
http_server_response_finish_payload_out(resp);
}
}
if (ret >= 0 && !resp->payload_corked &&
o_stream_uncork_flush(output) < 0) {
if (ret >= 0 && conn->conn.output != NULL &&
!resp->payload_corked &&
o_stream_uncork_flush(conn->conn.output) < 0) {
http_server_connection_handle_output_error(conn);
ret = -1;
}
o_stream_unref(&output);
return ret;
}

Expand Down

0 comments on commit 2d5e289

Please sign in to comment.