1 /* This case tests basic DELETE function. */
2 #include    "tx_api.h"
3 #include    "nx_api.h"
4 #include    "fx_api.h"
5 #include    "nxd_http_client.h"
6 #include    "nxd_http_server.h"
7 
8 extern void test_control_return(UINT);
9 
10 #if !defined(NX_DISABLE_IPV4)
11 
12 #define     DEMO_STACK_SIZE         4096
13 
14 /* This is a HTTP get packet captured by wireshark. DELETE index.html*/
15 static char pkt[] = {
16 0x00, 0x11, 0x22, 0x33, 0x44, 0x57, 0xb8, 0xca, /* .."3DW.. */
17 0x3a, 0x95, 0xdb, 0x0b, 0x08, 0x00, 0x45, 0x00, /* :.....E. */
18 0x00, 0x81, 0x00, 0x21, 0x40, 0x00, 0x80, 0x06, /* ...!@... */
19 0x78, 0x21, 0xc0, 0xa8, 0x00, 0x69, 0xc0, 0xa8, /* x!...i.. */
20 0x00, 0x7b, 0xc1, 0x4b, 0x00, 0x50, 0x40, 0x81, /* .{.K.P@. */
21 0xf0, 0x40, 0x77, 0x7f, 0x23, 0xc7, 0x50, 0x18, /* .@w.#.P. */
22 0xfa, 0xf0, 0xf0, 0x51, 0x00, 0x00, 0x44, 0x45, /* ...Q..DE */
23 0x4c, 0x45, 0x54, 0x45, 0x20, 0x2f, 0x69, 0x6e, /* LETE /in */
24 0x64, 0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x20, /* dex.htm  */
25 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, /* HTTP/1.1 */
26 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41, /* ..User-A */
27 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x63, 0x75, /* gent: cu */
28 0x72, 0x6c, 0x2f, 0x37, 0x2e, 0x33, 0x32, 0x2e, /* rl/7.32. */
29 0x30, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, /* 0..Host: */
30 0x20, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, /*  192.168 */
31 0x2e, 0x30, 0x2e, 0x31, 0x32, 0x33, 0x0d, 0x0a, /* .0.123.. */
32 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20, /* Accept:  */
33 0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a        /*          */
34 };
35 
36 
37 /* Set up FileX and file memory resources. */
38 static CHAR             *ram_disk_memory;
39 static FX_MEDIA         ram_disk;
40 static unsigned char    media_memory[512];
41 
42 /* Define device drivers.  */
43 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
44 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
45 
46 
47 /* Set up the HTTP client global variables. */
48 
49 #define CLIENT_PACKET_SIZE  (NX_HTTP_SERVER_MIN_PACKET_SIZE * 2)
50 
51 static TX_THREAD       client_thread;
52 static NX_PACKET_POOL  client_pool;
53 static NX_HTTP_CLIENT  my_client;
54 static NX_IP           client_ip;
55 static UINT            error_counter;
56 
57 static NX_TCP_SOCKET   client_socket;
58 
59 /* Set up the HTTP server global variables */
60 
61 #define SERVER_PACKET_SIZE  (NX_HTTP_SERVER_MIN_PACKET_SIZE * 2)
62 
63 static NX_HTTP_SERVER  my_server;
64 static NX_PACKET_POOL  server_pool;
65 static TX_THREAD       server_thread;
66 static NX_IP           server_ip;
67 #ifdef __PRODUCT_NETXDUO__
68 static NXD_ADDRESS     server_ip_address;
69 #else
70 static ULONG           server_ip_address;
71 #endif
72 
73 
74 static void thread_client_entry(ULONG thread_input);
75 static void thread_server_entry(ULONG thread_input);
76 
77 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(192,168,0,105)
78 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(192,168,0,123)
79 
80 
81 #ifdef CTEST
test_application_define(void * first_unused_memory)82 VOID test_application_define(void *first_unused_memory)
83 #else
84 void    netx_http_delete_basic_test_application_define(void *first_unused_memory)
85 #endif
86 {
87 
88 CHAR    *pointer;
89 UINT    status;
90 
91     error_counter = 0;
92 
93     /* Setup the working pointer.  */
94     pointer =  (CHAR *) first_unused_memory;
95 
96     /* Create a helper thread for the server. */
97     tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
98                      pointer, DEMO_STACK_SIZE,
99                      4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
100 
101     pointer =  pointer + DEMO_STACK_SIZE;
102 
103     /* Initialize the NetX system.  */
104     nx_system_initialize();
105 
106     /* Create the server packet pool.  */
107     status =  nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
108                                     pointer, SERVER_PACKET_SIZE*8);
109     pointer = pointer + SERVER_PACKET_SIZE * 8;
110     if (status)
111         error_counter++;
112 
113     /* Create an IP instance.  */
114     status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
115                           0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
116                           pointer, 4096, 1);
117     pointer =  pointer + 4096;
118     if (status)
119         error_counter++;
120 
121     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
122     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
123     pointer = pointer + 1024;
124     if (status)
125         error_counter++;
126 
127 
128      /* Enable TCP traffic.  */
129     status = nx_tcp_enable(&server_ip);
130     if (status)
131         error_counter++;
132 
133     /* Set up the server's IPv4 address here. */
134 #ifdef __PRODUCT_NETXDUO__
135     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
136     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
137 #else
138     server_ip_address = HTTP_SERVER_ADDRESS;
139 #endif
140 
141     /* Create the HTTP Server.  */
142     status = nx_http_server_create(&my_server, "My HTTP Server", &server_ip, &ram_disk,
143                           pointer, 2048, &server_pool, NX_NULL, NX_NULL);
144     pointer =  pointer + 2048;
145     if (status)
146         error_counter++;
147 
148     /* Save the memory pointer for the RAM disk.  */
149     ram_disk_memory =  pointer;
150 
151     /* Create the HTTP Client thread. */
152     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
153                      pointer, DEMO_STACK_SIZE,
154                      6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
155     pointer =  pointer + DEMO_STACK_SIZE;
156     if (status)
157         error_counter++;
158 
159     /* Create the Client packet pool.  */
160     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
161                                     pointer, CLIENT_PACKET_SIZE*8);
162     pointer = pointer + CLIENT_PACKET_SIZE * 8;
163     if (status)
164         error_counter++;
165 
166     /* Create an IP instance.  */
167     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
168                           0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
169                           pointer, 2048, 1);
170     pointer =  pointer + 2048;
171     if (status)
172         error_counter++;
173 
174     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
175     pointer =  pointer + 2048;
176     if (status)
177         error_counter++;
178 
179      /* Enable TCP traffic.  */
180     status = nx_tcp_enable(&client_ip);
181     if (status)
182         error_counter++;
183 
184 }
185 
186 
thread_client_entry(ULONG thread_input)187 void thread_client_entry(ULONG thread_input)
188 {
189 
190 UINT            status;
191 NX_PACKET       *send_packet;
192 NX_PACKET       *recv_packet;
193 NX_PACKET       *my_packet;
194 CHAR            *buffer_ptr;
195 
196     /* Format the RAM disk - the memory for the RAM disk was setup in
197       tx_application_define above.  This must be set up before the client(s) start
198       sending requests. */
199     status = fx_media_format(&ram_disk,
200                             _fx_ram_driver,         /* Driver entry               */
201                             ram_disk_memory,        /* RAM disk memory pointer    */
202                             media_memory,           /* Media buffer pointer       */
203                             sizeof(media_memory),   /* Media buffer size          */
204                             "MY_RAM_DISK",          /* Volume Name                */
205                             1,                      /* Number of FATs             */
206                             32,                     /* Directory Entries          */
207                             0,                      /* Hidden sectors             */
208                             256,                    /* Total sectors              */
209                             128,                    /* Sector size                */
210                             1,                      /* Sectors per cluster        */
211                             1,                      /* Heads                      */
212                             1);                     /* Sectors per track          */
213 
214     /* Check the media format status.  */
215     if (status != FX_SUCCESS)
216         error_counter++;
217 
218     /* Open the RAM disk.  */
219     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory));
220 
221     /* Check the media open status.  */
222     if (status != FX_SUCCESS)
223         error_counter++;
224 
225     /* Create an HTTP client instance.  */
226     status = nx_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 600);
227 
228     /* Check status.  */
229     if (status)
230         error_counter++;
231 
232 #ifdef __PRODUCT_NETXDUO__
233 
234     /* Now upload an HTML file to the HTTP IP server using the 'duo' service (supports IPv4 and IPv6). */
235     status =  nxd_http_client_put_start(&my_client, &server_ip_address, "/index.htm",
236                                             "name", "password", 103, 5 * NX_IP_PERIODIC_RATE);
237 #else
238 
239     /* Now upload an HTML file to the HTTP IP server using the 'NetX' service (supports only IPv4). */
240     status =  nx_http_client_put_start(&my_client, HTTP_SERVER_ADDRESS, "/index.htm",
241                                    "name", "password", 103, 5 * NX_IP_PERIODIC_RATE);
242 #endif
243 
244     /* Check status.  */
245     if (status)
246         error_counter++;
247 
248     /* Allocate a packet.  */
249     status =  nx_packet_allocate(&client_pool, &send_packet, NX_TCP_PACKET, NX_WAIT_FOREVER);
250 
251     /* Check status.  */
252     if(status)
253         error_counter++;
254 
255     /* Build a simple 103-byte HTML page.  */
256     nx_packet_data_append(send_packet, "<HTML>\r\n", 8,
257                         &client_pool, NX_WAIT_FOREVER);
258     nx_packet_data_append(send_packet,
259                  "<HEAD><TITLE>NetX HTTP Test</TITLE></HEAD>\r\n", 44,
260                         &client_pool, NX_WAIT_FOREVER);
261     nx_packet_data_append(send_packet, "<BODY>\r\n", 8,
262                         &client_pool, NX_WAIT_FOREVER);
263     nx_packet_data_append(send_packet, "<H1>Another NetX Test Page!</H1>\r\n", 25,
264                         &client_pool, NX_WAIT_FOREVER);
265     nx_packet_data_append(send_packet, "</BODY>\r\n", 9,
266                         &client_pool, NX_WAIT_FOREVER);
267     nx_packet_data_append(send_packet, "</HTML>\r\n", 9,
268                         &client_pool, NX_WAIT_FOREVER);
269 
270     /* Complete the PUT by writing the total length.  */
271     status =  nx_http_client_put_packet(&my_client, send_packet, 1 * NX_IP_PERIODIC_RATE);
272     if(status)
273         error_counter++;
274 
275     status = nx_http_client_delete(&my_client);
276     if(status)
277         error_counter++;
278 
279     /* Create a socket.  */
280     status = nx_tcp_socket_create(&client_ip, &client_socket, "Client Socket",
281                                   NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1024,
282                                   NX_NULL, NX_NULL);
283     if(status)
284         error_counter++;
285 
286     /* Bind the socket.  */
287     status = nx_tcp_client_socket_bind(&client_socket, 50295, 1 * NX_IP_PERIODIC_RATE);
288     if(status)
289         error_counter++;
290 
291     /* Call connect to send an SYN.  */
292     status = nx_tcp_client_socket_connect(&client_socket, HTTP_SERVER_ADDRESS, 80, 2 * NX_IP_PERIODIC_RATE);
293     if(status)
294         error_counter++;
295 
296     /* Allocate a packet.  */
297     status = nx_packet_allocate(&client_pool, &my_packet, NX_TCP_PACKET, 1 * NX_IP_PERIODIC_RATE);
298     if(status)
299         error_counter++;
300 
301     /* Write DELETE 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_tcp_socket_send(&client_socket, my_packet, 1 * NX_IP_PERIODIC_RATE);
308     if(status)
309         error_counter++;
310 
311 
312     /* Receive the response from http server. */
313     status =  nx_tcp_socket_receive(&client_socket, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
314     if(status)
315         error_counter++;
316     else
317     {
318         buffer_ptr = (CHAR *)recv_packet ->nx_packet_prepend_ptr;
319 
320         /* Check the status, If success , it should be 200. */
321         if((buffer_ptr[9] != '2') || (buffer_ptr[10] != '0') || (buffer_ptr[11] != '0'))
322             error_counter++;
323 
324         nx_packet_release(recv_packet);
325     }
326 
327     tx_thread_sleep(1 * NX_IP_PERIODIC_RATE);
328 
329     if(error_counter)
330     {
331         printf("ERROR!\n");
332         test_control_return(1);
333     }
334     else
335     {
336         printf("SUCCESS!\n");
337         test_control_return(0);
338     }
339 
340 }
341 
342 
343 /* Define the helper HTTP server thread.  */
thread_server_entry(ULONG thread_input)344 void    thread_server_entry(ULONG thread_input)
345 {
346 
347 UINT            status;
348 
349     /* Print out test information banner.  */
350     printf("NetX Test:   HTTP Delete Basic Test....................................");
351 
352     /* Check for earlier error. */
353     if(error_counter)
354     {
355         printf("ERROR!\n");
356         test_control_return(1);
357     }
358 
359     /* OK to start the HTTP Server.   */
360     status = nx_http_server_start(&my_server);
361     if(status)
362         error_counter++;
363 
364     tx_thread_sleep(2 * NX_IP_PERIODIC_RATE);
365 
366     status = nx_http_server_delete(&my_server);
367     if(status)
368         error_counter++;
369 
370 }
371 #else
372 
373 #ifdef CTEST
test_application_define(void * first_unused_memory)374 VOID test_application_define(void *first_unused_memory)
375 #else
376 void    netx_http_delete_basic_test_application_define(void *first_unused_memory)
377 #endif
378 {
379 
380     /* Print out test information banner.  */
381     printf("NetX Test:   HTTP Delete Basic Test....................................N/A\n");
382 
383     test_control_return(3);
384 }
385 #endif
386