- Notifications
You must be signed in to change notification settings - Fork 28
These macros should be customized according to your application by yourself.
MAX_HEADER_SIZE 1024
It is the maximum buffer size in bytes for each HTTP message header.
MAX_HEADER_SIZE + MAX_BODY_SIZE is the total buffer size in byes for each HTTP message.MAX_BODY_SIZE 2048
It is the maximum buffer size in bytes for each HTTP message body.
MAX_HEADER_SIZE + MAX_BODY_SIZE is the total buffer size in byes for each HTTP message.MHS_PORT 8001
Default listening port of Micro HTTP Server.
MAX_HTTP_CLIENT 5
The maximum number of clients could connect at the same time.
MAX_HEADER_FIELDS 20
The maximum number of the HTTP message header feilds.
typedefintSOCKET;Define socket datatype.
typedefstruct_HTTPServer {
SOCKETsock;
SOCKET_max_sock;
fd_set_read_sock_pool;
fd_set_write_sock_pool;
} HTTPServer;- sock: The HTTP server listening socket.
- _max_sock: This should always be the maximum file descriptor number of usable sockets in HTTP server application.
- _read_sock_pool: It is the file descriptor set that the HTTP server application may to read.
- _write_sock_pool: It is the file descriptor set that the HTTP server application may to write.
typedefstruct_HTTPHeaderField {
char*key;
char*value;
} HTTPHeaderField;It is one HTTP message header key-value pair field.
- key: The character string pointer of one HTTP message header field name.
- value: The character string of one HTTP message header field value.
typedefenum {
HTTP_GET,
HTTP_POST,
HTTP_PUT,
HTTP_DELETE,
HTTP_NUM_METHOD
} HTTPMethod;List the HTTP request methods.
HTTP_NUM_MEHTOD will be the amount of kinds request method.
typedefstruct_HTTPReqHeader {
HTTPMethodMethod;
char*URI;
char*Version;
HTTPHeaderFieldFields[MAX_HEADER_FIELDS];
unsigned intAmount;
} HTTPReqHeader;It is HTTP request message header.
- Method: Represents the method of this request.
- URI: The character string pointer of the URI of this request.
- Version: The character string pointer represents the HTTP version of this request.
- Fields: The HTTPHeaderField array records HTTP request message header fields one by one.
- Amount: The total amount of this HTTP request message header fields.
typedefstruct_HTTPReqMessage {
HTTPReqHeaderHeader;
uint8_t*Body;
uint8_t*_buf;
uint16_t_index;
} HTTPReqMessage;It is whole HTTP request message.
- Header: The HTTP request message header.
- Body: The character string pointer of this HTTP request message body.
- _buf: The real buffer where the HTTP request message located which is a character string pointer.
- _index: The length of the real buffer in bytes.
typedefstruct_HTTPResHeader {
char*Version;
char*StatusCode;
char*Description;
HTTPHeaderFieldFields[MAX_HEADER_FIELDS];
unsigned intAmount;
} HTTPResHeader;It is HTTP respnose message header.
- Version: The character string pointer represents the HTTP version of this response.
- StatusCode: The character string pointer of the HTTP status code of this response.
- Description: The character string pointer of the description of the status code of this response.
- Fields: The HTTPHeaderField array records HTTP response message header fields one by one.
- Amount: The total amount of this HTTP response message header fields.
PS. HTTPResHeader is not being used for now. Members in HTTPResHeader have not been mapped by teh Micro HTTP Server library, because ones may have the HTTP response string directly to have better performance.
typedefstruct_HTTPResMessage {
HTTPResHeaderHeader;
uint8_t*Body;
uint8_t*_buf;
uint16_t_index;
} HTTPResMessage;It is whole HTTP response message.
- Header: The HTTP response message header.
- Body: The character string pointer of this HTTP response message body.
- _buf: The real buffer where the HTTP response message located which is a character string pointer.
- _index: The length of the real buffer in bytes.
typedefvoid (*HTTPREQ_CALLBACK)(HTTPReqMessage*req, HTTPResMessage*res);The callback function pointer type of the HTTP request.
- req: The parsed HTTP request message.
- res: The HTTP response message.
PS. Both req and res have allocated memory spaces for buffers by the Micro HTTP Server library.
voidHTTPServerInit(HTTPServer*srv, uint16_tport);Initial Micro HTTP Server with designated listening port.
- srv: The pointer of HTTP server instance.
- port: Server listening port number.
voidHTTPServerRun(HTTPServer*srv, HTTPREQ_CALLBACKcallback);or
#defineHTTPServerRunLoop(srv, callback) { \
while(1) { \
HTTPServerRun(srv, callback); \
}}HTTPServerRun will check the server socket and all of interesting client sockets, then parse the requests and send the responses one time.
HTTPServerRunLoop will do the works that HTTPServerRun does forever.
- srv: The pointer of HTTP server instance.
- callback: The callback function will be executed when HTTP server gets a new HTTP request message.
voidHTTPServerClose(HTTPServer*srv);Close the HTTP server.
- srv: The pointer of HTTP server instance.
These macros should be customized according to your application by yourself.
MAX_HTTP_ROUTES 10
Amount of HTTP URI routes.
STATIC_FILE_FOLDER "static/"
Static files' sub-directory path.
typedefHTTPREQ_CALLBACKSAF;Define server application function (SAF) datatype.
intAddRoute(HTTPMethodmethod, char*uri, SAFsaf);Add a request method, URI and the corresponding server application function (SAF) into the route table.
- method: HTTP request method.
- uri: HTTP request URI.
- saf: The server application function (SAF) will be executed when HTTP server gets a matech HTTP request method and URI.
voidDispatch(HTTPReqMessage*req, HTTPResMessage*res);Dispatch the HTTP method and URI according to the route table.
- req: The parsed HTTP request message.
- res: The HTTP response message.