HomePort
Data Structures | Enumerations | Functions | Variables
httpd_request.c File Reference
Include dependency graph for httpd_request.c:

Go to the source code of this file.

Data Structures

struct  hpd_httpd_request
 An http request. More...
 

Enumerations

enum  state {
  S_START, S_BEGIN, S_URL, S_HEADER_FIELD,
  S_HEADER_VALUE, S_HEADER_COMPLETE, S_BODY, S_COMPLETE,
  S_STOP, S_ERROR
}
 The possible states of a request. More...
 

Functions

static hpd_error_t header_parser_field_value_pair_complete (void *data, const char *field, size_t field_length, const char *value, size_t value_length)
 Callback for the header parser. More...
 
hpd_error_t hpd_httpd_request_get_argument (hpd_httpd_request_t *req, const char *key, const char **val)
 Get a specific argument of a request. More...
 
hpd_error_t hpd_httpd_request_get_arguments (hpd_httpd_request_t *req, hpd_map_t **arguments)
 Get a linked map of all URL arguements for a request. More...
 
hpd_error_t hpd_httpd_request_get_cookie (hpd_httpd_request_t *req, const char *key, const char **val)
 Get a specific cookie for a request. More...
 
hpd_error_t hpd_httpd_request_get_cookies (hpd_httpd_request_t *req, hpd_map_t **cookies)
 Get a all cookies for a request. More...
 
hpd_error_t hpd_httpd_request_get_header (hpd_httpd_request_t *req, const char *key, const char **value)
 Get a specific header of a request. More...
 
hpd_error_t hpd_httpd_request_get_headers (hpd_httpd_request_t *req, hpd_map_t **headers)
 Get a linked map of all headers for a request. More...
 
hpd_error_t hpd_httpd_request_get_ip (hpd_httpd_request_t *req, const char **ip)
 Get the IP of a request. More...
 
hpd_error_t hpd_httpd_request_get_method (hpd_httpd_request_t *req, hpd_httpd_method_t *method)
 Get the method of the http request. More...
 
hpd_error_t hpd_httpd_request_get_url (hpd_httpd_request_t *req, const char **url)
 Get the URL of this request. More...
 
hpd_error_t hpd_httpd_request_keep_open (hpd_httpd_request_t *req)
 Keep the connection for a request open. More...
 
hpd_error_t http_request_create (hpd_httpd_request_t **req, hpd_httpd_t *httpd, hpd_httpd_settings_t *settings, hpd_tcpd_conn_t *conn, const hpd_module_t *context)
 Create a new ws_request. More...
 
hpd_error_t http_request_destroy (hpd_httpd_request_t *req)
 Destroy a ws_request. More...
 
hpd_error_t http_request_get_connection (hpd_httpd_request_t *req, hpd_tcpd_conn_t **conn)
 Get the connection of a request. More...
 
hpd_error_t http_request_get_context (hpd_httpd_request_t *req, const hpd_module_t **context)
 
hpd_error_t http_request_parse (hpd_httpd_request_t *req, const char *buf, size_t len)
 Parse a new chunk of the message. More...
 
static int parser_body (http_parser *parser, const char *buf, size_t len)
 Body callback for http_parser. More...
 
static int parser_hdr_cmpl (http_parser *parser)
 Header complete callback for http_parser. More...
 
static int parser_hdr_field (http_parser *parser, const char *buf, size_t len)
 Header field callback for http_parser. More...
 
static int parser_hdr_value (http_parser *parser, const char *buf, size_t len)
 Header value callback for http_parser. More...
 
static int parser_msg_begin (http_parser *parser)
 Message begin callback for http_parser. More...
 
static int parser_msg_cmpl (http_parser *parser)
 Messages complete callback for http_parser. More...
 
static int parser_url (http_parser *parser, const char *buf, size_t len)
 URL callback for http_parser. More...
 
static hpd_error_t url_parser_key_value (void *data, const char *key, size_t key_len, const char *value, size_t value_len)
 Callback for URL parser. More...
 
static hpd_error_t url_parser_path_complete (void *data, const char *parsedSegment, size_t segment_length)
 Callback for URL parser. More...
 

Variables

static http_parser_settings parser_settings
 Global settings for http_parser. More...
 

Data Structure Documentation

struct hpd_httpd_request

An http request.

Public interface

nodata_cb and data_cb functions in hpd_tcpd_settings provides a request as their parameters. This allows the implenter of a webserver to receive requests.

ws_response_create() uses the request to create a response to the client.

Internal interface

Warning
These methods is for use only within the webserver library, implementers of the webserver should use the public interface described above.

ws_request_create() Creates new requests. Requests should be deallocated using ws_request_destroy() afterwards.

Initially requests are empty, they are filled with data using ws_request_parse() which appends a new string to the request. Note that the request do not save any data itself, beside from its state. All data in the string will be passed on as pointers to the callbacks defined in hpd_tcpd_settings.

ws_request_get_client() gets the client that sent the request.

States

A request will take states in the following order, the states of S_STOP and S_ERROR can be assumed as always possible target for a transition. The labels on the edges denote the callback from hpd_tcpd_settings, that will be called upon the transition.

Definition at line 121 of file httpd_request.c.

Collaboration diagram for hpd_httpd_request:
Data Fields
hpd_map_t * arguments URL Arguments.
hpd_tcpd_conn_t * conn Connection to client.
const hpd_module_t * context
hpd_map_t * cookies Cookie Pairs.
void * data User data.
struct hp * header_parser Header Parser.
hpd_map_t * headers Header Pairs.
hpd_httpd_method_t method
http_parser parser HTTP parser.
hpd_httpd_settings_t * settings Settings.
enum state state Current state.
char * url URL.
struct up * url_parser URL Parser.
hpd_httpd_t * webserver HTTP Webserver.

Enumeration Type Documentation

enum state

The possible states of a request.

Enumerator
S_START 

The initial state.

S_BEGIN 

Message begun received.

S_URL 

Receiving URL.

S_HEADER_FIELD 

Receiving a header field.

S_HEADER_VALUE 

Receiving a header value.

S_HEADER_COMPLETE 

Received all headers.

S_BODY 

Receiving body.

S_COMPLETE 

Received all of message.

S_STOP 

Callback requested a stop.

S_ERROR 

An error has happened.

Definition at line 37 of file httpd_request.c.

Function Documentation

static hpd_error_t header_parser_field_value_pair_complete ( void *  data,
const char *  field,
size_t  field_length,
const char *  value,
size_t  value_length 
)
static

Callback for the header parser.

Is called for each header pairs with a field and a value. If the header pair is a cookie, the cookied will be parsed and stored in the list of cookies. Multiple headers will be combined into a a single with a comma-seperated list of values, according to the RFC 2616.

Parameters
dataThe HTTP Request
fieldThe field, not null-terminated
field_lengthThe length of the field
valueThe value, not null-terminated
value_lengthThe length of the value

Definition at line 216 of file httpd_request.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_get_argument ( hpd_httpd_request_t req,
const char *  key,
const char **  val 
)

Get a specific argument of a request.

Parameters
reqhttp request
keyKey value of argument to get
Returns
Value of argument as string, or NULL if not found

Definition at line 841 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_arguments ( hpd_httpd_request_t req,
hpd_map_t **  arguments 
)

Get a linked map of all URL arguements for a request.

Parameters
reqhttp request
Returns
Arguments as a linkedmap (struct lm)

Definition at line 824 of file httpd_request.c.

hpd_error_t hpd_httpd_request_get_cookie ( hpd_httpd_request_t req,
const char *  key,
const char **  val 
)

Get a specific cookie for a request.

Parameters
reqhttp request
keyKey of cookie to get
Returns
The value of the cookie as String, or NULL if cookie was not found

Definition at line 874 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_cookies ( hpd_httpd_request_t req,
hpd_map_t **  cookies 
)

Get a all cookies for a request.

Parameters
reqhttp request
Returns
Cookies as a linkedmap (struct lm)

Definition at line 856 of file httpd_request.c.

hpd_error_t hpd_httpd_request_get_header ( hpd_httpd_request_t req,
const char *  key,
const char **  value 
)

Get a specific header of a request.

Parameters
reqhttp request
keyKey for the header to get
Returns
The value of the header with the specified key, or NULL if not found

Definition at line 809 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_headers ( hpd_httpd_request_t req,
hpd_map_t **  headers 
)

Get a linked map of all headers for a request.

Parameters
reqhttp request
Returns
Headers as a linkedmap (struct lm)

Definition at line 791 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_get_ip ( hpd_httpd_request_t req,
const char **  ip 
)

Get the IP of a request.

Parameters
reqhttp request
Returns
IP as a string

Definition at line 905 of file httpd_request.c.

Here is the call graph for this function:

hpd_error_t hpd_httpd_request_get_method ( hpd_httpd_request_t req,
hpd_httpd_method_t method 
)

Get the method of the http request.

Parameters
reqhttp request
Returns
The method as a enum http_method

Definition at line 759 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_get_url ( hpd_httpd_request_t req,
const char **  url 
)

Get the URL of this request.

Parameters
reqhttp request
Returns
URL as a string

Definition at line 775 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t hpd_httpd_request_keep_open ( hpd_httpd_request_t req)

Keep the connection for a request open.

Normally connections are closed when there has been no activity on it for the amount specified in the timeout field in the httpd settings struct. To keep the connection open forever, issue or call to this function with the http request. The connection will still be closed and destroyed when the client closes it.

Parameters
reqhttp request to keep open

Definition at line 924 of file httpd_request.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t http_request_create ( hpd_httpd_request_t **  req,
hpd_httpd_t httpd,
hpd_httpd_settings_t settings,
hpd_tcpd_conn_t conn,
const hpd_module_t context 
)

Create a new ws_request.

The created ws_request is ready to receive data through ws_reqeust_parse(), and it should be freed using ws_request_destroy() to avoid memory leaks.

Parameters
webserverThe httpd creating the request
settingsThe settings for the webserver receiving the request. This will determine which callbacks to call on events.
connThe connection on which the request is being received
Returns
The newly create ws_request.

Definition at line 601 of file httpd_request.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t http_request_destroy ( hpd_httpd_request_t req)

Destroy a ws_request.

All ws_requests should be freed by a call to this function to avoid memory leaks.

Parameters
reqThe request to be destroyed.

Definition at line 660 of file httpd_request.c.

Here is the call graph for this function:

Here is the caller graph for this function:

hpd_error_t http_request_get_connection ( hpd_httpd_request_t req,
hpd_tcpd_conn_t **  conn 
)

Get the connection of a request.

Parameters
reqhttp request
Returns
The connection

Definition at line 889 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t http_request_get_context ( hpd_httpd_request_t req,
const hpd_module_t **  context 
)

Definition at line 931 of file httpd_request.c.

Here is the caller graph for this function:

hpd_error_t http_request_parse ( hpd_httpd_request_t req,
const char *  buf,
size_t  len 
)

Parse a new chunk of the message.

This will sent the chunk to the http_parser, which will parse the new chunk and call the callbacks defined in parser_settings on events. The callbacks will change state of the ws_request and make calls on the functions defined in hpd_tcpd_settings.

Parameters
reqThe request, to which the chunk should be added.
bufThe chunk, which is not assumed to be \0 terminated.
lenLength of the chuck.
Returns
What http_parser_execute() returns.

Definition at line 697 of file httpd_request.c.

Here is the caller graph for this function:

static int parser_body ( http_parser *  parser,
const char *  buf,
size_t  len 
)
static

Body callback for http_parser.

Called from http_parser each time it receives a chunk of the message body. Each chunk will be sent on to on_request_body() from hpd_tcpd_settings.

Parameters
parserThe http_parser calling.
bufThe buffer containing the chunk. Note that the buffer is not \0 terminated.
lenThe length of the chunk.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 528 of file httpd_request.c.

static int parser_hdr_cmpl ( http_parser *  parser)
static

Header complete callback for http_parser.

Called from http_parser when all headers are parsed. If any remains are left of the message, they are assumed to be the body. If there were no headers in the message, this will trigger a call to on_request_url_complete() from hpd_tcpd_settings. For both messages with or without headers, on_request_header_complete() will also be called.

Parameters
parserThe http_parser calling.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 476 of file httpd_request.c.

Here is the call graph for this function:

static int parser_hdr_field ( http_parser *  parser,
const char *  buf,
size_t  len 
)
static

Header field callback for http_parser.

Called from http_parser with chunks of a header field. The first call to this callback will trigger a on_request_url_complete. Each chunk is sent on to the on_request_header_field() callback from hpd_tcpd_settings.

Parameters
parserThe http_parser calling.
bufThe buffer containing the chunk. Note that the buffer is not \0 terminated.
lenThe length of the chunk.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 380 of file httpd_request.c.

Here is the call graph for this function:

static int parser_hdr_value ( http_parser *  parser,
const char *  buf,
size_t  len 
)
static

Header value callback for http_parser.

Called from http_parser with chunks of a header value. The chunk will be sent on to the on_request_header_value() callback in hpd_tcpd_settings.

Parameters
parserThe http_parser calling.
bufThe buffer containing the chunk. Note that the buffer is not \0 terminated.
lenThe length of the chunk.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 433 of file httpd_request.c.

Here is the call graph for this function:

static int parser_msg_begin ( http_parser *  parser)
static

Message begin callback for http_parser.

Called when the http request message begins, by the http_parser. This will call on_request_begin() and on_request_method() from hpd_tcpd_settings.

Parameters
parserThe http_parser calling.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 284 of file httpd_request.c.

static int parser_msg_cmpl ( http_parser *  parser)
static

Messages complete callback for http_parser.

Called from http_parser when the full message have been parsed. This will trigger a call to on_request_complete() in hpd_tcpd_settings.

Parameters
parserThe http_parser calling.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 561 of file httpd_request.c.

static int parser_url ( http_parser *  parser,
const char *  buf,
size_t  len 
)
static

URL callback for http_parser.

Called from http_parser with chunks of the URL. Each chunk is sent to the on_request_url() callback in hpd_tcpd_settings and the the URL parser.

Parameters
parserThe http_parser calling.
bufThe buffer containing the chunk. Note that the buffer is not \0 terminated.
lenThe length of the chunk.
Returns
1 to signal the parser to stop, or 0 to signal a continue.

Definition at line 321 of file httpd_request.c.

Here is the call graph for this function:

static hpd_error_t url_parser_key_value ( void *  data,
const char *  key,
size_t  key_len,
const char *  value,
size_t  value_len 
)
static

Callback for URL parser.

Called when the URL parser has parsed an argument pair, with a key and a value.

Parameters
dataThe HTTP Request
keyThe key, not null-terminated
key_lenThe length of the key
valueThe value, not null-terminated
value_lenThe length of the value

Definition at line 195 of file httpd_request.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static hpd_error_t url_parser_path_complete ( void *  data,
const char *  parsedSegment,
size_t  segment_length 
)
static

Callback for URL parser.

Called when the full path has been parsed and stores the full path in the url field of the request.

Parameters
dataThe HTTP Request
parsendSegmentFull path, not null-terminated.
segment_lengthLength of path in characters

Definition at line 171 of file httpd_request.c.

Here is the caller graph for this function:

Variable Documentation

http_parser_settings parser_settings
static
Initial value:
=
{
.on_message_begin = parser_msg_begin,
.on_url = parser_url,
.on_status = NULL,
.on_header_field = parser_hdr_field,
.on_header_value = parser_hdr_value,
.on_headers_complete = parser_hdr_cmpl,
.on_body = parser_body,
.on_message_complete = parser_msg_cmpl
}
static int parser_hdr_value(http_parser *parser, const char *buf, size_t len)
Header value callback for http_parser.
static int parser_hdr_field(http_parser *parser, const char *buf, size_t len)
Header field callback for http_parser.
static int parser_msg_cmpl(http_parser *parser)
Messages complete callback for http_parser.
static int parser_msg_begin(http_parser *parser)
Message begin callback for http_parser.
static int parser_url(http_parser *parser, const char *buf, size_t len)
URL callback for http_parser.
static int parser_body(http_parser *parser, const char *buf, size_t len)
Body callback for http_parser.
static int parser_hdr_cmpl(http_parser *parser)
Header complete callback for http_parser.

Global settings for http_parser.

Definition at line 149 of file httpd_request.c.