Debian Codegraph @

Xpjproject/pjlib-util/src/pjlib-util-test/http_client.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "test.h"
20
21 #if INCLUDE_HTTP_CLIENT_TEST
22
23 #define THIS_FILE "test_http"
24 //#define VERBOSE
25 #define STR_PREC(s) (int)s.slen, s.ptr
26 #define USE_LOCAL_SERVER
27
28 #include <pjlib.h>
29 #include <pjlib-util.h>
30
31 #define ACTION_REPLY 0
32 #define ACTION_IGNORE -1
33
34 static struct server_t
35 {
36 pj_sock_t sock;
37 pj_uint16_t port;
38 pj_thread_t *thread;
39
40 /* Action:
41 * 0: reply with the response in resp.
42 * -1: ignore query (to simulate timeout).
43 * other: reply with that error
44 */
45 int action;
46 pj_bool_t send_content_length;
47 unsigned data_size;
48 unsigned buf_size;
49 } g_server;
50
51 static pj_bool_t thread_quit;
52 static pj_timer_heap_t *timer_heap;
53 static pj_ioqueue_t *ioqueue;
54 static pj_pool_t *pool;
55 static pj_http_req *http_req;
56 static pj_bool_t test_cancel = PJ_FALSE;
57 static pj_size_t total_size;
58 static pj_size_t send_size = 0;
59 static pj_status_t sstatus;
60 static pj_sockaddr_in addr;
61 static int counter = 0;
62
63 static int server_thread(void *p)
64 {
65 struct server_t *srv = (struct server_t*)p;
66 char *pkt = (char*)pj_pool_alloc(pool, srv->buf_size);
67 pj_sock_t newsock = PJ_INVALID_SOCKET;
68
69 while (!thread_quit) {
70 pj_ssize_t pkt_len;
71 int rc;
72 pj_fd_set_t rset;
73 pj_time_val timeout = {0, 500};
74
75 while (!thread_quit) {
76 PJ_FD_ZERO(&rset);
77 PJ_FD_SET(srv->sock, &rset);
78 rc = pj_sock_select((int)srv->sock+1, &rset, NULL, NULL, &timeout);
79 if (rc != 1) {
80 continue;
81 }
82
83 rc = pj_sock_accept(srv->sock, &newsock, NULL, NULL);
84 if (rc == PJ_SUCCESS) {
85 break;
86 }
87 }
88
89 if (thread_quit)
90 break;
91
92 while (!thread_quit) {
93 PJ_FD_ZERO(&rset);
94 PJ_FD_SET(newsock, &rset);
95 rc = pj_sock_select((int)newsock+1, &rset, NULL, NULL, &timeout);
96 if (rc != 1) {
97 PJ_LOG(3,("http test", "client timeout"));
98 continue;
99 }
100
101 pkt_len = srv->buf_size;
102 rc = pj_sock_recv(newsock, pkt, &pkt_len, 0);
103 if (rc == PJ_SUCCESS) {
104 break;
105 }
106 }
107
108 if (thread_quit)
109 break;
110
111 /* Simulate network RTT */
112 pj_thread_sleep(50);
113
114 if (srv->action == ACTION_IGNORE) {
115 continue;
116 } else if (srv->action == ACTION_REPLY) {
117 pj_size_t send_len = 0;
118 unsigned ctr = 0;
119 pkt_len = pj_ansi_snprintf(pkt, srv->buf_size,
120 "HTTP/1.0 200 OK\r\n");
121 PJ_ASSERT_ON_FAIL(pkt_len>0, {
122 PJ_LOG(2, (THIS_FILE, "Error creating response, pkt_len=%ld",
123 pkt_len));
124 pj_sock_close(newsock);
125 continue;
126 })
127 if (srv->send_content_length) {
128 pj_ansi_snprintf(pkt + pkt_len, srv->buf_size - pkt_len,
129 "Content-Length: %d\r\n",
130 srv->data_size);
131 }
132 pkt_len = pj_ansi_strxcat(pkt, "\r\n", srv->buf_size);
133 if (pkt_len < 0) {
134 PJ_LOG(2, (THIS_FILE, "Error creating response 2, pkt_len=%ld",
135 pkt_len));
136 pj_sock_close(newsock);
137 continue;
138 }
139 rc = pj_sock_send(newsock, pkt, &pkt_len, 0);
140 if (rc != PJ_SUCCESS) {
141 pj_sock_close(newsock);
142 continue;
143 }
144 while (send_len < srv->data_size) {
145 pkt_len = srv->data_size - send_len;
146 if (pkt_len > (signed)srv->buf_size)
147 pkt_len = srv->buf_size;
148 send_len += pkt_len;
149 pj_create_random_string(pkt, pkt_len);
150 pj_ansi_snprintf(pkt, srv->buf_size, "\nPacket: %d", ++ctr);
151 pkt[pj_ansi_strlen(pkt)] = '\n';
152 rc = pj_sock_send(newsock, pkt, &pkt_len, 0);
153 if (rc != PJ_SUCCESS)
154 break;
155 }
156 pj_sock_close(newsock);
157 }
158 }
159
160 return 0;
161 }
162
163 static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size)
164 {
165 PJ_UNUSED_ARG(hreq);
166 PJ_UNUSED_ARG(data);
167
168 PJ_LOG(5, (THIS_FILE, "\nData received: %lu bytes", (unsigned long)size));
169 if (size > 0) {
170 #ifdef VERBOSE
171 printf("%.*s\n", (int)size, (char *)data);
172 #endif
173 }
174 }
175
176 static void on_send_data(pj_http_req *hreq,
177 void **data, pj_size_t *size)
178 {
179 char *sdata;
180 pj_size_t sendsz = 8397;
181
182 PJ_UNUSED_ARG(hreq);
183
184 if (send_size + sendsz > total_size) {
185 sendsz = total_size - send_size;
186 }
187 send_size += sendsz;
188
189 sdata = (char*)pj_pool_alloc(pool, sendsz);
190 pj_create_random_string(sdata, sendsz);
191 pj_ansi_snprintf(sdata, sendsz, "\nSegment #%d\n", ++counter);
192 *data = sdata;
193 *size = sendsz;
194
195 PJ_LOG(5, (THIS_FILE, "\nSending data progress: %lu out of %lu bytes",
196 (unsigned long)send_size, (unsigned long)total_size));
197 }
198
199
200 static void on_complete(pj_http_req *hreq, pj_status_t status,
201 const pj_http_resp *resp)
202 {
203 PJ_UNUSED_ARG(hreq);
204
205 if (status == PJ_ECANCELLED) {
206 PJ_LOG(5, (THIS_FILE, "Request cancelled"));
207 return;
208 } else if (status == PJ_ETIMEDOUT) {
209 PJ_LOG(5, (THIS_FILE, "Request timed out!"));
210 return;
211 } else if (status != PJ_SUCCESS) {
212 PJ_LOG(3, (THIS_FILE, "Error %d", status));
213 return;
214 }
215 PJ_LOG(5, (THIS_FILE, "\nData completed: %lu bytes",
216 (unsigned long)resp->size));
217 if (resp->size > 0 && resp->data) {
218 #ifdef VERBOSE
219 printf("%.*s\n", (int)resp->size, (char *)resp->data);
220 #endif
221 }
222 }
223
224 static void on_response(pj_http_req *hreq, const pj_http_resp *resp)
225 {
226 pj_size_t i;
227
228 PJ_UNUSED_ARG(hreq);
229 PJ_UNUSED_ARG(resp);
230 PJ_UNUSED_ARG(i);
231
232 #ifdef VERBOSE
233 printf("%.*s, %d, %.*s\n", STR_PREC(resp->version),
234 resp->status_code, STR_PREC(resp->reason));
235 for (i = 0; i < resp->headers.count; i++) {
236 printf("%.*s : %.*s\n",
237 STR_PREC(resp->headers.header[i].name),
238 STR_PREC(resp->headers.header[i].value));
239 }
240 #endif
241
242 if (test_cancel) {
243 /* Need to delay closing the client socket here, otherwise the
244 * server will get SIGPIPE when sending response.
245 */
246 pj_thread_sleep(100);
247 pj_http_req_cancel(hreq, PJ_TRUE);
248 test_cancel = PJ_FALSE;
249 }
250 }
251
252
253 pj_status_t parse_url(const char *url, pj_http_url *hurl)
254 {
255 pj_str_t surl;
256 pj_status_t status;
257
258 pj_cstr(&surl, url);
259 status = pj_http_req_parse_url(&surl, hurl);
260 #ifdef VERBOSE
261 if (!status) {
262 printf("URL: %s\nProtocol: %.*s\nHost: %.*s\nPort: %d\nPath: %.*s\n\n",
263 url, STR_PREC(hurl->protocol), STR_PREC(hurl->host),
264 hurl->port, STR_PREC(hurl->path));
265 } else {
266 }
267 #endif
268 return status;
269 }
270
271 static int parse_url_test()
272 {
273 struct test_data
274 {
275 char *url;
276 pj_status_t result;
277 const char *username;
278 const char *passwd;
279 const char *host;
280 int port;
281 const char *path;
282 } test_data[] =
283 {
284 /* Simple URL without '/' in the end */
285 {"http://www.pjsip.org", PJ_SUCCESS, "", "", "www.pjsip.org", 80, "/"},
286
287 /* Simple URL with port number but without '/' in the end */
288 {"http://pjsip.org:8080", PJ_SUCCESS, "", "", "pjsip.org", 8080, "/"},
289
290 /* URL with path */
291 {"http://127.0.0.1:280/Joomla/index.php?option=com_content&task=view&id=5&Itemid=6",
292 PJ_SUCCESS, "", "", "127.0.0.1", 280,
293 "/Joomla/index.php?option=com_content&task=view&id=5&Itemid=6"},
294
295 /* URL with port and path */
296 {"http://pjsip.org:81/about-us/", PJ_SUCCESS, "", "", "pjsip.org", 81, "/about-us/"},
297
298 /* unsupported protocol */
299 {"ftp://www.pjsip.org", PJ_ENOTSUP, "", "", "", 80, ""},
300
301 /* invalid format */
302 {"http:/pjsip.org/about-us/", PJLIB_UTIL_EHTTPINURL, "", "", "", 80, ""},
303
304 /* invalid port number */
305 {"http://pjsip.org:xyz/", PJLIB_UTIL_EHTTPINPORT, "", "", "", 80, ""},
306
307 /* with username and password */
308 {"http://user:[email protected]", PJ_SUCCESS, "user", "pass", "pjsip.org", 80, "/"},
309
310 /* password only*/
311 {"http://:[email protected]", PJ_SUCCESS, "", "pass", "pjsip.org", 80, "/"},
312
313 /* user only*/
314 {"http://user:@pjsip.org", PJ_SUCCESS, "user", "", "pjsip.org", 80, "/"},
315
316 /* empty username and passwd*/
317 {"http://:@pjsip.org", PJ_SUCCESS, "", "", "pjsip.org", 80, "/"},
318
319 /* '@' character in username and path */
320 {"http://[email protected]/@", PJ_SUCCESS, "user", "", "pjsip.org", 80, "/@"},
321
322 /* '@' character in path */
323 {"http://pjsip.org/@", PJ_SUCCESS, "", "", "pjsip.org", 80, "/@"},
324
325 /* '@' character in path */
326 {"http://pjsip.org/one@", PJ_SUCCESS, "", "", "pjsip.org", 80, "/one@"},
327
328 /* Invalid URL */
329 {"http://:", PJ_EINVAL, "", "", "", 0, ""},
330
331 /* Invalid URL */
332 {"http://@", PJ_EINVAL, "", "", "", 0, ""},
333
334 /* Invalid URL */
335 {"http", PJ_EINVAL, "", "", "", 0, ""},
336
337 /* Invalid URL */
338 {"http:/", PJ_EINVAL, "", "", "", 0, ""},
339
340 /* Invalid URL */
341 {"http://", PJ_EINVAL, "", "", "", 0, ""},
342
343 /* Invalid URL */
344 {"http:///", PJ_EINVAL, "", "", "", 0, ""},
345
346 /* Invalid URL */
347 {"http://@/", PJ_EINVAL, "", "", "", 0, ""},
348
349 /* Invalid URL */
350 {"http:///@", PJ_EINVAL, "", "", "", 0, ""},
351
352 /* Invalid URL */
353 {"http://:::", PJ_EINVAL, "", "", "", 0, ""},
354 };
355 unsigned i;
356
357 for (i=0; i<PJ_ARRAY_SIZE(test_data); ++i) {
358 struct test_data *ptd;
359 pj_http_url hurl;
360 pj_status_t status;
361
362 ptd = &test_data[i];
363
364 PJ_LOG(3, (THIS_FILE, ".. %s", ptd->url));
365 status = parse_url(ptd->url, &hurl);
366
367 if (status != ptd->result) {
368 PJ_LOG(3,(THIS_FILE, "%d", status));
369 return -11;
370 }
371 if (status != PJ_SUCCESS)
372 continue;
373 if (pj_strcmp2(&hurl.username, ptd->username))
374 return -12;
375 if (pj_strcmp2(&hurl.passwd, ptd->passwd))
376 return -13;
377 if (pj_strcmp2(&hurl.host, ptd->host))
378 return -14;
379 if (hurl.port != ptd->port)
380 return -15;
381 if (pj_strcmp2(&hurl.path, ptd->path))
382 return -16;
383 }
384
385 return 0;
386 }
387
388 /*
389 * GET request scenario 1: using on_response() and on_data_read()
390 * Server replies with content-length. Application cancels the
391 * request upon receiving the response, then start it again.
392 */
393 int http_client_test1()
394 {
395 pj_str_t url;
396 pj_http_req_callback hcb;
397 pj_http_req_param param;
398 char urlbuf[80];
399
400 pj_bzero(&hcb, sizeof(hcb));
401 hcb.on_complete = &on_complete;
402 hcb.on_data_read = &on_data_read;
403 hcb.on_response = &on_response;
404 pj_http_req_param_default(&param);
405
406 /* Create pool, timer, and ioqueue */
407 pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
408 if (pj_timer_heap_create(pool, 16, &timer_heap))
409 return -31;
410 if (pj_ioqueue_create(pool, 16, &ioqueue))
411 return -32;
412
413 #ifdef USE_LOCAL_SERVER
414
415 thread_quit = PJ_FALSE;
416 g_server.action = ACTION_REPLY;
417 g_server.send_content_length = PJ_TRUE;
418 g_server.data_size = 2970;
419 g_server.buf_size = 1024;
420
421 sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
422 &g_server.sock);
423 if (sstatus != PJ_SUCCESS)
424 return -41;
425
426 pj_sockaddr_in_init(&addr, NULL, 0);
427
428 sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
429 if (sstatus != PJ_SUCCESS)
430 return -43;
431
432 {
433 pj_sockaddr_in addr2;
434 int addr_len = sizeof(addr2);
435 sstatus = pj_sock_getsockname(g_server.sock, &addr2, &addr_len);
436 if (sstatus != PJ_SUCCESS)
437 return -44;
438 g_server.port = pj_sockaddr_in_get_port(&addr2);
439 pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
440 "http://127.0.0.1:%d/about-us/",
441 g_server.port);
442 url = pj_str(urlbuf);
443 }
444
445 sstatus = pj_sock_listen(g_server.sock, 8);
446 if (sstatus != PJ_SUCCESS)
447 return -45;
448
449 sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
450 0, 0, &g_server.thread);
451 if (sstatus != PJ_SUCCESS)
452 return -47;
453
454 #else
455 pj_cstr(&url, "http://www.teluu.com/about-us/");
456 #endif
457
458 if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
459 &param, &hcb, &http_req))
460 return -33;
461
462 test_cancel = PJ_TRUE;
463 if (pj_http_req_start(http_req))
464 return -35;
465
466 while (pj_http_req_is_running(http_req)) {
467 pj_time_val delay = {0, 50};
468 pj_ioqueue_poll(ioqueue, &delay);
469 pj_timer_heap_poll(timer_heap, NULL);
470 }
471
472 if (pj_http_req_start(http_req))
473 return -37;
474
475 while (pj_http_req_is_running(http_req)) {
476 pj_time_val delay = {0, 50};
477 pj_ioqueue_poll(ioqueue, &delay);
478 pj_timer_heap_poll(timer_heap, NULL);
479 }
480
481 #ifdef USE_LOCAL_SERVER
482 thread_quit = PJ_TRUE;
483 pj_thread_join(g_server.thread);
484 pj_sock_close(g_server.sock);
485 #endif
486
487 pj_http_req_destroy(http_req);
488 pj_ioqueue_destroy(ioqueue);
489 pj_timer_heap_destroy(timer_heap);
490 pj_pool_release(pool);
491
492 return PJ_SUCCESS;
493 }
494
495 /*
496 * GET request scenario 2: using on_complete() to get the
497 * complete data. Server does not reply with content-length.
498 * Request timed out, application sets a longer timeout, then
499 * then restart the request.
500 */
501 int http_client_test2()
502 {
503 pj_str_t url;
504 pj_http_req_callback hcb;
505 pj_http_req_param param;
506 pj_time_val timeout;
507 char urlbuf[80];
508
509 pj_bzero(&hcb, sizeof(hcb));
510 hcb.on_complete = &on_complete;
511 hcb.on_response = &on_response;
512 pj_http_req_param_default(&param);
513
514 /* Create pool, timer, and ioqueue */
515 pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
516 if (pj_timer_heap_create(pool, 16, &timer_heap))
517 return -41;
518 if (pj_ioqueue_create(pool, 16, &ioqueue))
519 return -42;
520
521 #ifdef USE_LOCAL_SERVER
522
523 pj_cstr(&url, "http://127.0.0.1:380");
524 param.timeout.sec = 0;
525 param.timeout.msec = 2000;
526
527 thread_quit = PJ_FALSE;
528 g_server.action = ACTION_IGNORE;
529 g_server.send_content_length = PJ_FALSE;
530 g_server.data_size = 4173;
531 g_server.buf_size = 1024;
532
533 sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
534 &g_server.sock);
535 if (sstatus != PJ_SUCCESS)
536 return -41;
537
538 pj_sockaddr_in_init(&addr, NULL, 0);
539
540 sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
541 if (sstatus != PJ_SUCCESS)
542 return -43;
543
544 {
545 pj_sockaddr_in addr2;
546 int addr_len = sizeof(addr2);
547 sstatus = pj_sock_getsockname(g_server.sock, &addr2, &addr_len);
548 if (sstatus != PJ_SUCCESS)
549 return -44;
550 g_server.port = pj_sockaddr_in_get_port(&addr2);
551 pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
552 "http://127.0.0.1:%d",
553 g_server.port);
554 url = pj_str(urlbuf);
555 }
556
557 sstatus = pj_sock_listen(g_server.sock, 8);
558 if (sstatus != PJ_SUCCESS)
559 return -45;
560
561 sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
562 0, 0, &g_server.thread);
563 if (sstatus != PJ_SUCCESS)
564 return -47;
565
566 #else
567 pj_cstr(&url, "http://www.google.com.sg");
568 param.timeout.sec = 0;
569 param.timeout.msec = 50;
570 #endif
571
572 pj_http_headers_add_elmt2(&param.headers, (char*)"Accept",
573 (char*)"image/gif, image/x-xbitmap, image/jpeg, "
574 "image/pjpeg, application/x-ms-application,"
575 " application/vnd.ms-xpsdocument, "
576 "application/xaml+xml, "
577 "application/x-ms-xbap, "
578 "application/x-shockwave-flash, "
579 "application/vnd.ms-excel, "
580 "application/vnd.ms-powerpoint, "
581 "application/msword, */*");
582 pj_http_headers_add_elmt2(&param.headers, (char*)"Accept-Language",
583 (char*)"en-sg");
584 pj_http_headers_add_elmt2(&param.headers, (char*)"User-Agent",
585 (char*)"Mozilla/4.0 (compatible; MSIE 7.0; "
586 "Windows NT 6.0; SLCC1; "
587 ".NET CLR 2.0.50727; "
588 ".NET CLR 3.0.04506)");
589 if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
590 &param, &hcb, &http_req))
591 return -43;
592
593 if (pj_http_req_start(http_req))
594 return -45;
595
596 while (pj_http_req_is_running(http_req)) {
597 pj_time_val delay = {0, 50};
598 pj_ioqueue_poll(ioqueue, &delay);
599 pj_timer_heap_poll(timer_heap, NULL);
600 }
601
602 #ifdef USE_LOCAL_SERVER
603 g_server.action = ACTION_REPLY;
604 #endif
605
606 timeout.sec = 0; timeout.msec = 10000;
607 pj_http_req_set_timeout(http_req, &timeout);
608 if (pj_http_req_start(http_req))
609 return -47;
610
611 while (pj_http_req_is_running(http_req)) {
612 pj_time_val delay = {0, 50};
613 pj_ioqueue_poll(ioqueue, &delay);
614 pj_timer_heap_poll(timer_heap, NULL);
615 }
616
617 #ifdef USE_LOCAL_SERVER
618 thread_quit = PJ_TRUE;
619 pj_thread_join(g_server.thread);
620 pj_sock_close(g_server.sock);
621 #endif
622
623 pj_http_req_destroy(http_req);
624 pj_ioqueue_destroy(ioqueue);
625 pj_timer_heap_destroy(timer_heap);
626 pj_pool_release(pool);
627
628 return PJ_SUCCESS;
629 }
630
631 /*
632 * PUT request scenario 1: sending the whole data at once
633 */
634 int http_client_test_put1()
635 {
636 pj_str_t url;
637 pj_http_req_callback hcb;
638 pj_http_req_param param;
639 char *data;
640 int length = 3875;
641 char urlbuf[80];
642
643 pj_bzero(&hcb, sizeof(hcb));
644 hcb.on_complete = &on_complete;
645 hcb.on_data_read = &on_data_read;
646 hcb.on_response = &on_response;
647
648 /* Create pool, timer, and ioqueue */
649 pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
650 if (pj_timer_heap_create(pool, 16, &timer_heap))
651 return -51;
652 if (pj_ioqueue_create(pool, 16, &ioqueue))
653 return -52;
654
655 #ifdef USE_LOCAL_SERVER
656 thread_quit = PJ_FALSE;
657 g_server.action = ACTION_REPLY;
658 g_server.send_content_length = PJ_TRUE;
659 g_server.data_size = 0;
660 g_server.buf_size = 4096;
661
662 sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
663 &g_server.sock);
664 if (sstatus != PJ_SUCCESS)
665 return -41;
666
667 pj_sockaddr_in_init(&addr, NULL, 0);
668
669 sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
670 if (sstatus != PJ_SUCCESS)
671 return -43;
672
673 {
674 pj_sockaddr_in addr2;
675 int addr_len = sizeof(addr2);
676 sstatus = pj_sock_getsockname(g_server.sock, &addr2, &addr_len);
677 if (sstatus != PJ_SUCCESS)
678 return -44;
679 g_server.port = pj_sockaddr_in_get_port(&addr2);
680 pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
681 "http://127.0.0.1:%d/test/test.txt",
682 g_server.port);
683 url = pj_str(urlbuf);
684 }
685
686 sstatus = pj_sock_listen(g_server.sock, 8);
687 if (sstatus != PJ_SUCCESS)
688 return -45;
689
690 sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
691 0, 0, &g_server.thread);
692 if (sstatus != PJ_SUCCESS)
693 return -47;
694
695 #else
696 pj_cstr(&url, "http://127.0.0.1:280/test/test.txt");
697
698 #endif
699
700 pj_http_req_param_default(&param);
701 pj_strset2(&param.method, (char*)"PUT");
702 data = (char*)pj_pool_alloc(pool, length);
703 pj_create_random_string(data, length);
704 pj_ansi_snprintf(data, length, "PUT test\n");
705 param.reqdata.data = data;
706 param.reqdata.size = length;
707 if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
708 &param, &hcb, &http_req))
709 return -53;
710
711 if (pj_http_req_start(http_req))
712 return -55;
713
714 while (pj_http_req_is_running(http_req)) {
715 pj_time_val delay = {0, 50};
716 pj_ioqueue_poll(ioqueue, &delay);
717 pj_timer_heap_poll(timer_heap, NULL);
718 }
719
720 #ifdef USE_LOCAL_SERVER
721 thread_quit = PJ_TRUE;
722 pj_thread_join(g_server.thread);
723 pj_sock_close(g_server.sock);
724 #endif
725
726 pj_http_req_destroy(http_req);
727 pj_ioqueue_destroy(ioqueue);
728 pj_timer_heap_destroy(timer_heap);
729 pj_pool_release(pool);
730
731 return PJ_SUCCESS;
732 }
733
734 /*
735 * PUT request scenario 2: using on_send_data() callback to
736 * sending the data in chunks
737 */
738 int http_client_test_put2()
739 {
740 pj_str_t url;
741 pj_http_req_callback hcb;
742 pj_http_req_param param;
743 char urlbuf[80];
744
745 pj_bzero(&hcb, sizeof(hcb));
746 hcb.on_complete = &on_complete;
747 hcb.on_send_data = &on_send_data;
748 hcb.on_data_read = &on_data_read;
749 hcb.on_response = &on_response;
750
751 /* Create pool, timer, and ioqueue */
752 pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
753 if (pj_timer_heap_create(pool, 16, &timer_heap))
754 return -51;
755 if (pj_ioqueue_create(pool, 16, &ioqueue))
756 return -52;
757
758 #ifdef USE_LOCAL_SERVER
759 thread_quit = PJ_FALSE;
760 g_server.action = ACTION_REPLY;
761 g_server.send_content_length = PJ_TRUE;
762 g_server.data_size = 0;
763 g_server.buf_size = 16384;
764
765 sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
766 &g_server.sock);
767 if (sstatus != PJ_SUCCESS)
768 return -41;
769
770 pj_sockaddr_in_init(&addr, NULL, 0);
771
772 sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
773 if (sstatus != PJ_SUCCESS)
774 return -43;
775
776 {
777 pj_sockaddr_in addr2;
778 int addr_len = sizeof(addr2);
779 sstatus = pj_sock_getsockname(g_server.sock, &addr2, &addr_len);
780 if (sstatus != PJ_SUCCESS)
781 return -44;
782 g_server.port = pj_sockaddr_in_get_port(&addr2);
783 pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
784 "http://127.0.0.1:%d/test/test2.txt",
785 g_server.port);
786 url = pj_str(urlbuf);
787 }
788
789 sstatus = pj_sock_listen(g_server.sock, 8);
790 if (sstatus != PJ_SUCCESS)
791 return -45;
792
793 sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
794 0, 0, &g_server.thread);
795 if (sstatus != PJ_SUCCESS)
796 return -47;
797
798 #else
799 pj_cstr(&url, "http://127.0.0.1:280/test/test2.txt");
800
801 #endif
802
803 pj_http_req_param_default(&param);
804 pj_strset2(&param.method, (char*)"PUT");
805 total_size = 15383;
806 send_size = 0;
807 param.reqdata.total_size = total_size;
808 if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
809 &param, &hcb, &http_req))
810 return -53;
811
812 if (pj_http_req_start(http_req))
813 return -55;
814
815 while (pj_http_req_is_running(http_req)) {
816 pj_time_val delay = {0, 50};
817 pj_ioqueue_poll(ioqueue, &delay);
818 pj_timer_heap_poll(timer_heap, NULL);
819 }
820
821 #ifdef USE_LOCAL_SERVER
822 thread_quit = PJ_TRUE;
823 pj_thread_join(g_server.thread);
824 pj_sock_close(g_server.sock);
825 #endif
826
827 pj_http_req_destroy(http_req);
828 pj_ioqueue_destroy(ioqueue);
829 pj_timer_heap_destroy(timer_heap);
830 pj_pool_release(pool);
831
832 return PJ_SUCCESS;
833 }
834
835 int http_client_test_delete()
836 {
837 pj_str_t url;
838 pj_http_req_callback hcb;
839 pj_http_req_param param;
840 char urlbuf[80];
841
842 pj_bzero(&hcb, sizeof(hcb));
843 hcb.on_complete = &on_complete;
844 hcb.on_response = &on_response;
845
846 /* Create pool, timer, and ioqueue */
847 pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
848 if (pj_timer_heap_create(pool, 16, &timer_heap))
849 return -61;
850 if (pj_ioqueue_create(pool, 16, &ioqueue))
851 return -62;
852
853 #ifdef USE_LOCAL_SERVER
854 thread_quit = PJ_FALSE;
855 g_server.action = ACTION_REPLY;
856 g_server.send_content_length = PJ_TRUE;
857 g_server.data_size = 0;
858 g_server.buf_size = 1024;
859
860 sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
861 &g_server.sock);
862 if (sstatus != PJ_SUCCESS)
863 return -41;
864
865 pj_sockaddr_in_init(&addr, NULL, 0);
866
867 sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
868 if (sstatus != PJ_SUCCESS)
869 return -43;
870
871 {
872 pj_sockaddr_in addr2;
873 int addr_len = sizeof(addr2);
874 sstatus = pj_sock_getsockname(g_server.sock, &addr2, &addr_len);
875 if (sstatus != PJ_SUCCESS)
876 return -44;
877 g_server.port = pj_sockaddr_in_get_port(&addr2);
878 pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
879 "http://127.0.0.1:%d/test/test2.txt",
880 g_server.port);
881 url = pj_str(urlbuf);
882 }
883
884 sstatus = pj_sock_listen(g_server.sock, 8);
885 if (sstatus != PJ_SUCCESS)
886 return -45;
887
888 sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
889 0, 0, &g_server.thread);
890 if (sstatus != PJ_SUCCESS)
891 return -47;
892
893 #else
894 pj_cstr(&url, "http://127.0.0.1:280/test/test2.txt");
895 #endif
896
897 pj_http_req_param_default(&param);
898 pj_strset2(&param.method, (char*)"DELETE");
899 if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
900 &param, &hcb, &http_req))
901 return -63;
902
903 if (pj_http_req_start(http_req))
904 return -65;
905
906 while (pj_http_req_is_running(http_req)) {
907 pj_time_val delay = {0, 50};
908 pj_ioqueue_poll(ioqueue, &delay);
909 pj_timer_heap_poll(timer_heap, NULL);
910 }
911
912 #ifdef USE_LOCAL_SERVER
913 thread_quit = PJ_TRUE;
914 pj_thread_join(g_server.thread);
915 pj_sock_close(g_server.sock);
916 #endif
917
918 pj_http_req_destroy(http_req);
919 pj_ioqueue_destroy(ioqueue);
920 pj_timer_heap_destroy(timer_heap);
921 pj_pool_release(pool);
922
923 return PJ_SUCCESS;
924 }
925
926 int http_client_test()
927 {
928 int rc;
929
930 PJ_LOG(3, (THIS_FILE, "..Testing URL parsing"));
931 rc = parse_url_test();
932 if (rc)
933 return rc;
934
935 PJ_LOG(3, (THIS_FILE, "..Testing GET request scenario 1"));
936 rc = http_client_test1();
937 if (rc)
938 return rc;
939
940 PJ_LOG(3, (THIS_FILE, "..Testing GET request scenario 2"));
941 rc = http_client_test2();
942 if (rc)
943 return rc;
944
945 PJ_LOG(3, (THIS_FILE, "..Testing PUT request scenario 1"));
946 rc = http_client_test_put1();
947 if (rc)
948 return rc;
949
950 PJ_LOG(3, (THIS_FILE, "..Testing PUT request scenario 2"));
951 rc = http_client_test_put2();
952 if (rc)
953 return rc;
954
955 PJ_LOG(3, (THIS_FILE, "..Testing DELETE request"));
956 rc = http_client_test_delete();
957 if (rc)
958 return rc;
959
960 return PJ_SUCCESS;
961 }
962
963 #else
964 /* To prevent warning about "translation unit is empty"
965 * when this test is disabled.
966 */
967 int dummy_http_client_test;
968 #endif /* INCLUDE_HTTP_CLIENT_TEST */