Simple Web Server

[ Start > PikeDevel > HowTo > Simple Web Server ] [ Edit this Page | Show Page Versions | Show Raw Source ]


Creating a simple web server is simple in Pike. The Protocols.HTTP module includes a set of classes that do all of the heavy lifting for you. All you have to do is handle the request… you'll have access to an object that holds all of the relevant information about the incoming request. It's really too easy for words!

Getting Started with Protocols.HTTP.Server

The heart of the built in web server functionality is found in the Protocols.HTTP.Server module. Aside from a number of helpful convenience functions, there are 3 classes: Port, SSLPort and Request. The class Port impliments a standard HTTP server, SSLPort implements a server which carries HTTP over SSL (ie HTTPS), and Request represents a request to the web server. We won't be covering the SSL version in any detail, though it's not much more complicated than the standard HTTP port.

As a developer looking to implement a web server, your task is pretty simple. All you need to do is create an instance of INVALID API SYMBOL and write a function that takes a Request object and returns a result. You don't even need to specify a port number (though it's recommended), as the default port 80 will be used. You can also specify an interface address to listen on, if you don't want to listen on all interfaces. Once you create the port instance, you just need to exit into the backend loop (if you haven't already done so) by returning a negative value from main().

The handler function

The only code you need to write in order for your web server to be functional is a function that accepts the Request object and (presumably) returns a result to the requestor. The Request object contains data fields that map to various parts of the incoming request, already parsed and formatted for you to use. When you've finished handling the request, and are ready to return a response, you can use the Request()->response_and_finish() method to return the data. This method accepts a mapping that contains various key-value pairs that spell out exactly what kind of response to return to the requestor.

Here it is, our bare minimum web server (and it's not really fair to call it bare-minimum, as it's already got a lot of functionality built in), all in less than 20 lines of code! This version doesn't do a lot of error handling, but it's useful for peeking into the Request object and seeing the kinds of information that a browser sends to a webserver.

It's probably worth noting that you must return a response to the requestor, otherwise the request will hang around and (depending on the client) never time out. That means you should be prepared to catch errors in your handler function, and at least return something in the event things wrong in your code.

#!/usr/local/bin/pike

constant default_port = 8080; constant my_version = "0.0";

Protocols.HTTP.Server.Port port;

int main(int argc, array(string) argv) { int my_port = default_port; if(argc>1) my_port=(int)argv[1];

write("FinServe starting on port " + my_port + "n");

port = Protocols.HTTP.Server.Port(handle_request, my_port); return -1; }

void handle_request(Protocols.HTTP.Server.Request request) { write(sprintf("got request: %On", request));

mapping response = ([]);

response->server="FinServe " + my_version; response->type = "text/html"; response->error = 200; response->data = sprintf("%O", mkmapping(indices(request), values(request))); response->data = "<h1>FinServe " + my_version + "</h1>&#110;<pre>" + response->data + "</pre>&#110;";

request->response_and_finish(response); }


Powered by PikeWiki2

 
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University