Re: Simple Proposal for setting HTTP headers

On 08/08/2013 05:15 AM, Geoffrey Sneddon wrote:
> On 08/08/13 10:53, Robin Berjon wrote:
>> On 08/08/2013 00:32 , James Graham wrote:
>>> So, having looked at the code, and talked to the mozbase people, it
>>> seems like this might not be the easiest path; it's based on
>>> SimpleHTTPServer, which wants you to provide a response code, headers
>>> dict and body string. That's just not flexibility for our purposes, so
>>> it would be necessary to work around the existing behaviour to get what
>>> we want.
>>
>> Actually, as demonstrated by gsnedders, you can overwride the
>> SimpleHTTPServer behaviour to get asis-like control. See:
>>
>> http://hg.gsnedders.com/http-parsing/file/bbab4a6298d7/tests/response/response.py
>>
>>
>>
>> So it ought to be possible to start from there.
>
> Specifically, SimpleHTTPServer, through its superclass, has a wfile (a
> file-like object of the socket) attribute. All the functions it provides
> are just an abstraction around this: you can perfectly well just write
> entire HTTP responses (including the response line) to the socket in the
> do_GET, do_HEAD, etc. methods. (Depending on quite what you need, you
> may be better off just inheriting from BaseHTTPServer directly.)

Right, I didn't say "impossible", I said "not easy". But I think using 
BaseHTTPServer directly may be the best plan so far; it at least deals 
with the request-parsing and low-level networking parts whilst allowing 
considerable flexibility in how the response is written.

As for why you would want custom things: pretty much any existing use of 
PHP is custom behaviour. And some things are difficult to do without 
logic e.g. tests where you want to do one thing the first time a 
resource is requested and something different the second time it's 
requested (these are quite common). For that you need something with IO 
and conditionals, which isn't practical to implement as a DSL with a 
small set of predefined functions.

Received on Thursday, 8 August 2013 14:25:11 UTC