HTTP
[User Protocols]

Collaboration diagram for HTTP:


Detailed Description

Hypertext transfer protocol.

A read-only file system is sufficient to run an HTTP server.


Data Structures

struct  _REQUEST
 HTTP request information structure. More...
struct  _AUTHINFO
 HTTP authorization information structure. More...
struct  _CGIFUNCTION
 Registered CGI function. More...
struct  _REQUEST
 HTTP request information structure. More...
struct  _AUTHINFO
 HTTP authorization information structure. More...
struct  _CGIFUNCTION
 Registered CGI function. More...

Defines

#define METHOD_GET   1
#define METHOD_POST   2
#define METHOD_HEAD   3
#define MAX_BUFFER_SIZE   256
#define MAX_ASP_FUNC_SIZE   64
#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

Typedefs

typedef _REQUEST REQUEST
typedef typedef__END_DECLS
struct _AUTHINFO 
AUTHINFO
typedef typedef__END_DECLS
struct _CGIFUNCTION 
CGIFUNCTION

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 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.
u_char 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.
int NutRegisterCgi (char *name, int(*func)(FILE *, REQUEST *))
 Register a CGI function.
void NutCgiProcessRequest (FILE *stream, REQUEST *req)
 Process an incoming CGI request.
char * NutHttpURLEncode (char *str)
void NutHttpURLDecode (char *str)
 URLDecodes 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 paramter.
int NutRegisterHttpRoot (char *path)
 Register the HTTP server's root directory.

Variables

CONFNET confnet
 Global network configuration structure.
AUTHINFOauthList = 0
CGIFUNCTION *volatile cgiFunctionList = 0


Define Documentation

#define METHOD_GET   1

Examples:
httpd/httpserv.c.

Definition at line 77 of file httpd.h.

Referenced by NutCgiProcessRequest().

#define METHOD_POST   2

Examples:
httpd/httpserv.c.

Definition at line 78 of file httpd.h.

Referenced by NutCgiProcessRequest(), and NutHttpProcessPostQuery().

#define METHOD_HEAD   3

Examples:
httpd/httpserv.c.

Definition at line 79 of file httpd.h.

#define MAX_BUFFER_SIZE   256

Definition at line 79 of file asp.c.

Referenced by NutHttpProcessAsp().

#define MAX_ASP_FUNC_SIZE   64

Definition at line 85 of file asp.c.

Referenced by NutHttpProcessAsp().

#define BUFSIZE   512

Definition at line 69 of file ssi.c.

#define MIN ( a,
 )     (a<b?a:b)

Definition at line 71 of file ssi.c.

#define SSI_TYPE_FILE   0x01

Definition at line 73 of file ssi.c.

#define SSI_TYPE_VIRTUAL   0x02

Definition at line 74 of file ssi.c.

#define SSI_TYPE_EXEC   0x03

Definition at line 75 of file ssi.c.


Typedef Documentation

typedef struct _REQUEST REQUEST

Definition at line 81 of file httpd.h.

typedef typedef__END_DECLS struct _AUTHINFO AUTHINFO

Definition at line 116 of file httpd.h.

typedef typedef__END_DECLS struct _CGIFUNCTION CGIFUNCTION

Definition at line 139 of file httpd.h.


Enumeration Type Documentation

anonymous enum

Enumerator:
ASP_STATE_IDLE 
ASP_STATE_START 
ASP_STATE_COPY_FUNC 

Definition at line 87 of file asp.c.


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:
stream Stream of the socket connection, previously opened for binary read and write.
Examples:
httpd/httpserv.c.

Definition at line 801 of file httpd.c.

References atoi(), fgets(), NutHeapAlloc(), NutHeapFree(), _REQUEST::req_agent, _REQUEST::req_auth, _REQUEST::req_cookie, _REQUEST::req_length, _REQUEST::req_type, strchr(), strcpy(), strlen(), and strncasecmp.

Referenced by Service().

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:
req Request object to parse

Definition at line 409 of file httpd.c.

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

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:
stream Stream of the socket connection, previously opened for binary read and write.
req The associated client request.
status Response status, error code or 200, if no error occured.
title Error text, or OK, if no error occured.
Examples:
httpd/httpserv.c.

Definition at line 154 of file httpd.c.

References fprintf_P, NutVersionString(), prog_char, and _REQUEST::req_version.

Referenced by NutHttpSendError(), and ShowForm().

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

Send bottom lines of a standard HTML header.

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

Parameters:
stream Stream of the socket connection, previously opened for binary read and write.
mime_type Points 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.
bytes Content length of the data following this header. Ignored, if negative.
Examples:
httpd/httpserv.c.

Definition at line 175 of file httpd.c.

References fprintf_P, fputs_P, and prog_char.

Referenced by NutHttpSendError(), and ShowForm().

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

Send a HTTP error response.

A canned error file is used.

Parameters:
stream Stream of the socket connection, previously opened for binary read and write.
req Contains the HTTP request.
status Error code to be returned.

Definition at line 198 of file httpd.c.

References fprintf_P, NutHttpSendHeaderBot(), NutHttpSendHeaderTop(), prog_char, _REQUEST::req_url, and strrchr().

Referenced by NutCgiProcessRequest().

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:
name Name 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.

Definition at line 256 of file httpd.c.

References ext, strcasecmp, strlen(), and 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. Escpecialy used for server side includes (shtml files)

Parameters:
name Name 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.

Definition at line 284 of file httpd.c.

References ext, handler, strcasecmp, and strlen().

u_char 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. Escpecialy used for server side includes (shtml files)

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

Definition at line 308 of file httpd.c.

References ext, 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:
req Request to be checked.
Returns:
0, if access granted, -1 otherwise.

Definition at line 155 of file auth.c.

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

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:
dirname Name of the directory to protect.
login Required 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.

Definition at line 99 of file auth.c.

References _AUTHINFO::auth_next, authList, NutHeapAlloc(), NutHeapFree(), strcpy(), and strlen().

Referenced by main().

void NutClearAuth ( void   ) 

Clear all authorization entries.

Clears all authorization entries and frees the used ressouces.

Definition at line 131 of file auth.c.

References authList, and NutHeapFree().

void NutRegisterSsi ( void   ) 

Register SSI handler for shtml files.

shtml files may use the following ssi commands:

Examples:
httpd/httpserv.c.

Definition at line 522 of file ssi.c.

References NutSetMimeHandler().

Referenced by main().

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

Definition at line 147 of file asp.c.

References _filelength(), _read(), ASP_STATE_COPY_FUNC, ASP_STATE_IDLE, ASP_STATE_START, fwrite(), MAX_ASP_FUNC_SIZE, MAX_BUFFER_SIZE, NutHeapAlloc(), and NutHeapFree().

Referenced by NutRegisterAsp().

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

Definition at line 298 of file asp.c.

Referenced by main().

void NutRegisterAsp ( void   ) 

Register ASP handler for asp files.

asp files may use the following syntax:

<my_function%>

Definition at line 319 of file asp.c.

References NutHttpProcessAsp(), and NutSetMimeHandler().

Referenced by main().

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

Register a CGI function.

Todo:
Duplicate names not checked.
Parameters:
name Name of this CGI function.
func The function to be called, if the client requests the specified name.
Returns:
0 on success, -1 otherwise.

Definition at line 72 of file cgi.c.

References _CGIFUNCTION::cgi_next, cgiFunctionList, and NutHeapAlloc().

Referenced by main().

void NutCgiProcessRequest ( FILE stream,
REQUEST req 
)

Process an incoming CGI request.

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

Parameters:
stream Stream of the socket connection, previously opened for binary read and write.
req Contains the HTTP request.

Definition at line 96 of file cgi.c.

References cgiFunctionList, METHOD_GET, METHOD_POST, NutHttpSendError(), _REQUEST::req_method, _REQUEST::req_url, and strcasecmp.

char* NutHttpURLEncode ( char *  str  ) 

Definition at line 334 of file httpd.c.

References NutHeapAlloc(), and strlen().

void NutHttpURLDecode ( char *  str  ) 

URLDecodes a string.

Takes a url-encoded string and decodes it.

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

Definition at line 380 of file httpd.c.

References strtol().

Referenced by NutHttpProcessPostQuery(), and NutHttpProcessQueryString().

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:
stream Input stream
req Request object to parse

Definition at line 464 of file httpd.c.

References fread(), METHOD_POST, NutHeapAlloc(), NutHeapAllocClear(), NutHeapFree(), NutHttpURLDecode(), _REQUEST::req_length, _REQUEST::req_method, _REQUEST::req_numqptrs, _REQUEST::req_qptrs, and _REQUEST::req_query.

char* NutHttpGetParameter ( REQUEST req,
char *  name 
)

Gets a request parameter value by name.

Parameters:
req Request object
name Name of parameter
Returns:
Pointer to the parameter value.

Definition at line 533 of file httpd.c.

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

int NutHttpGetParameterCount ( REQUEST req  ) 

Gets the number of request parameters.

Parameters:
req Request object
Returns:
The number of request parameters

Definition at line 549 of file httpd.c.

References _REQUEST::req_numqptrs.

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

char* NutHttpGetParameterName ( REQUEST req,
int  index 
)

Gets the name of a request parameter.

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

Definition at line 563 of file httpd.c.

References NutHttpGetParameterCount(), and _REQUEST::req_qptrs.

Referenced by ShowForm().

char* NutHttpGetParameterValue ( REQUEST req,
int  index 
)

Get the value of a request paramter.

Parameters:
req Request object
index Index to the requested parameter.
Returns:
Pointer to the paramter value at the given index, or NULL if index is out of range.

Definition at line 579 of file httpd.c.

References NutHttpGetParameterCount(), and _REQUEST::req_qptrs.

Referenced by ShowForm().

int NutRegisterHttpRoot ( char *  path  ) 

Register the HTTP server's root directory.

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

Parameters:
path Pathname 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.

Definition at line 777 of file httpd.c.

References NutHeapAlloc(), NutHeapFree(), strcpy(), and strlen().

Referenced by main().


Variable Documentation

CONFNET confnet

Global network configuration structure.

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

Definition at line 85 of file confnet.c.

AUTHINFO* authList = 0

Definition at line 65 of file auth.c.

Referenced by NutClearAuth(), and NutRegisterAuth().

CGIFUNCTION* volatile cgiFunctionList = 0

Definition at line 59 of file cgi.c.

Referenced by NutCgiProcessRequest(), and NutRegisterCgi().

char* { ... } ext [inherited]

Definition at line 122 of file httpd.c.

char* { ... } type [inherited]

Definition at line 123 of file httpd.c.

void(* { ... } handler)(FILE *stream, int fd, int file_len, char *http_root, REQUEST *req) [inherited]


© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/