1 /* This case tests chunked response processing.  */
2 #include    "tx_api.h"
3 #include    "nx_api.h"
4 #include    "fx_api.h"
5 #include    "nx_web_http_client.h"
6 #include    "nx_web_http_server.h"
7 
8 extern void test_control_return(UINT);
9 
10 #if !defined(NX_DISABLE_IPV4)
11 
12 #include "test_device_cert.c"
13 #include "test_ca_cert.c"
14 #define ca_cert_der test_ca_cert_der
15 #define ca_cert_der_len test_ca_cert_der_len
16 
17 #define     DEMO_STACK_SIZE         4096
18 
19 /* Set up FileX and file memory resources. */
20 static CHAR             ram_disk_memory[4096];
21 static FX_MEDIA         ram_disk;
22 static UCHAR            media_memory[4096];
23 
24 static UCHAR            server_stack[16000];
25 
26 /* Define device drivers.  */
27 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
28 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
29 
30 /* Set up the HTTP client global variables. */
31 
32 #define         CLIENT_PACKET_SIZE  (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
33 
34 static TX_THREAD           client_thread;
35 static NX_PACKET_POOL      client_pool;
36 static NX_WEB_HTTP_CLIENT  my_client;
37 static NX_IP               client_ip;
38 static UINT                error_counter;
39 
40 /* Set up the HTTP server global variables */
41 
42 #define         SERVER_PACKET_SIZE  (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
43 
44 static NX_WEB_HTTP_SERVER  my_server;
45 static NX_PACKET_POOL      server_pool;
46 static TX_THREAD           server_thread;
47 static NX_IP               server_ip;
48 static NXD_ADDRESS         server_ip_address;
49 static UINT                http_server_start = 0;
50 static UINT                http_client_stop = 0;
51 
52 static void thread_client_entry(ULONG thread_input);
53 static void thread_server_entry(ULONG thread_input);
54 
55 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(192,168,0,105)
56 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(192,168,0,123)
57 
58 #ifdef NX_WEB_HTTPS_ENABLE
59 static UINT                https_server_start = 0;
60 static UINT                https_client_stop = 0;
61 static UINT loop = 2;
62 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
63 static CHAR crypto_metadata_server[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
64 static CHAR crypto_metadata_client[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
65 static UCHAR tls_packet_buffer[18500];
66 static NX_SECURE_X509_CERT certificate;
67 static NX_SECURE_X509_CERT trusted_certificate;
68 static NX_SECURE_X509_CERT remote_certificate, remote_issuer;
69 static UCHAR remote_cert_buffer[2000];
70 static UCHAR remote_issuer_buffer[2000];
71 #else
72 static UINT loop = 1;
73 #endif /* NX_WEB_HTTPS_ENABLE  */
74 
75 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr);
76 
77 #if 1
78 
79 static char pkt[] = {
80 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, /* HTTP/1.1 */
81 0x20, 0x32, 0x30, 0x30, 0x20, 0x0d, 0x0a, 0x43, /*  200 ..C */
82 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, /* ontent-T */
83 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, /* ype: tex */
84 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0d, /* t/plain. */
85 0x0a, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, /* .Transfe */
86 0x72, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, /* r-Encodi */
87 0x6e, 0x67, 0x3a, 0x20, 0x63, 0x68, 0x75, 0x6e, /* ng: chun */
88 0x6b, 0x65, 0x64, 0x0d, 0x0a, 0x0d, 0x0a,       /* ked....  */ /*71*/
89 0x31, 0x34, 0x0d, 0x0a, 0x74, 0x65, 0x73, 0x74, /* 14..test */
90 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
91 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
92 0x0d, 0x0a,                                     /* ..       */ /*26*/
93 0x30, 0x63, 0x0d, 0x0a, 0x74, 0x65, 0x73, 0x74, /* 0c..test */
94 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
95 0x0d, 0x0a,                                     /* ..       */ /*18*/
96 0x31, 0x34, 0x0d, 0x0a, 0x74, 0x65, 0x73, 0x74, /* 14..test */
97 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
98 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
99 0x0d, 0x0a,                                     /* ..       */ /*26*/
100 0x30, 0x0d, 0x0a, 0x0d, 0x0a,                   /* 0....    */ /*5*/
101 };
102 
103 /* Packet_length: 146, header length: 71, boday length: 75 */
104 static UINT test_pkt_frag[][5] =
105 {
106     {146, 0, 0, 0, 0},   /* headerxx\r\n....\r\nxx\r\n....\r\nxx\r\n....\r\n0\r\n\r\n */
107     {71, 75, 0, 0, 0},   /* header||xx\r\n....\r\nxx\r\n....\r\nxx\r\n....\r\n0\r\n\r\n */
108     {71, 73, 2, 0, 0},   /* header||xx\r\n....\r\nxx\r\n....\r\nxx\r\n....\r\n0\r\n||\r\n */
109     {71, 42, 2, 29, 2},   /* header||xx\r\n....\r\nxx\r\n....||\r\n||xx\r\n....\r\n0\r\n||\r\n */
110     {71, 26, 18, 26, 5}, /* header||xx\r\n....\r\n||xx\r\n....\r\n||xx\r\n....\r\n||0\r\n\r\n */
111     {84, 13, 16, 27, 6}, /* headerxx\r\n..||..\r\n||xx\r\n....||\r\nxx\r\n....\r||\n0\r\n\r\n */ /* */
112     {73, 25, 20, 16, 12},/* headerxx||\r\n....\r\nx||x\r\n....\r\nxx\r||\n..||..\r\n0\r\n\r\n */
113     {71, 23, 39, 12, 1}, /* header||xx\r\n..||..\r\nxx\r\n....\r\nxx\r\n..||..\r\n0\r\n\r||\n */
114 };
115 
116 static UINT test_content_length = 0x14 + 0xc + 0x14;
117 
118 #else
119 
120 static char pkt[] = {
121 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, /* HTTP/1.0 */
122 0x20, 0x32, 0x30, 0x30, 0x20, 0x0d, 0x0a, 0x43, /*  200 ..C */
123 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, /* ontent-T */
124 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, /* ype: tex */
125 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e, 0x0d, /* t/plain. */
126 0x0a, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, /* .Transfe */
127 0x72, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, /* r-Encodi */
128 0x6e, 0x67, 0x3a, 0x20, 0x63, 0x68, 0x75, 0x6e, /* ng: chun */
129 0x6b, 0x65, 0x64, 0x0d, 0x0a, 0x0d, 0x0a,       /* ked....  */ /*71*/
130 0x31, 0x34, 0x0d, 0x0a, 0x74, 0x65, 0x73, 0x74, /* 14..test */
131 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
132 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
133 0x0d, 0x0a,                                     /* ..       */ /*26*/
134 0x30, 0x63, 0x3B, 0x45, 0x4c, 0x0d, 0x0a, 0x74, 0x65, 0x73, 0x74, /* 0c;EL..test */
135 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
136 0x0d, 0x0a,                                     /* ..       */ /*18*/
137 0x31, 0x32, 0x34, 0x0d, 0x0a, 0x74, 0x65, 0x73, 0x74, /* 124..test */
138 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
139 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
140 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
141 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
142 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
143 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
144 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
145 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
146 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
147 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
148 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
149 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
150 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
151 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
152 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
153 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
154 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
155 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
156 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
157 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
158 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
159 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
160 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
161 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
162 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
163 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
164 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
165 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
166 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
167 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
168 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
169 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
170 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
171 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
172 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
173 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
174 0x0d, 0x0a,                                     /* ..       */ /*299*/
175 0x30, 0x0d, 0x0a, 0x0d, 0x0a,                   /* 0....    */ /*5*/
176 };
177 
178 /* Packet_length: 419, header length: 71, boday length: 348 */
179 static UINT test_pkt_frag[][5] =
180 {
181     {422, 0, 0, 0, 0},   /* headerxx\r\n....\r\nxx\r\n....\r\nxx\r\n....\r\n0\r\n\r\n */
182 };
183 
184 static UINT test_content_length = 0x14 + 0xc + 0x124;
185 
186 #endif
187 
188 static UINT test_num = sizeof(test_pkt_frag)/(5 * sizeof(UINT));
189 static UINT test_count = 0;
190 
191 #ifdef CTEST
test_application_define(void * first_unused_memory)192 VOID test_application_define(void *first_unused_memory)
193 #else
194 void    netx_web_chunked_response_process_test_application_define(void *first_unused_memory)
195 #endif
196 {
197 CHAR    *pointer;
198 UINT    status;
199 
200 
201     error_counter = 0;
202 
203     /* Setup the working pointer.  */
204     pointer =  (CHAR *) first_unused_memory;
205 
206     /* Create a helper thread for the server. */
207     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
208                      pointer, DEMO_STACK_SIZE,
209                      NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
210 
211     pointer =  pointer + DEMO_STACK_SIZE;
212 
213     /* Initialize the NetX system.  */
214     nx_system_initialize();
215 
216     /* Create the server packet pool.  */
217     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
218                                     pointer, SERVER_PACKET_SIZE*16);
219     pointer = pointer + SERVER_PACKET_SIZE * 16;
220     if (status)
221         error_counter++;
222 
223     /* Create an IP instance.  */
224     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
225                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
226                           pointer, 4096, 1);
227     pointer =  pointer + 4096;
228     if (status)
229         error_counter++;
230 
231     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
232     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
233     pointer = pointer + 1024;
234     if (status)
235         error_counter++;
236 
237      /* Enable TCP traffic.  */
238     status = nx_tcp_enable(&server_ip);
239     if (status)
240         error_counter++;
241 
242     /* Create the HTTP Client thread. */
243     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
244                               pointer, DEMO_STACK_SIZE,
245                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
246     pointer =  pointer + DEMO_STACK_SIZE;
247     if (status)
248         error_counter++;
249 
250     /* Create the Client packet pool.  */
251     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
252                                     pointer, CLIENT_PACKET_SIZE*16);
253     pointer = pointer + CLIENT_PACKET_SIZE * 16;
254     if (status)
255         error_counter++;
256 
257     /* Create an IP instance.  */
258     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
259                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
260                           pointer, 2048, 1);
261     pointer =  pointer + 2048;
262     if (status)
263         error_counter++;
264 
265     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
266     pointer =  pointer + 2048;
267     if (status)
268         error_counter++;
269 
270      /* Enable TCP traffic.  */
271     status = nx_tcp_enable(&client_ip);
272     if (status)
273         error_counter++;
274 }
275 
276 #ifdef NX_WEB_HTTPS_ENABLE
277 /* Define the TLS setup callback function.  */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)278 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
279 {
280 UINT status;
281 
282 
283     /* Initialize and create TLS session.  */
284     status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
285 
286     /* Check status.  */
287     if (status)
288     {
289         return(status);
290     }
291 
292     /* Allocate space for packet reassembly.  */
293     status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
294 
295     /* Check status.  */
296     if (status)
297     {
298         return(status);
299     }
300 
301     /* Add a CA Certificate to our trusted store for verifying incoming server certificates.  */
302     nx_secure_x509_certificate_initialize(&trusted_certificate, ca_cert_der, ca_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
303     nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
304 
305     /* Need to allocate space for the certificate coming in from the remote host.  */
306     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
307     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
308 
309     return(NX_SUCCESS);
310 }
311 #endif /* NX_WEB_HTTPS_ENABLE  */
312 
thread_client_entry(ULONG thread_input)313 void thread_client_entry(ULONG thread_input)
314 {
315 UINT            i;
316 UINT            status;
317 NX_PACKET       *recv_packet;
318 UINT            content_length = 0;
319 
320 
321     /* Give IP task and driver a chance to initialize the system. */
322     tx_thread_sleep(NX_IP_PERIODIC_RATE);
323 
324     /* Set server IP address.  */
325     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
326     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
327 
328     /* First loop test HTTP, second loop test HTTPS.  */
329     for (i = 0; i < loop ; i++)
330     {
331         if (i == 0)
332         {
333 
334             /* Wait HTTP server started.  */
335             while(!http_server_start)
336             {
337                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
338             }
339         }
340 #ifdef NX_WEB_HTTPS_ENABLE
341         else
342         {
343 
344             /* Wait HTTPS server started.  */
345             while(!https_server_start)
346             {
347                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
348             }
349         }
350 #endif /* NX_WEB_HTTPS_ENABLE  */
351 
352         for (test_count = 0; test_count < test_num; test_count++)
353         {
354 
355             /* Create an HTTP client instance.  */
356             status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
357 
358             /* Check status.  */
359             if (status)
360             {
361                 error_counter++;
362             }
363 
364             /* Send a GET request.  */
365             if (i == 0)
366             {
367                 status = nx_web_http_client_get_start(&my_client, &server_ip_address,
368                                                       NX_WEB_HTTP_SERVER_PORT, "/index.htm",
369                                                       "www.abc.com", "name", "password", NX_WAIT_FOREVER);
370             }
371 #ifdef NX_WEB_HTTPS_ENABLE
372             else
373             {
374                 status = nx_web_http_client_get_secure_start(&my_client, &server_ip_address,
375                                                              NX_WEB_HTTPS_SERVER_PORT, "/index.htm",
376                                                              "www.abc.com", "name", "password",
377                                                              tls_setup_callback, NX_WAIT_FOREVER);
378             }
379 #endif /* NX_WEB_HTTPS_ENABLE  */
380 
381             /* Check status.  */
382             if (status)
383             {
384                 printf("\n%d connect fail!\n", status);
385                 error_counter++;
386             }
387 
388             content_length = 0;
389 
390             /* Get response from server.  */
391             while (1)
392             {
393                 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
394 
395                 if (status)
396                 {
397                     if (status == NX_WEB_HTTP_GET_DONE)
398                     {
399                         nx_packet_release(recv_packet);
400                     }
401                     break;
402                 }
403                 else
404                 {
405                     content_length += recv_packet -> nx_packet_length;
406                     nx_packet_release(recv_packet);
407                 }
408             }
409 
410             /* Check status.  */
411             if ((status != NX_WEB_HTTP_GET_DONE) || (content_length != test_content_length))
412             {
413                 error_counter++;
414             }
415 
416             /* Check if all the packets are released.  */
417             if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
418             {
419                 printf("\npacket leak!\n");
420                 error_counter++;
421             }
422 
423             status = nx_web_http_client_delete(&my_client);
424             if (status)
425                error_counter++;
426         }
427 
428         /* Set the flag.  */
429         if (i == 0)
430         {
431             http_client_stop = 1;
432         }
433 #ifdef NX_WEB_HTTPS_ENABLE
434         else
435         {
436             https_client_stop = 1;
437         }
438 #endif /* NX_WEB_HTTPS_ENABLE  */
439     }
440 }
441 
442 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)443 void    thread_server_entry(ULONG thread_input)
444 {
445 UINT            i;
446 UINT            status;
447 FX_FILE         my_file;
448 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
449 
450 
451     /* Print out test information banner.  */
452     printf("NetX Test:   Web Chunked Response Process Test.........................");
453 
454     /* Check for earlier error. */
455     if(error_counter)
456     {
457         printf("ERROR!\n");
458         test_control_return(1);
459     }
460 
461     fx_media_format(&ram_disk,
462                     _fx_ram_driver,               // Driver entry
463                     ram_disk_memory,              // RAM disk memory pointer
464                     media_memory,                 // Media buffer pointer
465                     sizeof(media_memory),         // Media buffer size
466                     "MY_RAM_DISK",                // Volume Name
467                     1,                            // Number of FATs
468                     32,                           // Directory Entries
469                     0,                            // Hidden sectors
470                     256,                          // Total sectors
471                     512,                          // Sector size
472                     8,                            // Sectors per cluster
473                     1,                            // Heads
474                     1);                           // Sectors per track
475 
476     /* Open the RAM disk.  */
477     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
478     status += fx_file_create(&ram_disk, "index.htm");
479     status += fx_file_open(&ram_disk, &my_file, "index.htm", FX_OPEN_FOR_WRITE);
480     status += fx_file_write(&my_file, "https server", 12);
481     status += fx_file_close(&my_file);
482     if(status)
483         error_counter++;
484 
485     /* Give NetX a chance to initialize the system. */
486     tx_thread_sleep(NX_IP_PERIODIC_RATE);
487 
488     /* First loop test HTTP, second loop test HTTPS.  */
489     for (i = 0; i < loop; i++)
490     {
491 
492         if (i == 1)
493         {
494             server_port = NX_WEB_HTTPS_SERVER_PORT;
495         }
496 
497         /* Create the HTTP Server. */
498         status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
499                                            &server_stack, sizeof(server_stack), &server_pool,
500                                            NX_NULL, server_request_callback);
501         if (status)
502             error_counter++;
503 
504 #ifdef NX_WEB_HTTPS_ENABLE
505         /* Set TLS for HTTPS.  */
506         if (i == 1)
507         {
508             /* Initialize device certificate (used for all sessions in HTTPS server). */
509             memset(&certificate, 0, sizeof(certificate));
510             nx_secure_x509_certificate_initialize(&certificate, test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, test_device_cert_key_der, test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
511 
512             /* Setup TLS session data for the TCP server. */
513             status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
514                                                          crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
515                                                          &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
516             if (status)
517                 error_counter++;
518         }
519 #endif /* NX_WEB_HTTPS_ENABLE  */
520 
521         /* OK to start the HTTP Server.   */
522         status = nx_web_http_server_start(&my_server);
523         if (status)
524             error_counter++;
525 
526         /* Set the flag.  */
527         if (i == 0)
528         {
529             http_server_start = 1;
530 
531             /* Wait HTTP test finished.  */
532             while(!http_client_stop)
533             {
534                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
535             }
536         }
537 #ifdef NX_WEB_HTTPS_ENABLE
538         else
539         {
540             https_server_start = 1;
541 
542             /* Wait HTTPS test finished.  */
543             while(!https_client_stop)
544             {
545                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
546             }
547         }
548 #endif /* NX_WEB_HTTPS_ENABLE  */
549 
550         status = nx_web_http_server_delete(&my_server);
551         if (status)
552             error_counter++;
553     }
554 
555     /* Check packet pool.  */
556     if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
557     {
558         error_counter++;
559     }
560 
561     if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
562     {
563         error_counter++;
564     }
565 
566     if(error_counter)
567     {
568         printf("ERROR!\n");
569         test_control_return(1);
570     }
571     else
572     {
573         printf("SUCCESS!\n");
574         test_control_return(0);
575     }
576 }
577 
578 /* Define the server request callback function.  */
server_request_callback(NX_WEB_HTTP_SERVER * server_ptr,UINT request_type,CHAR * resource,NX_PACKET * packet_ptr)579 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr)
580 {
581 NX_PACKET   *response_pkt;
582 UINT         status;
583 UINT         i = 0;
584 UCHAR       *data_ptr;
585 
586     /* Process multipart data.  */
587     if(request_type == NX_WEB_HTTP_SERVER_GET_REQUEST)
588     {
589         data_ptr = pkt;
590 
591         for (i = 0; i < 5; i++)
592         {
593             if (!test_pkt_frag[test_count][i])
594                 break;
595 
596             /* Allocate a response packet.  */
597             status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
598 
599             /* Check status.  */
600             if (status)
601             {
602                 error_counter++;
603             }
604 
605             status = nx_packet_data_append(response_pkt, data_ptr, test_pkt_frag[test_count][i], &server_pool, NX_WAIT_FOREVER);
606 
607             if (status == NX_SUCCESS)
608             {
609                 if (nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
610                 {
611                     nx_packet_release(response_pkt);
612                 }
613             }
614 
615             data_ptr += test_pkt_frag[test_count][i];
616         }
617 
618     }
619     else
620     {
621         /* Indicate we have not processed the response to client yet.  */
622         return(NX_SUCCESS);
623     }
624 
625     /* Indicate the response to client is transmitted.  */
626     return(NX_WEB_HTTP_CALLBACK_COMPLETED);
627 }
628 
629 #else
630 
631 #ifdef CTEST
test_application_define(void * first_unused_memory)632 VOID test_application_define(void *first_unused_memory)
633 #else
634 void    netx_web_chunked_response_process_test_application_define(void *first_unused_memory)
635 #endif
636 {
637 
638     /* Print out test information banner.  */
639     printf("NetX Test:   Web Chunked Response Process Test.........................N/A\n");
640 
641     test_control_return(3);
642 }
643 #endif
644