How To Dynamic Web Rendering?

Adam D. Ruppe destructionator at gmail.com
Sun May 15 16:39:57 PDT 2011


I wrote:
> When I wrote my http code,

I just spent a little time updating this. It's still built on my
old netman.d, which uses Linux system calls so prolly linux only
(it does select() and friends directly, should be easy enough to port
to WinSock or std.socket but I wrote what I knew and left it at that).

Removed some of the logic since the CGI class does it in a more
structured way, and added support for HTTP 1.0 and better support
for non-conforming clients.

These steps aren't really necessary to work - today's browsers
know to use \r\n and HTTP 1.1, but the ab program - apache benchmark
- does not, and I wanted to see what happens when I attack it.


Cache headers (if-modified, etc.) are not handled properly on the
server, and cgi.d doesn't expose them at this time. This thing isn't
meant to be used in production, unless it's in the back end, behind
something more battle tested and standards conformant, like Apache.


That said, ab's results weren't too bad if and only if the handler
runs quickly. The reason for that is it is single-threaded. (the
network manager class is based on a cooperative multitasking setup -
each connection gets it's onDataReceived function called when new
data comes in. It needs to look at it and return as soon as
possible. Fine when it's all simple code, but with more complex
websites it probably won't be.)


While it's only a couple hundred lines long so I can't complain
too much, this concurrency issue will have to be addressed before
I can say it's really suitable for serious work.


... but, it is good enough to function as a notification server!
Async notifications are fast, even if single threaded.

Anyway while the implementation needs work, I am pretty happy
with the api.


Here's some code:

import arsd.httpd;

// the handler function is identical to one written with standard
// CGI
void handler(Cgi cgi) {
	cgi.write("Hello!");
	cgi.write(to!string(cgi.get)); // show dynamic output
	cgi.close();
}

// but instead of mixing in GenericMain or whatever, we call
// serveHttp.
void main() {
        // function pointer to handler, port to listen on
	serveHttp(&handler, 5000);
}



Nice and simple!


More information about the Digitalmars-d-learn mailing list