Webserver+3d print
cyclone_tcp/http/http_server.h@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file http_server.h |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief HTTP server (HyperText Transfer Protocol) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | #ifndef _HTTP_SERVER_H |
Sergunb | 0:8918a71cdbe9 | 30 | #define _HTTP_SERVER_H |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 33 | #include "os_port.h" |
Sergunb | 0:8918a71cdbe9 | 34 | #include "core/socket.h" |
Sergunb | 0:8918a71cdbe9 | 35 | #include "web_socket/web_socket.h" |
Sergunb | 0:8918a71cdbe9 | 36 | |
Sergunb | 0:8918a71cdbe9 | 37 | //HTTP server support |
Sergunb | 0:8918a71cdbe9 | 38 | #ifndef HTTP_SERVER_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 39 | #define HTTP_SERVER_SUPPORT ENABLED |
Sergunb | 0:8918a71cdbe9 | 40 | #elif (HTTP_SERVER_SUPPORT != ENABLED && HTTP_SERVER_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 41 | #error HTTP_SERVER_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 42 | #endif |
Sergunb | 0:8918a71cdbe9 | 43 | |
Sergunb | 0:8918a71cdbe9 | 44 | //Support for persistent connections |
Sergunb | 0:8918a71cdbe9 | 45 | #ifndef HTTP_SERVER_PERSISTENT_CONN_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 46 | #define HTTP_SERVER_PERSISTENT_CONN_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 47 | #elif (HTTP_SERVER_PERSISTENT_CONN_SUPPORT != ENABLED && HTTP_SERVER_PERSISTENT_CONN_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 48 | #error HTTP_SERVER_PERSISTENT_CONN_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 49 | #endif |
Sergunb | 0:8918a71cdbe9 | 50 | |
Sergunb | 0:8918a71cdbe9 | 51 | //File system support |
Sergunb | 0:8918a71cdbe9 | 52 | #ifndef HTTP_SERVER_FS_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 53 | #define HTTP_SERVER_FS_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 54 | #elif (HTTP_SERVER_FS_SUPPORT != ENABLED && HTTP_SERVER_FS_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 55 | #error HTTP_SERVER_FS_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 56 | #endif |
Sergunb | 0:8918a71cdbe9 | 57 | |
Sergunb | 0:8918a71cdbe9 | 58 | //Server Side Includes support |
Sergunb | 0:8918a71cdbe9 | 59 | #ifndef HTTP_SERVER_SSI_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 60 | #define HTTP_SERVER_SSI_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 61 | #elif (HTTP_SERVER_SSI_SUPPORT != ENABLED && HTTP_SERVER_SSI_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 62 | #error HTTP_SERVER_SSI_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 63 | #endif |
Sergunb | 0:8918a71cdbe9 | 64 | |
Sergunb | 0:8918a71cdbe9 | 65 | //HTTP over SSL/TLS |
Sergunb | 0:8918a71cdbe9 | 66 | #ifndef HTTP_SERVER_TLS_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 67 | #define HTTP_SERVER_TLS_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 68 | #elif (HTTP_SERVER_TLS_SUPPORT != ENABLED && HTTP_SERVER_TLS_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 69 | #error HTTP_SERVER_TLS_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 70 | #endif |
Sergunb | 0:8918a71cdbe9 | 71 | |
Sergunb | 0:8918a71cdbe9 | 72 | //Basic access authentication support |
Sergunb | 0:8918a71cdbe9 | 73 | #ifndef HTTP_SERVER_BASIC_AUTH_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 74 | #define HTTP_SERVER_BASIC_AUTH_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 75 | #elif (HTTP_SERVER_BASIC_AUTH_SUPPORT != ENABLED && HTTP_SERVER_BASIC_AUTH_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 76 | #error HTTP_SERVER_BASIC_AUTH_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 77 | #endif |
Sergunb | 0:8918a71cdbe9 | 78 | |
Sergunb | 0:8918a71cdbe9 | 79 | //Digest access authentication support |
Sergunb | 0:8918a71cdbe9 | 80 | #ifndef HTTP_SERVER_DIGEST_AUTH_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 81 | #define HTTP_SERVER_DIGEST_AUTH_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 82 | #elif (HTTP_SERVER_DIGEST_AUTH_SUPPORT != ENABLED && HTTP_SERVER_DIGEST_AUTH_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 83 | #error HTTP_SERVER_DIGEST_AUTH_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 84 | #endif |
Sergunb | 0:8918a71cdbe9 | 85 | |
Sergunb | 0:8918a71cdbe9 | 86 | //WebSocket support |
Sergunb | 0:8918a71cdbe9 | 87 | #ifndef HTTP_SERVER_WEB_SOCKET_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 88 | #define HTTP_SERVER_WEB_SOCKET_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 89 | #elif (HTTP_SERVER_WEB_SOCKET_SUPPORT != ENABLED && HTTP_SERVER_WEB_SOCKET_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 90 | #error HTTP_SERVER_WEB_SOCKET_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 91 | #endif |
Sergunb | 0:8918a71cdbe9 | 92 | |
Sergunb | 0:8918a71cdbe9 | 93 | //Multipart content type support |
Sergunb | 0:8918a71cdbe9 | 94 | #ifndef HTTP_SERVER_MULTIPART_TYPE_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 95 | #define HTTP_SERVER_MULTIPART_TYPE_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 96 | #elif (HTTP_SERVER_MULTIPART_TYPE_SUPPORT != ENABLED && HTTP_SERVER_MULTIPART_TYPE_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 97 | #error HTTP_SERVER_MULTIPART_TYPE_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 98 | #endif |
Sergunb | 0:8918a71cdbe9 | 99 | |
Sergunb | 0:8918a71cdbe9 | 100 | //Stack size required to run the HTTP server |
Sergunb | 0:8918a71cdbe9 | 101 | #ifndef HTTP_SERVER_STACK_SIZE |
Sergunb | 0:8918a71cdbe9 | 102 | #define HTTP_SERVER_STACK_SIZE 650 |
Sergunb | 0:8918a71cdbe9 | 103 | #elif (HTTP_SERVER_STACK_SIZE < 1) |
Sergunb | 0:8918a71cdbe9 | 104 | #error HTTP_SERVER_STACK_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 105 | #endif |
Sergunb | 0:8918a71cdbe9 | 106 | |
Sergunb | 0:8918a71cdbe9 | 107 | //Priority at which the HTTP server should run |
Sergunb | 0:8918a71cdbe9 | 108 | #ifndef HTTP_SERVER_PRIORITY |
Sergunb | 0:8918a71cdbe9 | 109 | #define HTTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL |
Sergunb | 0:8918a71cdbe9 | 110 | #endif |
Sergunb | 0:8918a71cdbe9 | 111 | |
Sergunb | 0:8918a71cdbe9 | 112 | //HTTP connection timeout |
Sergunb | 0:8918a71cdbe9 | 113 | #ifndef HTTP_SERVER_TIMEOUT |
Sergunb | 0:8918a71cdbe9 | 114 | #define HTTP_SERVER_TIMEOUT 10000 |
Sergunb | 0:8918a71cdbe9 | 115 | #elif (HTTP_SERVER_TIMEOUT < 1000) |
Sergunb | 0:8918a71cdbe9 | 116 | #error HTTP_SERVER_TIMEOUT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 117 | #endif |
Sergunb | 0:8918a71cdbe9 | 118 | |
Sergunb | 0:8918a71cdbe9 | 119 | //Maximum time the server will wait for a subsequent |
Sergunb | 0:8918a71cdbe9 | 120 | //request before closing the connection |
Sergunb | 0:8918a71cdbe9 | 121 | #ifndef HTTP_SERVER_IDLE_TIMEOUT |
Sergunb | 0:8918a71cdbe9 | 122 | #define HTTP_SERVER_IDLE_TIMEOUT 5000 |
Sergunb | 0:8918a71cdbe9 | 123 | #elif (HTTP_SERVER_IDLE_TIMEOUT < 1000) |
Sergunb | 0:8918a71cdbe9 | 124 | #error HTTP_SERVER_IDLE_TIMEOUT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 125 | #endif |
Sergunb | 0:8918a71cdbe9 | 126 | |
Sergunb | 0:8918a71cdbe9 | 127 | //Maximum length of the pending connection queue |
Sergunb | 0:8918a71cdbe9 | 128 | #ifndef HTTP_SERVER_BACKLOG |
Sergunb | 0:8918a71cdbe9 | 129 | #define HTTP_SERVER_BACKLOG 4 |
Sergunb | 0:8918a71cdbe9 | 130 | #elif (HTTP_SERVER_BACKLOG < 1) |
Sergunb | 0:8918a71cdbe9 | 131 | #error HTTP_SERVER_BACKLOG parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 132 | #endif |
Sergunb | 0:8918a71cdbe9 | 133 | |
Sergunb | 0:8918a71cdbe9 | 134 | //Maximum number of requests per connection |
Sergunb | 0:8918a71cdbe9 | 135 | #ifndef HTTP_SERVER_MAX_REQUESTS |
Sergunb | 0:8918a71cdbe9 | 136 | #define HTTP_SERVER_MAX_REQUESTS 1000 |
Sergunb | 0:8918a71cdbe9 | 137 | #elif (HTTP_SERVER_MAX_REQUESTS < 1) |
Sergunb | 0:8918a71cdbe9 | 138 | #error HTTP_SERVER_MAX_REQUESTS parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 139 | #endif |
Sergunb | 0:8918a71cdbe9 | 140 | |
Sergunb | 0:8918a71cdbe9 | 141 | //Size of buffer used for input/output operations |
Sergunb | 0:8918a71cdbe9 | 142 | #ifndef HTTP_SERVER_BUFFER_SIZE |
Sergunb | 0:8918a71cdbe9 | 143 | #define HTTP_SERVER_BUFFER_SIZE 1024 |
Sergunb | 0:8918a71cdbe9 | 144 | #elif (HTTP_SERVER_BUFFER_SIZE < 128) |
Sergunb | 0:8918a71cdbe9 | 145 | #error HTTP_SERVER_BUFFER_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 146 | #endif |
Sergunb | 0:8918a71cdbe9 | 147 | |
Sergunb | 0:8918a71cdbe9 | 148 | //Maximum size of root directory |
Sergunb | 0:8918a71cdbe9 | 149 | #ifndef HTTP_SERVER_ROOT_DIR_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 150 | #define HTTP_SERVER_ROOT_DIR_MAX_LEN 31 |
Sergunb | 0:8918a71cdbe9 | 151 | #elif (HTTP_SERVER_ROOT_DIR_MAX_LEN < 7) |
Sergunb | 0:8918a71cdbe9 | 152 | #error HTTP_SERVER_ROOT_DIR_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 153 | #endif |
Sergunb | 0:8918a71cdbe9 | 154 | |
Sergunb | 0:8918a71cdbe9 | 155 | //Maximum size of default index file |
Sergunb | 0:8918a71cdbe9 | 156 | #ifndef HTTP_SERVER_DEFAULT_DOC_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 157 | #define HTTP_SERVER_DEFAULT_DOC_MAX_LEN 31 |
Sergunb | 0:8918a71cdbe9 | 158 | #elif (HTTP_SERVER_DEFAULT_DOC_MAX_LEN < 7) |
Sergunb | 0:8918a71cdbe9 | 159 | #error HTTP_SERVER_DEFAULT_DOC_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 160 | #endif |
Sergunb | 0:8918a71cdbe9 | 161 | |
Sergunb | 0:8918a71cdbe9 | 162 | //Maximum length of HTTP method |
Sergunb | 0:8918a71cdbe9 | 163 | #ifndef HTTP_SERVER_METHOD_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 164 | #define HTTP_SERVER_METHOD_MAX_LEN 7 |
Sergunb | 0:8918a71cdbe9 | 165 | #elif (HTTP_SERVER_METHOD_MAX_LEN < 1) |
Sergunb | 0:8918a71cdbe9 | 166 | #error HTTP_SERVER_METHOD_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 167 | #endif |
Sergunb | 0:8918a71cdbe9 | 168 | |
Sergunb | 0:8918a71cdbe9 | 169 | //Maximum length of URI |
Sergunb | 0:8918a71cdbe9 | 170 | #ifndef HTTP_SERVER_URI_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 171 | #define HTTP_SERVER_URI_MAX_LEN 255 |
Sergunb | 0:8918a71cdbe9 | 172 | #elif (HTTP_SERVER_URI_MAX_LEN < 31) |
Sergunb | 0:8918a71cdbe9 | 173 | #error HTTP_SERVER_URI_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 174 | #endif |
Sergunb | 0:8918a71cdbe9 | 175 | |
Sergunb | 0:8918a71cdbe9 | 176 | //Maximum length of query strings |
Sergunb | 0:8918a71cdbe9 | 177 | #ifndef HTTP_SERVER_QUERY_STRING_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 178 | #define HTTP_SERVER_QUERY_STRING_MAX_LEN 255 |
Sergunb | 0:8918a71cdbe9 | 179 | #elif (HTTP_SERVER_QUERY_STRING_MAX_LEN < 7) |
Sergunb | 0:8918a71cdbe9 | 180 | #error HTTP_SERVER_QUERY_STRING_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 181 | #endif |
Sergunb | 0:8918a71cdbe9 | 182 | |
Sergunb | 0:8918a71cdbe9 | 183 | //Maximum host name length |
Sergunb | 0:8918a71cdbe9 | 184 | #ifndef HTTP_SERVER_HOST_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 185 | #define HTTP_SERVER_HOST_MAX_LEN 31 |
Sergunb | 0:8918a71cdbe9 | 186 | #elif (HTTP_SERVER_HOST_MAX_LEN < 7) |
Sergunb | 0:8918a71cdbe9 | 187 | #error HTTP_SERVER_HOST_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 188 | #endif |
Sergunb | 0:8918a71cdbe9 | 189 | |
Sergunb | 0:8918a71cdbe9 | 190 | //Maximum user name length |
Sergunb | 0:8918a71cdbe9 | 191 | #ifndef HTTP_SERVER_USERNAME_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 192 | #define HTTP_SERVER_USERNAME_MAX_LEN 31 |
Sergunb | 0:8918a71cdbe9 | 193 | #elif (HTTP_SERVER_USERNAME_MAX_LEN < 7) |
Sergunb | 0:8918a71cdbe9 | 194 | #error HTTP_SERVER_USERNAME_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 195 | #endif |
Sergunb | 0:8918a71cdbe9 | 196 | |
Sergunb | 0:8918a71cdbe9 | 197 | //Maximum length of CGI parameters |
Sergunb | 0:8918a71cdbe9 | 198 | #ifndef HTTP_SERVER_CGI_PARAM_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 199 | #define HTTP_SERVER_CGI_PARAM_MAX_LEN 31 |
Sergunb | 0:8918a71cdbe9 | 200 | #elif (HTTP_SERVER_CGI_PARAM_MAX_LEN < 7) |
Sergunb | 0:8918a71cdbe9 | 201 | #error HTTP_SERVER_CGI_PARAM_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 202 | #endif |
Sergunb | 0:8918a71cdbe9 | 203 | |
Sergunb | 0:8918a71cdbe9 | 204 | //Maximum recursion limit |
Sergunb | 0:8918a71cdbe9 | 205 | #ifndef HTTP_SERVER_SSI_MAX_RECURSION |
Sergunb | 0:8918a71cdbe9 | 206 | #define HTTP_SERVER_SSI_MAX_RECURSION 3 |
Sergunb | 0:8918a71cdbe9 | 207 | #elif (HTTP_SERVER_SSI_MAX_RECURSION < 1 || HTTP_SERVER_SSI_MAX_RECURSION > 8) |
Sergunb | 0:8918a71cdbe9 | 208 | #error HTTP_SERVER_SSI_MAX_RECURSION parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 209 | #endif |
Sergunb | 0:8918a71cdbe9 | 210 | |
Sergunb | 0:8918a71cdbe9 | 211 | //Maximum age for static resources |
Sergunb | 0:8918a71cdbe9 | 212 | #ifndef HTTP_SERVER_MAX_AGE |
Sergunb | 0:8918a71cdbe9 | 213 | #define HTTP_SERVER_MAX_AGE 0 |
Sergunb | 0:8918a71cdbe9 | 214 | #elif (HTTP_SERVER_MAX_AGE < 0) |
Sergunb | 0:8918a71cdbe9 | 215 | #error HTTP_SERVER_MAX_AGE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 216 | #endif |
Sergunb | 0:8918a71cdbe9 | 217 | |
Sergunb | 0:8918a71cdbe9 | 218 | //Nonce cache size |
Sergunb | 0:8918a71cdbe9 | 219 | #ifndef HTTP_SERVER_NONCE_CACHE_SIZE |
Sergunb | 0:8918a71cdbe9 | 220 | #define HTTP_SERVER_NONCE_CACHE_SIZE 8 |
Sergunb | 0:8918a71cdbe9 | 221 | #elif (HTTP_SERVER_NONCE_CACHE_SIZE < 1) |
Sergunb | 0:8918a71cdbe9 | 222 | #error HTTP_SERVER_NONCE_CACHE_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 223 | #endif |
Sergunb | 0:8918a71cdbe9 | 224 | |
Sergunb | 0:8918a71cdbe9 | 225 | //Lifetime of nonces |
Sergunb | 0:8918a71cdbe9 | 226 | #ifndef HTTP_SERVER_NONCE_LIFETIME |
Sergunb | 0:8918a71cdbe9 | 227 | #define HTTP_SERVER_NONCE_LIFETIME 60000 |
Sergunb | 0:8918a71cdbe9 | 228 | #elif (HTTP_SERVER_NONCE_LIFETIME < 1000) |
Sergunb | 0:8918a71cdbe9 | 229 | #error HTTP_SERVER_NONCE_LIFETIME parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 230 | #endif |
Sergunb | 0:8918a71cdbe9 | 231 | |
Sergunb | 0:8918a71cdbe9 | 232 | //Nonce size |
Sergunb | 0:8918a71cdbe9 | 233 | #ifndef HTTP_SERVER_NONCE_SIZE |
Sergunb | 0:8918a71cdbe9 | 234 | #define HTTP_SERVER_NONCE_SIZE 16 |
Sergunb | 0:8918a71cdbe9 | 235 | #elif (HTTP_SERVER_NONCE_SIZE < 1) |
Sergunb | 0:8918a71cdbe9 | 236 | #error HTTP_SERVER_NONCE_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 237 | #endif |
Sergunb | 0:8918a71cdbe9 | 238 | |
Sergunb | 0:8918a71cdbe9 | 239 | //Maximum length for boundary string |
Sergunb | 0:8918a71cdbe9 | 240 | #ifndef HTTP_SERVER_BOUNDARY_MAX_LEN |
Sergunb | 0:8918a71cdbe9 | 241 | #define HTTP_SERVER_BOUNDARY_MAX_LEN 70 |
Sergunb | 0:8918a71cdbe9 | 242 | #elif (HTTP_SERVER_BOUNDARY_MAX_LEN < 1) |
Sergunb | 0:8918a71cdbe9 | 243 | #error HTTP_SERVER_BOUNDARY_MAX_LEN parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 244 | #endif |
Sergunb | 0:8918a71cdbe9 | 245 | |
Sergunb | 0:8918a71cdbe9 | 246 | //File system support? |
Sergunb | 0:8918a71cdbe9 | 247 | #if (HTTP_SERVER_FS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 248 | #include "fs_port.h" |
Sergunb | 0:8918a71cdbe9 | 249 | #else |
Sergunb | 0:8918a71cdbe9 | 250 | #include "resource_manager.h" |
Sergunb | 0:8918a71cdbe9 | 251 | #endif |
Sergunb | 0:8918a71cdbe9 | 252 | |
Sergunb | 0:8918a71cdbe9 | 253 | //HTTP over SSL/TLS supported? |
Sergunb | 0:8918a71cdbe9 | 254 | #if (HTTP_SERVER_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 255 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 256 | #include "tls.h" |
Sergunb | 0:8918a71cdbe9 | 257 | #endif |
Sergunb | 0:8918a71cdbe9 | 258 | |
Sergunb | 0:8918a71cdbe9 | 259 | //Basic authentication supported? |
Sergunb | 0:8918a71cdbe9 | 260 | #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 261 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 262 | #include "base64.h" |
Sergunb | 0:8918a71cdbe9 | 263 | #endif |
Sergunb | 0:8918a71cdbe9 | 264 | |
Sergunb | 0:8918a71cdbe9 | 265 | //Digest authentication supported? |
Sergunb | 0:8918a71cdbe9 | 266 | #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 267 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 268 | #include "md5.h" |
Sergunb | 0:8918a71cdbe9 | 269 | #endif |
Sergunb | 0:8918a71cdbe9 | 270 | |
Sergunb | 0:8918a71cdbe9 | 271 | //WebSocket supported? |
Sergunb | 0:8918a71cdbe9 | 272 | #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 273 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 274 | #include "base64.h" |
Sergunb | 0:8918a71cdbe9 | 275 | #endif |
Sergunb | 0:8918a71cdbe9 | 276 | |
Sergunb | 0:8918a71cdbe9 | 277 | //HTTP port number |
Sergunb | 0:8918a71cdbe9 | 278 | #define HTTP_PORT 80 |
Sergunb | 0:8918a71cdbe9 | 279 | //HTTPS port number (HTTP over SSL/TLS) |
Sergunb | 0:8918a71cdbe9 | 280 | #define HTTPS_PORT 443 |
Sergunb | 0:8918a71cdbe9 | 281 | |
Sergunb | 0:8918a71cdbe9 | 282 | //Forward declaration of HttpServerContext structure |
Sergunb | 0:8918a71cdbe9 | 283 | struct _HttpServerContext; |
Sergunb | 0:8918a71cdbe9 | 284 | #define HttpServerContext struct _HttpServerContext |
Sergunb | 0:8918a71cdbe9 | 285 | |
Sergunb | 0:8918a71cdbe9 | 286 | //Forward declaration of HttpConnection structure |
Sergunb | 0:8918a71cdbe9 | 287 | struct _HttpConnection; |
Sergunb | 0:8918a71cdbe9 | 288 | #define HttpConnection struct _HttpConnection |
Sergunb | 0:8918a71cdbe9 | 289 | |
Sergunb | 0:8918a71cdbe9 | 290 | |
Sergunb | 0:8918a71cdbe9 | 291 | /** |
Sergunb | 0:8918a71cdbe9 | 292 | * @brief HTTP version numbers |
Sergunb | 0:8918a71cdbe9 | 293 | **/ |
Sergunb | 0:8918a71cdbe9 | 294 | |
Sergunb | 0:8918a71cdbe9 | 295 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 296 | { |
Sergunb | 0:8918a71cdbe9 | 297 | HTTP_VERSION_0_9 = 0x0009, |
Sergunb | 0:8918a71cdbe9 | 298 | HTTP_VERSION_1_0 = 0x0100, |
Sergunb | 0:8918a71cdbe9 | 299 | HTTP_VERSION_1_1 = 0x0101 |
Sergunb | 0:8918a71cdbe9 | 300 | } HttpVersion; |
Sergunb | 0:8918a71cdbe9 | 301 | |
Sergunb | 0:8918a71cdbe9 | 302 | |
Sergunb | 0:8918a71cdbe9 | 303 | /** |
Sergunb | 0:8918a71cdbe9 | 304 | * @brief HTTP authentication schemes |
Sergunb | 0:8918a71cdbe9 | 305 | **/ |
Sergunb | 0:8918a71cdbe9 | 306 | |
Sergunb | 0:8918a71cdbe9 | 307 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 308 | { |
Sergunb | 0:8918a71cdbe9 | 309 | HTTP_AUTH_MODE_NONE = 0, |
Sergunb | 0:8918a71cdbe9 | 310 | HTTP_AUTH_MODE_BASIC = 1, |
Sergunb | 0:8918a71cdbe9 | 311 | HTTP_AUTH_MODE_DIGEST = 2 |
Sergunb | 0:8918a71cdbe9 | 312 | } HttpAuthMode; |
Sergunb | 0:8918a71cdbe9 | 313 | |
Sergunb | 0:8918a71cdbe9 | 314 | |
Sergunb | 0:8918a71cdbe9 | 315 | /** |
Sergunb | 0:8918a71cdbe9 | 316 | * @brief Access status |
Sergunb | 0:8918a71cdbe9 | 317 | **/ |
Sergunb | 0:8918a71cdbe9 | 318 | |
Sergunb | 0:8918a71cdbe9 | 319 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 320 | { |
Sergunb | 0:8918a71cdbe9 | 321 | HTTP_ACCESS_DENIED = 0, |
Sergunb | 0:8918a71cdbe9 | 322 | HTTP_ACCESS_ALLOWED = 1, |
Sergunb | 0:8918a71cdbe9 | 323 | HTTP_ACCESS_BASIC_AUTH_REQUIRED = 2, |
Sergunb | 0:8918a71cdbe9 | 324 | HTTP_ACCESS_DIGEST_AUTH_REQUIRED = 3 |
Sergunb | 0:8918a71cdbe9 | 325 | } HttpAccessStatus; |
Sergunb | 0:8918a71cdbe9 | 326 | |
Sergunb | 0:8918a71cdbe9 | 327 | |
Sergunb | 0:8918a71cdbe9 | 328 | /** |
Sergunb | 0:8918a71cdbe9 | 329 | * @brief Flags used by I/O functions |
Sergunb | 0:8918a71cdbe9 | 330 | **/ |
Sergunb | 0:8918a71cdbe9 | 331 | |
Sergunb | 0:8918a71cdbe9 | 332 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 333 | { |
Sergunb | 0:8918a71cdbe9 | 334 | HTTP_FLAG_WAIT_ALL = 0x0800, |
Sergunb | 0:8918a71cdbe9 | 335 | HTTP_FLAG_BREAK_CHAR = 0x1000, |
Sergunb | 0:8918a71cdbe9 | 336 | HTTP_FLAG_BREAK_CRLF = 0x100A, |
Sergunb | 0:8918a71cdbe9 | 337 | HTTP_FLAG_WAIT_ACK = 0x2000, |
Sergunb | 0:8918a71cdbe9 | 338 | HTTP_FLAG_NO_DELAY = 0x4000, |
Sergunb | 0:8918a71cdbe9 | 339 | HTTP_FLAG_DELAY = 0x8000 |
Sergunb | 0:8918a71cdbe9 | 340 | } HttpFlags; |
Sergunb | 0:8918a71cdbe9 | 341 | |
Sergunb | 0:8918a71cdbe9 | 342 | |
Sergunb | 0:8918a71cdbe9 | 343 | /** |
Sergunb | 0:8918a71cdbe9 | 344 | * @brief HTTP connection states |
Sergunb | 0:8918a71cdbe9 | 345 | **/ |
Sergunb | 0:8918a71cdbe9 | 346 | |
Sergunb | 0:8918a71cdbe9 | 347 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 348 | { |
Sergunb | 0:8918a71cdbe9 | 349 | HTTP_CONN_STATE_IDLE = 0, |
Sergunb | 0:8918a71cdbe9 | 350 | HTTP_CONN_STATE_REQ_LINE = 1, |
Sergunb | 0:8918a71cdbe9 | 351 | HTTP_CONN_STATE_REQ_HEADER = 2, |
Sergunb | 0:8918a71cdbe9 | 352 | HTTP_CONN_STATE_REQ_BODY = 3, |
Sergunb | 0:8918a71cdbe9 | 353 | HTTP_CONN_STATE_RESP_HEADER = 4, |
Sergunb | 0:8918a71cdbe9 | 354 | HTTP_CONN_STATE_RESP_BODY = 5, |
Sergunb | 0:8918a71cdbe9 | 355 | HTTP_CONN_STATE_SHUTDOWN = 6, |
Sergunb | 0:8918a71cdbe9 | 356 | HTTP_CONN_STATE_CLOSE = 7 |
Sergunb | 0:8918a71cdbe9 | 357 | } HttpConnState; |
Sergunb | 0:8918a71cdbe9 | 358 | |
Sergunb | 0:8918a71cdbe9 | 359 | |
Sergunb | 0:8918a71cdbe9 | 360 | //The HTTP_FLAG_BREAK macro causes the httpReadStream() function to stop |
Sergunb | 0:8918a71cdbe9 | 361 | //reading data whenever the specified break character is encountered |
Sergunb | 0:8918a71cdbe9 | 362 | #define HTTP_FLAG_BREAK(c) (HTTP_FLAG_BREAK_CHAR | LSB(c)) |
Sergunb | 0:8918a71cdbe9 | 363 | |
Sergunb | 0:8918a71cdbe9 | 364 | |
Sergunb | 0:8918a71cdbe9 | 365 | //HTTP over SSL/TLS supported? |
Sergunb | 0:8918a71cdbe9 | 366 | #if (HTTP_SERVER_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 367 | |
Sergunb | 0:8918a71cdbe9 | 368 | /** |
Sergunb | 0:8918a71cdbe9 | 369 | * @brief SSL/TLS initialization callback function |
Sergunb | 0:8918a71cdbe9 | 370 | **/ |
Sergunb | 0:8918a71cdbe9 | 371 | |
Sergunb | 0:8918a71cdbe9 | 372 | typedef error_t (*TlsInitCallback)(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 373 | TlsContext *tlsContext); |
Sergunb | 0:8918a71cdbe9 | 374 | |
Sergunb | 0:8918a71cdbe9 | 375 | #endif |
Sergunb | 0:8918a71cdbe9 | 376 | |
Sergunb | 0:8918a71cdbe9 | 377 | |
Sergunb | 0:8918a71cdbe9 | 378 | /** |
Sergunb | 0:8918a71cdbe9 | 379 | * @brief Random data generation callback function |
Sergunb | 0:8918a71cdbe9 | 380 | **/ |
Sergunb | 0:8918a71cdbe9 | 381 | |
Sergunb | 0:8918a71cdbe9 | 382 | typedef error_t (*HttpRandCallback)(uint8_t *data, size_t length); |
Sergunb | 0:8918a71cdbe9 | 383 | |
Sergunb | 0:8918a71cdbe9 | 384 | |
Sergunb | 0:8918a71cdbe9 | 385 | /** |
Sergunb | 0:8918a71cdbe9 | 386 | * @brief HTTP authentication callback function |
Sergunb | 0:8918a71cdbe9 | 387 | **/ |
Sergunb | 0:8918a71cdbe9 | 388 | |
Sergunb | 0:8918a71cdbe9 | 389 | typedef HttpAccessStatus (*HttpAuthCallback)(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 390 | const char_t *user, const char_t *uri); |
Sergunb | 0:8918a71cdbe9 | 391 | |
Sergunb | 0:8918a71cdbe9 | 392 | |
Sergunb | 0:8918a71cdbe9 | 393 | /** |
Sergunb | 0:8918a71cdbe9 | 394 | * @brief CGI callback function |
Sergunb | 0:8918a71cdbe9 | 395 | **/ |
Sergunb | 0:8918a71cdbe9 | 396 | |
Sergunb | 0:8918a71cdbe9 | 397 | typedef error_t (*HttpCgiCallback)(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 398 | const char_t *param); |
Sergunb | 0:8918a71cdbe9 | 399 | |
Sergunb | 0:8918a71cdbe9 | 400 | |
Sergunb | 0:8918a71cdbe9 | 401 | /** |
Sergunb | 0:8918a71cdbe9 | 402 | * @brief HTTP request callback function |
Sergunb | 0:8918a71cdbe9 | 403 | **/ |
Sergunb | 0:8918a71cdbe9 | 404 | |
Sergunb | 0:8918a71cdbe9 | 405 | typedef error_t (*HttpRequestCallback)(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 406 | const char_t *uri); |
Sergunb | 0:8918a71cdbe9 | 407 | |
Sergunb | 0:8918a71cdbe9 | 408 | |
Sergunb | 0:8918a71cdbe9 | 409 | /** |
Sergunb | 0:8918a71cdbe9 | 410 | * @brief URI not found callback function |
Sergunb | 0:8918a71cdbe9 | 411 | **/ |
Sergunb | 0:8918a71cdbe9 | 412 | |
Sergunb | 0:8918a71cdbe9 | 413 | typedef error_t (*HttpUriNotFoundCallback)(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 414 | const char_t *uri); |
Sergunb | 0:8918a71cdbe9 | 415 | |
Sergunb | 0:8918a71cdbe9 | 416 | |
Sergunb | 0:8918a71cdbe9 | 417 | /** |
Sergunb | 0:8918a71cdbe9 | 418 | * @brief HTTP status code |
Sergunb | 0:8918a71cdbe9 | 419 | **/ |
Sergunb | 0:8918a71cdbe9 | 420 | |
Sergunb | 0:8918a71cdbe9 | 421 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 422 | { |
Sergunb | 0:8918a71cdbe9 | 423 | uint_t value; |
Sergunb | 0:8918a71cdbe9 | 424 | const char_t message[28]; |
Sergunb | 0:8918a71cdbe9 | 425 | } HttpStatusCodeDesc; |
Sergunb | 0:8918a71cdbe9 | 426 | |
Sergunb | 0:8918a71cdbe9 | 427 | |
Sergunb | 0:8918a71cdbe9 | 428 | /** |
Sergunb | 0:8918a71cdbe9 | 429 | * @brief Authorization header |
Sergunb | 0:8918a71cdbe9 | 430 | **/ |
Sergunb | 0:8918a71cdbe9 | 431 | |
Sergunb | 0:8918a71cdbe9 | 432 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 433 | { |
Sergunb | 0:8918a71cdbe9 | 434 | bool_t found; ///<The Authorization header has been found |
Sergunb | 0:8918a71cdbe9 | 435 | HttpAuthMode mode; ///<Authentication scheme |
Sergunb | 0:8918a71cdbe9 | 436 | char_t user[HTTP_SERVER_USERNAME_MAX_LEN + 1]; ///<User name |
Sergunb | 0:8918a71cdbe9 | 437 | #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 438 | const char_t *password; ///<Password |
Sergunb | 0:8918a71cdbe9 | 439 | #endif |
Sergunb | 0:8918a71cdbe9 | 440 | #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 441 | const char_t *realm; |
Sergunb | 0:8918a71cdbe9 | 442 | const char_t *nonce; ///<Server nonce |
Sergunb | 0:8918a71cdbe9 | 443 | const char_t *uri; ///<Digest URI |
Sergunb | 0:8918a71cdbe9 | 444 | const char_t *qop; |
Sergunb | 0:8918a71cdbe9 | 445 | const char_t *nc; ///<Nonce count |
Sergunb | 0:8918a71cdbe9 | 446 | const char_t *cnonce; ///<Client nonce |
Sergunb | 0:8918a71cdbe9 | 447 | const char_t *response; |
Sergunb | 0:8918a71cdbe9 | 448 | const char_t *opaque; |
Sergunb | 0:8918a71cdbe9 | 449 | #endif |
Sergunb | 0:8918a71cdbe9 | 450 | } HttpAuthorizationHeader; |
Sergunb | 0:8918a71cdbe9 | 451 | |
Sergunb | 0:8918a71cdbe9 | 452 | |
Sergunb | 0:8918a71cdbe9 | 453 | /** |
Sergunb | 0:8918a71cdbe9 | 454 | * @brief Authenticate header |
Sergunb | 0:8918a71cdbe9 | 455 | **/ |
Sergunb | 0:8918a71cdbe9 | 456 | |
Sergunb | 0:8918a71cdbe9 | 457 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 458 | { |
Sergunb | 0:8918a71cdbe9 | 459 | HttpAuthMode mode; ///<Authentication scheme |
Sergunb | 0:8918a71cdbe9 | 460 | #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 461 | bool_t stale; ///<STALE flag |
Sergunb | 0:8918a71cdbe9 | 462 | #endif |
Sergunb | 0:8918a71cdbe9 | 463 | } HttpAuthenticateHeader; |
Sergunb | 0:8918a71cdbe9 | 464 | |
Sergunb | 0:8918a71cdbe9 | 465 | |
Sergunb | 0:8918a71cdbe9 | 466 | /** |
Sergunb | 0:8918a71cdbe9 | 467 | * @brief HTTP request |
Sergunb | 0:8918a71cdbe9 | 468 | **/ |
Sergunb | 0:8918a71cdbe9 | 469 | |
Sergunb | 0:8918a71cdbe9 | 470 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 471 | { |
Sergunb | 0:8918a71cdbe9 | 472 | uint_t version; ///<HTTP version number |
Sergunb | 0:8918a71cdbe9 | 473 | char_t method[HTTP_SERVER_METHOD_MAX_LEN + 1]; ///<HTTP method |
Sergunb | 0:8918a71cdbe9 | 474 | char_t uri[HTTP_SERVER_URI_MAX_LEN + 1]; ///<Resource identifier |
Sergunb | 0:8918a71cdbe9 | 475 | char_t queryString[HTTP_SERVER_QUERY_STRING_MAX_LEN + 1]; ///<Query string |
Sergunb | 0:8918a71cdbe9 | 476 | char_t host[HTTP_SERVER_HOST_MAX_LEN + 1]; ///<Host name |
Sergunb | 0:8918a71cdbe9 | 477 | bool_t keepAlive; |
Sergunb | 0:8918a71cdbe9 | 478 | bool_t chunkedEncoding; |
Sergunb | 0:8918a71cdbe9 | 479 | size_t contentLength; |
Sergunb | 0:8918a71cdbe9 | 480 | size_t byteCount; |
Sergunb | 0:8918a71cdbe9 | 481 | bool_t firstChunk; |
Sergunb | 0:8918a71cdbe9 | 482 | bool_t lastChunk; |
Sergunb | 0:8918a71cdbe9 | 483 | #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 484 | HttpAuthorizationHeader auth; ///<Authorization header |
Sergunb | 0:8918a71cdbe9 | 485 | #endif |
Sergunb | 0:8918a71cdbe9 | 486 | #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 487 | bool_t upgradeWebSocket; |
Sergunb | 0:8918a71cdbe9 | 488 | bool_t connectionUpgrade; |
Sergunb | 0:8918a71cdbe9 | 489 | char_t clientKey[WEB_SOCKET_CLIENT_KEY_SIZE + 1]; |
Sergunb | 0:8918a71cdbe9 | 490 | #endif |
Sergunb | 0:8918a71cdbe9 | 491 | #if (HTTP_SERVER_MULTIPART_TYPE_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 492 | char_t boundary[HTTP_SERVER_BOUNDARY_MAX_LEN + 1]; ///<Boundary string |
Sergunb | 0:8918a71cdbe9 | 493 | size_t boundaryLength; ///<Boundary string length |
Sergunb | 0:8918a71cdbe9 | 494 | #endif |
Sergunb | 0:8918a71cdbe9 | 495 | } HttpRequest; |
Sergunb | 0:8918a71cdbe9 | 496 | |
Sergunb | 0:8918a71cdbe9 | 497 | |
Sergunb | 0:8918a71cdbe9 | 498 | /** |
Sergunb | 0:8918a71cdbe9 | 499 | * @brief HTTP response |
Sergunb | 0:8918a71cdbe9 | 500 | **/ |
Sergunb | 0:8918a71cdbe9 | 501 | |
Sergunb | 0:8918a71cdbe9 | 502 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 503 | { |
Sergunb | 0:8918a71cdbe9 | 504 | uint_t version; ///<HTTP version number |
Sergunb | 0:8918a71cdbe9 | 505 | uint_t statusCode; ///<HTTP status code |
Sergunb | 0:8918a71cdbe9 | 506 | bool_t keepAlive; |
Sergunb | 0:8918a71cdbe9 | 507 | bool_t noCache; |
Sergunb | 0:8918a71cdbe9 | 508 | uint_t maxAge; |
Sergunb | 0:8918a71cdbe9 | 509 | const char_t *location; |
Sergunb | 0:8918a71cdbe9 | 510 | const char_t *contentType; |
Sergunb | 0:8918a71cdbe9 | 511 | bool_t chunkedEncoding; |
Sergunb | 0:8918a71cdbe9 | 512 | size_t contentLength; |
Sergunb | 0:8918a71cdbe9 | 513 | size_t byteCount; |
Sergunb | 0:8918a71cdbe9 | 514 | #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 515 | HttpAuthenticateHeader auth; ///<Authenticate header |
Sergunb | 0:8918a71cdbe9 | 516 | #endif |
Sergunb | 0:8918a71cdbe9 | 517 | } HttpResponse; |
Sergunb | 0:8918a71cdbe9 | 518 | |
Sergunb | 0:8918a71cdbe9 | 519 | |
Sergunb | 0:8918a71cdbe9 | 520 | /** |
Sergunb | 0:8918a71cdbe9 | 521 | * @brief HTTP server settings |
Sergunb | 0:8918a71cdbe9 | 522 | **/ |
Sergunb | 0:8918a71cdbe9 | 523 | |
Sergunb | 0:8918a71cdbe9 | 524 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 525 | { |
Sergunb | 0:8918a71cdbe9 | 526 | NetInterface *interface; ///<Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 527 | uint16_t port; ///<HTTP server port number |
Sergunb | 0:8918a71cdbe9 | 528 | uint_t backlog; ///<Maximum length of the pending connection queue |
Sergunb | 0:8918a71cdbe9 | 529 | uint_t maxConnections; ///<Maximum number of simultaneous connections |
Sergunb | 0:8918a71cdbe9 | 530 | HttpConnection *connections; ///<HTTP client connections |
Sergunb | 0:8918a71cdbe9 | 531 | char_t rootDirectory[HTTP_SERVER_ROOT_DIR_MAX_LEN + 1]; ///<Web root directory |
Sergunb | 0:8918a71cdbe9 | 532 | char_t defaultDocument[HTTP_SERVER_DEFAULT_DOC_MAX_LEN + 1]; ///<Default home page |
Sergunb | 0:8918a71cdbe9 | 533 | #if (HTTP_SERVER_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 534 | bool_t useTls; ///<HTTP over SSL/TLS |
Sergunb | 0:8918a71cdbe9 | 535 | TlsInitCallback tlsInitCallback; ///<SSL/TLS initialization callback function |
Sergunb | 0:8918a71cdbe9 | 536 | #endif |
Sergunb | 0:8918a71cdbe9 | 537 | #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 538 | HttpRandCallback randCallback; ///<Random data generation callback function |
Sergunb | 0:8918a71cdbe9 | 539 | HttpAuthCallback authCallback; ///<HTTP authentication callback function |
Sergunb | 0:8918a71cdbe9 | 540 | #endif |
Sergunb | 0:8918a71cdbe9 | 541 | HttpCgiCallback cgiCallback; ///<CGI callback function |
Sergunb | 0:8918a71cdbe9 | 542 | HttpRequestCallback requestCallback; ///<HTTP request callback function |
Sergunb | 0:8918a71cdbe9 | 543 | HttpUriNotFoundCallback uriNotFoundCallback; ///<URI not found callback function |
Sergunb | 0:8918a71cdbe9 | 544 | } HttpServerSettings; |
Sergunb | 0:8918a71cdbe9 | 545 | |
Sergunb | 0:8918a71cdbe9 | 546 | |
Sergunb | 0:8918a71cdbe9 | 547 | /** |
Sergunb | 0:8918a71cdbe9 | 548 | * @brief Nonce cache entry |
Sergunb | 0:8918a71cdbe9 | 549 | **/ |
Sergunb | 0:8918a71cdbe9 | 550 | |
Sergunb | 0:8918a71cdbe9 | 551 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 552 | { |
Sergunb | 0:8918a71cdbe9 | 553 | char_t nonce[HTTP_SERVER_NONCE_SIZE * 2 + 1]; ///<Nonce |
Sergunb | 0:8918a71cdbe9 | 554 | uint32_t count; ///<Nonce count |
Sergunb | 0:8918a71cdbe9 | 555 | systime_t timestamp; ///<Time stamp to manage entry lifetime |
Sergunb | 0:8918a71cdbe9 | 556 | } HttpNonceCacheEntry; |
Sergunb | 0:8918a71cdbe9 | 557 | |
Sergunb | 0:8918a71cdbe9 | 558 | |
Sergunb | 0:8918a71cdbe9 | 559 | /** |
Sergunb | 0:8918a71cdbe9 | 560 | * @brief HTTP server context |
Sergunb | 0:8918a71cdbe9 | 561 | **/ |
Sergunb | 0:8918a71cdbe9 | 562 | |
Sergunb | 0:8918a71cdbe9 | 563 | struct _HttpServerContext |
Sergunb | 0:8918a71cdbe9 | 564 | { |
Sergunb | 0:8918a71cdbe9 | 565 | HttpServerSettings settings; ///<User settings |
Sergunb | 0:8918a71cdbe9 | 566 | OsTask *taskHandle; ///<Listener task handle |
Sergunb | 0:8918a71cdbe9 | 567 | OsSemaphore semaphore; ///<Semaphore limiting the number of connections |
Sergunb | 0:8918a71cdbe9 | 568 | Socket *socket; ///<Listening socket |
Sergunb | 0:8918a71cdbe9 | 569 | HttpConnection *connections; ///<HTTP client connections |
Sergunb | 0:8918a71cdbe9 | 570 | #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 571 | OsMutex nonceCacheMutex; ///<Mutex preventing simultaneous access to the nonce cache |
Sergunb | 0:8918a71cdbe9 | 572 | HttpNonceCacheEntry nonceCache[HTTP_SERVER_NONCE_CACHE_SIZE]; ///<Nonce cache |
Sergunb | 0:8918a71cdbe9 | 573 | #endif |
Sergunb | 0:8918a71cdbe9 | 574 | }; |
Sergunb | 0:8918a71cdbe9 | 575 | |
Sergunb | 0:8918a71cdbe9 | 576 | |
Sergunb | 0:8918a71cdbe9 | 577 | /** |
Sergunb | 0:8918a71cdbe9 | 578 | * @brief HTTP connection |
Sergunb | 0:8918a71cdbe9 | 579 | * |
Sergunb | 0:8918a71cdbe9 | 580 | * An HttpConnection instance represents one |
Sergunb | 0:8918a71cdbe9 | 581 | * transaction with an HTTP client |
Sergunb | 0:8918a71cdbe9 | 582 | * |
Sergunb | 0:8918a71cdbe9 | 583 | **/ |
Sergunb | 0:8918a71cdbe9 | 584 | |
Sergunb | 0:8918a71cdbe9 | 585 | struct _HttpConnection |
Sergunb | 0:8918a71cdbe9 | 586 | { |
Sergunb | 0:8918a71cdbe9 | 587 | HttpServerSettings *settings; ///<Reference to the HTTP server settings |
Sergunb | 0:8918a71cdbe9 | 588 | HttpServerContext *serverContext; ///<Reference to the HTTP server context |
Sergunb | 0:8918a71cdbe9 | 589 | OsTask *taskHandle; ///<Client task handle |
Sergunb | 0:8918a71cdbe9 | 590 | OsEvent startEvent; |
Sergunb | 0:8918a71cdbe9 | 591 | bool_t running; |
Sergunb | 0:8918a71cdbe9 | 592 | Socket *socket; ///<Socket |
Sergunb | 0:8918a71cdbe9 | 593 | #if (HTTP_SERVER_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 594 | TlsContext *tlsContext; ///<SSL/TLS context |
Sergunb | 0:8918a71cdbe9 | 595 | #endif |
Sergunb | 0:8918a71cdbe9 | 596 | HttpRequest request; ///<Incoming HTTP request header |
Sergunb | 0:8918a71cdbe9 | 597 | HttpResponse response; ///<HTTP response header |
Sergunb | 0:8918a71cdbe9 | 598 | HttpAccessStatus status; ///<Access status |
Sergunb | 0:8918a71cdbe9 | 599 | char_t cgiParam[HTTP_SERVER_CGI_PARAM_MAX_LEN + 1]; ///<CGI parameter |
Sergunb | 0:8918a71cdbe9 | 600 | uint32_t dummy; ///<Force alignment of the buffer on 32-bit boundaries |
Sergunb | 0:8918a71cdbe9 | 601 | char_t buffer[HTTP_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations |
Sergunb | 0:8918a71cdbe9 | 602 | #if (NET_RTOS_SUPPORT == DISABLED) |
Sergunb | 0:8918a71cdbe9 | 603 | HttpConnState state; ///<Connection state |
Sergunb | 0:8918a71cdbe9 | 604 | systime_t timestamp; |
Sergunb | 0:8918a71cdbe9 | 605 | size_t bufferPos; |
Sergunb | 0:8918a71cdbe9 | 606 | size_t bufferLen; |
Sergunb | 0:8918a71cdbe9 | 607 | uint8_t *bodyStart; |
Sergunb | 0:8918a71cdbe9 | 608 | size_t bodyPos; |
Sergunb | 0:8918a71cdbe9 | 609 | size_t bodyLen; |
Sergunb | 0:8918a71cdbe9 | 610 | #endif |
Sergunb | 0:8918a71cdbe9 | 611 | }; |
Sergunb | 0:8918a71cdbe9 | 612 | |
Sergunb | 0:8918a71cdbe9 | 613 | |
Sergunb | 0:8918a71cdbe9 | 614 | //HTTP server related functions |
Sergunb | 0:8918a71cdbe9 | 615 | void httpServerGetDefaultSettings(HttpServerSettings *settings); |
Sergunb | 0:8918a71cdbe9 | 616 | error_t httpServerInit(HttpServerContext *context, const HttpServerSettings *settings); |
Sergunb | 0:8918a71cdbe9 | 617 | error_t httpServerStart(HttpServerContext *context); |
Sergunb | 0:8918a71cdbe9 | 618 | |
Sergunb | 0:8918a71cdbe9 | 619 | void httpListenerTask(void *param); |
Sergunb | 0:8918a71cdbe9 | 620 | void httpConnectionTask(void *param); |
Sergunb | 0:8918a71cdbe9 | 621 | |
Sergunb | 0:8918a71cdbe9 | 622 | error_t httpWriteHeader(HttpConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 623 | |
Sergunb | 0:8918a71cdbe9 | 624 | error_t httpReadStream(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 625 | void *data, size_t size, size_t *received, uint_t flags); |
Sergunb | 0:8918a71cdbe9 | 626 | |
Sergunb | 0:8918a71cdbe9 | 627 | error_t httpWriteStream(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 628 | const void *data, size_t length); |
Sergunb | 0:8918a71cdbe9 | 629 | |
Sergunb | 0:8918a71cdbe9 | 630 | error_t httpCloseStream(HttpConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 631 | |
Sergunb | 0:8918a71cdbe9 | 632 | error_t httpSendResponse(HttpConnection *connection, const char_t *uri); |
Sergunb | 0:8918a71cdbe9 | 633 | |
Sergunb | 0:8918a71cdbe9 | 634 | error_t httpSendErrorResponse(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 635 | uint_t statusCode, const char_t *message); |
Sergunb | 0:8918a71cdbe9 | 636 | |
Sergunb | 0:8918a71cdbe9 | 637 | error_t httpSendRedirectResponse(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 638 | uint_t statusCode, const char_t *uri); |
Sergunb | 0:8918a71cdbe9 | 639 | |
Sergunb | 0:8918a71cdbe9 | 640 | //HTTP authentication related functions |
Sergunb | 0:8918a71cdbe9 | 641 | bool_t httpCheckPassword(HttpConnection *connection, |
Sergunb | 0:8918a71cdbe9 | 642 | const char_t *password, HttpAuthMode mode); |
Sergunb | 0:8918a71cdbe9 | 643 | |
Sergunb | 0:8918a71cdbe9 | 644 | //WebSocket related functions |
Sergunb | 0:8918a71cdbe9 | 645 | bool_t httpCheckWebSocketHandshake(HttpConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 646 | WebSocket *httpUpgradeToWebSocket(HttpConnection *connection); |
Sergunb | 0:8918a71cdbe9 | 647 | |
Sergunb | 0:8918a71cdbe9 | 648 | //Miscellaneous functions |
Sergunb | 0:8918a71cdbe9 | 649 | error_t httpDecodePercentEncodedString(const char_t *input, |
Sergunb | 0:8918a71cdbe9 | 650 | char_t *output, size_t outputSize); |
Sergunb | 0:8918a71cdbe9 | 651 | |
Sergunb | 0:8918a71cdbe9 | 652 | #endif |
Sergunb | 0:8918a71cdbe9 | 653 |