Nut/OS  5.0.5
API Reference
HTTP

Hypertext transfer protocol. More...

Collaboration diagram for HTTP:

Data Structures

struct  _REQUEST
 HTTP request information structure. More...
struct  _MIMETYPES
struct  _AUTHINFO
 HTTP authorization information structure. More...
struct  _CGIFUNCTION
 Registered CGI function. More...
struct  _REQUEST_LOOKUP
 Structure for table of interpreted header names. More...

Defines

#define METHOD_GET   1
#define METHOD_POST   2
#define METHOD_HEAD   3
#define HTTP_CONN_CLOSE   1
#define HTTP_CONN_KEEP_ALIVE   2
#define HTTP_OF_USE_HOST_TIME   0x00000001UL
#define HTTP_OF_USE_FILE_TIME   0x00000002UL
#define MAX_BUFFER_SIZE   256
#define MAX_ASP_FUNC_SIZE   64
#define NUM_REQUEST_LOOKUP   sizeof(req_lookup) / sizeof(REQUEST_LOOKUP)
 Number of entries in the header name table.
#define MAX_REQUEST_NAME_SIZE   17
 Size of the largest entry in the header name table.
#define BUFSIZE   512
#define MIN(a, b)   (a<b?a:b)
#define SSI_TYPE_FILE   0x01
#define SSI_TYPE_VIRTUAL   0x02
#define SSI_TYPE_EXEC   0x03
#define SSI_TYPE_ECHO   0x04

Typedefs

typedef struct _REQUEST REQUEST
typedef struct _MIMETYPES MIMETYPES
typedef struct _AUTHINFO AUTHINFO
typedef struct _CGIFUNCTION CGIFUNCTION
typedef struct _REQUEST_LOOKUP REQUEST_LOOKUP
 Structure for table of interpreted header names.

Enumerations

enum  { ASP_STATE_IDLE = 0, ASP_STATE_START, ASP_STATE_COPY_FUNC }

Functions

void NutHttpProcessRequest (FILE *stream)
 Process the next HTTP request.
void NutHttpProcessQueryString (REQUEST *req)
 Parses the QueryString.
void NutHttpSendHeaderTop (FILE *stream, REQUEST *req, int status, char *title)
 Send top lines of a standard HTML header.
void NutHttpSendHeaderBottom (FILE *stream, REQUEST *req, char *mime_type, long bytes)
 Send bottom lines of a standard HTML header.
void NutHttpSendHeaderBot (FILE *stream, char *mime_type, long bytes)
 Send bottom lines of a standard HTML header.
void NutHttpSendError (FILE *stream, REQUEST *req, int status)
 Send a HTTP error response.
char * NutGetMimeType (char *name)
 Return the mime type description of a specified file name.
void * NutGetMimeHandler (char *name)
 Return the mime type handler of a specified file name.
uint8_t NutSetMimeHandler (char *extension, void(*handler)(FILE *stream, int fd, int file_len, char *http_root, REQUEST *req))
 Set the mime type handler for a specified file extension.
int NutHttpAuthValidate (REQUEST *req)
 Validate an authorization request.
int NutRegisterAuth (const char *dirname, const char *login)
 Register an authorization entry.
void NutClearAuth (void)
 Clear all authorization entries.
void NutRegisterSsi (void)
 Register SSI handler for shtml files.
void NutHttpProcessAsp (FILE *stream, int fd, int file_len, char *http_root, REQUEST *req)
int NutRegisterAspCallback (int(*func)(char *, FILE *))
void NutRegisterAsp (void)
 Register ASP handler for asp files.
void NutRegisterCgiBinPath (char *path)
 Register a new cgi-bin path.
int NutCgiCheckRequest (FILE *stream, REQUEST *req)
 Check if request is a cgi call.
int NutRegisterCgi (char *name, int(*func)(FILE *, REQUEST *))
 Register a CGI function.
void NutCgiProcessRequest (FILE *stream, REQUEST *req, int name_pos)
 Process an incoming CGI request.
void NutHttpURLDecode (char *str)
 URLDecodes a string.
int NutRegisterHttpRoot (char *path)
 Register the HTTP server's root directory.
void NutHttpSetOptionFlags (uint32_t flags)
 Set HTTP option flags.
uint32_t NutHttpGetOptionFlags (void)
 Retrieve HTTP option flags.
char * NutHttpURLEncode (char *str)
 URLEncodes a string.
void NutHttpProcessPostQuery (FILE *stream, REQUEST *req)
 Parses the QueryString.
char * NutHttpGetParameter (REQUEST *req, char *name)
 Gets a request parameter value by name.
int NutHttpGetParameterCount (REQUEST *req)
 Gets the number of request parameters.
char * NutHttpGetParameterName (REQUEST *req, int index)
 Gets the name of a request parameter.
char * NutHttpGetParameterValue (REQUEST *req, int index)
 Get the value of a request parameter.
int NutRegisterSsiVarHandler (char *(*handler)(char *name, REQUEST *req))
 Register SSI handler for variables.

Variables

CONFNET confnet
 Global network configuration structure.
AUTHINFOauthList = 0
CGIFUNCTION *volatile cgiFunctionList = 0
char * cgiBinPath = NULL
MIMETYPES mimeTypes []
 Known mime types.
MIMETYPES mimeTypes []
 Known mime types.
char * http_root
char * cgiBinPath

Detailed Description

Hypertext transfer protocol.

New applications should no longer use this first version of a HTTP library, but instead use the newer Micro HTTP library.


Define Documentation

#define METHOD_GET   1
#define METHOD_POST   2
#define METHOD_HEAD   3
Examples:
httpd/httpserv.c.

Referenced by NutHttpProcessRequest().

#define HTTP_CONN_CLOSE   1
#define HTTP_OF_USE_HOST_TIME   0x00000001UL

Referenced by NutHttpSendHeaderTop().

#define HTTP_OF_USE_FILE_TIME   0x00000002UL
#define MAX_BUFFER_SIZE   256

Referenced by NutHttpProcessAsp().

#define MAX_ASP_FUNC_SIZE   64

Referenced by NutHttpProcessAsp().

#define NUM_REQUEST_LOOKUP   sizeof(req_lookup) / sizeof(REQUEST_LOOKUP)

Number of entries in the header name table.

#define MAX_REQUEST_NAME_SIZE   17

Size of the largest entry in the header name table.

#define BUFSIZE   512
#define MIN (   a,
 
)    (a<b?a:b)
#define SSI_TYPE_FILE   0x01
#define SSI_TYPE_VIRTUAL   0x02
#define SSI_TYPE_EXEC   0x03
#define SSI_TYPE_ECHO   0x04

Typedef Documentation

typedef struct _REQUEST REQUEST
typedef struct _MIMETYPES MIMETYPES
typedef struct _AUTHINFO AUTHINFO
typedef struct _CGIFUNCTION CGIFUNCTION

Structure for table of interpreted header names.


Enumeration Type Documentation

anonymous enum
Enumerator:
ASP_STATE_IDLE 
ASP_STATE_START 
ASP_STATE_COPY_FUNC 

Function Documentation

void NutHttpProcessRequest ( FILE stream)

Process the next HTTP request.

Waits for the next HTTP request on an established connection and processes it.

Parameters:
streamStream of the socket connection, previously opened for binary read and write.
Examples:
httpd/httpserv.c, httpd_simple/httpd_simple.c, and httpd_upnp/httpserv.c.

References DestroyRequestInfo(), fflush(), fgets(), free(), HTTP_CONN_CLOSE, HTTP_CONN_KEEP_ALIVE, HTTP_KEEP_ALIVE_REQ, HTTP_MAX_REQUEST_SIZE, malloc(), METHOD_GET, METHOD_HEAD, METHOD_POST, NULL, NutDecodePath(), NutHttpProcessQueryString(), NutHttpSendError(), _REQUEST::req_connection, _REQUEST::req_method, _REQUEST::req_query, _REQUEST::req_url, _REQUEST::req_version, strcasecmp, strchr(), and strdup().

Referenced by main(), and Service().

Here is the call graph for this function:

void NutHttpProcessQueryString ( REQUEST req)

Parses the QueryString.

Reads the QueryString from a request, and parses it into name/value table. To save RAM, this method overwrites the contents of req_query, and creates a table of pointers into the req_query buffer.

Parameters:
reqRequest object to parse

References malloc(), NULL, NutHttpURLDecode(), _REQUEST::req_numqptrs, _REQUEST::req_qptrs, and _REQUEST::req_query.

Referenced by NutHttpProcessRequest().

Here is the call graph for this function:

void NutHttpSendHeaderTop ( FILE stream,
REQUEST req,
int  status,
char *  title 
)

Send top lines of a standard HTML header.

Sends HTTP and Server version lines.

Parameters:
streamStream of the socket connection, previously opened for binary read and write.
reqThe associated client request.
statusResponse status, error code or 200, if no error occurred.
titleError text, or OK, if no error occurred.
Examples:
httpd/httpserv.c.

References fprintf(), fprintf_P, gmtime(), HTTP_MAJOR_VERSION, HTTP_MINOR_VERSION, HTTP_OF_USE_HOST_TIME, NULL, NutVersionString(), prog_char, Rfc1123TimeString(), and time().

Referenced by NutHttpSendError(), and ShowForm().

Here is the call graph for this function:

void NutHttpSendHeaderBottom ( FILE stream,
REQUEST req,
char *  mime_type,
long  bytes 
)

Send bottom lines of a standard HTML header.

Sends Content-Type, Content-Lenght and Connection lines.

Parameters:
streamStream of the socket connection, previously opened for binary read and write.
mime_typePoints to a string that specifies the content type. Examples are "text/html", "image/png", "image/gif", "video/mpeg" or "text/css". A null pointer is ignored.
bytesContent length of the data following this header. Ignored, if negative.
Examples:
httpd/httpserv.c.

Referenced by NutHttpSendError(), NutHttpSendHeaderBot(), and ShowForm().

void NutHttpSendHeaderBot ( FILE stream,
char *  mime_type,
long  bytes 
)

Send bottom lines of a standard HTML header.

Sends Content-Type and Content-Length.

Deprecated:
Use NutHttpSendHeaderBottom().
Parameters:
streamStream of the socket connection, previously opened for binary read and write.
mime_typePoints to a string that specifies the content type. Examples are "text/html", "image/png", "image/gif", "video/mpeg" or "text/css". A null pointer is ignored.
bytesContent length of the data following this header. Ignored, if negative.

References NutHttpSendHeaderBottom().

Here is the call graph for this function:

void NutHttpSendError ( FILE stream,
REQUEST req,
int  status 
)

Send a HTTP error response.

A canned error file is used.

Parameters:
streamStream of the socket connection, previously opened for binary read and write.
reqContains the HTTP request.
statusError code to be returned.

References fprintf_P, HTTP_CONN_CLOSE, NULL, NutHttpSendHeaderBottom(), NutHttpSendHeaderTop(), prog_char, _REQUEST::req_connection, _REQUEST::req_url, and strrchr().

Referenced by NutCgiProcessRequest(), and NutHttpProcessRequest().

Here is the call graph for this function:

char * NutGetMimeType ( char *  name)

Return the mime type description of a specified file name.

The mime type returned is based on the file extension.

Parameters:
nameName of the file.
Returns:
A pointer to a static string, containing the associated mime type description. If the extension is not registered, "text/plain; charset=iso-8859-1" is returned. If the filename is empty, then "text/html; charset=iso-8859-1" is returned.

References _MIMETYPES::mtyp_type.

void * NutGetMimeHandler ( char *  name)

Return the mime type handler of a specified file name.

This is the function that handles / sends a specific file type to the client. Specially used for server side includes (shtml files)

Parameters:
nameName of the file.
Returns:
A pointer to a function of the type void (u_char * filename) If the extension is not registered, the handler for "text/plain; charset=iso-8859-1" is returned. If the filename is empty, then the handler for "text/html; charset=iso-8859-1" is returned.

References _MIMETYPES::mtyp_handler.

uint8_t NutSetMimeHandler ( char *  extension,
void(*)(FILE *stream, int fd, int file_len, char *http_root, REQUEST *req)  handler 
)

Set the mime type handler for a specified file extension.

This is the function that handles / sends a specific file type to the client. Specially used for server side includes (shtml files)

Parameters:
extensionFilename extension the handler should be registered for
handlerpointer to a function of the type void (u_char filename)
Returns:
1 on error or 0 on success

References _MIMETYPES::mtyp_ext, _MIMETYPES::mtyp_handler, NULL, and strcasecmp.

Referenced by NutRegisterAsp(), and NutRegisterSsi().

int NutHttpAuthValidate ( REQUEST req)

Validate an authorization request.

Note:
This function is automatically called by the HTTP library on incoming requests. Applications do not need to call this function.
Parameters:
reqRequest to be checked.
Returns:
0, if access granted, -1 otherwise.

References NutDecodeBase64(), rc, _REQUEST::req_auth, _REQUEST::req_url, strncmp(), and strrchr().

Here is the call graph for this function:

int NutRegisterAuth ( const char *  dirname,
const char *  login 
)

Register an authorization entry.

Protect a specified directory from unauthorized access.

Warning:
Directories not registered by this function are accessible by anyone.
Parameters:
dirnameName of the directory to protect.
loginRequired login to access this directory. This string must contain a user name, followed by a colon followed by an uncrypted password.
Returns:
0 on success, -1 otherwise.
Examples:
httpd/httpserv.c.

References _AUTHINFO::auth_dirname, _AUTHINFO::auth_login, _AUTHINFO::auth_next, authList, free(), malloc(), NULL, and strdup().

Referenced by main().

Here is the call graph for this function:

void NutClearAuth ( void  )

Clear all authorization entries.

Clears all authorization entries and frees the used ressouces.

References _AUTHINFO::auth_dirname, _AUTHINFO::auth_login, _AUTHINFO::auth_next, authList, and free().

Here is the call graph for this function:

void NutRegisterSsi ( void  )

Register SSI handler for shtml files.

shtml files may use the following ssi commands:

Examples:
httpd/httpserv.c.

References NutSetMimeHandler().

Referenced by main().

Here is the call graph for this function:

void NutHttpProcessAsp ( FILE stream,
int  fd,
int  file_len,
char *  http_root,
REQUEST req 
)

References _filelength(), _read(), ASP_STATE_COPY_FUNC, ASP_STATE_IDLE, ASP_STATE_START, free(), fwrite(), malloc(), MAX_ASP_FUNC_SIZE, MAX_BUFFER_SIZE, and NULL.

Referenced by NutRegisterAsp().

Here is the call graph for this function:

int NutRegisterAspCallback ( int(*)(char *, FILE *)  func)

References NULL.

Referenced by main().

void NutRegisterAsp ( void  )

Register ASP handler for asp files.

asp files may use the following syntax:

<my_function%>

References NutHttpProcessAsp(), and NutSetMimeHandler().

Referenced by main().

Here is the call graph for this function:

void NutRegisterCgiBinPath ( char *  path)

Register a new cgi-bin path.

This function allows to redfine the cgi-bin path. Default is "cgi-bin/"

Parameters:
pathNew path.

References cgiBinPath, free(), and strdup().

Referenced by main().

Here is the call graph for this function:

int NutCgiCheckRequest ( FILE stream,
REQUEST req 
)

Check if request is a cgi call.

This functions checks the request if it's a cgi all and in case calls the cgi

Parameters:
streamStream of the socket connection, previously opened for binary read and write.
reqContains the HTTP request.

References cgiBinPath, NutCgiProcessRequest(), _REQUEST::req_url, and strncasecmp.

Here is the call graph for this function:

int NutRegisterCgi ( char *  name,
int(*)(FILE *, REQUEST *)  func 
)

Register a CGI function.

Parameters:
nameName of this CGI function. No dublicates allowed
funcThe function to be called, if the client requests the specified name.
Returns:
0 on success, -1 otherwise.

References _CGIFUNCTION::cgi_func, _CGIFUNCTION::cgi_name, _CGIFUNCTION::cgi_next, cgiFunctionList, malloc(), NULL, strcmp(), and strdup().

Referenced by main(), and upnp_Init().

Here is the call graph for this function:

void NutCgiProcessRequest ( FILE stream,
REQUEST req,
int  name_pos 
)

Process an incoming CGI request.

Applications do not need to call this function. It is automatically called by NutHttpProcessRequest().

Parameters:
streamStream of the socket connection, previously opened for binary read and write.
reqContains the HTTP request.

References _CGIFUNCTION::cgi_func, _CGIFUNCTION::cgi_name, _CGIFUNCTION::cgi_next, METHOD_GET, METHOD_POST, NutHttpSendError(), _REQUEST::req_method, _REQUEST::req_url, and strcasecmp.

Referenced by NutCgiCheckRequest().

Here is the call graph for this function:

void NutHttpURLDecode ( char *  str)

URLDecodes a string.

Takes a url-encoded string and decodes it.

Parameters:
strString to decode. This is overwritten with the decoded string
Warning:
To save RAM, the str parameter will be overwritten with the encoded string.

References strtol().

Referenced by NutHttpProcessPostQuery(), and NutHttpProcessQueryString().

Here is the call graph for this function:

int NutRegisterHttpRoot ( char *  path)

Register the HTTP server's root directory.

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

Parameters:
pathPathname of the root directory. Must include the device name followed by a colon followed by a directory path followed by a trailing slash.
Returns:
0 on success, -1 otherwise.

References free(), malloc(), NULL, strcpy(), and strlen().

Referenced by main().

Here is the call graph for this function:

void NutHttpSetOptionFlags ( uint32_t  flags)

Set HTTP option flags.

Parameters:
flagsOption flags to set. Any of the following may be or'ed:
  • HTTP_OF_USE_HOST_TIME Date header will be included in response.
  • HTTP_OF_USE_FILE_TIME Handle file modification time.
uint32_t NutHttpGetOptionFlags ( void  )

Retrieve HTTP option flags.

Returns:
Option flags.
char* NutHttpURLEncode ( char *  str)

URLEncodes a string.

Parameters:
strString to encode
Returns:
A new allocated encoded string, or NULL if str is null, or if there's not enough memory for the new string.
Note:
Remember to free() to the returned string.

References malloc(), NULL, and strlen().

Here is the call graph for this function:

void NutHttpProcessPostQuery ( FILE stream,
REQUEST req 
)

Parses the QueryString.

Reads the query from input stream and parses it into name/value table. To save RAM, this method allocated ram and uses req_query to store the input data. Then it creates a table of pointers into the req_query buffer.

Parameters:
streamInput stream
reqRequest object to parse

References fread(), free(), malloc(), memset(), METHOD_POST, NULL, NutHttpURLDecode(), _REQUEST::req_length, _REQUEST::req_method, _REQUEST::req_numqptrs, _REQUEST::req_qptrs, and _REQUEST::req_query.

Here is the call graph for this function:

char* NutHttpGetParameter ( REQUEST req,
char *  name 
)

Gets a request parameter value by name.

Parameters:
reqRequest object
nameName of parameter
Returns:
Pointer to the parameter value.

References NULL, _REQUEST::req_numqptrs, _REQUEST::req_qptrs, and strcmp().

Here is the call graph for this function:

int NutHttpGetParameterCount ( REQUEST req)

Gets the number of request parameters.

The following code fragment retrieves all name/value pairs of the request.

 #include &lt;pro/httpd.h&gt;

 char *name;
 char *value;
 int i;
 int n = NutHttpGetParameterCount(req);

 for (i = 0; i &lt; n; i++) {
     name = NutHttpGetParameterName(req, i);
     value = NutHttpGetParameterValue(req, i);
 }
Parameters:
reqRequest object
Returns:
The number of request parameters

References _REQUEST::req_numqptrs.

Referenced by NutHttpGetParameterName(), NutHttpGetParameterValue(), and ShowForm().

char* NutHttpGetParameterName ( REQUEST req,
int  index 
)

Gets the name of a request parameter.

Parameters:
reqRequest object
indexIndex of the requested parameter.
Returns:
Pointer to the parameter name at the given index, or NULL if index is out of range.

References NULL, NutHttpGetParameterCount(), and _REQUEST::req_qptrs.

Referenced by ShowForm().

Here is the call graph for this function:

char* NutHttpGetParameterValue ( REQUEST req,
int  index 
)

Get the value of a request parameter.

Parameters:
reqRequest object
indexIndex to the requested parameter.
Returns:
Pointer to the parameter value at the given index, or NULL if index is out of range.

References NULL, NutHttpGetParameterCount(), and _REQUEST::req_qptrs.

Referenced by ShowForm().

Here is the call graph for this function:

int NutRegisterSsiVarHandler ( char *(*)(char *name, REQUEST *req)  handler)

Register SSI handler for variables.


Variable Documentation

Global network configuration structure.

Contains the current network configuration. Nut/Net will load this structure from non-volatile memory during initialization.

char* cgiBinPath = NULL
Initial value:
 {
    {
    ".txt", "text/plain", NULL}, {
    ".html", "text/html", NULL}, {
    ".shtml", "text/html", NULL}, {
    ".asp", "text/html", NULL}, {
    ".htm", "text/html", NULL}, {
    ".gif", "image/gif", NULL}, {
    ".jpg", "image/jpeg", NULL}, {
    ".png", "image/png", NULL}, {
    ".bmp", "image/bmp", NULL}, {
    ".pdf", "application/pdf", NULL}, {
    ".js",  "application/x-javascript", NULL}, {
    ".jar", "application/x-java-archive", NULL}, {
    ".css", "text/css", NULL}, {
    ".xml", "text/xml", NULL}, {
    ".svg", "image/svg+xml", NULL}, {
    NULL, NULL, NULL}
}

Known mime types.

Known mime types.

char* http_root