diff --git a/src/http_server/flb_http_server.c b/src/http_server/flb_http_server.c index f13b8e3c968..c1b434410e3 100644 --- a/src/http_server/flb_http_server.c +++ b/src/http_server/flb_http_server.c @@ -544,6 +544,7 @@ static int flb_http_server_client_activity_event_handler(void *data) close_connection = FLB_FALSE; +dispatch_requests: cfl_list_foreach_safe(iterator, backup_iterator, &session->request_queue) { @@ -602,6 +603,31 @@ static int flb_http_server_client_activity_event_handler(void *data) } } + /* + * An HTTP/1.x session parses one request at a time, and the bytes of a + * pipelined request that arrived in the same read stay in incoming_data. + * No read event will come for them, so parse them now instead of + * leaving the client waiting until it sends more data. + */ + if (!close_connection && + session->version != HTTP_PROTOCOL_VERSION_AUTODETECT && + session->version <= HTTP_PROTOCOL_VERSION_11 && + cfl_sds_len(session->incoming_data) > 0) { + result = flb_http1_server_session_ingest(&session->http1, NULL, 0); + + if (result < 0) { + /* Send the responses of the requests already handled */ + flb_http_server_session_write(session); + flb_http_server_session_destroy(session); + + return -1; + } + + if (!cfl_list_is_empty(&session->request_queue)) { + goto dispatch_requests; + } + } + result = flb_http_server_session_write(session); if (result != 0) { diff --git a/src/http_server/flb_http_server_http1.c b/src/http_server/flb_http_server_http1.c index 26d592627b0..2896bbb501b 100644 --- a/src/http_server/flb_http_server_http1.c +++ b/src/http_server/flb_http_server_http1.c @@ -72,9 +72,21 @@ static int http1_evict_request(struct flb_http1_server_session *session) session_buffer_length = cfl_sds_len(session_buffer); - request_length = mk_http_parser_request_size(&session->inner_parser, - session_buffer, - session_buffer_length); + if (!mk_http_parser_is_content_chunked(&session->inner_parser) && + session->inner_parser.header_content_length <= 0 && + session->inner_parser.i >= 0) { + /* + * A request without a body, such as a GET, ends at the blank line + * after its headers, and the parser stops on that final LF. Keep + * whatever follows it, which is the next pipelined request. + */ + request_length = (size_t) session->inner_parser.i + 1; + } + else { + request_length = mk_http_parser_request_size(&session->inner_parser, + session_buffer, + session_buffer_length); + } if (request_length == -1 || request_length > session_buffer_length) { @@ -91,6 +103,7 @@ static int http1_evict_request(struct flb_http1_server_session *session) session_buffer_length); session_buffer[session_buffer_length] = '\0'; + cfl_sds_set_len(session_buffer, session_buffer_length); } else { cfl_sds_set_len(session_buffer, 0); diff --git a/tests/integration/scenarios/in_http/tests/test_http1_pipelining.py b/tests/integration/scenarios/in_http/tests/test_http1_pipelining.py new file mode 100644 index 00000000000..944a85b8e85 --- /dev/null +++ b/tests/integration/scenarios/in_http/tests/test_http1_pipelining.py @@ -0,0 +1,93 @@ +"""Pipelined HTTP/1.1 requests sent in a single write are all answered.""" + +import socket + +import pytest + +from utils.test_service import FluentBitTestService + + +@pytest.fixture +def listener(tmp_path): + config = tmp_path / "http1_pipelining.yaml" + config.write_text("""service: + flush: 1 + grace: 1 + http_server: on + http_port: ${FLUENT_BIT_HTTP_MONITORING_PORT} +pipeline: + inputs: + - name: http + listen: 127.0.0.1 + port: ${FLUENT_BIT_TEST_LISTENER_PORT} + enable_health_endpoint: on + outputs: + - name: null + match: '*' +""") + service = FluentBitTestService(str(config)) + service.start() + try: + yield service.flb_listener_port + finally: + service.stop() + + +def read_response(stream): + """Read one response from a buffered socket file, return its status.""" + status_line = stream.readline() + assert status_line, "connection closed before a response arrived" + status = int(status_line.split()[1]) + + length = 0 + chunked = False + while True: + line = stream.readline() + if line in (b"\r\n", b"\n", b""): + break + name, _, value = line.decode("latin-1").partition(":") + if name.strip().lower() == "content-length": + length = int(value.strip()) + elif name.strip().lower() == "transfer-encoding" and "chunked" in value.lower(): + chunked = True + + if chunked: + while True: + size = int(stream.readline().split(b";")[0], 16) + stream.read(size + 2) + if size == 0: + break + else: + stream.read(length) + + return status + + +def test_http1_pipelined_requests_in_one_write(listener): + # A chunked body has an exact end. With Content-Length the body currently + # runs to the end of the buffer (#12652), which is a separate bug. + body = b'{"a":1}' + first = (b"POST /one HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\n" + b"Transfer-Encoding: chunked\r\n\r\n" + + f"{len(body):x}\r\n".encode() + body + b"\r\n0\r\n\r\n") + second = b"GET /health HTTP/1.1\r\nHost: localhost\r\n\r\n" + + with socket.create_connection(("127.0.0.1", listener), timeout=5) as sock: + # Both requests in one write, and nothing sent after them: the second + # one must be answered without waiting for more data from the client. + sock.sendall(first + second) + stream = sock.makefile("rb") + assert read_response(stream) == 201 + assert read_response(stream) == 200 + + +def test_http1_pipelined_header_only_requests(listener): + # Two requests without a body. The first one ends at its blank line, so + # the second one has to be kept in the buffer when the first is evicted. + request = b"GET /health HTTP/1.1\r\nHost: localhost\r\n\r\n" + + with socket.create_connection(("127.0.0.1", listener), timeout=5) as sock: + sock.sendall(request + request) + stream = sock.makefile("rb") + assert read_response(stream) == 200 + assert read_response(stream) == 200