1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Duo Component                                                    */
17 /**                                                                       */
18 /**   Hypertext Transfer Protocol (HTTP)                                  */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
27 /*                                                                        */
28 /*    nxd_http_client.h                                   PORTABLE C      */
29 /*                                                           6.1.6        */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Yuxin Zhou, Microsoft Corporation                                   */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the NetX Hypertext Transfer Protocol (HTTP)       */
37 /*    component, including all data types and external references.        */
38 /*    It is assumed that nx_api.h and nx_port.h have already been         */
39 /*    included, along with fx_api.h and fx_port.h.                        */
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 NXD_HTTP_CLIENT_H
56 #define NXD_HTTP_CLIENT_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 
62 #ifdef   __cplusplus
63 
64 /* Yes, C++ compiler is present.  Use standard C.  */
65 extern   "C" {
66 
67 #endif
68 
69 #include    "nx_api.h"
70 
71 /* Define the HTTP ID.  */
72 
73 #define NXD_HTTP_CLIENT_ID                  0x48545450UL
74 
75 
76 /* Define HTTP TCP socket create options.  */
77 
78 #ifndef NX_HTTP_TYPE_OF_SERVICE
79 #define NX_HTTP_TYPE_OF_SERVICE             NX_IP_NORMAL
80 #endif
81 
82 #ifndef NX_HTTP_FRAGMENT_OPTION
83 #define NX_HTTP_FRAGMENT_OPTION             NX_DONT_FRAGMENT
84 #endif
85 
86 #ifndef NX_HTTP_TIME_TO_LIVE
87 #define NX_HTTP_TIME_TO_LIVE                0x80
88 #endif
89 
90 #ifndef NX_HTTP_MAX_RESOURCE
91 #define NX_HTTP_MAX_RESOURCE                40
92 #endif
93 
94 #ifndef NX_HTTP_MAX_NAME
95 #define NX_HTTP_MAX_NAME                    20
96 #endif
97 
98 #ifndef NX_HTTP_MAX_PASSWORD
99 #define NX_HTTP_MAX_PASSWORD                20
100 #endif
101 
102 #ifndef NX_HTTP_CLIENT_TIMEOUT
103 #define NX_HTTP_CLIENT_TIMEOUT              (10 * NX_IP_PERIODIC_RATE)
104 #endif
105 
106 #ifndef NX_PHYSICAL_TRAILER
107 #define NX_PHYSICAL_TRAILER                 4
108 #endif
109 
110 #ifndef NX_HTTP_CLIENT_MIN_PACKET_SIZE
111 #define NX_HTTP_CLIENT_MIN_PACKET_SIZE      600
112 #endif
113 
114 /* NX_HTTP_MAX_STRING is base64 of "name:password" and plus 1 if an extra conversion is needed and plus 2 pad if needed. */
115 #define NX_HTTP_MAX_STRING                  ((NX_HTTP_MAX_NAME + NX_HTTP_MAX_PASSWORD + 1) * 4 / 3 + 1 + 2)
116 #define NX_HTTP_MAX_BINARY_MD5              16
117 #define NX_HTTP_MAX_ASCII_MD5               32
118 
119 
120 /* Define HTTP Client states.  */
121 
122 #define NX_HTTP_CLIENT_STATE_READY          1
123 #define NX_HTTP_CLIENT_STATE_GET            2
124 #define NX_HTTP_CLIENT_STATE_PUT            3
125 
126 
127 /* Define return code constants.  */
128 
129 #define NX_HTTP_ERROR                       0xE0        /* HTTP internal error                                  */
130 #define NX_HTTP_TIMEOUT                     0xE1        /* HTTP timeout occurred                                */
131 #define NX_HTTP_FAILED                      0xE2        /* HTTP error                                           */
132 #define NX_HTTP_DONT_AUTHENTICATE           0xE3        /* HTTP authentication not needed                       */
133 #define NX_HTTP_BASIC_AUTHENTICATE          0xE4        /* HTTP basic authentication requested                  */
134 #define NX_HTTP_DIGEST_AUTHENTICATE         0xE5        /* HTTP digest authentication requested                 */
135 #define NX_HTTP_NOT_FOUND                   0xE6        /* HTTP request not found                               */
136 #define NX_HTTP_DATA_END                    0xE7        /* HTTP end of content area                             */
137 #define NX_HTTP_CALLBACK_COMPLETED          0xE8        /* HTTP user callback completed the processing          */
138 #define NX_HTTP_POOL_ERROR                  0xE9        /* HTTP supplied pool payload is too small              */
139 #define NX_HTTP_NOT_READY                   0xEA        /* HTTP client not ready for operation                  */
140 #define NX_HTTP_AUTHENTICATION_ERROR        0xEB        /* HTTP client authentication failed                    */
141 #define NX_HTTP_GET_DONE                    0xEC        /* HTTP client get is complete                          */
142 #define NX_HTTP_BAD_PACKET_LENGTH           0xED        /* Invalid packet received - length incorrect           */
143 #define NX_HTTP_REQUEST_UNSUCCESSFUL_CODE   0xEE        /* Received an error code instead of 2xx from server    */
144 #define NX_HTTP_INCOMPLETE_PUT_ERROR        0xEF        /* Server responds before PUT is complete               */
145 #define NX_HTTP_PASSWORD_TOO_LONG           0xF0        /* Password exceeded expected length                    */
146 #define NX_HTTP_USERNAME_TOO_LONG           0xF1        /* Username exceeded expected length                    */
147 #define NX_HTTP_NO_QUERY_PARSED             0xF2        /* Server unable to find query in client request        */
148 
149 /* Define the default HTTP Server TCP port number. To change this at runtime
150    see nx_http_client_set_connect_port(). */
151 
152 #define NX_HTTP_SERVER_PORT                 80
153 
154 
155 #ifdef  NX_HTTP_DIGEST_ENABLE
156 
157 /* Include the MD5 digest header file.  */
158 
159 #include "nx_md5.h"
160 
161 #endif
162 
163 
164 /* Define the HTTP Client data structure.  */
165 
166 typedef struct NX_HTTP_CLIENT_STRUCT
167 {
168     ULONG           nx_http_client_id;                              /* HTTP Server ID                       */
169     CHAR           *nx_http_client_name;                            /* Name of this HTTP Client             */
170     UINT            nx_http_client_state;                           /* Current state of HTTP Client         */
171     UINT            nx_http_client_connect_port;                    /* Client port to connect to the server */
172     NX_IP          *nx_http_client_ip_ptr;                          /* Pointer to associated IP structure   */
173     NX_PACKET_POOL *nx_http_client_packet_pool_ptr;                 /* Pointer to HTTP Client packet pool   */
174     ULONG           nx_http_client_total_transfer_bytes;            /* Total number of bytes to transfer    */
175     ULONG           nx_http_client_actual_bytes_transferred;        /* Number of bytes actually transferred */
176     NX_PACKET      *nx_http_client_first_packet;                    /* Pointer to first packet with data    */
177     NX_TCP_SOCKET   nx_http_client_socket;                          /* HTTP Client TCP socket               */
178 #ifdef  NX_HTTP_DIGEST_ENABLE
179     NX_MD5          nx_http_client_md5data;                         /* HTTP Client MD5 work area            */
180 #endif
181 } NX_HTTP_CLIENT;
182 
183 
184 
185 #ifndef NX_HTTP_SOURCE_CODE
186 
187 /* Application caller is present, perform API mapping.  */
188 
189 /* Determine if error checking is desired.  If so, map API functions
190    to the appropriate error checking front-ends.  Otherwise, map API
191    functions to the core functions that actually perform the work.
192    Note: error checking is enabled by default.  */
193 
194 #ifdef NX_DISABLE_ERROR_CHECKING
195 
196 /* Services without error checking.  */
197 
198 #define nx_http_client_create                       _nx_http_client_create
199 #define nx_http_client_delete                       _nx_http_client_delete
200 #define nx_http_client_get_start                    _nx_http_client_get_start
201 #define nx_http_client_get_start_extended           _nx_http_client_get_start_extended
202 #define nx_http_client_get_packet                   _nx_http_client_get_packet
203 #define nx_http_client_put_start                    _nx_http_client_put_start
204 #define nx_http_client_put_start_extended           _nx_http_client_put_start_extended
205 #define nx_http_client_put_packet                   _nx_http_client_put_packet
206 #define nx_http_client_set_connect_port             _nx_http_client_set_connect_port
207 #define nxd_http_client_get_start                   _nxd_http_client_get_start
208 #define nxd_http_client_get_start_extended          _nxd_http_client_get_start_extended
209 #define nxd_http_client_put_start                   _nxd_http_client_put_start
210 #define nxd_http_client_put_start_extended          _nxd_http_client_put_start_extended
211 
212 #else
213 
214 /* Services with error checking.  */
215 
216 #define nx_http_client_create(p,n,i,pp,w)           _nxe_http_client_create(p,n,i,pp,w, sizeof(NX_HTTP_CLIENT))
217 #define nx_http_client_delete                       _nxe_http_client_delete
218 #define nx_http_client_get_start                    _nxe_http_client_get_start
219 #define nx_http_client_get_start_extended           _nxe_http_client_get_start_extended
220 #define nx_http_client_get_packet                   _nxe_http_client_get_packet
221 #define nx_http_client_put_start                    _nxe_http_client_put_start
222 #define nx_http_client_put_start_extended           _nxe_http_client_put_start_extended
223 #define nx_http_client_put_packet                   _nxe_http_client_put_packet
224 #define nx_http_client_set_connect_port             _nxe_http_client_set_connect_port
225 #define nxd_http_client_put_start                   _nxde_http_client_put_start
226 #define nxd_http_client_put_start_extended          _nxde_http_client_put_start_extended
227 #define nxd_http_client_get_start                   _nxde_http_client_get_start
228 #define nxd_http_client_get_start_extended          _nxde_http_client_get_start_extended
229 
230 #endif  /* NX_DISABLE_ERROR_CHECKING */
231 
232 /* Define the prototypes accessible to the application software.  */
233 
234 #ifdef NX_DISABLE_ERROR_CHECKING
235 UINT        _nx_http_client_create(NX_HTTP_CLIENT *client_ptr, CHAR *client_name, NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, ULONG window_size);
236 #else
237 UINT        _nxe_http_client_create(NX_HTTP_CLIENT *client_ptr, CHAR *client_name, NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, ULONG window_size, UINT http_client_size);
238 #endif  /* NX_DISABLE_ERROR_CHECKING */
239 UINT        nx_http_client_delete(NX_HTTP_CLIENT *client_ptr);
240 UINT        nx_http_client_get_start(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, CHAR *input_ptr, UINT input_size, CHAR *username, CHAR *password, ULONG wait_option);
241 UINT        nx_http_client_get_start_extended(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, UINT resource_length, CHAR *input_ptr, UINT input_size, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG wait_option);
242 UINT        nx_http_client_get_packet(NX_HTTP_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option);
243 UINT        nx_http_client_put_start(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, CHAR *username, CHAR *password, ULONG total_bytes, ULONG wait_option);
244 UINT        nx_http_client_put_start_extended(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, UINT resource_length, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG total_bytes, ULONG wait_option);
245 UINT        nx_http_client_put_packet(NX_HTTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, ULONG wait_option);
246 UINT        nx_http_client_set_connect_port(NX_HTTP_CLIENT *client_ptr, UINT port);
247 UINT        nxd_http_client_get_start(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, CHAR *input_ptr, UINT input_size, CHAR *username, CHAR *password, ULONG wait_option);
248 UINT        nxd_http_client_get_start_extended(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, UINT resource_length, CHAR *input_ptr, UINT input_size, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG wait_option);
249 UINT        nxd_http_client_put_start(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, CHAR *username, CHAR *password, ULONG total_bytes, ULONG wait_option);
250 UINT        nxd_http_client_put_start_extended(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, UINT resource_length, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG total_bytes, ULONG wait_option);
251 
252 #else
253 
254 /* HTTP source code is being compiled, do not perform any API mapping.  */
255 
256 UINT        _nxe_http_client_create(NX_HTTP_CLIENT *client_ptr, CHAR *client_name, NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, ULONG window_size, UINT http_client_size);
257 UINT        _nx_http_client_create(NX_HTTP_CLIENT *client_ptr, CHAR *client_name, NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, ULONG window_size);
258 UINT        _nxe_http_client_delete(NX_HTTP_CLIENT *client_ptr);
259 UINT        _nx_http_client_delete(NX_HTTP_CLIENT *client_ptr);
260 UINT        _nxe_http_client_get_start(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, CHAR *input_ptr, UINT input_size, CHAR *username, CHAR *password, ULONG wait_option);
261 UINT        _nx_http_client_get_start(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, CHAR *input_ptr, UINT input_size, CHAR *username, CHAR *password, ULONG wait_option);
262 UINT        _nxe_http_client_get_start_extended(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, UINT resource_length, CHAR *input_ptr, UINT input_size, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG wait_option);
263 UINT        _nx_http_client_get_start_extended(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, UINT resource_length, CHAR *input_ptr, UINT input_size, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG wait_option);
264 UINT        _nxe_http_client_get_packet(NX_HTTP_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option);
265 UINT        _nx_http_client_get_packet(NX_HTTP_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option);
266 UINT        _nxe_http_client_put_start(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, CHAR *username, CHAR *password, ULONG total_bytes, ULONG wait_option);
267 UINT        _nx_http_client_put_start(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, CHAR *username, CHAR *password, ULONG total_bytes, ULONG wait_option);
268 UINT        _nxe_http_client_put_start_extended(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, UINT resource_length, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG total_bytes, ULONG wait_option);
269 UINT        _nx_http_client_put_start_extended(NX_HTTP_CLIENT *client_ptr, ULONG ip_address, CHAR *resource, UINT resource_length, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG total_bytes, ULONG wait_option);
270 UINT        _nxe_http_client_put_packet(NX_HTTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, ULONG wait_option);
271 UINT        _nx_http_client_put_packet(NX_HTTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, ULONG wait_option);
272 UINT        _nxe_http_client_set_connect_port(NX_HTTP_CLIENT *client_ptr, UINT port);
273 UINT        _nx_http_client_set_connect_port(NX_HTTP_CLIENT *client_ptr, UINT port);
274 UINT        _nxde_http_client_get_start(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, CHAR *input_ptr, UINT input_size, CHAR *username, CHAR *password, ULONG wait_option);
275 UINT        _nxd_http_client_get_start(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, CHAR *input_ptr, UINT input_size, CHAR *username, CHAR *password, ULONG wait_option);
276 UINT        _nxde_http_client_get_start_extended(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, UINT resource_length, CHAR *input_ptr, UINT input_size, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG wait_option);
277 UINT        _nxd_http_client_get_start_extended(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, UINT resource_length, CHAR *input_ptr, UINT input_size, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG wait_option);
278 UINT        _nxde_http_client_put_start(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, CHAR *username, CHAR *password, ULONG total_bytes, ULONG wait_option);
279 UINT        _nxd_http_client_put_start(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, CHAR *username, CHAR *password, ULONG total_bytes, ULONG wait_option);
280 UINT        _nxde_http_client_put_start_extended(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, UINT resource_length, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG total_bytes, ULONG wait_option);
281 UINT        _nxd_http_client_put_start_extended(NX_HTTP_CLIENT *client_ptr, NXD_ADDRESS *ip_address, CHAR *resource, UINT resource_length, CHAR *username, UINT username_length, CHAR *password, UINT password_length, ULONG total_bytes, ULONG wait_option);
282 
283 /* Define internal HTTP functions.  */
284 
285 UINT        _nx_http_client_type_get(CHAR *name, CHAR *http_type_string);
286 UINT        _nx_http_client_content_length_get(NX_PACKET *packet_ptr);
287 UINT        _nx_http_client_calculate_content_offset(NX_PACKET *packet_ptr);
288 UINT        _nx_http_client_number_convert(UINT number, CHAR *string);
289 
290 
291 #endif  /* NX_HTTP_SOURCE_CODE */
292 
293 /* Determine if a C++ compiler is being used.  If so, complete the standard
294    C conditional started above.  */
295 #ifdef   __cplusplus
296         }
297 #endif
298 
299 #endif  /* NXD_HTTP_CLIENT_H */
300