Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_server: always set the content-length on HTTP/1.x (fix #9010) #9036

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions src/flb_http_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ char *flb_http_request_get_header(struct flb_http_request *request,
}

result = flb_hash_table_get(request->headers,
lowercase_name,
lowercase_name,
strlen(lowercase_name),
&value, &value_length);

Expand All @@ -103,7 +103,7 @@ char *flb_http_request_get_header(struct flb_http_request *request,

int flb_http_request_set_header(struct flb_http_request *request,
char *name, size_t name_length,
char *value, size_t value_length)
char *value, size_t value_length)
{
char *lowercase_name;
int result;
Expand All @@ -121,17 +121,17 @@ int flb_http_request_set_header(struct flb_http_request *request,

if (value_length == 0) {
if (value[0] == '\0') {
value_length = 1;
value_length = 1;
}
else {
value_length = strlen(value);
}
}

result = flb_hash_table_add(request->headers,
(const char *) lowercase_name,
result = flb_hash_table_add(request->headers,
(const char *) lowercase_name,
name_length,
(void *) value,
(void *) value,
value_length);

flb_free(lowercase_name);
Expand All @@ -156,7 +156,7 @@ int flb_http_request_unset_header(struct flb_http_request *request,
return -1;
}

result = flb_hash_table_del(request->headers,
result = flb_hash_table_del(request->headers,
(const char *) lowercase_name);

flb_free(lowercase_name);
Expand Down Expand Up @@ -213,7 +213,7 @@ void flb_http_response_destroy(struct flb_http_response *response)
}

struct flb_http_response *flb_http_response_begin(
struct flb_http_server_session *session,
struct flb_http_server_session *session,
void *stream)
{
if (session->version == HTTP_PROTOCOL_HTTP2) {
Expand All @@ -226,17 +226,29 @@ struct flb_http_response *flb_http_response_begin(

int flb_http_response_commit(struct flb_http_response *response)
{
int len;
char tmp[64];
struct flb_http_server_session *session;

session = (struct flb_http_server_session *) response->stream->parent;

if (response->body == NULL) {
flb_http_response_set_header(response,
"content-length",
flb_http_response_set_header(response,
"content-length",
strlen("content-length"),
"0",
"0",
1);
}

session = (struct flb_http_server_session *) response->stream->parent;
else {
/* if the session is HTTP/1.x, always set the content-length header */
if (session->version < HTTP_PROTOCOL_HTTP2) {
len = snprintf(tmp, sizeof(tmp) - 1, "%zu", cfl_sds_len(response->body));
flb_http_response_set_header(response,
"content-length",
strlen("content-length"),
tmp, len);
}
}

if (session->version == HTTP_PROTOCOL_HTTP2) {
return flb_http2_response_commit(response);
Expand All @@ -245,7 +257,7 @@ int flb_http_response_commit(struct flb_http_response *response)
return flb_http1_response_commit(response);
}

int flb_http_response_set_header(struct flb_http_response *response,
int flb_http_response_set_header(struct flb_http_response *response,
char *name, size_t name_length,
char *value, size_t value_length)
{
Expand All @@ -257,7 +269,7 @@ int flb_http_response_set_header(struct flb_http_response *response,

if (value_length == 0) {
if (value[0] == '\0') {
value_length = 1;
value_length = 1;
}
else {
value_length = strlen(value);
Expand All @@ -267,18 +279,18 @@ int flb_http_response_set_header(struct flb_http_response *response,
session = (struct flb_http_server_session *) response->stream->parent;

if (session->version == HTTP_PROTOCOL_HTTP2) {
return flb_http2_response_set_header(response,
name, name_length,
return flb_http2_response_set_header(response,
name, name_length,
value, value_length);
}
else {
return flb_http1_response_set_header(response,
name, name_length,
return flb_http1_response_set_header(response,
name, name_length,
value, value_length);
}
}

int flb_http_response_set_trailer_header(struct flb_http_response *response,
int flb_http_response_set_trailer_header(struct flb_http_response *response,
char *name, size_t name_length,
char *value, size_t value_length)
{
Expand All @@ -291,7 +303,7 @@ int flb_http_response_set_trailer_header(struct flb_http_response *response,

if (value_length == 0) {
if (value[0] == '\0') {
value_length = 1;
value_length = 1;
}
else {
value_length = strlen(value);
Expand All @@ -305,10 +317,10 @@ int flb_http_response_set_trailer_header(struct flb_http_response *response,
return -1;
}

result = flb_hash_table_add(response->trailer_headers,
(const char *) lowercase_name,
result = flb_hash_table_add(response->trailer_headers,
(const char *) lowercase_name,
name_length,
(void *) value,
(void *) value,
value_length);

flb_free(lowercase_name);
Expand All @@ -320,7 +332,7 @@ int flb_http_response_set_trailer_header(struct flb_http_response *response,
return 0;
}

int flb_http_response_set_status(struct flb_http_response *response,
int flb_http_response_set_status(struct flb_http_response *response,
int status)
{
struct flb_http_server_session *session;
Expand All @@ -336,7 +348,7 @@ int flb_http_response_set_status(struct flb_http_response *response,
return flb_http1_response_set_status(response, status);
}

int flb_http_response_set_message(struct flb_http_response *response,
int flb_http_response_set_message(struct flb_http_response *response,
char *message)
{
if (response->message != NULL) {
Expand All @@ -346,23 +358,23 @@ int flb_http_response_set_message(struct flb_http_response *response,
}

response->message = cfl_sds_create((const char *) message);

if (response->message == NULL) {
return -1;
}

return 0;
}

int flb_http_response_set_body(struct flb_http_response *response,
int flb_http_response_set_body(struct flb_http_response *response,
unsigned char *body, size_t body_length)
{
struct flb_http_server_session *session;

session = (struct flb_http_server_session *) response->stream->parent;

response->body = cfl_sds_create_len((const char *) body, body_length);

if (session->version == HTTP_PROTOCOL_HTTP2) {
return flb_http2_response_set_body(response, body, body_length);
}
Expand All @@ -373,7 +385,7 @@ int flb_http_response_set_body(struct flb_http_response *response,
/* HTTP STREAM */

int flb_http_stream_init(struct flb_http_stream *stream,
void *parent,
void *parent,
int32_t stream_id,
int role,
void *user_data)
Expand Down Expand Up @@ -405,7 +417,7 @@ int flb_http_stream_init(struct flb_http_stream *stream,
return 0;
}

struct flb_http_stream *flb_http_stream_create(void *parent,
struct flb_http_stream *flb_http_stream_create(void *parent,
int32_t stream_id,
int role,
void *user_data)
Expand Down
Loading