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)
11
12 #define DEMO_STACK_SIZE 4096
13
14 /* Define device drivers. */
15 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
16
17 /* Set up the HTTP client global variables. */
18
19 #define CLIENT_PACKET_SIZE (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
20
21 static TX_THREAD client_thread;
22 static NX_PACKET_POOL client_pool;
23 static NX_WEB_HTTP_CLIENT my_client;
24 static NX_IP client_ip;
25 static UINT error_counter;
26
27 /* Set up the HTTP server global variables */
28
29 #define SERVER_PACKET_SIZE (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
30
31 static NX_TCP_SOCKET server_socket;
32 static NX_PACKET_POOL server_pool;
33 static TX_THREAD server_thread;
34 static NX_IP server_ip;
35 static NXD_ADDRESS server_ip_address;
36
37 static void thread_client_entry(ULONG thread_input);
38 static void thread_server_entry(ULONG thread_input);
39
40 #define HTTP_SERVER_ADDRESS IP_ADDRESS(1,2,3,4)
41 #define HTTP_CLIENT_ADDRESS IP_ADDRESS(1,2,3,5)
42
43 /* Define status maps. */
44 static NX_WEB_HTTP_CLIENT_STATUS_MAP test_status_maps[] =
45 {
46 {"100", NX_WEB_HTTP_STATUS_CODE_CONTINUE},
47 {"101", NX_WEB_HTTP_STATUS_CODE_SWITCHING_PROTOCOLS},
48 {"201", NX_WEB_HTTP_STATUS_CODE_CREATED},
49 {"202", NX_WEB_HTTP_STATUS_CODE_ACCEPTED},
50 {"203", NX_WEB_HTTP_STATUS_CODE_NON_AUTH_INFO},
51 {"204", NX_WEB_HTTP_STATUS_CODE_NO_CONTENT},
52 {"205", NX_WEB_HTTP_STATUS_CODE_RESET_CONTENT},
53 {"206", NX_WEB_HTTP_STATUS_CODE_PARTIAL_CONTENT},
54 {"300", NX_WEB_HTTP_STATUS_CODE_MULTIPLE_CHOICES},
55 {"301", NX_WEB_HTTP_STATUS_CODE_MOVED_PERMANETLY},
56 {"302", NX_WEB_HTTP_STATUS_CODE_FOUND},
57 {"303", NX_WEB_HTTP_STATUS_CODE_SEE_OTHER},
58 {"304", NX_WEB_HTTP_STATUS_CODE_NOT_MODIFIED},
59 {"305", NX_WEB_HTTP_STATUS_CODE_USE_PROXY},
60 {"307", NX_WEB_HTTP_STATUS_CODE_TEMPORARY_REDIRECT},
61 {"400", NX_WEB_HTTP_STATUS_CODE_BAD_REQUEST},
62 {"401", NX_WEB_HTTP_STATUS_CODE_UNAUTHORIZED},
63 {"402", NX_WEB_HTTP_STATUS_CODE_PAYMENT_REQUIRED},
64 {"403", NX_WEB_HTTP_STATUS_CODE_FORBIDDEN},
65 {"404", NX_WEB_HTTP_STATUS_CODE_NOT_FOUND},
66 {"405", NX_WEB_HTTP_STATUS_CODE_METHOD_NOT_ALLOWED},
67 {"406", NX_WEB_HTTP_STATUS_CODE_NOT_ACCEPTABLE},
68 {"407", NX_WEB_HTTP_STATUS_CODE_PROXY_AUTH_REQUIRED},
69 {"408", NX_WEB_HTTP_STATUS_CODE_REQUEST_TIMEOUT},
70 {"409", NX_WEB_HTTP_STATUS_CODE_CONFLICT},
71 {"410", NX_WEB_HTTP_STATUS_CODE_GONE},
72 {"411", NX_WEB_HTTP_STATUS_CODE_LENGTH_REQUIRED},
73 {"412", NX_WEB_HTTP_STATUS_CODE_PRECONDITION_FAILED},
74 {"413", NX_WEB_HTTP_STATUS_CODE_ENTITY_TOO_LARGE},
75 {"414", NX_WEB_HTTP_STATUS_CODE_URL_TOO_LARGE},
76 {"415", NX_WEB_HTTP_STATUS_CODE_UNSUPPORTED_MEDIA},
77 {"416", NX_WEB_HTTP_STATUS_CODE_RANGE_NOT_SATISFY},
78 {"417", NX_WEB_HTTP_STATUS_CODE_EXPECTATION_FAILED},
79 {"500", NX_WEB_HTTP_STATUS_CODE_INTERNAL_ERROR},
80 {"501", NX_WEB_HTTP_STATUS_CODE_NOT_IMPLEMENTED},
81 {"502", NX_WEB_HTTP_STATUS_CODE_BAD_GATEWAY},
82 {"503", NX_WEB_HTTP_STATUS_CODE_SERVICE_UNAVAILABLE},
83 {"504", NX_WEB_HTTP_STATUS_CODE_GATEWAY_TIMEOUT},
84 {"505", NX_WEB_HTTP_STATUS_CODE_VERSION_ERROR},
85 };
86
87 static UINT test_count = sizeof(test_status_maps)/sizeof(NX_WEB_HTTP_CLIENT_STATUS_MAP);
88
89 static char pkt[] = {
90 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, /* HTTP/1.1 */
91 0x20, 0x32, 0x30, 0x30, 0x20, 0x0d, 0x0a, 0x0d, /* 200 ... */
92 0x0a, /* . */
93 };
94
95 #ifdef CTEST
test_application_define(void * first_unused_memory)96 VOID test_application_define(void *first_unused_memory)
97 #else
98 void netx_web_status_code_test_application_define(void *first_unused_memory)
99 #endif
100 {
101 CHAR *pointer;
102 UINT status;
103
104
105 error_counter = 0;
106
107 /* Setup the working pointer. */
108 pointer = (CHAR *) first_unused_memory;
109
110 /* Create a helper thread for the server. */
111 tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
112 pointer, DEMO_STACK_SIZE,
113 NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);
114
115 pointer = pointer + DEMO_STACK_SIZE;
116
117 /* Initialize the NetX system. */
118 nx_system_initialize();
119
120 /* Create the server packet pool. */
121 status = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
122 pointer, SERVER_PACKET_SIZE*8);
123 pointer = pointer + SERVER_PACKET_SIZE * 8;
124 if (status)
125 error_counter++;
126
127 /* Create an IP instance. */
128 status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
129 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
130 pointer, 4096, 1);
131 pointer = pointer + 4096;
132 if (status)
133 error_counter++;
134
135 /* Enable ARP and supply ARP cache memory for the server IP instance. */
136 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
137 pointer = pointer + 1024;
138 if (status)
139 error_counter++;
140
141
142 /* Enable TCP traffic. */
143 status = nx_tcp_enable(&server_ip);
144 if (status)
145 error_counter++;
146
147 /* Create the HTTP Client thread. */
148 status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
149 pointer, DEMO_STACK_SIZE,
150 NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
151 pointer = pointer + DEMO_STACK_SIZE;
152 if (status)
153 error_counter++;
154
155 /* Create the Client packet pool. */
156 status = nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
157 pointer, CLIENT_PACKET_SIZE*8);
158 pointer = pointer + CLIENT_PACKET_SIZE * 8;
159 if (status)
160 error_counter++;
161
162 /* Create an IP instance. */
163 status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
164 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
165 pointer, 2048, 1);
166 pointer = pointer + 2048;
167 if (status)
168 error_counter++;
169
170 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
171 pointer = pointer + 2048;
172 if (status)
173 error_counter++;
174
175 /* Enable TCP traffic. */
176 status = nx_tcp_enable(&client_ip);
177 if (status)
178 error_counter++;
179 }
180
thread_client_entry(ULONG thread_input)181 void thread_client_entry(ULONG thread_input)
182 {
183 UINT i;
184 UINT status;
185 NX_PACKET *recv_packet;
186
187
188 /* Wait server to set up. */
189 tx_thread_sleep(NX_IP_PERIODIC_RATE);
190
191 /* Set server IP address. */
192 server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
193 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
194
195 /* First loop test HTTP, second loop test HTTPS. */
196 for (i = 0; i < test_count ; i++)
197 {
198
199 /* Create an HTTP client instance. */
200 status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
201
202 /* Check status. */
203 if (status)
204 error_counter++;
205
206 /* Send a GET request. */
207 status = nx_web_http_client_get_start(&my_client, &server_ip_address,
208 NX_WEB_HTTP_SERVER_PORT, "http://1.2.3.4/test.txt",
209 "1.2.3.4", "name", "password", NX_WAIT_FOREVER);
210
211 /* Check status. */
212 if (status)
213 error_counter++;
214
215 /* Get response from server. */
216 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
217
218 /* Check status. */
219 if (test_status_maps[i].nx_web_http_client_status_string[0] == '2')
220 {
221 if (status != NX_SUCCESS && status != NX_WEB_HTTP_GET_DONE)
222 error_counter++;
223 }
224 else if (status != test_status_maps[i].nx_web_http_client_status_code)
225 {
226 error_counter++;
227 }
228
229 if (recv_packet)
230 {
231 nx_packet_release(recv_packet);
232 }
233
234 status = nx_web_http_client_delete(&my_client);
235 if (status)
236 error_counter++;
237
238 tx_thread_resume(&server_thread);
239 }
240 }
241
242 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)243 void thread_server_entry(ULONG thread_input)
244 {
245 UINT status;
246 NX_PACKET *recv_packet;
247 NX_PACKET *send_packet;
248 UINT i;
249
250 /* Print out test information banner. */
251 printf("NetX Test: Web Status Code Test......................................");
252
253 /* Check for earlier error. */
254 if(error_counter)
255 {
256 printf("ERROR!\n");
257 test_control_return(1);
258 }
259
260 /* Create a TCP socket act as the HTTP server. */
261 status = nx_tcp_socket_create(&server_ip, &server_socket, "Socket Server", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
262 NX_IP_TIME_TO_LIVE, 2048, NX_NULL, NX_NULL);
263
264 /* Check status. */
265 if (status)
266 {
267 error_counter++;
268 }
269
270 /* Bind the TCP socket to the IP port. */
271 status = nx_tcp_server_socket_listen(&server_ip, 80, &server_socket, 5, NX_NULL);
272
273 /* Check status. */
274 if (status)
275 {
276 error_counter++;
277 }
278
279 /* Act as the HTTP server to receive the Client query and send the HTTP server response. */
280 for (i = 0; i < test_count; i++ )
281 {
282
283 /* Wait for a connection request. */
284 status = nx_tcp_server_socket_accept(&server_socket, 500);
285
286 /* Check status. */
287 if (status)
288 {
289 error_counter++;
290 }
291
292 /* Receive a TCP packet. */
293 status = nx_tcp_socket_receive(&server_socket, &recv_packet, 10 * NX_IP_PERIODIC_RATE);
294
295 /* Check status. */
296 if (status)
297 {
298 error_counter++;
299 }
300
301 /* Release the packet. */
302 nx_packet_release(recv_packet);
303
304 nx_packet_allocate(&server_pool, &send_packet, NX_TCP_PACKET, NX_WAIT_FOREVER);
305
306 /* Set the test status code. */
307 pkt[9] = test_status_maps[i].nx_web_http_client_status_string[0];
308 pkt[10] = test_status_maps[i].nx_web_http_client_status_string[1];
309 pkt[11] = test_status_maps[i].nx_web_http_client_status_string[2];
310
311 nx_packet_data_append(send_packet, pkt, sizeof(pkt), &server_pool, NX_WAIT_FOREVER);
312
313 /* Send the TCP response packet. */
314 status = nx_tcp_socket_send(&server_socket, send_packet, 200);
315
316 /* Check status. */
317 if (status)
318 {
319 error_counter++;
320 }
321
322 /* Disconnect the server socket. */
323 status = nx_tcp_socket_disconnect(&server_socket, 200);
324
325 /* Unaccept the server socket. */
326 status = nx_tcp_server_socket_unaccept(&server_socket);
327
328 /* Unbind the UDP socket. */
329 status = nx_tcp_server_socket_relisten(&server_ip, 80, &server_socket);
330
331 /* Check status. */
332 if (status)
333 {
334 error_counter++;
335 }
336
337 tx_thread_suspend(&server_thread);
338 }
339
340 /* Unaccept the server socket. */
341 status = nx_tcp_server_socket_unaccept(&server_socket);
342
343 /* Unbind the UDP socket. */
344 status = nx_tcp_server_socket_unlisten(&server_ip, 80);
345
346 /* Check status. */
347 if (status)
348 {
349 error_counter++;
350 }
351 /* Delete the TCP socket. */
352 status = nx_tcp_socket_delete(&server_socket);
353
354 /* Check status. */
355 if (status)
356 {
357 error_counter++;
358 }
359
360 /* Check packet pool. */
361 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
362 {
363 error_counter++;
364 }
365
366 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
367 {
368 error_counter++;
369 }
370
371 if(error_counter)
372 {
373 printf("ERROR!\n");
374 test_control_return(1);
375 }
376 else
377 {
378 printf("SUCCESS!\n");
379 test_control_return(0);
380 }
381 }
382 #else
383
384 #ifdef CTEST
test_application_define(void * first_unused_memory)385 VOID test_application_define(void *first_unused_memory)
386 #else
387 void netx_web_status_code_test_application_define(void *first_unused_memory)
388 #endif
389 {
390
391 /* Print out test information banner. */
392 printf("NetX Test: Web Status Code Test......................................N/A\n");
393
394 test_control_return(3);
395 }
396 #endif
397
398