Is there interest in a std.http?

Adam D. Ruppe destructionator at gmail.com
Mon Nov 19 12:56:20 PST 2012


On Monday, 19 November 2012 at 20:38:56 UTC, Tyler Jameson Little 
wrote:
> I'd like to see an HTTP module in Phobos, but I wanted to gauge 
> interest first and see if this has been discussed before.

I've been asked to put my cgi.d in phobos before (which includes 
a http server as well as a cgi, fastcgi, and scgi implementation 
of its common interface), so there's probably some interest.

I haven't put mine in just because I'm not particularly motivated 
to go through the red tape.

The file is cgi.d in here:
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff

Feel free to take whatever you want from it. There's also an 
extremely minimal http.d file in there, which is a very simple 
http client. (I didn't go far with this though because I tend to 
use curl for most my work anyway.)



The way I did it is with a function:

void handleRequest(Cgi cgi) { }

And then there's a mixin called GenericMain that calls your 
function.

mixin GenericMain!handleRequest;


Depending on how you compile it, the main function constructs the 
cgi object differently. -version=fastcgi means it is plugged into 
a FastCGI loop. version=scgi ditto. -version=embedded_httpd 
includes a simple little threaded http server. No version builds 
it as a standard CGI binary, one process per request. However it 
is compiled though, the Cgi methods all remain the same, so your 
function shouldn't have to change.


> * Access to underlying TcpSocket (for protocol upgrades, like 
> WebSocket)

My cgi.d I don't think can do this yet. I've been thinking about 
adding it though, but haven't.

> * HTTP body is a stream

class Cgi has a method onRequestBodyDataReceived and one 
handleIncomingDataChunk which can be overridden by subclasses to 
do this kind of thing.

It's a bit of a pain though, I made it thinking you'd want to 
handle it after it is all loaded.

> * Simple HTTP requests

This is where I think my cgi.d is a winner because most the data 
is available so easily

cgi.get["foo"] returns a string
cgi.getArray["foo"] returns an array of strings

ditto for post

cgi.request("foo", 0) returns with a default value, converting 
types as requested

There's also members like cookie, queryString, host, etc., that 
gives nice access to other headers.

> * Errors can be recovered from (allow user-defined error 
> handlers):
> * User settable size limits

eh, I'm partially there on these. There's some constructor args 
you can play with and some degree of exception catching but 
probably not fully what you have in mind.


More information about the Digitalmars-d mailing list