From 99fc09d8a9c53fc93bb15df6b91c87608706d694 Mon Sep 17 00:00:00 2001 From: Jonas Haag Date: Sun, 28 Apr 2013 20:07:32 +0200 Subject: [PATCH] Fixed #57: File wrapper should accept optional 'chunksize' argument --- LICENSE | 2 +- Makefile | 2 +- bjoern/filewrapper.c | 10 ++++++++-- bjoern/server.c | 6 ++++-- tests/filewrapper.py | 7 ++++--- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/LICENSE b/LICENSE index ca3ae04e..351d25f7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ - Copyright (c) 2010-2011 Jonas Haag and contributors. + Copyright (c) 2010-2013 Jonas Haag and contributors. All rights reserved. License: 2-clause-BSD (Berkley Software Distribution) license diff --git a/Makefile b/Makefile index 400c2d60..df5e0d53 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ debug: CFLAGS='-D DEBUG' make again $(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c - @echo ' -> ' $(CC) -c $< -o $@ + @echo ' -> ' $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ @$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ # foo.o: shortcut to $(BUILD_DIR)/foo.o diff --git a/bjoern/filewrapper.c b/bjoern/filewrapper.c index d6f5a89d..26d5fa4e 100644 --- a/bjoern/filewrapper.c +++ b/bjoern/filewrapper.c @@ -4,16 +4,22 @@ static PyObject* FileWrapper_New(PyTypeObject* cls, PyObject* args, PyObject* kwargs) { PyObject* file; - if(!PyArg_ParseTuple(args, "O:FileWrapper", &file)) + unsigned int ignored_blocksize; + + if(!PyArg_ParseTuple(args, "O|I:FileWrapper", &file, &ignored_blocksize)) return NULL; + if(!PyFile_Check(file)) { TYPE_ERROR("FileWrapper argument", "file", file); return NULL; } + Py_INCREF(file); - FileWrapper* wrapper = PyObject_NEW(FileWrapper, &FileWrapper_Type); PyFile_IncUseCount((PyFileObject*)file); + + FileWrapper* wrapper = PyObject_NEW(FileWrapper, cls); wrapper->file = file; + return (PyObject*)wrapper; } diff --git a/bjoern/server.c b/bjoern/server.c index d6948737..6fb92d26 100644 --- a/bjoern/server.c +++ b/bjoern/server.c @@ -234,13 +234,15 @@ ev_io_on_write(struct ev_loop* mainloop, ev_io* watcher, const int events) /* current_chunk contains the HTTP headers */ if(send_chunk(request)) goto out; - assert(!request->current_chunk_p); + assert(request->current_chunk_p == 0); /* abuse current_chunk_p to store the file fd */ request->current_chunk_p = PyObject_AsFileDescriptor(request->iterable); goto out; } + if(do_sendfile(request)) goto out; + } else { /* iterable */ if(send_chunk(request)) @@ -307,7 +309,7 @@ send_chunk(Request* request) assert(request->current_chunk != NULL); assert(!(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk) - && PyString_GET_SIZE(request->current_chunk) != 0)); + && PyString_GET_SIZE(request->current_chunk) != 0)); bytes_sent = write( request->client_fd, diff --git a/tests/filewrapper.py b/tests/filewrapper.py index 54c50b8e..85333d5d 100644 --- a/tests/filewrapper.py +++ b/tests/filewrapper.py @@ -3,9 +3,10 @@ import bjoern W = { - 'callable-iterator' : lambda f, e: iter(lambda: f.read(64*1024), ''), - 'xreadlines' : lambda f, e: f, - 'filewrapper' : lambda f, env: env['wsgi.file_wrapper'](f) + 'callable-iterator': lambda f, e: iter(lambda: f.read(64*1024), ''), + 'xreadlines': lambda f, e: f, + 'filewrapper': lambda f, env: env['wsgi.file_wrapper'](f), + 'filewrapper2': lambda f, env: env['wsgi.file_wrapper'](f, 1) } F = len(sys.argv) > 1 and sys.argv[1] or 'README.rst'