Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers http_server.h Source File

http_server.h

Go to the documentation of this file.
00001 /**
00002  * @file http_server.h
00003  * @brief HTTP server (HyperText Transfer Protocol)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _HTTP_SERVER_H
00030 #define _HTTP_SERVER_H
00031 
00032 //Dependencies
00033 #include "os_port.h"
00034 #include "core/socket.h"
00035 #include "web_socket/web_socket.h"
00036 
00037 //HTTP server support
00038 #ifndef HTTP_SERVER_SUPPORT
00039    #define HTTP_SERVER_SUPPORT ENABLED
00040 #elif (HTTP_SERVER_SUPPORT != ENABLED && HTTP_SERVER_SUPPORT != DISABLED)
00041    #error HTTP_SERVER_SUPPORT parameter is not valid
00042 #endif
00043 
00044 //Support for persistent connections
00045 #ifndef HTTP_SERVER_PERSISTENT_CONN_SUPPORT
00046    #define HTTP_SERVER_PERSISTENT_CONN_SUPPORT DISABLED
00047 #elif (HTTP_SERVER_PERSISTENT_CONN_SUPPORT != ENABLED && HTTP_SERVER_PERSISTENT_CONN_SUPPORT != DISABLED)
00048    #error HTTP_SERVER_PERSISTENT_CONN_SUPPORT parameter is not valid
00049 #endif
00050 
00051 //File system support
00052 #ifndef HTTP_SERVER_FS_SUPPORT
00053    #define HTTP_SERVER_FS_SUPPORT DISABLED
00054 #elif (HTTP_SERVER_FS_SUPPORT != ENABLED && HTTP_SERVER_FS_SUPPORT != DISABLED)
00055    #error HTTP_SERVER_FS_SUPPORT parameter is not valid
00056 #endif
00057 
00058 //Server Side Includes support
00059 #ifndef HTTP_SERVER_SSI_SUPPORT
00060    #define HTTP_SERVER_SSI_SUPPORT DISABLED
00061 #elif (HTTP_SERVER_SSI_SUPPORT != ENABLED && HTTP_SERVER_SSI_SUPPORT != DISABLED)
00062    #error HTTP_SERVER_SSI_SUPPORT parameter is not valid
00063 #endif
00064 
00065 //HTTP over SSL/TLS
00066 #ifndef HTTP_SERVER_TLS_SUPPORT
00067    #define HTTP_SERVER_TLS_SUPPORT DISABLED
00068 #elif (HTTP_SERVER_TLS_SUPPORT != ENABLED && HTTP_SERVER_TLS_SUPPORT != DISABLED)
00069    #error HTTP_SERVER_TLS_SUPPORT parameter is not valid
00070 #endif
00071 
00072 //Basic access authentication support
00073 #ifndef HTTP_SERVER_BASIC_AUTH_SUPPORT
00074    #define HTTP_SERVER_BASIC_AUTH_SUPPORT DISABLED
00075 #elif (HTTP_SERVER_BASIC_AUTH_SUPPORT != ENABLED && HTTP_SERVER_BASIC_AUTH_SUPPORT != DISABLED)
00076    #error HTTP_SERVER_BASIC_AUTH_SUPPORT parameter is not valid
00077 #endif
00078 
00079 //Digest access authentication support
00080 #ifndef HTTP_SERVER_DIGEST_AUTH_SUPPORT
00081    #define HTTP_SERVER_DIGEST_AUTH_SUPPORT DISABLED
00082 #elif (HTTP_SERVER_DIGEST_AUTH_SUPPORT != ENABLED && HTTP_SERVER_DIGEST_AUTH_SUPPORT != DISABLED)
00083    #error HTTP_SERVER_DIGEST_AUTH_SUPPORT parameter is not valid
00084 #endif
00085 
00086 //WebSocket support
00087 #ifndef HTTP_SERVER_WEB_SOCKET_SUPPORT
00088    #define HTTP_SERVER_WEB_SOCKET_SUPPORT DISABLED
00089 #elif (HTTP_SERVER_WEB_SOCKET_SUPPORT != ENABLED && HTTP_SERVER_WEB_SOCKET_SUPPORT != DISABLED)
00090    #error HTTP_SERVER_WEB_SOCKET_SUPPORT parameter is not valid
00091 #endif
00092 
00093 //Multipart content type support
00094 #ifndef HTTP_SERVER_MULTIPART_TYPE_SUPPORT
00095    #define HTTP_SERVER_MULTIPART_TYPE_SUPPORT DISABLED
00096 #elif (HTTP_SERVER_MULTIPART_TYPE_SUPPORT != ENABLED && HTTP_SERVER_MULTIPART_TYPE_SUPPORT != DISABLED)
00097    #error HTTP_SERVER_MULTIPART_TYPE_SUPPORT parameter is not valid
00098 #endif
00099 
00100 //Stack size required to run the HTTP server
00101 #ifndef HTTP_SERVER_STACK_SIZE
00102    #define HTTP_SERVER_STACK_SIZE 650
00103 #elif (HTTP_SERVER_STACK_SIZE < 1)
00104    #error HTTP_SERVER_STACK_SIZE parameter is not valid
00105 #endif
00106 
00107 //Priority at which the HTTP server should run
00108 #ifndef HTTP_SERVER_PRIORITY
00109    #define HTTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
00110 #endif
00111 
00112 //HTTP connection timeout
00113 #ifndef HTTP_SERVER_TIMEOUT
00114    #define HTTP_SERVER_TIMEOUT 10000
00115 #elif (HTTP_SERVER_TIMEOUT < 1000)
00116    #error HTTP_SERVER_TIMEOUT parameter is not valid
00117 #endif
00118 
00119 //Maximum time the server will wait for a subsequent
00120 //request before closing the connection
00121 #ifndef HTTP_SERVER_IDLE_TIMEOUT
00122    #define HTTP_SERVER_IDLE_TIMEOUT 5000
00123 #elif (HTTP_SERVER_IDLE_TIMEOUT < 1000)
00124    #error HTTP_SERVER_IDLE_TIMEOUT parameter is not valid
00125 #endif
00126 
00127 //Maximum length of the pending connection queue
00128 #ifndef HTTP_SERVER_BACKLOG
00129    #define HTTP_SERVER_BACKLOG 4
00130 #elif (HTTP_SERVER_BACKLOG < 1)
00131    #error HTTP_SERVER_BACKLOG parameter is not valid
00132 #endif
00133 
00134 //Maximum number of requests per connection
00135 #ifndef HTTP_SERVER_MAX_REQUESTS
00136    #define HTTP_SERVER_MAX_REQUESTS 1000
00137 #elif (HTTP_SERVER_MAX_REQUESTS < 1)
00138    #error HTTP_SERVER_MAX_REQUESTS parameter is not valid
00139 #endif
00140 
00141 //Size of buffer used for input/output operations
00142 #ifndef HTTP_SERVER_BUFFER_SIZE
00143    #define HTTP_SERVER_BUFFER_SIZE 1024
00144 #elif (HTTP_SERVER_BUFFER_SIZE < 128)
00145    #error HTTP_SERVER_BUFFER_SIZE parameter is not valid
00146 #endif
00147 
00148 //Maximum size of root directory
00149 #ifndef HTTP_SERVER_ROOT_DIR_MAX_LEN
00150    #define HTTP_SERVER_ROOT_DIR_MAX_LEN 31
00151 #elif (HTTP_SERVER_ROOT_DIR_MAX_LEN < 7)
00152    #error HTTP_SERVER_ROOT_DIR_MAX_LEN parameter is not valid
00153 #endif
00154 
00155 //Maximum size of default index file
00156 #ifndef HTTP_SERVER_DEFAULT_DOC_MAX_LEN
00157    #define HTTP_SERVER_DEFAULT_DOC_MAX_LEN 31
00158 #elif (HTTP_SERVER_DEFAULT_DOC_MAX_LEN < 7)
00159    #error HTTP_SERVER_DEFAULT_DOC_MAX_LEN parameter is not valid
00160 #endif
00161 
00162 //Maximum length of HTTP method
00163 #ifndef HTTP_SERVER_METHOD_MAX_LEN
00164    #define HTTP_SERVER_METHOD_MAX_LEN 7
00165 #elif (HTTP_SERVER_METHOD_MAX_LEN < 1)
00166    #error HTTP_SERVER_METHOD_MAX_LEN parameter is not valid
00167 #endif
00168 
00169 //Maximum length of URI
00170 #ifndef HTTP_SERVER_URI_MAX_LEN
00171    #define HTTP_SERVER_URI_MAX_LEN 255
00172 #elif (HTTP_SERVER_URI_MAX_LEN < 31)
00173    #error HTTP_SERVER_URI_MAX_LEN parameter is not valid
00174 #endif
00175 
00176 //Maximum length of query strings
00177 #ifndef HTTP_SERVER_QUERY_STRING_MAX_LEN
00178    #define HTTP_SERVER_QUERY_STRING_MAX_LEN 255
00179 #elif (HTTP_SERVER_QUERY_STRING_MAX_LEN < 7)
00180    #error HTTP_SERVER_QUERY_STRING_MAX_LEN parameter is not valid
00181 #endif
00182 
00183 //Maximum host name length
00184 #ifndef HTTP_SERVER_HOST_MAX_LEN
00185    #define HTTP_SERVER_HOST_MAX_LEN 31
00186 #elif (HTTP_SERVER_HOST_MAX_LEN < 7)
00187    #error HTTP_SERVER_HOST_MAX_LEN parameter is not valid
00188 #endif
00189 
00190 //Maximum user name length
00191 #ifndef HTTP_SERVER_USERNAME_MAX_LEN
00192    #define HTTP_SERVER_USERNAME_MAX_LEN 31
00193 #elif (HTTP_SERVER_USERNAME_MAX_LEN < 7)
00194    #error HTTP_SERVER_USERNAME_MAX_LEN parameter is not valid
00195 #endif
00196 
00197 //Maximum length of CGI parameters
00198 #ifndef HTTP_SERVER_CGI_PARAM_MAX_LEN
00199    #define HTTP_SERVER_CGI_PARAM_MAX_LEN 31
00200 #elif (HTTP_SERVER_CGI_PARAM_MAX_LEN < 7)
00201    #error HTTP_SERVER_CGI_PARAM_MAX_LEN parameter is not valid
00202 #endif
00203 
00204 //Maximum recursion limit
00205 #ifndef HTTP_SERVER_SSI_MAX_RECURSION
00206    #define HTTP_SERVER_SSI_MAX_RECURSION 3
00207 #elif (HTTP_SERVER_SSI_MAX_RECURSION < 1 || HTTP_SERVER_SSI_MAX_RECURSION > 8)
00208    #error HTTP_SERVER_SSI_MAX_RECURSION parameter is not valid
00209 #endif
00210 
00211 //Maximum age for static resources
00212 #ifndef HTTP_SERVER_MAX_AGE
00213    #define HTTP_SERVER_MAX_AGE 0
00214 #elif (HTTP_SERVER_MAX_AGE < 0)
00215    #error HTTP_SERVER_MAX_AGE parameter is not valid
00216 #endif
00217 
00218 //Nonce cache size
00219 #ifndef HTTP_SERVER_NONCE_CACHE_SIZE
00220    #define HTTP_SERVER_NONCE_CACHE_SIZE 8
00221 #elif (HTTP_SERVER_NONCE_CACHE_SIZE < 1)
00222    #error HTTP_SERVER_NONCE_CACHE_SIZE parameter is not valid
00223 #endif
00224 
00225 //Lifetime of nonces
00226 #ifndef HTTP_SERVER_NONCE_LIFETIME
00227    #define HTTP_SERVER_NONCE_LIFETIME 60000
00228 #elif (HTTP_SERVER_NONCE_LIFETIME < 1000)
00229    #error HTTP_SERVER_NONCE_LIFETIME parameter is not valid
00230 #endif
00231 
00232 //Nonce size
00233 #ifndef HTTP_SERVER_NONCE_SIZE
00234    #define HTTP_SERVER_NONCE_SIZE 16
00235 #elif (HTTP_SERVER_NONCE_SIZE < 1)
00236    #error HTTP_SERVER_NONCE_SIZE parameter is not valid
00237 #endif
00238 
00239 //Maximum length for boundary string
00240 #ifndef HTTP_SERVER_BOUNDARY_MAX_LEN
00241    #define HTTP_SERVER_BOUNDARY_MAX_LEN 70
00242 #elif (HTTP_SERVER_BOUNDARY_MAX_LEN < 1)
00243    #error HTTP_SERVER_BOUNDARY_MAX_LEN parameter is not valid
00244 #endif
00245 
00246 //File system support?
00247 #if (HTTP_SERVER_FS_SUPPORT == ENABLED)
00248    #include "fs_port.h"
00249 #else
00250    #include "resource_manager.h"
00251 #endif
00252 
00253 //HTTP over SSL/TLS supported?
00254 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
00255    #include "crypto.h"
00256    #include "tls.h"
00257 #endif
00258 
00259 //Basic authentication supported?
00260 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
00261    #include "crypto.h"
00262    #include "base64.h"
00263 #endif
00264 
00265 //Digest authentication supported?
00266 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00267    #include "crypto.h"
00268    #include "md5.h"
00269 #endif
00270 
00271 //WebSocket supported?
00272 #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
00273    #include "crypto.h"
00274    #include "base64.h"
00275 #endif
00276 
00277 //HTTP port number
00278 #define HTTP_PORT 80
00279 //HTTPS port number (HTTP over SSL/TLS)
00280 #define HTTPS_PORT 443
00281 
00282 //Forward declaration of HttpServerContext structure
00283 struct _HttpServerContext;
00284 #define HttpServerContext struct _HttpServerContext
00285 
00286 //Forward declaration of HttpConnection structure
00287 struct _HttpConnection;
00288 #define HttpConnection struct _HttpConnection
00289 
00290 
00291 /**
00292  * @brief HTTP version numbers
00293  **/
00294 
00295 typedef enum
00296 {
00297    HTTP_VERSION_0_9 = 0x0009,
00298    HTTP_VERSION_1_0 = 0x0100,
00299    HTTP_VERSION_1_1 = 0x0101
00300 } HttpVersion;
00301 
00302 
00303 /**
00304  * @brief HTTP authentication schemes
00305  **/
00306 
00307 typedef enum
00308 {
00309    HTTP_AUTH_MODE_NONE   = 0,
00310    HTTP_AUTH_MODE_BASIC  = 1,
00311    HTTP_AUTH_MODE_DIGEST = 2
00312 } HttpAuthMode;
00313 
00314 
00315 /**
00316  * @brief Access status
00317  **/
00318 
00319 typedef enum
00320 {
00321    HTTP_ACCESS_DENIED               = 0,
00322    HTTP_ACCESS_ALLOWED              = 1,
00323    HTTP_ACCESS_BASIC_AUTH_REQUIRED  = 2,
00324    HTTP_ACCESS_DIGEST_AUTH_REQUIRED = 3
00325 } HttpAccessStatus;
00326 
00327 
00328 /**
00329  * @brief Flags used by I/O functions
00330  **/
00331 
00332 typedef enum
00333 {
00334    HTTP_FLAG_WAIT_ALL   = 0x0800,
00335    HTTP_FLAG_BREAK_CHAR = 0x1000,
00336    HTTP_FLAG_BREAK_CRLF = 0x100A,
00337    HTTP_FLAG_WAIT_ACK   = 0x2000,
00338    HTTP_FLAG_NO_DELAY   = 0x4000,
00339    HTTP_FLAG_DELAY      = 0x8000
00340 } HttpFlags;
00341 
00342 
00343 /**
00344  * @brief HTTP connection states
00345  **/
00346 
00347 typedef enum
00348 {
00349    HTTP_CONN_STATE_IDLE        = 0,
00350    HTTP_CONN_STATE_REQ_LINE    = 1,
00351    HTTP_CONN_STATE_REQ_HEADER  = 2,
00352    HTTP_CONN_STATE_REQ_BODY    = 3,
00353    HTTP_CONN_STATE_RESP_HEADER = 4,
00354    HTTP_CONN_STATE_RESP_BODY   = 5,
00355    HTTP_CONN_STATE_SHUTDOWN    = 6,
00356    HTTP_CONN_STATE_CLOSE       = 7
00357 } HttpConnState;
00358 
00359 
00360 //The HTTP_FLAG_BREAK macro causes the httpReadStream() function to stop
00361 //reading data whenever the specified break character is encountered
00362 #define HTTP_FLAG_BREAK(c) (HTTP_FLAG_BREAK_CHAR | LSB(c))
00363 
00364 
00365 //HTTP over SSL/TLS supported?
00366 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
00367 
00368 /**
00369  * @brief SSL/TLS initialization callback function
00370  **/
00371 
00372 typedef error_t (*TlsInitCallback)(HttpConnection *connection,
00373    TlsContext *tlsContext);
00374 
00375 #endif
00376 
00377 
00378 /**
00379  * @brief Random data generation callback function
00380  **/
00381 
00382 typedef error_t (*HttpRandCallback)(uint8_t *data, size_t length);
00383 
00384 
00385 /**
00386  * @brief HTTP authentication callback function
00387  **/
00388 
00389 typedef HttpAccessStatus (*HttpAuthCallback)(HttpConnection *connection,
00390    const char_t *user, const char_t *uri);
00391 
00392 
00393 /**
00394  * @brief CGI callback function
00395  **/
00396 
00397 typedef error_t (*HttpCgiCallback)(HttpConnection *connection,
00398    const char_t *param);
00399 
00400 
00401 /**
00402  * @brief HTTP request callback function
00403  **/
00404 
00405 typedef error_t (*HttpRequestCallback)(HttpConnection *connection,
00406    const char_t *uri);
00407 
00408 
00409 /**
00410  * @brief URI not found callback function
00411  **/
00412 
00413 typedef error_t (*HttpUriNotFoundCallback)(HttpConnection *connection,
00414    const char_t *uri);
00415 
00416 
00417 /**
00418  * @brief HTTP status code
00419  **/
00420 
00421 typedef struct
00422 {
00423    uint_t value;
00424    const char_t message[28];
00425 } HttpStatusCodeDesc;
00426 
00427 
00428 /**
00429  * @brief Authorization header
00430  **/
00431 
00432 typedef struct
00433 {
00434    bool_t found;                                  ///<The Authorization header has been found
00435    HttpAuthMode mode;                             ///<Authentication scheme
00436    char_t user[HTTP_SERVER_USERNAME_MAX_LEN + 1]; ///<User name
00437 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
00438    const char_t *password;                        ///<Password
00439 #endif
00440 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00441    const char_t *realm;
00442    const char_t *nonce;                           ///<Server nonce
00443    const char_t *uri;                             ///<Digest URI
00444    const char_t *qop;
00445    const char_t *nc;                              ///<Nonce count
00446    const char_t *cnonce;                          ///<Client nonce
00447    const char_t *response;
00448    const char_t *opaque;
00449 #endif
00450 } HttpAuthorizationHeader;
00451 
00452 
00453 /**
00454  * @brief Authenticate header
00455  **/
00456 
00457 typedef struct
00458 {
00459    HttpAuthMode mode; ///<Authentication scheme
00460 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00461    bool_t stale;      ///<STALE flag
00462 #endif
00463 } HttpAuthenticateHeader;
00464 
00465 
00466 /**
00467  * @brief HTTP request
00468  **/
00469 
00470 typedef struct
00471 {
00472    uint_t version;                                           ///<HTTP version number
00473    char_t method[HTTP_SERVER_METHOD_MAX_LEN + 1];            ///<HTTP method
00474    char_t uri[HTTP_SERVER_URI_MAX_LEN + 1];                  ///<Resource identifier
00475    char_t queryString[HTTP_SERVER_QUERY_STRING_MAX_LEN + 1]; ///<Query string
00476    char_t host[HTTP_SERVER_HOST_MAX_LEN + 1];                ///<Host name
00477    bool_t keepAlive;
00478    bool_t chunkedEncoding;
00479    size_t contentLength;
00480    size_t byteCount;
00481    bool_t firstChunk;
00482    bool_t lastChunk;
00483 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00484    HttpAuthorizationHeader auth;                             ///<Authorization header
00485 #endif
00486 #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
00487    bool_t upgradeWebSocket;
00488    bool_t connectionUpgrade;
00489    char_t clientKey[WEB_SOCKET_CLIENT_KEY_SIZE + 1];
00490 #endif
00491 #if (HTTP_SERVER_MULTIPART_TYPE_SUPPORT == ENABLED)
00492    char_t boundary[HTTP_SERVER_BOUNDARY_MAX_LEN + 1];        ///<Boundary string
00493    size_t boundaryLength;                                    ///<Boundary string length
00494 #endif
00495 } HttpRequest;
00496 
00497 
00498 /**
00499  * @brief HTTP response
00500  **/
00501 
00502 typedef struct
00503 {
00504    uint_t version;              ///<HTTP version number
00505    uint_t statusCode;           ///<HTTP status code
00506    bool_t keepAlive;
00507    bool_t noCache;
00508    uint_t maxAge;
00509    const char_t *location;
00510    const char_t *contentType;
00511    bool_t chunkedEncoding;
00512    size_t contentLength;
00513    size_t byteCount;
00514 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00515    HttpAuthenticateHeader auth; ///<Authenticate header
00516 #endif
00517 } HttpResponse;
00518 
00519 
00520 /**
00521  * @brief HTTP server settings
00522  **/
00523 
00524 typedef struct
00525 {
00526    NetInterface *interface;                                     ///<Underlying network interface
00527    uint16_t port;                                               ///<HTTP server port number
00528    uint_t backlog;                                              ///<Maximum length of the pending connection queue
00529    uint_t maxConnections;                                       ///<Maximum number of simultaneous connections
00530    HttpConnection *connections;                                 ///<HTTP client connections
00531    char_t rootDirectory[HTTP_SERVER_ROOT_DIR_MAX_LEN + 1];      ///<Web root directory
00532    char_t defaultDocument[HTTP_SERVER_DEFAULT_DOC_MAX_LEN + 1]; ///<Default home page
00533 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
00534    bool_t useTls;                                               ///<HTTP over SSL/TLS
00535    TlsInitCallback tlsInitCallback;                             ///<SSL/TLS initialization callback function
00536 #endif
00537 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00538    HttpRandCallback randCallback;                               ///<Random data generation callback function
00539    HttpAuthCallback authCallback;                               ///<HTTP authentication callback function
00540 #endif
00541    HttpCgiCallback cgiCallback;                                 ///<CGI callback function
00542    HttpRequestCallback requestCallback;                         ///<HTTP request callback function
00543    HttpUriNotFoundCallback uriNotFoundCallback;                 ///<URI not found callback function
00544 } HttpServerSettings;
00545 
00546 
00547 /**
00548  * @brief Nonce cache entry
00549  **/
00550 
00551 typedef struct
00552 {
00553    char_t nonce[HTTP_SERVER_NONCE_SIZE * 2 + 1]; ///<Nonce
00554    uint32_t count;                               ///<Nonce count
00555    systime_t timestamp;                          ///<Time stamp to manage entry lifetime
00556 } HttpNonceCacheEntry;
00557 
00558 
00559 /**
00560  * @brief HTTP server context
00561  **/
00562 
00563 struct _HttpServerContext
00564 {
00565    HttpServerSettings settings;                                  ///<User settings
00566    OsTask *taskHandle;                                           ///<Listener task handle
00567    OsSemaphore semaphore;                                        ///<Semaphore limiting the number of connections
00568    Socket *socket;                                               ///<Listening socket
00569    HttpConnection *connections;                                  ///<HTTP client connections
00570 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
00571    OsMutex nonceCacheMutex;                                      ///<Mutex preventing simultaneous access to the nonce cache
00572    HttpNonceCacheEntry nonceCache[HTTP_SERVER_NONCE_CACHE_SIZE]; ///<Nonce cache
00573 #endif
00574 };
00575 
00576 
00577 /**
00578  * @brief HTTP connection
00579  *
00580  * An HttpConnection instance represents one
00581  * transaction with an HTTP client
00582  *
00583  **/
00584 
00585 struct _HttpConnection
00586 {
00587    HttpServerSettings *settings;                       ///<Reference to the HTTP server settings
00588    HttpServerContext *serverContext;                   ///<Reference to the HTTP server context
00589    OsTask *taskHandle;                                 ///<Client task handle
00590    OsEvent startEvent;
00591    bool_t running;
00592    Socket *socket;                                     ///<Socket
00593 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
00594    TlsContext *tlsContext;                             ///<SSL/TLS context
00595 #endif
00596    HttpRequest request;                                ///<Incoming HTTP request header
00597    HttpResponse response;                              ///<HTTP response header
00598    HttpAccessStatus status;                            ///<Access status
00599    char_t cgiParam[HTTP_SERVER_CGI_PARAM_MAX_LEN + 1]; ///<CGI parameter
00600    uint32_t dummy;                                     ///<Force alignment of the buffer on 32-bit boundaries
00601    char_t buffer[HTTP_SERVER_BUFFER_SIZE];             ///<Memory buffer for input/output operations
00602 #if (NET_RTOS_SUPPORT == DISABLED)
00603    HttpConnState state;                                ///<Connection state
00604    systime_t timestamp;
00605    size_t bufferPos;
00606    size_t bufferLen;
00607    uint8_t *bodyStart;
00608    size_t bodyPos;
00609    size_t bodyLen;
00610 #endif
00611 };
00612 
00613 
00614 //HTTP server related functions
00615 void httpServerGetDefaultSettings(HttpServerSettings *settings);
00616 error_t httpServerInit(HttpServerContext *context, const HttpServerSettings *settings);
00617 error_t httpServerStart(HttpServerContext *context);
00618 
00619 void httpListenerTask(void *param);
00620 void httpConnectionTask(void *param);
00621 
00622 error_t httpWriteHeader(HttpConnection *connection);
00623 
00624 error_t httpReadStream(HttpConnection *connection,
00625    void *data, size_t size, size_t *received, uint_t flags);
00626 
00627 error_t httpWriteStream(HttpConnection *connection,
00628    const void *data, size_t length);
00629 
00630 error_t httpCloseStream(HttpConnection *connection);
00631 
00632 error_t httpSendResponse(HttpConnection *connection, const char_t *uri);
00633 
00634 error_t httpSendErrorResponse(HttpConnection *connection,
00635    uint_t statusCode, const char_t *message);
00636 
00637 error_t httpSendRedirectResponse(HttpConnection *connection,
00638    uint_t statusCode, const char_t *uri);
00639 
00640 //HTTP authentication related functions
00641 bool_t httpCheckPassword(HttpConnection *connection,
00642    const char_t *password, HttpAuthMode mode);
00643 
00644 //WebSocket related functions
00645 bool_t httpCheckWebSocketHandshake(HttpConnection *connection);
00646 WebSocket *httpUpgradeToWebSocket(HttpConnection *connection);
00647 
00648 //Miscellaneous functions
00649 error_t httpDecodePercentEncodedString(const char_t *input,
00650    char_t *output, size_t outputSize);
00651 
00652 #endif
00653