Skip to content

Commit

Permalink
wip single chunk merging
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashaag committed Aug 9, 2024
1 parent 25b14e5 commit 7638adb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SOURCE_DIR = bjoern
BUILD_DIR = build
PYTHON ?= python2
PYTHON ?= python3

PYTHON_INCLUDE = $(shell ${PYTHON}-config --includes)
PYTHON_LDFLAGS = $(shell ${PYTHON}-config --ldflags)
Expand Down
1 change: 1 addition & 0 deletions bjoern/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void Request_reset(Request* request)
{
memset(&request->state, 0, sizeof(Request) - (size_t)&((Request*)NULL)->state);
request->state.response_length_unknown = true;
request->content_length = -1;
request->parser.last_call_was_header_value = true;
request->parser.invalid_header = false;
request->parser.field = NULL;
Expand Down
1 change: 1 addition & 0 deletions bjoern/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct {
PyObject* client_addr;

request_state state;
ssize_t content_length;

PyObject* status;
PyObject* headers;
Expand Down
2 changes: 1 addition & 1 deletion bjoern/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ void server_run(ServerInfo* server_info)

#ifdef WANT_SIGNAL_HANDLING
ev_timer_init(&timeout_watcher, ev_timer_ontick, 0., SIGNAL_CHECK_INTERVAL);
ev_timer_start(mainloop, &timeout_watcher);
ev_set_priority(&timeout_watcher, EV_MINPRI);
ev_timer_start(mainloop, &timeout_watcher);
#endif

/* This is the program main loop */
Expand Down
14 changes: 11 additions & 3 deletions bjoern/wsgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ wsgi_call_application(Request* request)
Py_DECREF(retval);
first_chunk = NULL;
}
if (request->state.response_length_unknown) {
request->state.response_length_unknown = false;
request->content_length = _PEP3333_Bytes_GET_SIZE(retval);
}
} else if(!request->state.response_length_unknown && FileWrapper_CheckExact(retval) && FileWrapper_GetFd(retval) != -1) {
DBG_REQ(request, "WSGI iterable is wsgi.file_wrapper instance and Content-Length is known");
request->iterable = retval;
Expand Down Expand Up @@ -123,9 +127,9 @@ wsgi_call_application(Request* request)
!strncmp(_PEP3333_Bytes_AS_DATA(request->status), "304", 3)) {
request->state.response_length_unknown = false;
}

/* keep-alive cruft */
if(http_should_keep_alive(&request->parser.parser)) {
if(http_should_keep_alive(&request->parser.parser)) {
if(request->state.response_length_unknown) {
if(request->parser.parser.http_major > 0 && request->parser.parser.http_minor > 0) {
/* On HTTP 1.1, we can use Transfer-Encoding: chunked. */
Expand Down Expand Up @@ -239,7 +243,7 @@ clean_headers(PyObject* headers, bool* found_content_length)
static void
wsgi_getheaders(Request* request, PyObject** buf, Py_ssize_t *length)
{
Py_ssize_t length_upperbound = strlen("HTTP/1.1 ") + _PEP3333_Bytes_GET_SIZE(request->status) + strlen("\r\nConnection: Keep-Alive") + strlen("\r\nTransfer-Encoding: chunked") + strlen("\r\n\r\n");
Py_ssize_t length_upperbound = strlen("HTTP/1.1 ") + _PEP3333_Bytes_GET_SIZE(request->status) + strlen("\r\nConnection: Keep-Alive") + strlen("\r\nTransfer-Encoding: chunked") + strlen("\r\nContent-Length: 999999999999999") + strlen("\r\n\r\n");
for(Py_ssize_t i=0; i<PyList_GET_SIZE(request->headers); ++i) {
PyObject* tuple = PyList_GET_ITEM(request->headers, i);
PyObject* field = PyTuple_GET_ITEM(tuple, 0);
Expand Down Expand Up @@ -282,6 +286,10 @@ wsgi_getheaders(Request* request, PyObject** buf, Py_ssize_t *length)
buf_write2("\r\nConnection: Keep-Alive");
if(request->state.chunked_response) {
buf_write2("\r\nTransfer-Encoding: chunked");
}else if(request->content_length>=0) {
char buf[100];
sprintf(buf, "\r\nContent-Length: %ld", request->content_length);
buf_write2(buf);
}
} else {
buf_write2("\r\nConnection: close");
Expand Down

0 comments on commit 7638adb

Please sign in to comment.