webserver Specification Sheet


Inetpak (c) 1998 by David Stes. All Rights Reserved.

Webserver

Inherits from:Object

Maturity Index:Experimental

Class Description

The Webserver class is an experimental class, for building small web servers. The main function of such a server creates a Webserver instance (by sending +new or +argc:argv: and then go into a loop, by sending run to the instance :

main(int n,char **v)
  {
    id server = [Webserver argc:n argv:v];
    [server run];
    exit(0);
  }
By making argc and argv available to the Webserver class, the application can support the -d flag (debug) and the -p portnum (set a port number other than the default WWW port 80).

The user may override -contents in a subclass, to print HTML onto the stdout. This will be wrapped by the Webserver class into a HTTP response, and returned to the client.

Note: contents is sent after forking a child process to handle the request. The message accepted may be overridden to make modifications to the parent process.

Method Types

Creation

Options

Blocking interface

Non-blocking interface

Messages to be overridden by subclass

Methods



new

+ new

Creates an instance of the Webserver class, listening at port number 80, with a blocking poll message, and that will run as a daemon. These defaults can be changed by setportnum:, setblocking: etc.



argc:argv:

+ argc :(int) n argv :(char**) v

Creates an instance of the Webserver class. Can be used to process some standard command line options such as -d (run the server in debug mode, without detaching from the controlling terminal) and -p (to set a port number different from 80).



setportnum:

- setportnum :(int) num

This method is normally used by +argc:argv: to set the portnumber as specified with a -p command line option. If the Webserver instance was created with +new, the program may use the setportnum: message to change the portnumber (defaults to 80).



setdaemon:

- setdaemon :(BOOL) flag

This message is normally sent by +argc:argv: to turn daemon mode off (and to turn debug mode on) when a -d command line option is given. If the Webserver instance was created with +new, the program may use the setdaemon: message to prevent the server to run in daemon mode, i.e. by default, the server will detach itself from the controlling terminal.



setblocking:

- setblocking :(BOOL) flag

By default, the poll message will block and wait for requests. By sending a setblocking: message with a NO argument, poll will immediately return if there is no request to be accepted.



run

- run

This method never returns. It repeatedly sends poll messages to itself.



if:readable:

- if :(int) fd readable : aBlock

Registers aBlock as handler for activity on the file descriptor fd. If fd is ready for reading (as indicated by the C function select()), aBlock will be evaluated.



if:writable:

- if :(int) fd writable : aBlock

Registers aBlock as handler for activity on the file descriptor fd. If fd is ready for writing (as indicated by the C function select()), aBlock will be evaluated.



poll

- poll

The first time this message is sent, the Webserver starts listening at the indicated port number (as set by setportnum:). When running in blocking mode, the poll message will then block until a request arrives. The method then sends itself an accepted message, and then forks a child process to handle the request. Subclasses should implement the contents method, to reply to HTTP GET requests.



listenfd

- (int) listenfd

File descriptor that can be used to select on, when using the Webserver in non-blocking mode.



accepted

- accepted

Sent after a request is received but before a child is forked to handle the request.



contents

- contents

Sent after a GET request is received, and after a child is forked to handle the request. The accepted message is sent just before the child process is forked.