Nut/OS  5.0.5
API Reference
uHTTP

Micro hypertext transfer protocol library. More...

Collaboration diagram for uHTTP:

Data Structures

struct  _HTTP_STREAM
 Stream information structure for Winsock implementations. More...
struct  _HTTP_REQUEST
 HTTP request information structure. More...
struct  _HTTPD_SESSION
 HTTP session information structure. More...

Modules

 Modules
 Environment variables
 Media types
 Stream I/O
 Utilities

Defines

#define HTTP_ASSERT   NUTASSERT
#define HTTP_MAJOR_VERSION   1
#define HTTP_MINOR_VERSION   1
#define HTTP_VERSION   0x11
#define HTTP_ROOT   (http_root ? http_root : HTTP_DEFAULT_ROOT)

Typedefs

typedef struct _HTTP_REQUEST HTTP_REQUEST
 HTTP request information structure type.
typedef struct _HTTPD_SESSION HTTPD_SESSION
 HTTP session information structure type.
typedef int(* HTTP_AUTH_VALIDATOR )(HTTPD_SESSION *)
 HTTP authentication function type.
typedef int(* HTTP_LOC_REDIRECTOR )(HTTPD_SESSION *)
 HTTP redirection function type.

Functions

int HttpRegisterRootPath (char *path)
 Register the HTTP server's root directory.
int HttpParseHeader (HTTPD_SESSION *hs)
 Parse HTTP header.
int HttpParseMultipartHeader (HTTPD_SESSION *hs, const char *bnd, long *avail)
 Parse HTTP multipart header.
void HttpSendHeaderTop (HTTPD_SESSION *hs, int status)
 Send initial HTTP response header.
void HttpSendHeaderDate (HTTPD_SESSION *hs, time_t mtime)
 Send HTTP date response header.
void HttpSendHeaderBottom (HTTPD_SESSION *hs, char *type, char *subtype, long bytes)
 Send final HTTP response header.
void HttpSendError (HTTPD_SESSION *hs, int status)
 Send HTTP error response.
int HttpSendRedirection (HTTPD_SESSION *hs, int code,...)
 Send HTTP redirection response.
char * HttpArgParseFirst (HTTP_REQUEST *req)
 Get first argument from an HTTP request line.
char * HttpArgParseNext (HTTP_REQUEST *req)
 Get next argument from an HTTP request line.
char * HttpArgReadNext (HTTPD_SESSION *hs, long *avail)
 Read next argument from an HTTP post request.
char * HttpArgValue (HTTP_REQUEST *req)
 Get value of the last read argument.
const char * HttpArgValueSub (const char *str, const char *name, int *len)
 Retrieve parameter value from HTTP header line.
void HttpdClientHandler (HTTP_STREAM *sp)
 Default client handler.

Variables

int _HTTP_STREAM::strm_ipos
int _HTTP_STREAM::strm_ilen
char _HTTP_STREAM::strm_ibuf [1460+128]
TCPSOCKET_HTTP_STREAM::strm_sock
char * http_root
HTTP_AUTH_VALIDATOR httpd_auth_validator
 HTTP authentication function pointer.
HTTP_LOC_REDIRECTOR httpd_loc_redirector
 HTTP redirection function pointer.

HTTP request methods

#define HTTP_METHOD_GET   1
 Get method.
#define HTTP_METHOD_POST   2
 Post method.
#define HTTP_METHOD_HEAD   3
 Head method.

HTTP connection types

#define HTTP_CONN_CLOSE   1
 Connection will be closed.
#define HTTP_CONN_KEEP_ALIVE   2
 Connection will not be closed.

HTTP static texts

const char ct_GET []
const char ct_HEAD []
const char ct_POST []
const char ct_Content_Disposition []
const char ct_Content_Type []
const char ct_Accept_Encoding []
const char ct_Authorization []
const char ct_Connection []
const char ct_close []
const char ct_Keep_Alive []
const char ct_Content_Length []
const char ct_Cookie []
const char ct_Host []
const char ct_Referer []
const char ct_User_Agent []
const char ct_Content_Encoding []
const char ct_Location []
const char ct_If_Modified_Since []
const char ct_Last_Modified []
const char ct_Expires []
const char ct_Date []

Detailed Description

Micro hypertext transfer protocol library.

This is the second version of a HTTP library for Nut/OS. It has been introduced in Nut/OS 5.0 and may later replace the original HTTP library.

The main advantage of this new version is its flexibility, which not only allows to replace almost any default behavior, but also offers various ways to extend its capabilities without modification of the original source code. To give an example: The list of hard coded media types can be configured with the Nut/OS Configurator. Furthermore, new media types may be either added to this list to be processed by a default handler or may be processed by an application specific handler. In the old library new media types had to be added to the hard coded default list, modifying the original source code.

Operating system (Nut/OS) dependent functions are no longer scattered in the source code. Instead, they are collected in a single module, which makes porting the library to other operating systems quite easy. So far the code had been successfully tested on Windows and a Linux port will probably follow some day.


Define Documentation

#define HTTP_MAJOR_VERSION   1

Referenced by HttpSendHeaderTop().

#define HTTP_MINOR_VERSION   1

Referenced by HttpSendHeaderTop().

#define HTTP_VERSION   0x11
#define HTTP_METHOD_GET   1

Get method.

Referenced by HttpParseHeader(), HttpSessionInfo(), and HttpSsiExecHandler().

#define HTTP_METHOD_POST   2

Post method.

Referenced by HttpParseHeader(), and HttpSessionInfo().

#define HTTP_METHOD_HEAD   3

Head method.

Referenced by HttpParseHeader(), and HttpSessionInfo().

#define HTTP_CONN_CLOSE   1

Connection will be closed.

#define HTTP_CONN_KEEP_ALIVE   2

Connection will not be closed.

#define HTTP_ROOT   (http_root ? http_root : HTTP_DEFAULT_ROOT)

Typedef Documentation

typedef struct _HTTP_REQUEST HTTP_REQUEST

HTTP request information structure type.

typedef struct _HTTPD_SESSION HTTPD_SESSION

HTTP session information structure type.

HTTP authentication function type.

HTTP redirection function type.


Function Documentation

int HttpRegisterRootPath ( char *  path)

Register the HTTP server's root directory.

Only one root directory is supported. Subsequent calls will override previous settings.

Parameters:
pathAbsolute path name of the root directory. If NULL, then the path will be reset to the configured default.
Returns:
0 on success, -1 otherwise.

References free(), http_root, NULL, and strdup().

Here is the call graph for this function:

int HttpParseMultipartHeader ( HTTPD_SESSION hs,
const char *  bnd,
long *  avail 
)

Parse HTTP multipart header.

Parameters:
hsPointer to the session info structure.

References ct_Content_Disposition, ct_Content_Type, free(), HTTP_MAX_REQUEST_SIZE, malloc(), MIN, NULL, rc, _HTTP_REQUEST::req_bnd_dispo, _HTTP_REQUEST::req_bnd_type, _HTTPD_SESSION::s_req, _HTTPD_SESSION::s_stream, strcasecmp, strdup(), StreamReadUntilChars(), strlen(), and strncmp().

Here is the call graph for this function:

void HttpSendHeaderTop ( HTTPD_SESSION hs,
int  status 
)

Send initial HTTP response header.

Parameters:
hsPointer to the session info structure.

References ct_Date, gmtime(), HTTP_MAJOR_VERSION, HTTP_MINOR_VERSION, HttpResponseText(), NULL, Rfc1123TimeString(), s_printf(), _HTTPD_SESSION::s_stream, s_vputs(), and time().

Referenced by HttpSendError(), HttpSendRedirection(), HttpSsiHandler(), and MediaTypeHandlerBinary().

Here is the call graph for this function:

void HttpSendHeaderDate ( HTTPD_SESSION hs,
time_t  mtime 
)

Send HTTP date response header.

Parameters:
hsPointer to the session info structure.

References ct_Last_Modified, gmtime(), NULL, Rfc1123TimeString(), _HTTPD_SESSION::s_stream, and s_vputs().

Referenced by MediaTypeHandlerBinary().

Here is the call graph for this function:

void HttpSendHeaderBottom ( HTTPD_SESSION hs,
char *  type,
char *  subtype,
long  bytes 
)

Send final HTTP response header.

Parameters:
hsPointer to the session info structure.

References ct_Content_Length, ct_Content_Type, GZIP_ID, HTTP_CONN_CLOSE, HTTP_CONN_KEEP_ALIVE, NULL, _HTTP_REQUEST::req_connection, s_printf(), s_puts(), _HTTPD_SESSION::s_req, _HTTPD_SESSION::s_stream, and s_vputs().

Referenced by HttpSendError(), HttpSendRedirection(), HttpSsiHandler(), and MediaTypeHandlerBinary().

Here is the call graph for this function:

void HttpSendError ( HTTPD_SESSION hs,
int  status 
)

Send HTTP error response.

Parameters:
hsPointer to the session info structure.

References HttpResponseText(), HttpSendHeaderBottom(), HttpSendHeaderTop(), _HTTP_REQUEST::req_realm, s_flush(), s_printf(), _HTTPD_SESSION::s_req, _HTTPD_SESSION::s_stream, and strlen().

Referenced by HttpdClientHandler(), HttpParseHeader(), HttpSsiHandler(), and MediaTypeHandlerBinary().

Here is the call graph for this function:

int HttpSendRedirection ( HTTPD_SESSION hs,
int  code,
  ... 
)

Send HTTP redirection response.

Parameters:
hsPointer to the session info structure.

References ct_Location, free(), HttpSendHeaderBottom(), HttpSendHeaderTop(), malloc(), NULL, s_flush(), s_printf(), _HTTPD_SESSION::s_stream, s_vputs(), strcat(), and strlen().

Referenced by HttpLocationRedir().

Here is the call graph for this function:

char* HttpArgParseFirst ( HTTP_REQUEST req)

Get first argument from an HTTP request line.

The function returns the name only. Use HttpArgValue() to retrieve its value.

Parameters:
reqPointer to the request information structure.

References HttpArgParseNext(), _HTTP_REQUEST::req_argp, and _HTTP_REQUEST::req_query.

Here is the call graph for this function:

char* HttpArgParseNext ( HTTP_REQUEST req)

Get next argument from an HTTP request line.

Parameters:
reqPointer to the request information structure.

References free(), HttpUrlUnescape(), malloc(), memcpy(), NULL, _HTTP_REQUEST::req_argn, _HTTP_REQUEST::req_argp, _HTTP_REQUEST::req_argv, strchr(), and strdup().

Referenced by HttpArgParseFirst().

Here is the call graph for this function:

char* HttpArgReadNext ( HTTPD_SESSION hs,
long *  avail 
)

Read next argument from an HTTP post request.

The function returns a pointer to the name. Use HttpArgValue() to retrieve its value.

Parameters:
hsPointer to the session info structure.
availPointer to the variable that contains the number of bytes available in the request body. It will be updated by this function.
Returns:
Pointer to a string that contains the name of the argument. NULL is returned, if all arguments had been read or in case of an error.

References free(), HttpUrlUnescape(), malloc(), NULL, _HTTP_REQUEST::req_argn, _HTTP_REQUEST::req_argv, _HTTPD_SESSION::s_req, _HTTPD_SESSION::s_stream, strchr(), strdup(), and StreamReadUntilChars().

Here is the call graph for this function:

char* HttpArgValue ( HTTP_REQUEST req)

Get value of the last read argument.

Returns the value of an argument previously read by HttpArgParseFirst(), HttpArgParseNext() or HttpArgReadNext().

Parameters:
reqPointer to the request information structure.
Returns:
Pointer to a string that contains the name of the argument. NULL is returned, if no value is available or in case of an error.

References _HTTP_REQUEST::req_argv.

const char* HttpArgValueSub ( const char *  str,
const char *  name,
int *  len 
)

Retrieve parameter value from HTTP header line.

A HTTP header line may contain a list of parameter name/value pairs, separated by semicolons.

<header-name>: <param-name>="<param-value>"; <param-name>="<param-value>"..

This function can be used to retrieve a value of a parameter that is specified by its name. Surrounding quotation marks are excluded from the result.

Parameters:
strPointer to the text buffer that contains the header line.
nameName of the parameter to retrieve.
lenPointer to a variable that receives the length of the value.
Returns:
Pointer to the value.

References NULL, strchr(), strlen(), and strncasecmp.

Here is the call graph for this function:


Variable Documentation

const char ct_GET[]

Constant string "GET".

Referenced by HttpParseHeader(), and HttpSessionInfo().

const char ct_HEAD[]

Constant string "HEAD".

Referenced by HttpParseHeader(), and HttpSessionInfo().

const char ct_POST[]

Constant string "POST".

Referenced by HttpParseHeader(), and HttpSessionInfo().

const char ct_Content_Disposition[]

Constant string "Content-Disposition".

Referenced by HttpParseMultipartHeader().

const char ct_Content_Type[]

Constant string "Content-Type".

Referenced by HttpParseHeader(), HttpParseMultipartHeader(), and HttpSendHeaderBottom().

const char ct_Accept_Encoding[]

Constant string "Accept-Encoding".

Referenced by HttpParseHeader().

const char ct_Authorization[]

Constant string "Authorization".

Referenced by HttpParseHeader().

const char ct_Connection[]

Constant string "Connection".

Referenced by HttpParseHeader().

const char ct_close[]

Constant string "close".

Referenced by HttpParseHeader(), and HttpSessionInfo().

const char ct_Keep_Alive[]

Constant string "keep-alive".

Referenced by HttpParseHeader(), and HttpSessionInfo().

const char ct_Content_Length[]

Constant string "Content-Length".

Referenced by HttpParseHeader(), and HttpSendHeaderBottom().

const char ct_Cookie[]

Constant string "Cookie".

Referenced by HttpParseHeader().

const char ct_Host[]

Constant string "Host".

Referenced by HttpParseHeader().

const char ct_Referer[]

Constant string "Referer".

Referenced by HttpParseHeader().

const char ct_User_Agent[]

Constant string "User-Agent".

Referenced by HttpParseHeader().

const char ct_Content_Encoding[]

Constant string "Content-Encoding".

const char ct_Location[]

Constant string "Location".

Referenced by HttpSendRedirection().

const char ct_If_Modified_Since[]

Constant string "If-Modified-Since".

Referenced by HttpParseHeader().

const char ct_Last_Modified[]

Constant string "Last-Modified".

Referenced by HttpSendHeaderDate().

const char ct_Expires[]

Constant string "Expires".

Referenced by HttpSsiHandler().

const char ct_Date[]

Constant string "Date".

Referenced by HttpSendHeaderTop().

HTTP authentication function pointer.

Referenced by HttpdClientHandler(), and HttpRegisterAuthBasic().

HTTP redirection function pointer.

Referenced by HttpdClientHandler(), and HttpRegisterRedir().