1 /* This case tests basic GET 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) && defined(NX_ENABLE_EXTENDED_NOTIFY_SUPPORT)
11
12 #define DEMO_STACK_SIZE 4096
13
14 /* Set up FileX and file memory resources. */
15 static CHAR ram_disk_memory[4096];
16 static FX_MEDIA ram_disk;
17 static UCHAR media_memory[4096];
18
19 static UCHAR server_stack[16000];
20
21 /* Define device drivers. */
22 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
23 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
24
25 /* Set up the HTTP client global variables. */
26
27 #define CLIENT_PACKET_SIZE (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
28
29 static TX_THREAD client_thread;
30 static NX_PACKET_POOL client_pool;
31 static NX_WEB_HTTP_CLIENT my_client;
32 static NX_IP client_ip;
33 static UINT error_counter;
34 static TX_SEMAPHORE establish_semaphore;
35 static TX_SEMAPHORE data_semaphore;
36
37 /* Set up the HTTP server global variables */
38
39 #define SERVER_PACKET_SIZE (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
40
41 static NX_WEB_HTTP_SERVER my_server;
42 static NX_PACKET_POOL server_pool;
43 static TX_THREAD server_thread;
44 static NX_IP server_ip;
45 static NXD_ADDRESS server_ip_address;
46 static UINT http_server_start = 0;
47 static UINT http_client_stop = 0;
48
49 static void thread_client_entry(ULONG thread_input);
50 static void thread_server_entry(ULONG thread_input);
51
52 #define HTTP_SERVER_ADDRESS IP_ADDRESS(1,2,3,4)
53 #define HTTP_CLIENT_ADDRESS IP_ADDRESS(1,2,3,5)
54
55 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr);
56
57 static char pkt[] = {
58 0x48, 0x54, /* ......HT */
59 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32, /* TP/1.0 2 */
60 0x30, 0x30, 0x20, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, /* 00 ..Con */
61 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, /* tent-Len */
62 0x67, 0x74, 0x68, 0x3a, 0x20, 0x20, 0x31, 0x32, /* gth: 12 */
63 0x31, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, /* 1..Conte */
64 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, /* nt-Type: */
65 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, /* text/pl */
66 0x61, 0x69, 0x6e, 0x0d, 0x0a, 0x0d, 0x0a, 0x3c, /* ain....< */
67 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0d, 0x0a, 0x0d, /* html>... */
68 };
69
70 static char pkt1[] = {
71 0x0a, 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, /* .<head>. */
72 0x0a, 0x0d, 0x0a, 0x3c, 0x74, 0x69, 0x74, 0x6c, /* ...<titl */
73 0x65, 0x3e, 0x4d, 0x61, 0x69, 0x6e, 0x20, 0x57, /* e>Main W */
74 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x3c, 0x2f, 0x74, /* indow</t */
75 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x3c, /* itle>..< */
76 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, 0x0a, /* /head>.. */
77 0x0d, 0x0a, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, /* ..<body> */
78 };
79
80 static char pkt2[] = {
81 0x0a, 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, /* .<head>. */
82 0x0a, 0x0d, 0x0a, 0x3c, 0x74, 0x69, 0x74, 0x6c, /* ...<titl */
83 0x65, 0x3e, 0x4d, 0x61, 0x69, 0x6e, 0x20, 0x57, /* e>Main W */
84 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x3c, 0x2f, 0x74, /* indow</t */
85 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x3c, /* itle>..< */
86 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, 0x0a, /* /head>.. */
87 0x0d, 0x0a, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, /* ..<body> */
88 };
89
90
91 #ifdef CTEST
test_application_define(void * first_unused_memory)92 VOID test_application_define(void *first_unused_memory)
93 #else
94 void netx_web_non_block_basic_test_application_define(void *first_unused_memory)
95 #endif
96 {
97 CHAR *pointer;
98 UINT status;
99
100
101 error_counter = 0;
102
103 /* Setup the working pointer. */
104 pointer = (CHAR *) first_unused_memory;
105
106 /* Create a helper thread for the server. */
107 tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
108 pointer, DEMO_STACK_SIZE,
109 NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
110
111 pointer = pointer + DEMO_STACK_SIZE;
112
113 /* Initialize the NetX system. */
114 nx_system_initialize();
115
116 /* Create the server packet pool. */
117 status = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
118 pointer, SERVER_PACKET_SIZE*8);
119 pointer = pointer + SERVER_PACKET_SIZE * 8;
120 if (status)
121 error_counter++;
122
123 /* Create an IP instance. */
124 status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
125 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
126 pointer, 4096, 1);
127 pointer = pointer + 4096;
128 if (status)
129 error_counter++;
130
131 /* Enable ARP and supply ARP cache memory for the server IP instance. */
132 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
133 pointer = pointer + 1024;
134 if (status)
135 error_counter++;
136
137
138 /* Enable TCP traffic. */
139 status = nx_tcp_enable(&server_ip);
140 if (status)
141 error_counter++;
142
143 /* Create the HTTP Client thread. */
144 status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
145 pointer, DEMO_STACK_SIZE,
146 NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
147 pointer = pointer + DEMO_STACK_SIZE;
148 if (status)
149 error_counter++;
150
151 /* Create the Client packet pool. */
152 status = nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
153 pointer, CLIENT_PACKET_SIZE*8);
154 pointer = pointer + CLIENT_PACKET_SIZE * 8;
155 if (status)
156 error_counter++;
157
158 /* Create an IP instance. */
159 status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
160 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
161 pointer, 2048, 1);
162 pointer = pointer + 2048;
163 if (status)
164 error_counter++;
165
166 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
167 pointer = pointer + 2048;
168 if (status)
169 error_counter++;
170
171 /* Enable TCP traffic. */
172 status = nx_tcp_enable(&client_ip);
173 if (status)
174 error_counter++;
175
176 /* Create the test control semaphore. */
177 tx_semaphore_create(&establish_semaphore, "Web Establish", 0);
178 tx_semaphore_create(&data_semaphore, "Web Data", 0);
179 }
180
http_response_callback(NX_WEB_HTTP_CLIENT * client_ptr,CHAR * field_name,UINT field_name_length,CHAR * field_value,UINT field_value_length)181 static VOID http_response_callback(NX_WEB_HTTP_CLIENT *client_ptr, CHAR *field_name, UINT field_name_length,
182 CHAR *field_value, UINT field_value_length)
183 {
184 if (memcmp(field_name, "Content-Type", field_name_length) == 0)
185 {
186 if (memcmp(field_value, "text/plain", field_value_length) != 0)
187 error_counter++;
188 }
189 else if(memcmp(field_name, "Content-Length", field_name_length) == 0)
190 {
191 if (memcmp(field_value, "121", field_value_length) != 0)
192 error_counter++;
193 }
194 #ifndef NX_WEB_HTTP_KEEPALIVE_DISABLE
195 else if(memcmp(field_name, "Connection", field_name_length) == 0)
196 {
197 if (memcmp(field_value, "keep-alive", field_value_length) != 0)
198 error_counter++;
199 }
200 #endif
201 }
202
tcp_establish_notify(NX_TCP_SOCKET * socket_ptr)203 static VOID tcp_establish_notify(NX_TCP_SOCKET *socket_ptr)
204 {
205
206 tx_semaphore_put(&establish_semaphore);
207 }
208
tcp_receive_callback(NX_TCP_SOCKET * socket_ptr)209 static VOID tcp_receive_callback(NX_TCP_SOCKET *socket_ptr)
210 {
211
212 tx_semaphore_put(&data_semaphore);
213 }
214
thread_client_entry(ULONG thread_input)215 void thread_client_entry(ULONG thread_input)
216 {
217 UINT status;
218 NX_PACKET *recv_packet;
219
220
221 /* Give IP task and driver a chance to initialize the system. */
222 tx_thread_sleep(NX_IP_PERIODIC_RATE);
223
224 /* Set server IP address. */
225 server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
226 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
227
228 /* Wait HTTP server started. */
229 while(!http_server_start)
230 {
231 tx_thread_sleep(NX_IP_PERIODIC_RATE);
232 }
233
234 /* Create an HTTP client instance. */
235 status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
236
237 /* Check status. */
238 if (status)
239 error_counter++;
240
241 nx_tcp_socket_establish_notify(&my_client.nx_web_http_client_socket, tcp_establish_notify);
242 nx_tcp_socket_receive_notify(&my_client.nx_web_http_client_socket, tcp_receive_callback);
243
244 /* Set the header callback routine. */
245 nx_web_http_client_response_header_callback_set(&my_client, http_response_callback);
246
247 /* Connect to server. */
248 status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_NO_WAIT);
249
250 /* Check status. */
251 if (status != NX_IN_PROGRESS)
252 error_counter++;
253
254 status = tx_semaphore_get(&establish_semaphore, NX_IP_PERIODIC_RATE);
255
256 /* Check status. */
257 if (status != TX_SUCCESS)
258 {
259 printf("ERROR!\n");
260 test_control_return(1);
261 }
262
263 /* Initialize the request. */
264 status = nx_web_http_client_request_initialize(&my_client, NX_WEB_HTTP_METHOD_GET, "/test.txt", "1.2.3.4",
265 0, NX_FALSE, "name", "password", NX_NO_WAIT);
266
267 /* Check status. */
268 if (status)
269 error_counter++;
270
271 /* Send the request. */
272 status = nx_web_http_client_request_send(&my_client, NX_NO_WAIT);
273
274 /* Check status. */
275 if (status)
276 error_counter++;
277
278 /* Get response from server. */
279 while (1)
280 {
281
282 status = tx_semaphore_get(&data_semaphore, 2 * NX_IP_PERIODIC_RATE);
283
284 /* Check status. */
285 if (status != TX_SUCCESS)
286 {
287 printf("ERROR!\n");
288 test_control_return(1);
289 }
290
291 while (1)
292 {
293
294 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, NX_NO_WAIT);
295
296 if (status)
297 break;
298 else
299 nx_packet_release(recv_packet);
300 }
301
302 /* Check status. */
303 if (status == NX_WEB_HTTP_GET_DONE)
304 {
305 nx_packet_release(recv_packet);
306 break;
307 }
308 else if (status == NX_NO_PACKET)
309 {
310 continue;
311 }
312 else
313 {
314 error_counter++;
315 break;
316 }
317 }
318
319 status = nx_web_http_client_delete(&my_client);
320 if (status)
321 error_counter++;
322
323 /* Set the flag. */
324 http_client_stop = 1;
325 }
326
327 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)328 void thread_server_entry(ULONG thread_input)
329 {
330 UINT status;
331 FX_FILE my_file;
332 UINT server_port = NX_WEB_HTTP_SERVER_PORT;
333
334
335 /* Print out test information banner. */
336 printf("NetX Test: Web Non Block Basic Test..................................");
337
338 /* Check for earlier error. */
339 if(error_counter)
340 {
341 printf("ERROR!\n");
342 test_control_return(1);
343 }
344
345 fx_media_format(&ram_disk,
346 _fx_ram_driver, // Driver entry
347 ram_disk_memory, // RAM disk memory pointer
348 media_memory, // Media buffer pointer
349 sizeof(media_memory), // Media buffer size
350 "MY_RAM_DISK", // Volume Name
351 1, // Number of FATs
352 32, // Directory Entries
353 0, // Hidden sectors
354 256, // Total sectors
355 512, // Sector size
356 8, // Sectors per cluster
357 1, // Heads
358 1); // Sectors per track
359
360 /* Open the RAM disk. */
361 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
362 status += fx_file_create(&ram_disk, "TEST.TXT");
363 status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
364 status += fx_file_write(&my_file, "https server", 12);
365 status += fx_file_close(&my_file);
366 if(status)
367 error_counter++;
368
369 /* Give NetX a chance to initialize the system. */
370 tx_thread_sleep(NX_IP_PERIODIC_RATE);
371
372 /* Create the HTTP Server. */
373 status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
374 &server_stack, sizeof(server_stack), &server_pool,
375 NX_NULL, server_request_callback);
376 if (status)
377 error_counter++;
378
379 /* OK to start the HTTP Server. */
380 status = nx_web_http_server_start(&my_server);
381 if (status)
382 error_counter++;
383
384 /* Set the flag. */
385 http_server_start = 1;
386
387 /* Wait HTTP test finished. */
388 while(!http_client_stop)
389 {
390 tx_thread_sleep(NX_IP_PERIODIC_RATE);
391 }
392
393 status = nx_web_http_server_delete(&my_server);
394 if (status)
395 error_counter++;
396
397 /* Check packet pool. */
398 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
399 {
400 error_counter++;
401 }
402
403 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
404 {
405 error_counter++;
406 }
407
408 if(error_counter)
409 {
410 printf("ERROR!\n");
411 test_control_return(1);
412 }
413 else
414 {
415 printf("SUCCESS!\n");
416 test_control_return(0);
417 }
418 }
419
420 /* Define the server request callback function. */
server_request_callback(NX_WEB_HTTP_SERVER * server_ptr,UINT request_type,CHAR * resource,NX_PACKET * packet_ptr)421 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr)
422 {
423 NX_PACKET *response_pkt;
424 UINT status;
425
426 /* Process multipart data. */
427 if(request_type == NX_WEB_HTTP_SERVER_GET_REQUEST)
428 {
429
430 /* Allocate a response packet. */
431 status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
432
433 /* Check status. */
434 if (status)
435 {
436 error_counter++;
437 }
438
439 status = nx_packet_data_append(response_pkt, pkt, sizeof(pkt), &server_pool, NX_WAIT_FOREVER);
440
441 if(status == NX_SUCCESS)
442 {
443 if(nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
444 {
445 nx_packet_release(response_pkt);
446 }
447 }
448
449 tx_thread_sleep(NX_IP_PERIODIC_RATE);
450
451 /* Allocate a response packet. */
452 status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
453
454 /* Check status. */
455 if (status)
456 {
457 error_counter++;
458 }
459
460 status = nx_packet_data_append(response_pkt, pkt1, sizeof(pkt1), &server_pool, NX_WAIT_FOREVER);
461
462 if(status == NX_SUCCESS)
463 {
464 if(nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
465 {
466 nx_packet_release(response_pkt);
467 }
468 }
469
470 tx_thread_sleep(NX_IP_PERIODIC_RATE);
471
472 /* Allocate a response packet. */
473 status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
474
475 /* Check status. */
476 if (status)
477 {
478 error_counter++;
479 }
480
481 status = nx_packet_data_append(response_pkt, pkt2, sizeof(pkt2), &server_pool, NX_WAIT_FOREVER);
482
483 if(status == NX_SUCCESS)
484 {
485 if(nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
486 {
487 nx_packet_release(response_pkt);
488 }
489 }
490 }
491 else
492 {
493 /* Indicate we have not processed the response to client yet. */
494 return(NX_SUCCESS);
495 }
496
497 /* Indicate the response to client is transmitted. */
498 return(NX_WEB_HTTP_CALLBACK_COMPLETED);
499 }
500
501 #else
502
503 #ifdef CTEST
test_application_define(void * first_unused_memory)504 VOID test_application_define(void *first_unused_memory)
505 #else
506 void netx_web_non_block_basic_test_application_define(void *first_unused_memory)
507 #endif
508 {
509
510 /* Print out test information banner. */
511 printf("NetX Test: Web Non Block Basic Test..................................N/A\n");
512
513 test_control_return(3);
514 }
515 #endif
516
517