Problems with settings Content-Length

46 views
Skip to first unread message

knabar

unread,
Sep 20, 2010, 4:02:19 PM9/20/10
to PyISAPIe
Are there any known problems with setting the content-length header
with IIS 7?

I have a Django application that returns JPEG images among other
content. When I just return the contents without setting the content
length, it works fine:

response = HttpResponse(content=content, mimetype='image/jpeg')
return response

If I set the content length, I get a 500 Internal Server error:

response = HttpResponse(content=content, mimetype='image/jpeg')
response['Content-Length'] = len(response.content)
return response

I believe I've tried all combinations of setting keepalive, different
buffer sizes (including None), also closing the connection explicitly
(with response['Connection'] = 'close'), but have not found a
combination that works.

Execution seems fine until control goes back into the DLL, and my C
skills are not good enough to debug further.

Andreas

P.S. PyISAPIe is otherwise rock solid, thanks for all the great work!

knabar

unread,
Sep 21, 2010, 8:43:01 AM9/21/10
to PyISAPIe
I have a little more information on this now:

- The actual error seems to be a "Request handler failed"
- I'm running Python 2.5.4 and PyISAPIe 1.1.0rc4

Also, this morning I still had the URL up in my browser and for fun
clicked reload and it worked - I got the Content-Length header and
all... Of course, any additional attempt failed again. I wondered if
it worked once after the PyISAPIe DLL is freshly loaded, but recycling
the application pool did not work, it still failed.

Andreas

knabar

unread,
Sep 21, 2010, 11:42:06 AM9/21/10
to PyISAPIe
I think I figured out the problem. I was passing a file object as the
content for the HttpResponse. In Http/WSGI/RunWSGI, the PyISAPIe
Write method gets called once per iteration over the content object,
but then internally compares the length of the Data parameter with the
content length I set earlier. Since iterating over a file does not
produce the whole file at once, it generated a ValueError exception
("Data length does not match specified content length"), which does
not get shown anywhere by default.

My current workaround is to read the whole file into a single string
and pass that as the content for HttpResponse, but for very large
files that seems very inefficient.

Not sure if the final version of PyISAPIe 1.1 will address this, it
sounds like it will ("...when keep-alive is disabled, content length
is no longer verified for writes and a single write is no longer
required."). If so, I can't wait for its release! :-)

Andreas

Phillip Sitbon

unread,
Sep 21, 2010, 1:42:59 PM9/21/10
to pyis...@googlegroups.com
Hi Andreas,

The change for 1.1 you mention is indeed to address the problem you're
having. It's specific to closing the connection because allowing this
behavior (partial writes with a specified content length) to begin
with is already a pretty big can of worms as far as request completion
and connection termination. I know I can check the overall length and
make sure all the writes add up properly, but in between the first and
the last write there's too much possibility for something to happen
that will utterly confuse either the server or the client.

I've been thinking about making this a more flexible option but for
now closing the connection is the best option. Also, I'll try to look
into why that ValueError isn't being shown.

Cheers,

Phillip

> --
> You received this message because you are subscribed to the Google Groups "PyISAPIe" group.
> To post to this group, send email to pyis...@googlegroups.com.
> To unsubscribe from this group, send email to pyisapie+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pyisapie?hl=en.
>
>

knabar

unread,
Sep 21, 2010, 2:01:15 PM9/21/10
to PyISAPIe
Hello Phillip,

regarding the error message, I think the exception is handled in
Header.cpp:171, but the actual exception message is not passed
anywhere.

Requiring the connection to be closed sounds ok to me, although I
might want an I_KNOW_WHAT_I_AM_DOING flag to override the checks for
certain situations :-)

Thanks again,

Andreas
Reply all
Reply to author
Forward
0 new messages