1 /* This case tests status 404.
2 * If Tthe server has not found anything matching the Request-URI.
3 * 404 should be sent.
4 * */
5 #include "tx_api.h"
6 #include "nx_api.h"
7 #include "fx_api.h"
8 #include "nx_web_http_client.h"
9 #include "nx_web_http_server.h"
10
11 extern void test_control_return(UINT);
12
13 #if !defined(NX_DISABLE_IPV4)
14
15 #include "test_device_cert.c"
16 #include "test_ca_cert.c"
17 #define ca_cert_der test_ca_cert_der
18 #define ca_cert_der_len test_ca_cert_der_len
19
20 #define DEMO_STACK_SIZE 4096
21
22 /* Set up FileX and file memory resources. */
23 static CHAR ram_disk_memory[4096];
24 static FX_MEDIA ram_disk;
25 static UCHAR media_memory[4096];
26
27 static UCHAR server_stack[16000];
28
29 /* Define device drivers. */
30 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
31 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
32
33 /* Set up the HTTP client global variables. */
34
35 #define CLIENT_PACKET_SIZE (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
36
37 static TX_THREAD client_thread;
38 static NX_PACKET_POOL client_pool;
39 static NX_WEB_HTTP_CLIENT my_client;
40 static NX_IP client_ip;
41 static UINT error_counter;
42
43 /* Set up the HTTP server global variables */
44
45 #define SERVER_PACKET_SIZE (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
46
47 static NX_WEB_HTTP_SERVER my_server;
48 static NX_PACKET_POOL server_pool;
49 static TX_THREAD server_thread;
50 static NX_IP server_ip;
51 static NXD_ADDRESS server_ip_address;
52 static UINT http_server_start = 0;
53 static UINT http_client_stop = 0;
54
55 static void thread_client_entry(ULONG thread_input);
56 static void thread_server_entry(ULONG thread_input);
57
58 #define HTTP_SERVER_ADDRESS IP_ADDRESS(192,168,0,105)
59 #define HTTP_CLIENT_ADDRESS IP_ADDRESS(192,168,0,123)
60
61 #ifdef NX_WEB_HTTPS_ENABLE
62 static UINT https_server_start = 0;
63 static UINT https_client_stop = 0;
64 static UINT loop = 2;
65 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
66 static CHAR crypto_metadata_server[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
67 static CHAR crypto_metadata_client[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
68 static UCHAR tls_packet_buffer[18500];
69 static NX_SECURE_X509_CERT certificate;
70 static NX_SECURE_X509_CERT trusted_certificate;
71 static NX_SECURE_X509_CERT remote_certificate, remote_issuer;
72 static UCHAR remote_cert_buffer[2000];
73 static UCHAR remote_issuer_buffer[2000];
74 #else
75 static UINT loop = 1;
76 #endif /* NX_WEB_HTTPS_ENABLE */
77
78 extern UINT _nx_web_http_client_receive(NX_WEB_HTTP_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option);
79 extern UINT _nx_web_http_client_send(NX_WEB_HTTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, ULONG wait_option);
80
81 /* This is a HTTP get packet captured by wireshark.
82 * curl 192.168.0.123/aaa.index
83 * */
84 static char pkt[] = {
85 0x00, 0x11, 0x22, 0x33, 0x44, 0x57, 0xb8, 0xca, /* .."3DW.. */
86 0x3a, 0x95, 0xdb, 0x0b, 0x08, 0x00, 0x45, 0x00, /* :.....E. */
87 0x00, 0x7c, 0x3a, 0xff, 0x40, 0x00, 0x80, 0x06, /* .|:.@... */
88 0x3d, 0x48, 0xc0, 0xa8, 0x00, 0x69, 0xc0, 0xa8, /* =H...i.. */
89 0x00, 0x7b, 0xcb, 0xef, 0x00, 0x50, 0x4d, 0x7b, /* .{...PM{ */
90 0x22, 0xf0, 0xcf, 0xed, 0x46, 0xa4, 0x50, 0x18, /* "...F.P. */
91 0xfa, 0xf0, 0x2e, 0x84, 0x00, 0x00, 0x47, 0x45, /* ......GE */
92 0x54, 0x20, 0x2f, 0x61, 0x61, 0x61, 0x2e, 0x68, /* T /aaa.h */
93 0x74, 0x6d, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, /* tm HTTP/ */
94 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x55, 0x73, 0x65, /* 1.1..Use */
95 0x72, 0x2d, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, /* r-Agent: */
96 0x20, 0x63, 0x75, 0x72, 0x6c, 0x2f, 0x37, 0x2e, /* curl/7. */
97 0x33, 0x32, 0x2e, 0x30, 0x0d, 0x0a, 0x48, 0x6f, /* 32.0..Ho */
98 0x73, 0x74, 0x3a, 0x20, 0x31, 0x39, 0x32, 0x2e, /* st: 192. */
99 0x31, 0x36, 0x38, 0x2e, 0x30, 0x2e, 0x31, 0x32, /* 168.0.12 */
100 0x33, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, /* 3..Accep */
101 0x74, 0x3a, 0x20, 0x2a, 0x2f, 0x2a, 0x0d, 0x0a, /* t: */
102 0x0d, 0x0a /* .. */
103 };
104
105 #ifdef CTEST
test_application_define(void * first_unused_memory)106 VOID test_application_define(void *first_unused_memory)
107 #else
108 void netx_web_status_404_test_application_define(void *first_unused_memory)
109 #endif
110 {
111 CHAR *pointer;
112 UINT status;
113
114
115 error_counter = 0;
116
117 /* Setup the working pointer. */
118 pointer = (CHAR *) first_unused_memory;
119
120 /* Create a helper thread for the server. */
121 tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
122 pointer, DEMO_STACK_SIZE,
123 NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
124
125 pointer = pointer + DEMO_STACK_SIZE;
126
127 /* Initialize the NetX system. */
128 nx_system_initialize();
129
130 /* Create the server packet pool. */
131 status = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
132 pointer, SERVER_PACKET_SIZE*8);
133 pointer = pointer + SERVER_PACKET_SIZE * 8;
134 if (status)
135 error_counter++;
136
137 /* Create an IP instance. */
138 status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
139 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
140 pointer, 4096, 1);
141 pointer = pointer + 4096;
142 if (status)
143 error_counter++;
144
145 /* Enable ARP and supply ARP cache memory for the server IP instance. */
146 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
147 pointer = pointer + 1024;
148 if (status)
149 error_counter++;
150
151 /* Enable TCP traffic. */
152 status = nx_tcp_enable(&server_ip);
153 if (status)
154 error_counter++;
155
156 /* Create the HTTP Client thread. */
157 status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
158 pointer, DEMO_STACK_SIZE,
159 NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
160 pointer = pointer + DEMO_STACK_SIZE;
161 if (status)
162 error_counter++;
163
164 /* Create the Client packet pool. */
165 status = nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
166 pointer, CLIENT_PACKET_SIZE*8);
167 pointer = pointer + CLIENT_PACKET_SIZE * 8;
168 if (status)
169 error_counter++;
170
171 /* Create an IP instance. */
172 status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
173 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
174 pointer, 2048, 1);
175 pointer = pointer + 2048;
176 if (status)
177 error_counter++;
178
179 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
180 pointer = pointer + 2048;
181 if (status)
182 error_counter++;
183
184 /* Enable TCP traffic. */
185 status = nx_tcp_enable(&client_ip);
186 if (status)
187 error_counter++;
188 }
189
190 #ifdef NX_WEB_HTTPS_ENABLE
191 /* Define the TLS setup callback function. */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)192 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
193 {
194 UINT status;
195
196
197 /* Initialize and create TLS session. */
198 status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
199
200 /* Check status. */
201 if (status)
202 {
203 return(status);
204 }
205
206 /* Allocate space for packet reassembly. */
207 status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
208
209 /* Check status. */
210 if (status)
211 {
212 return(status);
213 }
214
215 /* Add a CA Certificate to our trusted store for verifying incoming server certificates. */
216 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);
217 nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
218
219 /* Need to allocate space for the certificate coming in from the remote host. */
220 nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
221 nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
222
223 return(NX_SUCCESS);
224 }
225 #endif /* NX_WEB_HTTPS_ENABLE */
226
thread_client_entry(ULONG thread_input)227 void thread_client_entry(ULONG thread_input)
228 {
229 UINT i;
230 UINT status;
231 NX_PACKET *recv_packet;
232 NX_PACKET *my_packet;
233 CHAR *buffer_ptr;
234
235
236 /* Give IP task and driver a chance to initialize the system. */
237 tx_thread_sleep(NX_IP_PERIODIC_RATE);
238
239 /* Set server IP address. */
240 server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
241 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
242
243 /* First loop test HTTP, second loop test HTTPS. */
244 for (i = 0; i < loop ; i++)
245 {
246 if (i == 0)
247 {
248
249 /* Wait HTTP server started. */
250 while(!http_server_start)
251 {
252 tx_thread_sleep(NX_IP_PERIODIC_RATE);
253 }
254 }
255 #ifdef NX_WEB_HTTPS_ENABLE
256 else
257 {
258
259 /* Wait HTTPS server started. */
260 while(!https_server_start)
261 {
262 tx_thread_sleep(NX_IP_PERIODIC_RATE);
263 }
264 }
265 #endif /* NX_WEB_HTTPS_ENABLE */
266
267 /* Create an HTTP client instance. */
268 status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
269
270 /* Check status. */
271 if (status)
272 error_counter++;
273
274
275 /* Connect to server. */
276 if (i == 0)
277 {
278 status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_WAIT_FOREVER);
279
280 /* Check status. */
281 if (status)
282 error_counter++;
283 }
284 #ifdef NX_WEB_HTTPS_ENABLE
285 else
286 {
287 status = nx_web_http_client_secure_connect(&my_client, &server_ip_address, NX_WEB_HTTPS_SERVER_PORT,
288 tls_setup_callback, NX_WAIT_FOREVER);
289
290 /* Check status. */
291 if (status)
292 error_counter++;
293 }
294 #endif /* NX_WEB_HTTPS_ENABLE */
295
296 /* Allocate a packet. */
297 status = nx_web_http_client_request_packet_allocate(&my_client, &my_packet, 1 * NX_IP_PERIODIC_RATE);
298 if(status)
299 error_counter++;
300
301 /* Write Get packet into the packet payload. */
302 status = nx_packet_data_append(my_packet, &pkt[54] , (sizeof(pkt) - 54), &client_pool, 1 * NX_IP_PERIODIC_RATE);
303 if(status)
304 error_counter++;
305
306 /* Send the packet out. */
307 status = _nx_web_http_client_send(&my_client, my_packet, 1 * NX_IP_PERIODIC_RATE);
308 if(status)
309 {
310 nx_packet_release(my_packet);
311 error_counter++;
312 }
313
314 /* Receive the response from http server. */
315 status = _nx_web_http_client_receive(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
316 if(status)
317 error_counter++;
318 else
319 {
320 buffer_ptr = (CHAR *)recv_packet ->nx_packet_prepend_ptr;
321
322 /* Check the status, If success , it should be 304. */
323 if((buffer_ptr[9] != '4') || (buffer_ptr[10] != '0') || (buffer_ptr[11] != '4'))
324 error_counter++;
325
326 nx_packet_release(recv_packet);
327 }
328
329 status = nx_web_http_client_delete(&my_client);
330 if (status)
331 error_counter++;
332
333 /* Set the flag. */
334 if (i == 0)
335 {
336 http_client_stop = 1;
337 }
338 #ifdef NX_WEB_HTTPS_ENABLE
339 else
340 {
341 https_client_stop = 1;
342 }
343 #endif /* NX_WEB_HTTPS_ENABLE */
344 }
345 }
346
347
348 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)349 void thread_server_entry(ULONG thread_input)
350 {
351 UINT i;
352 UINT status;
353 FX_FILE my_file;
354 UINT server_port = NX_WEB_HTTP_SERVER_PORT;
355
356
357 /* Print out test information banner. */
358 printf("NetX Test: Web Status 404 Test.......................................");
359
360 /* Check for earlier error. */
361 if(error_counter)
362 {
363 printf("ERROR!\n");
364 test_control_return(1);
365 }
366
367 fx_media_format(&ram_disk,
368 _fx_ram_driver, // Driver entry
369 ram_disk_memory, // RAM disk memory pointer
370 media_memory, // Media buffer pointer
371 sizeof(media_memory), // Media buffer size
372 "MY_RAM_DISK", // Volume Name
373 1, // Number of FATs
374 32, // Directory Entries
375 0, // Hidden sectors
376 256, // Total sectors
377 512, // Sector size
378 8, // Sectors per cluster
379 1, // Heads
380 1); // Sectors per track
381
382 /* Open the RAM disk. */
383 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
384 status += fx_file_create(&ram_disk, "index.htm");
385 status += fx_file_open(&ram_disk, &my_file, "index.htm", FX_OPEN_FOR_WRITE);
386 status += fx_file_write(&my_file, "https server", 12);
387 status += fx_file_close(&my_file);
388 if(status)
389 error_counter++;
390
391 /* Give NetX a chance to initialize the system. */
392 tx_thread_sleep(NX_IP_PERIODIC_RATE);
393
394 /* First loop test HTTP, second loop test HTTPS. */
395 for (i = 0; i < loop; i++)
396 {
397
398 if (i == 1)
399 {
400 server_port = NX_WEB_HTTPS_SERVER_PORT;
401 }
402
403 /* Create the HTTP Server. */
404 status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
405 &server_stack, sizeof(server_stack), &server_pool,
406 NX_NULL, NX_NULL);
407 if (status)
408 error_counter++;
409
410 #ifdef NX_WEB_HTTPS_ENABLE
411 /* Set TLS for HTTPS. */
412 if (i == 1)
413 {
414 /* Initialize device certificate (used for all sessions in HTTPS server). */
415 memset(&certificate, 0, sizeof(certificate));
416 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);
417
418 /* Setup TLS session data for the TCP server. */
419 status = nx_web_http_server_secure_configure(&my_server, &nx_crypto_tls_ciphers,
420 crypto_metadata_server, sizeof(crypto_metadata_server), tls_packet_buffer, sizeof(tls_packet_buffer),
421 &certificate, NX_NULL, 0, NX_NULL, 0, NX_NULL, 0);
422 if (status)
423 error_counter++;
424 }
425 #endif /* NX_WEB_HTTPS_ENABLE */
426
427 /* OK to start the HTTP Server. */
428 status = nx_web_http_server_start(&my_server);
429 if (status)
430 error_counter++;
431
432 /* Set the flag. */
433 if (i == 0)
434 {
435 http_server_start = 1;
436
437 /* Wait HTTP test finished. */
438 while(!http_client_stop)
439 {
440 tx_thread_sleep(NX_IP_PERIODIC_RATE);
441 }
442 }
443 #ifdef NX_WEB_HTTPS_ENABLE
444 else
445 {
446 https_server_start = 1;
447
448 /* Wait HTTPS test finished. */
449 while(!https_client_stop)
450 {
451 tx_thread_sleep(NX_IP_PERIODIC_RATE);
452 }
453 }
454 #endif /* NX_WEB_HTTPS_ENABLE */
455
456 status = nx_web_http_server_delete(&my_server);
457 if (status)
458 error_counter++;
459 }
460
461 /* Check packet pool. */
462 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
463 {
464 error_counter++;
465 }
466
467 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
468 {
469 error_counter++;
470 }
471
472 if(error_counter)
473 {
474 printf("ERROR!\n");
475 test_control_return(1);
476 }
477 else
478 {
479 printf("SUCCESS!\n");
480 test_control_return(0);
481 }
482 }
483
484 #else
485
486 #ifdef CTEST
test_application_define(void * first_unused_memory)487 VOID test_application_define(void *first_unused_memory)
488 #else
489 void netx_web_status_404_test_application_define(void *first_unused_memory)
490 #endif
491 {
492
493 /* Print out test information banner. */
494 printf("NetX Test: Web Status 404 Test.......................................N/A\n");
495
496 test_control_return(3);
497 }
498 #endif
499