1 /* This case tests filed If-Modified-field.
2 * if the requested variant has not been modified since the time specified in this field,
3 * an entity will not be returned from the server; instead, a 304 (not modified) response
4 * will be returned without any message-body.
5 *
6 * In this case, client get the index.htm with If-Modified-Since = 20130101, and index.htm is
7 * modified on 2013...., So server should send 304.
8 * */
9
10 #include "tx_api.h"
11 #include "nx_api.h"
12 #include "fx_api.h"
13 #include "nxd_http_client.h"
14 #include "nxd_http_server.h"
15
16
17 #if defined(WIN32) || defined(__linux__)
18 #include <time.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #endif /* defined(WIN32) || defined(__linux__) */
22
23 extern void test_control_return(UINT);
24
25 #if !defined(NX_DISABLE_IPV4)
26
27 #define DEMO_STACK_SIZE 4096
28
29
30 /* Frame (190 bytes) */
31 /* This is a HTTP get packet captured by wireshark. If-Modified-since = 20130101 .*/
32 static char pkt[] = {
33 0x00, 0x11, 0x22, 0x33, 0x44, 0x57, 0xb8, 0xca, /* .."3DW.. */
34 0x3a, 0x95, 0xdb, 0x0b, 0x08, 0x00, 0x45, 0x00, /* :.....E. */
35 0x00, 0xb0, 0x09, 0xb8, 0x40, 0x00, 0x80, 0x06, /* ....@... */
36 0x6e, 0x5b, 0xc0, 0xa8, 0x00, 0x69, 0xc0, 0xa8, /* n[...i.. */
37 0x00, 0x7b, 0xc4, 0x77, 0x00, 0x50, 0x51, 0x6d, /* .{.w.PQm */
38 0xbc, 0x22, 0x77, 0x7f, 0x23, 0xc7, 0x50, 0x18, /* ."w.#.P. */
39 0xfa, 0xf0, 0x20, 0x9c, 0x00, 0x00, 0x47, 0x45, /* .. ...GE */
40 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64, 0x65, 0x78, /* T /index */
41 0x2e, 0x68, 0x74, 0x6d, 0x20, 0x48, 0x54, 0x54, /* .htm HTT */
42 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x55, /* P/1.1..U */
43 0x73, 0x65, 0x72, 0x2d, 0x41, 0x67, 0x65, 0x6e, /* ser-Agen */
44 0x74, 0x3a, 0x20, 0x63, 0x75, 0x72, 0x6c, 0x2f, /* t: curl/ */
45 0x37, 0x2e, 0x33, 0x32, 0x2e, 0x30, 0x0d, 0x0a, /* 7.32.0.. */
46 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x39, /* Host: 19 */
47 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x30, 0x2e, /* 2.168.0. */
48 0x31, 0x32, 0x33, 0x0d, 0x0a, 0x41, 0x63, 0x63, /* 123..Acc */
49 0x65, 0x70, 0x74, 0x3a, 0x20, 0x2a, 0x2f, 0x2a, /* ept: */
50 0x0d, 0x0a, 0x49, 0x66, 0x2d, 0x4d, 0x6f, 0x64, /* ..If-Mod */
51 0x69, 0x66, 0x69, 0x65, 0x64, 0x2d, 0x53, 0x69, /* ified-Si */
52 0x6e, 0x63, 0x65, 0x3a, 0x20, 0x54, 0x75, 0x65, /* nce: Tue */
53 0x2c, 0x20, 0x30, 0x31, 0x20, 0x4a, 0x61, 0x6e, /* , 01 Jan */
54 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x30, 0x30, /* 2013 00 */
55 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x30, 0x20, 0x47, /* :00:00 G */
56 0x4d, 0x54, 0x0d, 0x0a, 0x0d, 0x0a /* MT.... */
57 };
58
59
60
61 /* Set up FileX and file memory resources. */
62 static CHAR *ram_disk_memory;
63 static FX_MEDIA ram_disk;
64 static unsigned char media_memory[512];
65
66 /* Define device drivers. */
67 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
68 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
69 #if defined(WIN32) || defined(__linux__)
70 static VOID get_gmt(NX_HTTP_SERVER_DATE *now);
71 static UINT cache_info_get(CHAR *resource, UINT *max_age, NX_HTTP_SERVER_DATE *last_modified);
72 #endif
73
74
75 /* Set up the HTTP client global variables. */
76
77 #define CLIENT_PACKET_SIZE (NX_HTTP_SERVER_MIN_PACKET_SIZE * 2)
78
79 static TX_THREAD client_thread;
80 static NX_PACKET_POOL client_pool;
81 static NX_HTTP_CLIENT my_client;
82 static NX_IP client_ip;
83 static UINT error_counter;
84
85 static NX_TCP_SOCKET client_socket;
86
87 /* Set up the HTTP server global variables */
88
89 #define SERVER_PACKET_SIZE (NX_HTTP_SERVER_MIN_PACKET_SIZE * 2)
90
91 static NX_HTTP_SERVER my_server;
92 static NX_PACKET_POOL server_pool;
93 static TX_THREAD server_thread;
94 static NX_IP server_ip;
95 #ifdef __PRODUCT_NETXDUO__
96 static NXD_ADDRESS server_ip_address;
97 #else
98 static ULONG server_ip_address;
99 #endif
100
101
102 static void thread_client_entry(ULONG thread_input);
103 static void thread_server_entry(ULONG thread_input);
104
105 #define HTTP_SERVER_ADDRESS IP_ADDRESS(192,168,0,105)
106 #define HTTP_CLIENT_ADDRESS IP_ADDRESS(192,168,0,123)
107
108
109 #ifdef CTEST
test_application_define(void * first_unused_memory)110 VOID test_application_define(void *first_unused_memory)
111 #else
112 void netx_http_if_modified_since_test_application_define(void *first_unused_memory)
113 #endif
114 {
115
116 CHAR *pointer;
117 UINT status;
118
119 error_counter = 0;
120
121 /* Setup the working pointer. */
122 pointer = (CHAR *) first_unused_memory;
123
124 /* Create a helper thread for the server. */
125 tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
126 pointer, DEMO_STACK_SIZE,
127 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
128
129 pointer = pointer + DEMO_STACK_SIZE;
130
131 /* Initialize the NetX system. */
132 nx_system_initialize();
133
134 /* Create the server packet pool. */
135 status = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
136 pointer, SERVER_PACKET_SIZE*8);
137 pointer = pointer + SERVER_PACKET_SIZE * 8;
138 if (status)
139 error_counter++;
140
141 /* Create an IP instance. */
142 status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
143 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
144 pointer, 4096, 1);
145 pointer = pointer + 4096;
146 if (status)
147 error_counter++;
148
149 /* Enable ARP and supply ARP cache memory for the server IP instance. */
150 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
151 pointer = pointer + 1024;
152 if (status)
153 error_counter++;
154
155
156 /* Enable TCP traffic. */
157 status = nx_tcp_enable(&server_ip);
158 if (status)
159 error_counter++;
160
161 /* Set up the server's IPv4 address here. */
162 #ifdef __PRODUCT_NETXDUO__
163 server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
164 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
165 #else
166 server_ip_address = HTTP_SERVER_ADDRESS;
167 #endif
168
169 /* Create the HTTP Server. */
170 status = nx_http_server_create(&my_server, "My HTTP Server", &server_ip, &ram_disk,
171 pointer, 2048, &server_pool, NX_NULL, NX_NULL);
172 pointer = pointer + 2048;
173 if (status)
174 error_counter++;
175
176 /* Save the memory pointer for the RAM disk. */
177 ram_disk_memory = pointer;
178
179 /* Create the HTTP Client thread. */
180 status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
181 pointer, DEMO_STACK_SIZE,
182 6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
183 pointer = pointer + DEMO_STACK_SIZE;
184 if (status)
185 error_counter++;
186
187 /* Create the Client packet pool. */
188 status = nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
189 pointer, CLIENT_PACKET_SIZE*8);
190 pointer = pointer + CLIENT_PACKET_SIZE * 8;
191 if (status)
192 error_counter++;
193
194 /* Create an IP instance. */
195 status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
196 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
197 pointer, 2048, 1);
198 pointer = pointer + 2048;
199 if (status)
200 error_counter++;
201
202 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
203 pointer = pointer + 2048;
204 if (status)
205 error_counter++;
206
207 /* Enable TCP traffic. */
208 status = nx_tcp_enable(&client_ip);
209 if (status)
210 error_counter++;
211
212 #if defined(WIN32) || defined(__linux__)
213 nx_http_server_gmt_callback_set(&my_server, get_gmt);
214 nx_http_server_cache_info_callback_set(&my_server, cache_info_get);
215 #endif /* WIN32 || __linux__ */
216
217 }
218
219
thread_client_entry(ULONG thread_input)220 void thread_client_entry(ULONG thread_input)
221 {
222
223 UINT status;
224 NX_PACKET *send_packet;
225 NX_PACKET *recv_packet;
226 NX_PACKET *my_packet;
227 CHAR *buffer_ptr;
228
229 /* Format the RAM disk - the memory for the RAM disk was setup in
230 tx_application_define above. This must be set up before the client(s) start
231 sending requests. */
232 status = fx_media_format(&ram_disk,
233 _fx_ram_driver, /* Driver entry */
234 ram_disk_memory, /* RAM disk memory pointer */
235 media_memory, /* Media buffer pointer */
236 sizeof(media_memory), /* Media buffer size */
237 "MY_RAM_DISK", /* Volume Name */
238 1, /* Number of FATs */
239 32, /* Directory Entries */
240 0, /* Hidden sectors */
241 256, /* Total sectors */
242 128, /* Sector size */
243 1, /* Sectors per cluster */
244 1, /* Heads */
245 1); /* Sectors per track */
246
247 /* Check the media format status. */
248 if (status != FX_SUCCESS)
249 error_counter++;
250
251 /* Open the RAM disk. */
252 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory));
253
254 /* Check the media open status. */
255 if (status != FX_SUCCESS)
256 error_counter++;
257
258 /* Create an HTTP client instance. */
259 status = nx_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 600);
260
261 /* Check status. */
262 if (status)
263 error_counter++;
264
265 #ifdef __PRODUCT_NETXDUO__
266
267 /* Now upload an HTML file to the HTTP IP server using the 'duo' service (supports IPv4 and IPv6). */
268 status = nxd_http_client_put_start(&my_client, &server_ip_address, "/index.htm",
269 "name", "password", 103, 5 * NX_IP_PERIODIC_RATE);
270 #else
271
272 /* Now upload an HTML file to the HTTP IP server using the 'NetX' service (supports only IPv4). */
273 status = nx_http_client_put_start(&my_client, HTTP_SERVER_ADDRESS, "/index.htm",
274 "name", "password", 103, 5 * NX_IP_PERIODIC_RATE);
275 #endif
276
277 /* Check status. */
278 if (status)
279 error_counter++;
280
281 /* Allocate a packet. */
282 status = nx_packet_allocate(&client_pool, &send_packet, NX_TCP_PACKET, NX_WAIT_FOREVER);
283
284 /* Check status. */
285 if(status)
286 error_counter++;
287
288 /* Build a simple 103-byte HTML page. */
289 nx_packet_data_append(send_packet, "<HTML>\r\n", 8,
290 &client_pool, NX_WAIT_FOREVER);
291 nx_packet_data_append(send_packet,
292 "<HEAD><TITLE>NetX HTTP Test</TITLE></HEAD>\r\n", 44,
293 &client_pool, NX_WAIT_FOREVER);
294 nx_packet_data_append(send_packet, "<BODY>\r\n", 8,
295 &client_pool, NX_WAIT_FOREVER);
296 nx_packet_data_append(send_packet, "<H1>Another NetX Test Page!</H1>\r\n", 25,
297 &client_pool, NX_WAIT_FOREVER);
298 nx_packet_data_append(send_packet, "</BODY>\r\n", 9,
299 &client_pool, NX_WAIT_FOREVER);
300 nx_packet_data_append(send_packet, "</HTML>\r\n", 9,
301 &client_pool, NX_WAIT_FOREVER);
302
303 /* Complete the PUT by writing the total length. */
304 status = nx_http_client_put_packet(&my_client, send_packet, 1 * NX_IP_PERIODIC_RATE);
305 if(status)
306 error_counter++;
307
308 status = nx_http_client_delete(&my_client);
309 if(status)
310 error_counter++;
311
312 /* Create a socket. */
313 status = nx_tcp_socket_create(&client_ip, &client_socket, "Client Socket",
314 NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 1024,
315 NX_NULL, NX_NULL);
316 if(status)
317 error_counter++;
318
319 /* Bind the socket. */
320 status = nx_tcp_client_socket_bind(&client_socket, 50295, 1 * NX_IP_PERIODIC_RATE);
321 if(status)
322 error_counter++;
323
324 /* Call connect to send an SYN. */
325 status = nx_tcp_client_socket_connect(&client_socket, HTTP_SERVER_ADDRESS, 80, 2 * NX_IP_PERIODIC_RATE);
326 if(status)
327 error_counter++;
328
329 /* Allocate a packet. */
330 status = nx_packet_allocate(&client_pool, &my_packet, NX_TCP_PACKET, 1 * NX_IP_PERIODIC_RATE);
331 if(status)
332 error_counter++;
333
334 /* Write If-Mdified-since Get packet into the packet payload. */
335 status = nx_packet_data_append(my_packet, &pkt[54] , (sizeof(pkt)-54), &client_pool, 1 * NX_IP_PERIODIC_RATE);
336 if(status)
337 error_counter++;
338
339 /* Send the packet out. */
340 status = nx_tcp_socket_send(&client_socket, my_packet, 1 * NX_IP_PERIODIC_RATE);
341 if(status)
342 error_counter++;
343
344
345 /* Receive the response from http server. */
346 status = nx_tcp_socket_receive(&client_socket, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
347 if(status)
348 error_counter++;
349 else
350 {
351
352 buffer_ptr = (CHAR *)recv_packet ->nx_packet_prepend_ptr;
353
354 /* Check the status, If success , it should be 304. */
355 if((buffer_ptr[9] != '3') || (buffer_ptr[10] != '0') || (buffer_ptr[11] != '4'))
356 error_counter++;
357
358 nx_packet_release(recv_packet);
359 }
360
361 tx_thread_sleep(1 * NX_IP_PERIODIC_RATE);
362
363 if(error_counter)
364 {
365 printf("ERROR!\n");
366 test_control_return(1);
367 }
368 else
369 {
370 printf("SUCCESS!\n");
371 test_control_return(0);
372 }
373
374 }
375
376
377 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)378 void thread_server_entry(ULONG thread_input)
379 {
380
381 UINT status;
382
383 /* Print out test information banner. */
384 printf("NetX Test: HTTP If Modified Since Test...............................");
385
386 /* Check for earlier error. */
387 if(error_counter)
388 {
389 printf("ERROR!\n");
390 test_control_return(1);
391 }
392
393 /* OK to start the HTTP Server. */
394 status = nx_http_server_start(&my_server);
395 if(status)
396 error_counter++;
397
398 tx_thread_sleep(2 * NX_IP_PERIODIC_RATE);
399
400 status = nx_http_server_delete(&my_server);
401 if(status)
402 error_counter++;
403
404 }
405
406
407 #if defined(WIN32) || defined(__linux__)
get_gmt(NX_HTTP_SERVER_DATE * now)408 VOID get_gmt(NX_HTTP_SERVER_DATE *now)
409 {
410 time_t rawtime;
411 struct tm *timeinfo;
412
413 time (&rawtime);
414 timeinfo = gmtime(&rawtime);
415
416 now -> nx_http_server_day = timeinfo -> tm_mday;
417 now -> nx_http_server_month = timeinfo -> tm_mon;
418 now -> nx_http_server_year = timeinfo -> tm_year + 1900;
419 now -> nx_http_server_hour = timeinfo -> tm_hour;
420 now -> nx_http_server_minute = timeinfo -> tm_min;
421 now -> nx_http_server_second = timeinfo -> tm_sec;
422 now -> nx_http_server_weekday = timeinfo -> tm_wday;
423
424
425 }
426
427
cache_info_get(CHAR * resource,UINT * max_age,NX_HTTP_SERVER_DATE * last_modified)428 UINT cache_info_get(CHAR *resource, UINT *max_age, NX_HTTP_SERVER_DATE *last_modified)
429 {
430 *max_age = 315360000;
431
432 /* 0130101 */
433 last_modified -> nx_http_server_day = 1;
434 last_modified -> nx_http_server_month = 0;
435 last_modified -> nx_http_server_year = 113 + 1900; /* 2013 */
436 last_modified -> nx_http_server_hour = 0;
437 last_modified -> nx_http_server_minute = 0;
438 last_modified -> nx_http_server_second = 0;
439 last_modified -> nx_http_server_weekday = 2;
440
441 return NX_TRUE;
442 }
443 #endif /* WIN32 || __linux__ */
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_http_if_modified_since_test_application_define(void *first_unused_memory)
451 #endif
452 {
453
454 /* Print out test information banner. */
455 printf("NetX Test: HTTP If Modified Since Test...............................N/A\n");
456
457 test_control_return(3);
458 }
459 #endif
460