Skip to content

Commit

Permalink
in_http: adjust HTTP/1.1 buffer based on the request size
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
  • Loading branch information
edsiper committed Oct 10, 2024
1 parent 8eb4182 commit 241f1c9
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions plugins/in_http/http_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static int http_conn_event(void *data)
size_t size;
ssize_t available;
ssize_t bytes;
char *request_end;
size_t request_len;
struct flb_connection *connection;
struct http_conn *conn;
Expand Down Expand Up @@ -153,47 +152,44 @@ static int http_conn_event(void *data)
/* Do more logic parsing and checks for this request */
http_prot_handle(ctx, conn, &conn->session, &conn->request);

/* Evict the processed request from the connection buffer and reinitialize
/*
* Evict the processed request from the connection buffer and reinitialize
* the HTTP parser.
*/

request_end = NULL;
/* Use the last parser position as the request length */
request_len = mk_http_parser_request_size(&conn->session.parser,
conn->buf_data,
conn->buf_len);

if (NULL != conn->request.data.data) {
request_end = &conn->request.data.data[conn->request.data.len];
if (request_len == -1 || (request_len > conn->buf_len)) {
/* Unexpected but let's make sure things are safe */
conn->buf_len = 0;
flb_plg_debug(ctx->ins, "request length exceeds buffer length, closing connection");
http_conn_del(conn);
return -1;
}
else {
request_end = strstr(conn->buf_data, "\r\n\r\n");

if(NULL != request_end) {
request_end = &request_end[4];
}
}
/* If we have extra bytes in our bytes, adjust the extra bytes */
if (0 < (conn->buf_len - request_len)) {
memmove(conn->buf_data, &conn->buf_data[request_len],
conn->buf_len - request_len);

if (NULL != request_end) {
request_len = (size_t)(request_end - conn->buf_data);

if (0 < (conn->buf_len - request_len)) {
memmove(conn->buf_data, &conn->buf_data[request_len],
conn->buf_len - request_len);

conn->buf_data[conn->buf_len - request_len] = '\0';
conn->buf_len -= request_len;
}
else {
memset(conn->buf_data, 0, request_len);

conn->buf_len = 0;
}

/* Reinitialize the parser so the next request is properly
* handled, the additional memset intends to wipe any left over data
* from the headers parsed in the previous request.
*/
memset(&conn->session.parser, 0, sizeof(struct mk_http_parser));
mk_http_parser_init(&conn->session.parser);
http_conn_request_init(&conn->session, &conn->request);
conn->buf_data[conn->buf_len - request_len] = '\0';
conn->buf_len -= request_len;
}
else {
memset(conn->buf_data, 0, request_len);
conn->buf_len = 0;
}

/* Reinitialize the parser so the next request is properly
* handled, the additional memset intends to wipe any left over data
* from the headers parsed in the previous request.
*/
memset(&conn->session.parser, 0, sizeof(struct mk_http_parser));
mk_http_parser_init(&conn->session.parser);
http_conn_request_init(&conn->session, &conn->request);
}
else if (status == MK_HTTP_PARSER_ERROR) {
http_prot_handle_error(ctx, conn, &conn->session, &conn->request);
Expand Down

0 comments on commit 241f1c9

Please sign in to comment.