Using D, how would I listen to incoming HTTP traffic and respond to it?
For example (in pseudocode):
socket = new socket("locahost", 80)
socket.onRequestRecevied(handleRequest);
function response handleRequest(request) {
//do something with the request and respond
request.respond("hello world")
}
I know there is a lot more to it than that, but I haven't been able to find many resources on responding to incoming http request.
EDIT: My current attempts have yielded only exceptions like "Unable to create socket: Operation not permitted." which may mean I'm doing it correctly but am simply receiving a system error message.
it's the same as listening to normal incoming TCP connections and responding to them:
with
handleHttp(Socket)
containing the logic for receiving the http request and sending the response as defined be the http standard (you'll have to find that your self though)There's a multi-threaded (event-based) Web server called G-Wan which supports native D scripts.
I never used it with 'D' scripts, only with C++ scripts for which it worked as expected.
There is currently no HTTP server in the standard library. Adam Ruppe has some very good code on Github for Web work, but it currently doesn't include a standalone Web server.
The program below is a bare-bones, single-threaded basic HTTP server, for educational purposes. Generating a valid HTTP response is still up to you; but at least it parses the header, and gives you a chance to respond based on the details of the request.