processor.c File Reference

A HTTP Processor module that can handle HTTP requests received from a client, and return responses based on those requests. More...

Go to the source code of this file.

Defines

#define REQUEST   1
#define RESPONSE   2
#define HEADER   3
#define PARAM   4

Functions

void * http_processor (void *wrapper)
 Process a HTTP request.

void add_Impl (void *doGetFunc, void *doPostFunc)
 Callback for implementors of cervlet functions.

void send_error (HttpResponse res, int code, char *msg)
 Send an error message.

void send_redirect (HttpResponse res, char *location)
 Sends a temporary redirect response to the client using the specified redirect Location URL.

void out_print (HttpResponse res, const char *format,...)
 Prints a string into the given HttpResponse output buffer.

void destroy_wrapper (RequestWrapper w)
 Free a RequestWrapper object.

void set_header (HttpResponse res, char *name, char *value)
 Adds a response header with the given name and value.

void set_status (HttpResponse res, int code, char *msg)
 Sets the status code for the response.

void set_content_type (HttpResponse res, char *mime)
 Set the response content-type.

char * get_header (HttpRequest req, const char *name)
 Returns the value of the specified header.

char * get_parameter (HttpRequest req, const char *name)
 Returns the value of the specified parameter.

char * get_headers (HttpResponse res)
 Returns a string containing all (extra) headers found in the response.

char * get_status_string (int status)
 Lookup the corresponding HTTP status string for the given status code.


Variables

ssl_server_connectionmySSLServerConnection


Detailed Description

A HTTP Processor module that can handle HTTP requests received from a client, and return responses based on those requests.

This Processor delegates the actual handling of the request and reponse to so called cervlets, which must implement two methods; doGet and doPost. We call them cervlets because they are small C-modules closely resembling Java Servlets.

NOTES This Processor is command oriented and if a second slash '/' is found in the URL it's asumed to be the PATHINFO. In other words this processor perceive an URL as:

/COMMAND?args/PATHINFO

The doGet/doPost routines act's on the COMMAND. See the cervlet.c code in this dir. for an example.

2) This design is based on the Java Servlet API Framework, and to some extent; the Jakarta Catelina servlet engine by the very talented Craig R. McClanahan and others.

Author:
Jan-Henrik Haukeland, <hauk@tildeslash.com> , Christian Hopp, <chopp@iei.tu-clausthal.de>
Version:
$Id: processor.c,v 1.13 2002/12/21 11:25:55 hauk Exp $

Definition in file processor.c.


Define Documentation

#define HEADER   3
 

Definition at line 53 of file processor.c.

#define PARAM   4
 

Definition at line 54 of file processor.c.

#define REQUEST   1
 

Definition at line 51 of file processor.c.

#define RESPONSE   2
 

Definition at line 52 of file processor.c.


Function Documentation

void add_Impl void *    doGetFunc,
void *    doPostFunc
 

Callback for implementors of cervlet functions.

Parameters:
doGetFunc  doGet function
doPostFunc  doPost function

Definition at line 149 of file processor.c.

References ServiceImpl::doGet, ServiceImpl::doPost, and Impl.

Referenced by init_service().

void destroy_wrapper RequestWrapper    w
 

Free a RequestWrapper object.

Parameters:
w  A RequestWrapper object (Should not be NULL)

Definition at line 303 of file processor.c.

References close_accepted_ssl_socket(), close_socket(), myrun::httpdssl, requestwrapper::inetaddr, inetaddress::local_host, mySSLServerConnection, inetaddress::remote_host, Run, requestwrapper::socket, and requestwrapper::ssl.

Referenced by http_processor(), and start_httpd().

char* get_header HttpRequest    req,
const char *    name
 

Returns the value of the specified header.

Parameters:
req  HttpRequest object
name  Header name to lookup the value for
Returns:
The value of the specified header, NULL if not found

Definition at line 406 of file processor.c.

References request::headers, entry::name, entry::next, entry::value, and xstrdup().

char* get_headers HttpResponse    res
 

Returns a string containing all (extra) headers found in the response.

The headers are newline separated in the returned string.

Parameters:
res  HttpResponse object
Returns:
A String containing all headers set in the Response object

Definition at line 457 of file processor.c.

References response::headers, entry::name, entry::next, RES_STRLEN, entry::value, and xstrdup().

char* get_parameter HttpRequest    req,
const char *    name
 

Returns the value of the specified parameter.

Parameters:
req  HttpRequest object
name  The request parameter key to lookup the value for
Returns:
The value of the specified parameter, or NULL if not found

Definition at line 431 of file processor.c.

References entry::name, entry::next, request::params, entry::value, and xstrdup().

char* get_status_string int    status
 

Lookup the corresponding HTTP status string for the given status code.

Parameters:
status  A HTTP status code
Returns:
A default status message for the specified HTTP status code.

Definition at line 481 of file processor.c.

References SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_GATEWAY_TIMEOUT, SC_GONE, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_RANGE_NOT_SATISFIABLE, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LARGE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY, SC_VERSION_NOT_SUPPORTED, and xstrdup().

Referenced by send_error(), and send_redirect().

void* http_processor void *    wrapper
 

Process a HTTP request.

This is done by dispatching to the service function. Before the dispatch, an alarm clock is setup for handling request timeout.

Parameters:
wrapper  A Wrapper object wrapping the client socket connection

Definition at line 123 of file processor.c.

References destroy_wrapper(), SC_REQUEST_TIMEOUT, and requestwrapper::socket.

Referenced by start_httpd().

void out_print HttpResponse    res,
const char *    format,
...   
 

Prints a string into the given HttpResponse output buffer.

The actual response to the client is done in the service function, which will call the private function send_response. Cervlets should use this function (i.e. out_print) for sending a response, and never use the HttpResponse outputstream directly.

Despite the above warning, IF the HttpResponse.outputstream was used directly by a cervlet THEN the cervlet MUST set the is_committed flag in the HttpResponse object and is responsible for sending all HTTP headers and content by itself.

Parameters:
res  HttpResponse object
format  A formated string to be sent to the client

Definition at line 217 of file processor.c.

References response::bufsize, response::bufused, log(), response::outputbuffer, RES_STRLEN, xmalloc(), and xresize().

Referenced by send_error().

void send_error HttpResponse    res,
int    code,
char *    msg
 

Send an error message.

Parameters:
res  HttpResponse object
code  Error Code to lookup and send
msg  Optional error message (may be NULL)

Definition at line 166 of file processor.c.

References get_status_string(), out_print(), SERVER_URL, set_content_type(), and set_status().

void send_redirect HttpResponse    res,
char *    location
 

Sends a temporary redirect response to the client using the specified redirect Location URL.

The url must be absolute.

Parameters:
res  HttpResponse object
location  An absolute url to redirect to

Definition at line 192 of file processor.c.

References get_status_string(), SC_MOVED_TEMPORARILY, set_header(), and set_status().

void set_content_type HttpResponse    res,
char *    mime
 

Set the response content-type.

Parameters:
res  HttpResponse object
mime  Mime content type, e.g. text/html

Definition at line 393 of file processor.c.

References set_header().

Referenced by send_error().

void set_header HttpResponse    res,
char *    name,
char *    value
 

Adds a response header with the given name and value.

If the header had already been set the new value overwrites the previous one.

Parameters:
res  HttpResponse object
name  Header key name
value  Header key value

Definition at line 338 of file processor.c.

References response::headers, entry::name, NEW, entry::next, entry::value, and xstrdup().

Referenced by send_redirect(), and set_content_type().

void set_status HttpResponse    res,
int    code,
char *    msg
 

Sets the status code for the response.

Parameters:
res  HttpResponse object
code  A HTTP status code <100-510>
msg  The status code string message

Definition at line 379 of file processor.c.

References response::status, response::status_msg, and xstrdup().

Referenced by send_error(), and send_redirect().


Variable Documentation

ssl_server_connection* mySSLServerConnection
 

Definition at line 57 of file processor.c.

Referenced by destroy_wrapper(), start_httpd(), and stop_httpd().