1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Hypertext Transfer Protocol (HTTP)                                  */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
26 /*                                                                        */
27 /*    nx_web_http_common.h                                PORTABLE C      */
28 /*                                                           6.1.6        */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    Yuxin Zhou, Microsoft Corporation                                   */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the NetX Web Hypertext Transfer Protocol (HTTP)   */
36 /*    component, including all data types and external references.        */
37 /*    It is assumed that nx_api.h and nx_port.h have already been         */
38 /*    included, along with fx_api.h and fx_port.h. If HTTPS is being used */
39 /*    then nx_secure_tls_api.h must also be included.                     */
40 /*                                                                        */
41 /*  RELEASE HISTORY                                                       */
42 /*                                                                        */
43 /*    DATE              NAME                      DESCRIPTION             */
44 /*                                                                        */
45 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
46 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
47 /*                                            resulting in version 6.1    */
48 /*  04-02-2021     Yuxin Zhou               Modified comment(s), and      */
49 /*                                            improved the logic of       */
50 /*                                            parsing base64,             */
51 /*                                            resulting in version 6.1.6  */
52 /*                                                                        */
53 /**************************************************************************/
54 
55 #ifndef NX_WEB_HTTP_COMMON_H
56 #define NX_WEB_HTTP_COMMON_H
57 
58 /* Determine if a C++ compiler is being used.  If so, ensure that standard
59    C is used to process the API information.  */
60 
61 #ifdef   __cplusplus
62 
63 /* Yes, C++ compiler is present.  Use standard C.  */
64 extern   "C" {
65 
66 #endif
67 
68 /* Define the HTTP version.  */
69 #define NX_WEB_HTTP_VERSION                     "HTTP/1.1"
70 
71 /* Define HTTP TCP socket create options.  */
72 
73 #ifndef NX_WEB_HTTP_TYPE_OF_SERVICE
74 #define NX_WEB_HTTP_TYPE_OF_SERVICE             NX_IP_NORMAL
75 #endif
76 
77 #ifndef NX_WEB_HTTP_FRAGMENT_OPTION
78 #define NX_WEB_HTTP_FRAGMENT_OPTION             NX_DONT_FRAGMENT
79 #endif
80 
81 #ifndef NX_WEB_HTTP_TIME_TO_LIVE
82 #define NX_WEB_HTTP_TIME_TO_LIVE                0x80
83 #endif
84 
85 #ifndef NX_WEB_HTTP_MAX_RESOURCE
86 #define NX_WEB_HTTP_MAX_RESOURCE                40
87 #endif
88 
89 #ifndef NX_WEB_HTTP_MAX_NAME
90 #define NX_WEB_HTTP_MAX_NAME                    20
91 #endif
92 
93 #ifndef NX_WEB_HTTP_MAX_PASSWORD
94 #define NX_WEB_HTTP_MAX_PASSWORD                20
95 #endif
96 
97 /* To enabled HTTPS, define this symbol
98 #define NX_WEB_HTTPS_ENABLE
99 */
100 
101 /* To enable MD5 digest authentication, define this symbol
102 #define NX_WEB_HTTP_DIGEST_ENABLE
103 */
104 
105 #ifndef NX_PHYSICAL_TRAILER
106 #define NX_PHYSICAL_TRAILER                     4
107 #endif
108 
109 /* NX_WEB_HTTP_MAX_STRING is base64 of "name:password" and plus 1 if an extra conversion is needed and plus 2 pad if needed.. */
110 #define NX_WEB_HTTP_MAX_STRING                  ((NX_WEB_HTTP_MAX_NAME + NX_WEB_HTTP_MAX_PASSWORD  + 1 ) * 4 / 3 + 1 + 2)
111 
112 #define NX_WEB_HTTP_MAX_BINARY_MD5              16
113 #define NX_WEB_HTTP_MAX_ASCII_MD5               32
114 
115 
116 /* Define return code constants.  */
117 #define NX_WEB_HTTP_ERROR                       0x30000        /* HTTP internal error                                  */
118 #define NX_WEB_HTTP_TIMEOUT                     0x30001        /* HTTP timeout occurred                                */
119 #define NX_WEB_HTTP_FAILED                      0x30002        /* HTTP error                                           */
120 #define NX_WEB_HTTP_DONT_AUTHENTICATE           0x30003        /* HTTP authentication not needed                       */
121 #define NX_WEB_HTTP_BASIC_AUTHENTICATE          0x30004        /* HTTP basic authentication requested                  */
122 #define NX_WEB_HTTP_DIGEST_AUTHENTICATE         0x30005        /* HTTP digest authentication requested                 */
123 #define NX_WEB_HTTP_NOT_FOUND                   0x30006        /* HTTP request not found                               */
124 #define NX_WEB_HTTP_DATA_END                    0x30007        /* HTTP end of content area                             */
125 #define NX_WEB_HTTP_CALLBACK_COMPLETED          0x30008        /* HTTP user callback completed the processing          */
126 #define NX_WEB_HTTP_POOL_ERROR                  0x30009        /* HTTP supplied pool payload is too small              */
127 #define NX_WEB_HTTP_NOT_READY                   0x3000A        /* HTTP client not ready for operation                  */
128 #define NX_WEB_HTTP_GET_DONE                    0x3000C        /* HTTP client get is complete                          */
129 #define NX_WEB_HTTP_BAD_PACKET_LENGTH           0x3000D        /* Invalid packet received - length incorrect           */
130 #define NX_WEB_HTTP_REQUEST_UNSUCCESSFUL_CODE   0x3000E        /* Received an error code instead of 2xx from server    */
131 #define NX_WEB_HTTP_INCOMPLETE_PUT_ERROR        0x3000F        /* Server responds before PUT is complete               */
132 #define NX_WEB_HTTP_PASSWORD_TOO_LONG           0x30011        /* Password exceeded expected length                    */
133 #define NX_WEB_HTTP_USERNAME_TOO_LONG           0x30012        /* Username exceeded expected length                    */
134 #define NX_WEB_HTTP_NO_QUERY_PARSED             0x30013        /* Server unable to find query in client request        */
135 #define NX_WEB_HTTP_METHOD_ERROR                0x30014        /* Client method (e.g. GET, POST) was missing required information. */
136 #define NX_WEB_HTTP_IMPROPERLY_TERMINATED_PARAM 0x30015        /* Client request parameter not properly terminated     */
137 #define NX_WEB_HTTP_BOUNDARY_ALREADY_FOUND      0x30016        /* Boundary is already found.                           */
138 #define NX_WEB_HTTP_MISSING_CONTENT_LENGTH      0x30017        /* The Content-Length header was not found.             */
139 #define NX_WEB_HTTP_EXTENSION_NOT_FOUND         0x30018        /* A searched-for HTTP type extension was not found.    */
140 #define NX_WEB_HTTP_EXTENSION_MIME_DEFAULT      0x30019        /* No matching extension found, return default.         */
141 #define NX_WEB_HTTP_STATUS_CODE_CONTINUE               0x3001A        /* "100 Continue"                                       */
142 #define NX_WEB_HTTP_STATUS_CODE_SWITCHING_PROTOCOLS    0x3001B        /* "101 Switching Protocols"                            */
143 #define NX_WEB_HTTP_STATUS_CODE_CREATED                0x3001C        /* "201 Created"                                        */
144 #define NX_WEB_HTTP_STATUS_CODE_ACCEPTED               0x3001D        /* "202 Accepted"                                       */
145 #define NX_WEB_HTTP_STATUS_CODE_NON_AUTH_INFO          0x3001E        /* "203 Non-Authoritative Information"                  */
146 #define NX_WEB_HTTP_STATUS_CODE_NO_CONTENT             0x3001F        /* "204 No Content"                                     */
147 #define NX_WEB_HTTP_STATUS_CODE_RESET_CONTENT          0x30020        /* "205 Reset Content"                                  */
148 #define NX_WEB_HTTP_STATUS_CODE_PARTIAL_CONTENT        0x30021        /* "206 Partial Content"                                */
149 #define NX_WEB_HTTP_STATUS_CODE_MULTIPLE_CHOICES       0x30022        /* "300 Multiple Choices"                               */
150 #define NX_WEB_HTTP_STATUS_CODE_MOVED_PERMANETLY       0x30023        /* "301 Moved Permanently"                              */
151 #define NX_WEB_HTTP_STATUS_CODE_FOUND                  0x30024        /* "302 Found"                                          */
152 #define NX_WEB_HTTP_STATUS_CODE_SEE_OTHER              0x30025        /* "303 See Other"                                      */
153 #define NX_WEB_HTTP_STATUS_CODE_NOT_MODIFIED           0x30026        /* "304 Not Modified"                                   */
154 #define NX_WEB_HTTP_STATUS_CODE_USE_PROXY              0x30027        /* "305 Use Proxy"                                      */
155 #define NX_WEB_HTTP_STATUS_CODE_TEMPORARY_REDIRECT     0x30028        /* "307 Temporary Redirect"                             */
156 #define NX_WEB_HTTP_STATUS_CODE_BAD_REQUEST            0x30029        /* "400 Bad Request"                                    */
157 #define NX_WEB_HTTP_STATUS_CODE_UNAUTHORIZED           0x3002A        /* "401 Unauthorized"                                   */
158 #define NX_WEB_HTTP_STATUS_CODE_PAYMENT_REQUIRED       0x3002B        /* "402 Payment Required"                               */
159 #define NX_WEB_HTTP_STATUS_CODE_FORBIDDEN              0x3002C        /* "403 Forbidden"                                      */
160 #define NX_WEB_HTTP_STATUS_CODE_NOT_FOUND              0x3002D        /* "404 Not Found"                                      */
161 #define NX_WEB_HTTP_STATUS_CODE_METHOD_NOT_ALLOWED     0x3002E        /* "405 Method Not Allowed"                             */
162 #define NX_WEB_HTTP_STATUS_CODE_NOT_ACCEPTABLE         0x3002F        /* "406 Not Acceptable"                                 */
163 #define NX_WEB_HTTP_STATUS_CODE_PROXY_AUTH_REQUIRED    0x30030        /* "407 Proxy Authentication Required"                  */
164 #define NX_WEB_HTTP_STATUS_CODE_REQUEST_TIMEOUT        0x30031        /* "408 Request Time-out"                               */
165 #define NX_WEB_HTTP_STATUS_CODE_CONFLICT               0x30032        /* "409 Conflict"                                       */
166 #define NX_WEB_HTTP_STATUS_CODE_GONE                   0x30033        /* "410 Gone"                                           */
167 #define NX_WEB_HTTP_STATUS_CODE_LENGTH_REQUIRED        0x30034        /* "411 Length Required"                                */
168 #define NX_WEB_HTTP_STATUS_CODE_PRECONDITION_FAILED    0x30035        /* "412 Precondition Failed"                            */
169 #define NX_WEB_HTTP_STATUS_CODE_ENTITY_TOO_LARGE       0x30036        /* "413 Request Entity Too Large"                       */
170 #define NX_WEB_HTTP_STATUS_CODE_URL_TOO_LARGE          0x30037        /* "414 Request-URL Too Large"                          */
171 #define NX_WEB_HTTP_STATUS_CODE_UNSUPPORTED_MEDIA      0x30038        /* "415 Unsupported Media Type"                         */
172 #define NX_WEB_HTTP_STATUS_CODE_RANGE_NOT_SATISFY      0x30039        /* "416 Requested range not satisfiable"                */
173 #define NX_WEB_HTTP_STATUS_CODE_EXPECTATION_FAILED     0x3003A        /* "417 Expectation Failed"                             */
174 #define NX_WEB_HTTP_STATUS_CODE_INTERNAL_ERROR         0x3003B        /* "500 Internal Server Error"                          */
175 #define NX_WEB_HTTP_STATUS_CODE_NOT_IMPLEMENTED        0x3003C        /* "501 Not Implemented"                                */
176 #define NX_WEB_HTTP_STATUS_CODE_BAD_GATEWAY            0x3003D        /* "502 Bad Gateway"                                    */
177 #define NX_WEB_HTTP_STATUS_CODE_SERVICE_UNAVAILABLE    0x3003E        /* "503 Service Unavailable"                            */
178 #define NX_WEB_HTTP_STATUS_CODE_GATEWAY_TIMEOUT        0x3003F        /* "504 Gateway Time-out"                               */
179 #define NX_WEB_HTTP_STATUS_CODE_VERSION_ERROR          0x30040        /* "505 HTTP Version not supported"                     */
180 #define NX_WEB_HTTP_AUTHENTICATION_ERROR               NX_WEB_HTTP_STATUS_CODE_UNAUTHORIZED        /* HTTP client authentication failed                    */
181 
182 /* Define the HTTP Server TCP port number */
183 
184 #define NX_WEB_HTTP_SERVER_PORT               80        /* Port for HTTP server                                 */
185 #define NX_WEB_HTTPS_SERVER_PORT             443        /* Port for HTTPS server.                               */
186 
187 /* Define constants for the various HTTP methods supported. */
188 #define NX_WEB_HTTP_METHOD_NONE             0x0
189 #define NX_WEB_HTTP_METHOD_GET              0x1
190 #define NX_WEB_HTTP_METHOD_PUT              0x2
191 #define NX_WEB_HTTP_METHOD_POST             0x3
192 #define NX_WEB_HTTP_METHOD_DELETE           0x4
193 #define NX_WEB_HTTP_METHOD_HEAD             0x5
194 
195 /* Define status codes. */
196 #define NX_WEB_HTTP_STATUS_CONTINUE             "100 Continue"
197 #define NX_WEB_HTTP_STATUS_SWITCHING_PROTOCOLS  "101 Switching Protocols"
198 #define NX_WEB_HTTP_STATUS_OK                   "200 OK"
199 #define NX_WEB_HTTP_STATUS_CREATED              "201 Created"
200 #define NX_WEB_HTTP_STATUS_ACCEPTED             "202 Accepted"
201 #define NX_WEB_HTTP_STATUS_NON_AUTH_INFO        "203 Non-Authoritative Information"
202 #define NX_WEB_HTTP_STATUS_NO_CONTENT           "204 No Content"
203 #define NX_WEB_HTTP_STATUS_RESET_CONTENT        "205 Reset Content"
204 #define NX_WEB_HTTP_STATUS_PARTIAL_CONTENT      "206 Partial Content"
205 #define NX_WEB_HTTP_STATUS_MULTIPLE_CHOICES     "300 Multiple Choices"
206 #define NX_WEB_HTTP_STATUS_MOVED_PERMANETLY     "301 Moved Permanently"
207 #define NX_WEB_HTTP_STATUS_FOUND                "302 Found"
208 #define NX_WEB_HTTP_STATUS_SEE_OTHER            "303 See Other"
209 #define NX_WEB_HTTP_STATUS_NOT_MODIFIED         "304 Not Modified"
210 #define NX_WEB_HTTP_STATUS_USE_PROXY            "305 Use Proxy"
211 #define NX_WEB_HTTP_STATUS_TEMPORARY_REDIRECT   "307 Temporary Redirect"
212 #define NX_WEB_HTTP_STATUS_BAD_REQUEST          "400 Bad Request"
213 #define NX_WEB_HTTP_STATUS_UNAUTHORIZED         "401 Unauthorized"
214 #define NX_WEB_HTTP_STATUS_PAYMENT_REQUIRED     "402 Payment Required"
215 #define NX_WEB_HTTP_STATUS_FORBIDDEN            "403 Forbidden"
216 #define NX_WEB_HTTP_STATUS_NOT_FOUND            "404 Not Found"
217 #define NX_WEB_HTTP_STATUS_METHOD_NOT_ALLOWED   "405 Method Not Allowed"
218 #define NX_WEB_HTTP_STATUS_NOT_ACCEPTABLE       "406 Not Acceptable"
219 #define NX_WEB_HTTP_STATUS_PROXY_AUTH_REQUIRED  "407 Proxy Authentication Required"
220 #define NX_WEB_HTTP_STATUS_REQUEST_TIMEOUT      "408 Request Time-out"
221 #define NX_WEB_HTTP_STATUS_CONFLICT             "409 Conflict"
222 #define NX_WEB_HTTP_STATUS_GONE                 "410 Gone"
223 #define NX_WEB_HTTP_STATUS_LENGTH_REQUIRED      "411 Length Required"
224 #define NX_WEB_HTTP_STATUS_PRECONDITION_FAILED  "412 Precondition Failed"
225 #define NX_WEB_HTTP_STATUS_ENTITY_TOO_LARGE     "413 Request Entity Too Large"
226 #define NX_WEB_HTTP_STATUS_URL_TOO_LARGE        "414 Request-URL Too Large"
227 #define NX_WEB_HTTP_STATUS_UNSUPPORTED_MEDIA    "415 Unsupported Media Type"
228 #define NX_WEB_HTTP_STATUS_RANGE_NOT_SATISFY    "416 Requested range not satisfiable"
229 #define NX_WEB_HTTP_STATUS_EXPECTATION_FAILED   "417 Expectation Failed"
230 #define NX_WEB_HTTP_STATUS_INTERNAL_ERROR       "500 Internal Server Error"
231 #define NX_WEB_HTTP_STATUS_NOT_IMPLEMENTED      "501 Not Implemented"
232 #define NX_WEB_HTTP_STATUS_BAD_GATEWAY          "502 Bad Gateway"
233 #define NX_WEB_HTTP_STATUS_SERVICE_UNAVAILABLE  "503 Service Unavailable"
234 #define NX_WEB_HTTP_STATUS_GATEWAY_TIMEOUT      "504 Gateway Time-out"
235 #define NX_WEB_HTTP_STATUS_VERSION_ERROR        "505 HTTP Version not supported"
236 
237 
238 /* Define the max length of header field. */
239 
240 #ifndef NX_WEB_HTTP_MAX_HEADER_FIELD
241 #define NX_WEB_HTTP_MAX_HEADER_FIELD            256
242 #endif
243 
244 /* Determine if a C++ compiler is being used.  If so, complete the standard
245    C conditional started above.  */
246 #ifdef   __cplusplus
247         }
248 #endif
249 
250 #endif  /* NX_WEB_HTTP_COMMON_H */
251