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