1 /* This case tests chunked request.  */
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 static char pkt[] = {
78 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
79 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
80 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
81 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
82 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
83 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
84 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
85 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
86 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
87 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
88 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
89 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
90 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
91 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
92 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
93 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
94 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
95 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
96 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
97 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
98 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
99 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
100 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
101 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
102 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
103 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
104 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
105 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
106 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
107 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
108 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
109 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
110 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
111 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
112 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
113 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, /* testtest */
114 };
115 
116 static UINT test_chunk[] = {20, 120, 19, 34};
117 static UINT test_num = sizeof(test_chunk)/sizeof(UINT);
118 
119 #ifdef CTEST
test_application_define(void * first_unused_memory)120 VOID test_application_define(void *first_unused_memory)
121 #else
122 void    netx_web_chunked_request_additional_test_application_define(void *first_unused_memory)
123 #endif
124 {
125 CHAR    *pointer;
126 UINT    status;
127 
128 
129     error_counter = 0;
130 
131     /* Setup the working pointer.  */
132     pointer =  (CHAR *) first_unused_memory;
133 
134     /* Create a helper thread for the server. */
135     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
136                      pointer, DEMO_STACK_SIZE,
137                      NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
138 
139     pointer =  pointer + DEMO_STACK_SIZE;
140 
141     /* Initialize the NetX system.  */
142     nx_system_initialize();
143 
144     /* Create the server packet pool.  */
145     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
146                                     pointer, SERVER_PACKET_SIZE*16);
147     pointer = pointer + SERVER_PACKET_SIZE * 16;
148     if (status)
149         error_counter++;
150 
151     /* Create an IP instance.  */
152     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
153                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
154                           pointer, 4096, 1);
155     pointer =  pointer + 4096;
156     if (status)
157         error_counter++;
158 
159     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
160     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
161     pointer = pointer + 1024;
162     if (status)
163         error_counter++;
164 
165      /* Enable TCP traffic.  */
166     status = nx_tcp_enable(&server_ip);
167     if (status)
168         error_counter++;
169 
170     /* Create the HTTP Client thread. */
171     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
172                               pointer, DEMO_STACK_SIZE,
173                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
174     pointer =  pointer + DEMO_STACK_SIZE;
175     if (status)
176         error_counter++;
177 
178     /* Create the Client packet pool.  */
179     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
180                                     pointer, CLIENT_PACKET_SIZE*16);
181     pointer = pointer + CLIENT_PACKET_SIZE * 16;
182     if (status)
183         error_counter++;
184 
185     /* Create an IP instance.  */
186     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
187                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
188                           pointer, 2048, 1);
189     pointer =  pointer + 2048;
190     if (status)
191         error_counter++;
192 
193     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
194     pointer =  pointer + 2048;
195     if (status)
196         error_counter++;
197 
198      /* Enable TCP traffic.  */
199     status = nx_tcp_enable(&client_ip);
200     if (status)
201         error_counter++;
202 }
203 
204 #ifdef NX_WEB_HTTPS_ENABLE
205 /* Define the TLS setup callback function.  */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)206 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
207 {
208 UINT status;
209 
210 
211     /* Initialize and create TLS session.  */
212     status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
213 
214     /* Check status.  */
215     if (status)
216     {
217         return(status);
218     }
219 
220     /* Allocate space for packet reassembly.  */
221     status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
222 
223     /* Check status.  */
224     if (status)
225     {
226         return(status);
227     }
228 
229     /* Add a CA Certificate to our trusted store for verifying incoming server certificates.  */
230     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);
231     nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
232 
233     /* Need to allocate space for the certificate coming in from the remote host.  */
234     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
235     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
236 
237     return(NX_SUCCESS);
238 }
239 #endif /* NX_WEB_HTTPS_ENABLE  */
240 
thread_client_entry(ULONG thread_input)241 void thread_client_entry(ULONG thread_input)
242 {
243 UINT            i, j;
244 UINT            status;
245 NX_PACKET      *send_packet, *recv_packet;
246 UINT            chunk_size = 0, more_data = NX_FALSE;
247 CHAR           *put_test;
248 
249 
250     /* Give IP task and driver a chance to initialize the system. */
251     tx_thread_sleep(NX_IP_PERIODIC_RATE);
252 
253     /* Set server IP address.  */
254     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
255     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
256 
257     /* First loop test HTTP, second loop test HTTPS.  */
258     for (i = 0; i < loop ; i++)
259     {
260         if (i == 0)
261         {
262 
263             /* Wait HTTP server started.  */
264             while(!http_server_start)
265             {
266                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
267             }
268         }
269 #ifdef NX_WEB_HTTPS_ENABLE
270         else
271         {
272 
273             /* Wait HTTPS server started.  */
274             while(!https_server_start)
275             {
276                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
277             }
278         }
279 #endif /* NX_WEB_HTTPS_ENABLE  */
280 
281 
282         /* Create an HTTP client instance.  */
283         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
284 
285         /* Check status.  */
286         if (status)
287             error_counter++;
288 
289 
290         /* Check status.  */
291         if (status)
292             error_counter++;
293 
294         /* Test chunked PUT request.  */
295         /* Connect to server.  */
296         if (i == 0)
297         {
298             status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_WAIT_FOREVER);
299             put_test = "http_put_test.html";
300         }
301 #ifdef NX_WEB_HTTPS_ENABLE
302         else
303         {
304             status = nx_web_http_client_secure_connect(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
305                                                        tls_setup_callback, NX_WAIT_FOREVER);
306             put_test = "https_put_test.html";
307         }
308 #endif /* NX_WEB_HTTPS_ENABLE  */
309 
310         /* Check status.  */
311         if (status)
312             error_counter++;
313 
314         /* Initialize the request.  */
315         status = nx_web_http_client_request_initialize(&my_client, NX_WEB_HTTP_METHOD_PUT, put_test, "www.abc.com",
316                                                        0, NX_TRUE, "name", "password", NX_WAIT_FOREVER);
317 
318         /* Check status.  */
319         if (status)
320             error_counter++;
321 
322 #if 1
323 
324         /* For test, append chunked data to the header packet.  */
325         nx_packet_data_append(my_client.nx_web_http_client_request_packet_ptr, "\r\n19\r\n", 6, my_client.nx_web_http_client_packet_pool_ptr, NX_WAIT_FOREVER);
326         nx_packet_data_append(my_client.nx_web_http_client_request_packet_ptr, pkt, 25, my_client.nx_web_http_client_packet_pool_ptr, NX_WAIT_FOREVER);
327 #endif
328 
329         /* Send the request.  */
330         status = nx_web_http_client_request_send(&my_client, NX_WAIT_FOREVER);
331 
332         /* Check status.  */
333         if (status)
334             error_counter++;
335 
336         for (j = 0; j < test_num; j++)
337         {
338 
339             chunk_size = test_chunk[j];
340             if (j == test_num - 1)
341             {
342                 more_data = NX_FALSE;
343             }
344             else
345             {
346                 more_data = NX_TRUE;
347             }
348 
349             status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, NX_WAIT_FOREVER);
350             if (status)
351                 error_counter++;
352 
353             status = nx_web_http_client_request_chunked_set(&my_client, chunk_size, send_packet);
354             if (status)
355                 error_counter++;
356 
357             nx_packet_data_append(send_packet, pkt, chunk_size, my_client.nx_web_http_client_packet_pool_ptr, NX_WAIT_FOREVER);
358 
359             status = nx_web_http_client_request_packet_send(&my_client, send_packet, more_data, 1 * NX_IP_PERIODIC_RATE);
360             if (status)
361             {
362                 nx_packet_release(send_packet);
363                 error_counter++;
364             }
365         }
366 
367         /* Get response from server.  */
368         while (1)
369         {
370             status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
371 
372             if (status)
373             {
374                 break;
375             }
376             else
377             {
378                 nx_packet_release(recv_packet);
379             }
380         }
381 
382         /* Check status.  */
383         if (status != NX_WEB_HTTP_GET_DONE)
384             error_counter++;
385         else
386             nx_packet_release(recv_packet);
387 
388         /* Test chunked POST request.  */
389         /* Connect to server.  */
390         if (i == 0)
391         {
392             status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_WAIT_FOREVER);
393         }
394 #ifdef NX_WEB_HTTPS_ENABLE
395         else
396         {
397             status = nx_web_http_client_secure_connect(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
398                                                        tls_setup_callback, NX_WAIT_FOREVER);
399         }
400 #endif /* NX_WEB_HTTPS_ENABLE  */
401 
402         /* Check status.  */
403         if (status)
404             error_counter++;
405 
406         /* Initialize the request.  */
407         status = nx_web_http_client_request_initialize(&my_client, NX_WEB_HTTP_METHOD_POST, put_test, "www.abc.com",
408                                                        0, NX_TRUE, "name", "password", NX_WAIT_FOREVER);
409 
410         /* Check status.  */
411         if (status)
412             error_counter++;
413 
414         /* Send the request.  */
415         status = nx_web_http_client_request_send(&my_client, NX_WAIT_FOREVER);
416 
417         /* Check status.  */
418         if (status)
419             error_counter++;
420 
421         for (j = 0; j < test_num; j++)
422         {
423 
424             chunk_size = test_chunk[j];
425             if (j == test_num - 1)
426             {
427                 more_data = NX_FALSE;
428             }
429             else
430             {
431                 more_data = NX_TRUE;
432             }
433 
434             status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, NX_WAIT_FOREVER);
435             if (status)
436                 error_counter++;
437 
438             status = nx_web_http_client_request_chunked_set(&my_client, chunk_size, send_packet);
439             if (status)
440                 error_counter++;
441 
442             if (j == 1)
443             {
444                 chunk_size = (chunk_size >> 1);
445             }
446 
447             nx_packet_data_append(send_packet, pkt, chunk_size, my_client.nx_web_http_client_packet_pool_ptr, NX_WAIT_FOREVER);
448 
449             status = nx_web_http_client_request_packet_send(&my_client, send_packet, more_data, 1 * NX_IP_PERIODIC_RATE);
450             if (status)
451             {
452                 nx_packet_release(send_packet);
453                 error_counter++;
454             }
455 
456             /* Test one chunk in multiple request packets.  */
457             if (j == 1)
458             {
459                 status = nx_web_http_client_request_packet_allocate(&my_client, &send_packet, NX_WAIT_FOREVER);
460                 if (status)
461                     error_counter++;
462 
463                 nx_packet_data_append(send_packet, pkt, chunk_size, my_client.nx_web_http_client_packet_pool_ptr, NX_WAIT_FOREVER);
464 
465                 status = nx_web_http_client_request_packet_send(&my_client, send_packet, more_data, 1 * NX_IP_PERIODIC_RATE);
466                 if (status)
467                 {
468                     nx_packet_release(send_packet);
469                     error_counter++;
470                 }
471             }
472         }
473 
474         /* Get response from server.  */
475         while (1)
476         {
477             status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
478 
479             if (status)
480             {
481                 break;
482             }
483             else
484             {
485                 nx_packet_release(recv_packet);
486             }
487         }
488 
489         /* Check status.  */
490         if (status != NX_WEB_HTTP_GET_DONE)
491             error_counter++;
492         else
493             nx_packet_release(recv_packet);
494 
495         status = nx_web_http_client_delete(&my_client);
496         if (status)
497            error_counter++;
498 
499         /* Set the flag.  */
500         if (i == 0)
501         {
502             http_client_stop = 1;
503         }
504 #ifdef NX_WEB_HTTPS_ENABLE
505         else
506         {
507             https_client_stop = 1;
508         }
509 #endif /* NX_WEB_HTTPS_ENABLE  */
510     }
511 }
512 
513 
514 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)515 void    thread_server_entry(ULONG thread_input)
516 {
517 UINT            i;
518 UINT            status;
519 FX_FILE         my_file;
520 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
521 
522 
523     /* Print out test information banner.  */
524     printf("NetX Test:   Web Chunked Request Additional Test.......................");
525 
526     /* Check for earlier error. */
527     if(error_counter)
528     {
529         printf("ERROR!\n");
530         test_control_return(1);
531     }
532 
533     fx_media_format(&ram_disk,
534                     _fx_ram_driver,               // Driver entry
535                     ram_disk_memory,              // RAM disk memory pointer
536                     media_memory,                 // Media buffer pointer
537                     sizeof(media_memory),         // Media buffer size
538                     "MY_RAM_DISK",                // Volume Name
539                     1,                            // Number of FATs
540                     32,                           // Directory Entries
541                     0,                            // Hidden sectors
542                     256,                          // Total sectors
543                     512,                          // Sector size
544                     8,                            // Sectors per cluster
545                     1,                            // Heads
546                     1);                           // Sectors per track
547 
548     /* Open the RAM disk.  */
549     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
550     status += fx_file_create(&ram_disk, "index.htm");
551     status += fx_file_open(&ram_disk, &my_file, "index.htm", FX_OPEN_FOR_WRITE);
552     status += fx_file_write(&my_file, "https server", 12);
553     status += fx_file_close(&my_file);
554     if(status)
555         error_counter++;
556 
557     /* Give NetX a chance to initialize the system. */
558     tx_thread_sleep(NX_IP_PERIODIC_RATE);
559 
560     /* First loop test HTTP, second loop test HTTPS.  */
561     for (i = 0; i < loop; i++)
562     {
563 
564         if (i == 1)
565         {
566             server_port = NX_WEB_HTTPS_SERVER_PORT;
567         }
568 
569         /* Create the HTTP Server. */
570         status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
571                                            &server_stack, sizeof(server_stack), &server_pool,
572                                            NX_NULL, server_request_callback);
573         if (status)
574             error_counter++;
575 
576 #ifdef NX_WEB_HTTPS_ENABLE
577         /* Set TLS for HTTPS.  */
578         if (i == 1)
579         {
580             /* Initialize device certificate (used for all sessions in HTTPS server). */
581             memset(&certificate, 0, sizeof(certificate));
582             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);
583 
584             /* Setup TLS session data for the TCP server. */
585             status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
586                                                          crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
587                                                          &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
588             if (status)
589                 error_counter++;
590         }
591 #endif /* NX_WEB_HTTPS_ENABLE  */
592 
593         /* OK to start the HTTP Server.   */
594         status = nx_web_http_server_start(&my_server);
595         if (status)
596             error_counter++;
597 
598         /* Set the flag.  */
599         if (i == 0)
600         {
601             http_server_start = 1;
602 
603             /* Wait HTTP test finished.  */
604             while(!http_client_stop)
605             {
606                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
607             }
608         }
609 #ifdef NX_WEB_HTTPS_ENABLE
610         else
611         {
612             https_server_start = 1;
613 
614             /* Wait HTTPS test finished.  */
615             while(!https_client_stop)
616             {
617                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
618             }
619         }
620 #endif /* NX_WEB_HTTPS_ENABLE  */
621 
622         status = nx_web_http_server_delete(&my_server);
623         if (status)
624             error_counter++;
625     }
626 
627     /* Check packet pool.  */
628     if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
629     {
630         error_counter++;
631     }
632 
633     if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
634     {
635         error_counter++;
636     }
637 
638     if(error_counter)
639     {
640         printf("ERROR!\n");
641         test_control_return(1);
642     }
643     else
644     {
645         printf("SUCCESS!\n");
646         test_control_return(0);
647     }
648 }
649 
650 /* Define the server request callback function.  */
server_request_callback(NX_WEB_HTTP_SERVER * server_ptr,UINT request_type,CHAR * resource,NX_PACKET * packet_ptr)651 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr)
652 {
653 NX_PACKET   *chunked_pkt;
654 NX_PACKET   *response_pkt;
655 UINT         status;
656 UINT         i = 0;
657 UINT         content_length = 0;
658 UINT         total_length = 0;
659 
660     /* Process POST request.  */
661     if (request_type == NX_WEB_HTTP_SERVER_POST_REQUEST)
662     {
663 
664         for (i = 0; i < test_num; i++)
665         {
666             total_length += test_chunk[i];
667         }
668 
669         status = nx_web_http_server_packet_content_find(server_ptr, &packet_ptr, (ULONG *)&content_length);
670 
671         /* Release the received content packet.  */
672         nx_packet_release(packet_ptr);
673 
674         if (status)
675         {
676             return(status);
677         }
678 
679         while (1)
680         {
681             status = nx_web_http_server_packet_get(server_ptr, &chunked_pkt);
682             if (status)
683             {
684                 break;
685             }
686             else
687             {
688                 content_length += chunked_pkt -> nx_packet_length;
689                 nx_packet_release(chunked_pkt);
690             }
691         }
692 
693         if (content_length != total_length)
694         {
695             error_counter++;
696         }
697 
698        /* Generate HTTP header.  */
699         status = nx_web_http_server_callback_generate_response_header_extended(server_ptr, &response_pkt,
700                                                                                NX_WEB_HTTP_STATUS_OK, sizeof(NX_WEB_HTTP_STATUS_OK) - 1, 0,
701                                                                                "text/html", 9, NX_NULL, 0);
702         if (status)
703         {
704             return(status);
705         }
706 
707         status = nx_web_http_server_callback_packet_send(server_ptr, response_pkt);
708         if (status)
709         {
710             nx_packet_release(response_pkt);
711             return(status);
712         }
713     }
714     else
715     {
716         /* Indicate we have not processed the response to client yet.  */
717         return(NX_SUCCESS);
718     }
719 
720     /* Indicate the response to client is transmitted.  */
721     return(NX_WEB_HTTP_CALLBACK_COMPLETED);
722 }
723 
724 #else
725 
726 #ifdef CTEST
test_application_define(void * first_unused_memory)727 VOID test_application_define(void *first_unused_memory)
728 #else
729 void    netx_web_chunked_request_additional_test_application_define(void *first_unused_memory)
730 #endif
731 {
732 
733     /* Print out test information banner.  */
734     printf("NetX Test:   Web Chunked Request Additional Test.......................N/A\n");
735 
736     test_control_return(3);
737 }
738 #endif
739