[Twisted-Python] HTML shoudl not be baked into twisted.web HTTP implementation

James Y Knight foom at fuhm.net
Wed Dec 9 12:34:11 EST 2009


On Dec 9, 2009, at 11:52 AM, Jared Gisin wrote:
> I’m writing a HTTP server that exposes various resources as an API. Unless I’m missing something, twisted’s HTTP protocol implementation is in twisted.web.http.
>  
> The problem with this package is that it’s inexplicably wrapped up in HTML. HTML has nothing to do with HTTP as a whole. Sure, HTML is often what HTTP requests return, but there’s no reason why it should nor is there any RFC that says it should. HTTP request can return anything.
> 
> The software I’m writing is a programmatic interface. One never uses a web browser, so things such as displaying tracebacks (twisted.web.util.formatFailure) in HTML format is completely wrong. When implementing HTTP, why assume the client always wants HTML. It seem completely wrong from these modules and libraries to be so full of HTML output. HTML output should be provided as a separate config or option for twisted.web. In this case, why not just dump the traceback directly to the HTTP entity-body? As a consumer of twisted.web, I  should not have to battle with the hard-coded HTML output of this library. If I want the library to dump things in HTML output, I should have an option to tell it to do that (and I should be able to better customize the HTML), but I should not get HTML by default.

Customizability is great, and certainly error pages should really not be hard-coded at all. However, the default of returning HTML descriptions of errors is a good default, and should remain thus. There's usually two cases of interest:

1) A program is responding to the error. In that case, it will use the HTTP result code, and not care what the content is at all.
2) A human is reading the error message. At least 99% of the time, this will be happening through a web browser. They understand HTML. 

I'd expect your program to be in case #1, although I don't know if you have some need to parse out the particular tracebacks for some reason?

James


More information about the Twisted-Python mailing list