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 #ifdef NX_WEB_HTTPS_ENABLE
13 #include "globalsignrootca_cer.c"
14 #define ca_cert_der globalsignrootca_der
15 #define ca_cert_der_len globalsignrootca_der_len
16 #endif /* NX_WEB_HTTPS_ENABLE  */
17 
18 #define     DEMO_STACK_SIZE         4096
19 
20 /* Define device drivers.  */
21 extern  void _nx_pcap_network_driver(NX_IP_DRIVER*);
22 
23 /* Set up the HTTP client global variables. */
24 
25 #define         CLIENT_PACKET_SIZE  (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
26 
27 static TX_THREAD           client_thread;
28 static NX_PACKET_POOL      client_pool;
29 static NX_WEB_HTTP_CLIENT  my_client;
30 static NX_IP               client_ip;
31 static UINT                error_counter;
32 static NXD_ADDRESS         server_ip_address;
33 
34 static void thread_client_entry(ULONG thread_input);
35 
36 #define GATEWAY_IP_ADDRESS   IP_ADDRESS(192, 168, 100, 1)
37 #if 1
38 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(180, 149, 134, 141)/* weibo.com */
39 #define HOST_NAME "weibo.com"
40 #else
41 #define HTTP_SERVER_ADDRESS  IP_ADDRESS(115, 239, 210, 27)/* baidu.com */
42 #define HOST_NAME "www.baidu.com"
43 #endif
44 #define HTTP_CLIENT_ADDRESS  IP_ADDRESS(192, 168, 100, 22)
45 
46 #ifdef NX_WEB_HTTPS_ENABLE
47 static UINT loop = 2;
48 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
49 static CHAR crypto_metadata_client[20000 * NX_WEB_HTTP_SERVER_SESSION_MAX];
50 static UCHAR tls_packet_buffer[18500];
51 static NX_SECURE_X509_CERT trusted_certificate;
52 static NX_SECURE_X509_CERT remote_certificate, remote_issuer;
53 static UCHAR remote_cert_buffer[4096];
54 static UCHAR remote_issuer_buffer[4096];
55 
56 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session);
57 #else
58 static UINT loop = 1;
59 #endif /* NX_WEB_HTTPS_ENABLE  */
60 
61 #ifdef CTEST
test_application_define(void * first_unused_memory)62 VOID test_application_define(void *first_unused_memory)
63 #else
64 void    netx_web_external_server_test_application_define(void *first_unused_memory)
65 #endif
66 {
67 CHAR    *pointer;
68 UINT    status;
69 
70 
71     error_counter = 0;
72 
73     /* Setup the working pointer.  */
74     pointer =  (CHAR *) first_unused_memory;
75 
76     /* Initialize the NetX system.  */
77     nx_system_initialize();
78 
79     /* Create the HTTP Client thread. */
80     status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
81                               pointer, DEMO_STACK_SIZE,
82                               NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
83     pointer =  pointer + DEMO_STACK_SIZE;
84     if (status)
85         error_counter++;
86 
87     /* Create the Client packet pool.  */
88     status =  nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
89                                     pointer, CLIENT_PACKET_SIZE*16);
90     pointer = pointer + CLIENT_PACKET_SIZE * 16;
91     if (status)
92         error_counter++;
93 
94     /* Create an IP instance.  */
95     status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
96                           0xFFFFFF00UL, &client_pool, _nx_pcap_network_driver,
97                           pointer, 2048, 1);
98     pointer =  pointer + 2048;
99     if (status)
100         error_counter++;
101 
102     status  = nx_arp_enable(&client_ip, (void *) pointer, 1024);
103     pointer =  pointer + 2048;
104     if (status)
105         error_counter++;
106 
107      /* Enable TCP traffic.  */
108     status = nx_tcp_enable(&client_ip);
109     if (status)
110         error_counter++;
111 }
112 
113 #ifdef NX_WEB_HTTPS_ENABLE
114 /* Define the TLS setup callback function.  */
tls_setup_callback(NX_WEB_HTTP_CLIENT * client_ptr,NX_SECURE_TLS_SESSION * tls_session)115 static UINT tls_setup_callback(NX_WEB_HTTP_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *tls_session)
116 {
117 UINT status;
118 
119 
120     /* Initialize and create TLS session.  */
121     status = nx_secure_tls_session_create(tls_session, &nx_crypto_tls_ciphers, crypto_metadata_client, sizeof(crypto_metadata_client));
122 
123     /* Check status.  */
124     if (status)
125     {
126         return(status);
127     }
128 
129     /* Allocate space for packet reassembly.  */
130     status = nx_secure_tls_session_packet_buffer_set(&(client_ptr -> nx_web_http_client_tls_session), tls_packet_buffer, sizeof(tls_packet_buffer));
131 
132     /* Check status.  */
133     if (status)
134     {
135         return(status);
136     }
137 
138     /* Add a CA Certificate to our trusted store for verifying incoming server certificates.  */
139     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);
140     nx_secure_tls_trusted_certificate_add(&(client_ptr -> nx_web_http_client_tls_session), &trusted_certificate);
141 
142     /* Need to allocate space for the certificate coming in from the remote host.  */
143     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
144     nx_secure_tls_remote_certificate_allocate(&(client_ptr -> nx_web_http_client_tls_session), &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));
145 
146     return(NX_SUCCESS);
147 }
148 #endif /* NX_WEB_HTTPS_ENABLE  */
149 
thread_client_entry(ULONG thread_input)150 void thread_client_entry(ULONG thread_input)
151 {
152 UINT            i;
153 UINT            status;
154 NX_PACKET       *recv_packet;
155 
156     printf("NetX Test:   Web External Server Test..................................");
157 
158     /* Give IP task and driver a chance to initialize the system.  */
159     tx_thread_sleep(NX_IP_PERIODIC_RATE);
160 
161 #ifdef GATEWAY_IP_ADDRESS
162     /* Setup gateway for communicating with the outside world. */
163     status = nx_ip_gateway_address_set(&client_ip, GATEWAY_IP_ADDRESS);
164 
165     /* Check for errors.  */
166     if (status)
167     {
168         printf("Error in setting gateway address: 0x%02x\n", status);
169         return;
170     }
171 #endif
172 
173     /* Set server IP address.  */
174     server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
175     server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
176 
177     /* First loop test HTTP, second loop test HTTPS.  */
178     for (i = 0; i < loop ; i++)
179     {
180 
181         /* Create an HTTP client instance.  */
182         status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
183 
184         /* Check status.  */
185         if (status)
186             error_counter++;
187 
188         /* Send a GET request.  */
189         if (i == 0)
190         {
191             status = nx_web_http_client_get_start(&my_client, &server_ip_address,
192                                                   NX_WEB_HTTP_SERVER_PORT, "/index.htm",
193                                                   HOST_NAME,
194                                                   NX_NULL, NX_NULL, NX_WAIT_FOREVER);
195         }
196 #ifdef NX_WEB_HTTPS_ENABLE
197         else
198         {
199             status = nx_web_http_client_get_secure_start(&my_client, &server_ip_address,
200                                                          NX_WEB_HTTPS_SERVER_PORT, "/index.htm",
201                                                          HOST_NAME, NX_NULL, NX_NULL,
202                                                          tls_setup_callback, NX_WAIT_FOREVER);
203         }
204 #endif /* NX_WEB_HTTPS_ENABLE  */
205 
206         /* Check status.  */
207         if (status)
208             error_counter++;
209 
210         while (1)
211         {
212 
213             /* Get response from server.  */
214             status = nx_web_http_client_response_body_get(&my_client, &recv_packet, 1 * NX_IP_PERIODIC_RATE);
215 
216             if (status)
217             {
218                 break;
219             }
220             else
221             {
222                 nx_packet_release(recv_packet);
223             }
224         }
225 
226         /* Check status.  */
227         if (recv_packet)
228             nx_packet_release(recv_packet);
229 
230         status = nx_web_http_client_delete(&my_client);
231         if (status)
232             error_counter++;
233     }
234 
235     if(error_counter)
236     {
237         printf("ERROR!\n");
238         test_control_return(1);
239     }
240     else
241     {
242         printf("SUCCESS!\n");
243         test_control_return(0);
244     }
245 }
246 
247 #else
248 
249 #ifdef CTEST
test_application_define(void * first_unused_memory)250 VOID test_application_define(void *first_unused_memory)
251 #else
252 void    netx_web_external_server_test_application_define(void *first_unused_memory)
253 #endif
254 {
255 
256     /* Print out test information banner.  */
257     printf("NetX Test:   Web External Server Test..................................N/A\n");
258 
259     test_control_return(3);
260 }
261 #endif
262 
263