1 /* This case tests HTTP/1.1 server establish a persistent connection with a HTTP/1.1 client. */
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) && !defined(NX_WEB_HTTP_KEEPALIVE_DISABLE)
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(1,2,3,4)
56 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
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 test_count;
76 
77 #ifdef CTEST
test_application_define(void * first_unused_memory)78 VOID test_application_define(void *first_unused_memory)
79 #else
80 void    netx_web_keep_alive_test_application_define(void *first_unused_memory)
81 #endif
82 {
83 CHAR    *pointer;
84 UINT    status;
85 
86 
87     error_counter = 0;
88 
89     /* Setup the working pointer.  */
90     pointer =  (CHAR *) first_unused_memory;
91 
92     /* Create a helper thread for the server. */
93     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
94                      pointer, DEMO_STACK_SIZE,
95                      NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
96 
97     pointer =  pointer + DEMO_STACK_SIZE;
98 
99     /* Initialize the NetX system.  */
100     nx_system_initialize();
101 
102     /* Create the server packet pool.  */
103     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
104                                     pointer, SERVER_PACKET_SIZE*8);
105     pointer = pointer + SERVER_PACKET_SIZE * 8;
106     if (status)
107         error_counter++;
108 
109     /* Create an IP instance.  */
110     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
111                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
112                           pointer, 4096, 1);
113     pointer =  pointer + 4096;
114     if (status)
115         error_counter++;
116 
117     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
118     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
119     pointer = pointer + 1024;
120     if (status)
121         error_counter++;
122 
123 
124      /* Enable TCP traffic.  */
125     status = nx_tcp_enable(&server_ip);
126     if (status)
127         error_counter++;
128 
129     /* Create the HTTP Client thread. */
130     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
131                               pointer, DEMO_STACK_SIZE,
132                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
133     pointer =  pointer + DEMO_STACK_SIZE;
134     if (status)
135         error_counter++;
136 
137     /* Create the Client packet pool.  */
138     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
139                                     pointer, CLIENT_PACKET_SIZE*8);
140     pointer = pointer + CLIENT_PACKET_SIZE * 8;
141     if (status)
142         error_counter++;
143 
144     /* Create an IP instance.  */
145     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
146                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
147                           pointer, 2048, 1);
148     pointer =  pointer + 2048;
149     if (status)
150         error_counter++;
151 
152     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
153     pointer =  pointer + 2048;
154     if (status)
155         error_counter++;
156 
157      /* Enable TCP traffic.  */
158     status = nx_tcp_enable(&client_ip);
159     if (status)
160         error_counter++;
161 }
162 
163 #ifdef NX_WEB_HTTPS_ENABLE
164 /* Define the TLS setup callback function.  */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)165 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
166 {
167 UINT status;
168 
169 
170     /* Initialize and create TLS session.  */
171     status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
172 
173     /* Check status.  */
174     if (status)
175     {
176         return(status);
177     }
178 
179     /* Allocate space for packet reassembly.  */
180     status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
181 
182     /* Check status.  */
183     if (status)
184     {
185         return(status);
186     }
187 
188     /* Add a CA Certificate to our trusted store for verifying incoming server certificates.  */
189     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);
190     nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
191 
192     /* Need to allocate space for the certificate coming in from the remote host.  */
193     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
194     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
195 
196     return(NX_SUCCESS);
197 }
198 #endif /* NX_WEB_HTTPS_ENABLE  */
199 
http_response_callback(NX_WEB_HTTP_CLIENT * client_ptr,CHAR * field_name,UINT field_name_length,CHAR * field_value,UINT field_value_length)200 static VOID http_response_callback(NX_WEB_HTTP_CLIENT *client_ptr, CHAR *field_name, UINT field_name_length,
201                                    CHAR *field_value, UINT field_value_length)
202 {
203     if(memcmp(field_name, "Connection", field_name_length) == 0)
204     {
205         if ((test_count == 0) && (memcmp(field_value, "keep-alive", field_value_length) != 0))
206             error_counter++;
207         else if((test_count == 1) && (memcmp(field_value, "Close", field_value_length) != 0))
208             error_counter++;
209     }
210 }
211 
thread_client_entry(ULONG thread_input)212 void thread_client_entry(ULONG thread_input)
213 {
214 UINT            i;
215 UINT            status;
216 NX_PACKET       *recv_packet;
217 
218 
219     /* Give IP task and driver a chance to initialize the system.  */
220     tx_thread_sleep(NX_IP_PERIODIC_RATE);
221 
222     /* Set server IP address.  */
223     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
224     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
225 
226     /* First loop test HTTP, second loop test HTTPS.  */
227     for (i = 0; i < loop ; i++)
228     {
229         if (i == 0)
230         {
231 
232             /* Wait HTTP server started.  */
233             while(!http_server_start)
234             {
235                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
236             }
237         }
238 #ifdef NX_WEB_HTTPS_ENABLE
239         else
240         {
241 
242             /* Wait HTTPS server started.  */
243             while(!https_server_start)
244             {
245                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
246             }
247         }
248 #endif /* NX_WEB_HTTPS_ENABLE  */
249 
250         /* Create an HTTP client instance.  */
251         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
252 
253         /* Check status.  */
254         if (status)
255             error_counter++;
256 
257         /* Set the header callback routine. */
258         nx_web_http_client_response_header_callback_set(&my_client, http_response_callback);
259 
260         /* Connect to server.  */
261         if (i == 0)
262         {
263             status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_WAIT_FOREVER);
264 
265             /* Check status.  */
266             if (status)
267                 error_counter++;
268         }
269 #ifdef NX_WEB_HTTPS_ENABLE
270         else
271         {
272             status = nx_web_http_client_secure_connect(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
273                                                        tls_setup_callback, NX_WAIT_FOREVER);
274 
275             /* Check status.  */
276             if (status)
277                 error_counter++;
278         }
279 #endif /* NX_WEB_HTTPS_ENABLE  */
280 
281         /* First send GET with "connection: keep-alive", the connection should be kept alive.
282            Then send GET with "connection: close", the connection should be colsed.
283            If the connection is closed, the third request sending should be failed.  */
284         for (test_count = 0; test_count < 3; test_count++)
285         {
286 
287             /* Initialize HTTP request. */
288             status = nx_web_http_client_request_initialize(&my_client,
289                                                            NX_WEB_HTTP_METHOD_GET,
290                                                            "/index.htm",
291                                                            "www.abc.com",
292                                                            0,
293                                                            NX_FALSE,
294                                                            NX_NULL,
295                                                            NX_NULL,
296                                                            1 * NX_IP_PERIODIC_RATE);
297 
298             /* For HTTPS, if the connection is closed, the initialization should be failed.  */
299             if ((i == 1) && (test_count == 2))
300             {
301                 if (!status)
302                     error_counter++;
303                 break;
304             }
305             else if (status)
306             {
307                 error_counter++;
308                 break;
309             }
310 
311             /* Add the connection field.  */
312             if (test_count == 0)
313             {
314                 status = nx_web_http_client_request_header_add(&my_client, "Connection", 10, "keep-alive", 10, 1 * NX_IP_PERIODIC_RATE);
315             }
316             else if (test_count == 1)
317             {
318                 status = nx_web_http_client_request_header_add(&my_client, "Connection", 10, "close", 5, 1 * NX_IP_PERIODIC_RATE);
319             }
320 
321             if (status)
322                 error_counter++;
323 
324             /* Send the request.  */
325             status = nx_web_http_client_request_send(&my_client, 1 * NX_IP_PERIODIC_RATE);
326 
327             if ((i == 0) && (test_count == 2))
328             {
329                 if (!status)
330                     error_counter++;
331                 break;
332             }
333             else if (status)
334             {
335                 error_counter++;
336                 break;
337             }
338 
339             /* Get the response from server.  */
340             while(1)
341             {
342                 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
343 
344                 if (status)
345                     break;
346                 else
347                     nx_packet_release(recv_packet);
348             }
349 
350             /* Check the status.  */
351             if (status != NX_WEB_HTTP_GET_DONE)
352                 error_counter++;
353             else
354                 nx_packet_release(recv_packet);
355         }
356 
357         /* Delele the client.  */
358         status = nx_web_http_client_delete(&my_client);
359         if (status)
360             error_counter++;
361 
362         /* Set the flag.  */
363         if (i == 0)
364         {
365             http_client_stop = 1;
366         }
367 #ifdef NX_WEB_HTTPS_ENABLE
368         else
369         {
370             https_client_stop = 1;
371         }
372 #endif /* NX_WEB_HTTPS_ENABLE  */
373     }
374 }
375 
376 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)377 void    thread_server_entry(ULONG thread_input)
378 {
379 UINT            i;
380 UINT            status;
381 FX_FILE         my_file;
382 UINT            server_port = NX_WEB_HTTP_SERVER_PORT;
383 
384 
385     /* Print out test information banner.  */
386     printf("NetX Test:   Web Keep Alive Test.......................................");
387 
388     /* Check for earlier error.  */
389     if(error_counter)
390     {
391         printf("ERROR!\n");
392         test_control_return(1);
393     }
394 
395     fx_media_format(&ram_disk,
396                     _fx_ram_driver,               // Driver entry
397                     ram_disk_memory,              // RAM disk memory pointer
398                     media_memory,                 // Media buffer pointer
399                     sizeof(media_memory),         // Media buffer size
400                     "MY_RAM_DISK",                // Volume Name
401                     1,                            // Number of FATs
402                     32,                           // Directory Entries
403                     0,                            // Hidden sectors
404                     256,                          // Total sectors
405                     512,                          // Sector size
406                     8,                            // Sectors per cluster
407                     1,                            // Heads
408                     1);                           // Sectors per track
409 
410     /* Open the RAM disk.  */
411     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
412     status += fx_file_create(&ram_disk, "index.htm");
413     status += fx_file_open(&ram_disk, &my_file, "index.htm", FX_OPEN_FOR_WRITE);
414     status += fx_file_write(&my_file, "https server", 12);
415     status += fx_file_close(&my_file);
416     if(status)
417         error_counter++;
418 
419     /* Give NetX a chance to initialize the system.  */
420     tx_thread_sleep(NX_IP_PERIODIC_RATE);
421 
422     /* First loop test HTTP, second loop test HTTPS.  */
423     for (i = 0; i < loop; i++)
424     {
425 
426         if (i == 1)
427         {
428             server_port = NX_WEB_HTTPS_SERVER_PORT;
429         }
430 
431         /* Create the HTTP Server. */
432         status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
433                                            &server_stack, sizeof(server_stack), &server_pool,
434                                            NX_NULL, NX_NULL);
435         if (status)
436             error_counter++;
437 
438 #ifdef NX_WEB_HTTPS_ENABLE
439         /* Set TLS for HTTPS.  */
440         if (i == 1)
441         {
442             /* Initialize device certificate (used for all sessions in HTTPS server).  */
443             memset(&certificate, 0, sizeof(certificate));
444             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);
445 
446             /* Setup TLS session data for the TCP server.  */
447             status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
448                                                          crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
449                                                          &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
450             if (status)
451                 error_counter++;
452         }
453 #endif /* NX_WEB_HTTPS_ENABLE  */
454 
455         /* OK to start the HTTP Server.  */
456         status = nx_web_http_server_start(&my_server);
457         if (status)
458             error_counter++;
459 
460         /* Set the flag.  */
461         if (i == 0)
462         {
463             http_server_start = 1;
464 
465             /* Wait HTTP test finished.  */
466             while(!http_client_stop)
467             {
468                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
469             }
470         }
471 #ifdef NX_WEB_HTTPS_ENABLE
472         else
473         {
474             https_server_start = 1;
475 
476             /* Wait HTTPS test finished.  */
477             while(!https_client_stop)
478             {
479                 tx_thread_sleep(NX_IP_PERIODIC_RATE);
480             }
481         }
482 #endif /* NX_WEB_HTTPS_ENABLE  */
483 
484         status = nx_web_http_server_delete(&my_server);
485         if (status)
486             error_counter++;
487     }
488 
489     /* Check packet pool.  */
490     if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
491     {
492         error_counter++;
493     }
494 
495     if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
496     {
497         error_counter++;
498     }
499 
500     if(error_counter)
501     {
502         printf("ERROR!\n");
503         test_control_return(1);
504     }
505     else
506     {
507         printf("SUCCESS!\n");
508         test_control_return(0);
509     }
510 }
511 #else
512 
513 #ifdef CTEST
test_application_define(void * first_unused_memory)514 VOID test_application_define(void *first_unused_memory)
515 #else
516 void    netx_web_keep_alive_test_application_define(void *first_unused_memory)
517 #endif
518 {
519 
520     /* Print out test information banner.  */
521     printf("NetX Test:   Web Keep Alive Test.......................................N/A\n");
522 
523     test_control_return(3);
524 }
525 #endif
526 
527