1
/* $Id: http_client.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#if INCLUDE_HTTP_CLIENT_TEST
24
#define THIS_FILE "test_http"
26
#define STR_PREC(s) (int)s.slen, s.ptr
27
#define USE_LOCAL_SERVER
30
#include <pjlib-util.h>
32
#define ACTION_REPLY 0
33
#define ACTION_IGNORE -1
35
static struct server_t
42
* 0: reply with the response in resp.
43
* -1: ignore query (to simulate timeout).
44
* other: reply with that error
47
pj_bool_t send_content_length;
52
static pj_bool_t thread_quit;
53
static pj_timer_heap_t *timer_heap;
54
static pj_ioqueue_t *ioqueue;
55
static pj_pool_t *pool;
56
static pj_http_req *http_req;
57
static pj_bool_t test_cancel = PJ_FALSE;
58
static pj_size_t total_size;
59
static pj_size_t send_size = 0;
60
static pj_status_t sstatus;
61
static pj_sockaddr_in addr;
62
static int counter = 0;
64
static int server_thread(void *p)
66
struct server_t *srv = (struct server_t*)p;
67
char *pkt = (char*)pj_pool_alloc(pool, srv->buf_size);
68
pj_sock_t newsock = PJ_INVALID_SOCKET;
70
while (!thread_quit) {
74
pj_time_val timeout = {0, 500};
76
while (!thread_quit) {
78
PJ_FD_SET(srv->sock, &rset);
79
rc = pj_sock_select(srv->sock+1, &rset, NULL, NULL, &timeout);
84
rc = pj_sock_accept(srv->sock, &newsock, NULL, NULL);
85
if (rc == PJ_SUCCESS) {
93
while (!thread_quit) {
95
PJ_FD_SET(newsock, &rset);
96
rc = pj_sock_select(newsock+1, &rset, NULL, NULL, &timeout);
98
PJ_LOG(3,("http test", "client timeout"));
102
pkt_len = srv->buf_size;
103
rc = pj_sock_recv(newsock, pkt, &pkt_len, 0);
104
if (rc == PJ_SUCCESS) {
112
/* Simulate network RTT */
115
if (srv->action == ACTION_IGNORE) {
117
} else if (srv->action == ACTION_REPLY) {
118
unsigned send_size = 0, ctr = 0;
119
pj_ansi_sprintf(pkt, "HTTP/1.0 200 OK\r\n");
120
if (srv->send_content_length) {
121
pj_ansi_sprintf(pkt + pj_ansi_strlen(pkt),
122
"Content-Length: %d\r\n",
125
pj_ansi_sprintf(pkt + pj_ansi_strlen(pkt), "\r\n");
126
pkt_len = pj_ansi_strlen(pkt);
127
rc = pj_sock_send(newsock, pkt, &pkt_len, 0);
128
if (rc != PJ_SUCCESS) {
129
pj_sock_close(newsock);
132
while (send_size < srv->data_size) {
133
pkt_len = srv->data_size - send_size;
134
if (pkt_len > (signed)srv->buf_size)
135
pkt_len = srv->buf_size;
136
send_size += pkt_len;
137
pj_create_random_string(pkt, pkt_len);
138
pj_ansi_sprintf(pkt, "\nPacket: %d", ++ctr);
139
pkt[pj_ansi_strlen(pkt)] = '\n';
140
rc = pj_sock_send(newsock, pkt, &pkt_len, 0);
141
if (rc != PJ_SUCCESS)
144
pj_sock_close(newsock);
151
static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size)
156
PJ_LOG(5, (THIS_FILE, "\nData received: %d bytes", size));
159
printf("%.*s\n", (int)size, (char *)data);
164
static void on_send_data(pj_http_req *hreq,
165
void **data, pj_size_t *size)
168
pj_size_t sendsz = 8397;
172
if (send_size + sendsz > total_size) {
173
sendsz = total_size - send_size;
177
sdata = (char*)pj_pool_alloc(pool, sendsz);
178
pj_create_random_string(sdata, sendsz);
179
pj_ansi_sprintf(sdata, "\nSegment #%d\n", ++counter);
183
PJ_LOG(5, (THIS_FILE, "\nSending data progress: %d out of %d bytes",
184
send_size, total_size));
188
static void on_complete(pj_http_req *hreq, pj_status_t status,
189
const pj_http_resp *resp)
193
if (status == PJ_ECANCELLED) {
194
PJ_LOG(5, (THIS_FILE, "Request cancelled"));
196
} else if (status == PJ_ETIMEDOUT) {
197
PJ_LOG(5, (THIS_FILE, "Request timed out!"));
199
} else if (status != PJ_SUCCESS) {
200
PJ_LOG(3, (THIS_FILE, "Error %d", status));
203
PJ_LOG(5, (THIS_FILE, "\nData completed: %d bytes", resp->size));
204
if (resp->size > 0 && resp->data) {
206
printf("%.*s\n", (int)resp->size, (char *)resp->data);
211
static void on_response(pj_http_req *hreq, const pj_http_resp *resp)
220
printf("%.*s, %d, %.*s\n", STR_PREC(resp->version),
221
resp->status_code, STR_PREC(resp->reason));
222
for (i = 0; i < resp->headers.count; i++) {
223
printf("%.*s : %.*s\n",
224
STR_PREC(resp->headers.header[i].name),
225
STR_PREC(resp->headers.header[i].value));
230
/* Need to delay closing the client socket here, otherwise the
231
* server will get SIGPIPE when sending response.
233
pj_thread_sleep(100);
234
pj_http_req_cancel(hreq, PJ_TRUE);
235
test_cancel = PJ_FALSE;
240
pj_status_t parse_url(const char *url, pj_http_url *hurl)
246
status = pj_http_req_parse_url(&surl, hurl);
249
printf("URL: %s\nProtocol: %.*s\nHost: %.*s\nPort: %d\nPath: %.*s\n\n",
250
url, STR_PREC(hurl->protocol), STR_PREC(hurl->host),
251
hurl->port, STR_PREC(hurl->path));
258
static int parse_url_test()
264
const char *username;
271
/* Simple URL without '/' in the end */
272
{"http://www.pjsip.org", PJ_SUCCESS, "", "", "www.pjsip.org", 80, "/"},
274
/* Simple URL with port number but without '/' in the end */
275
{"http://pjsip.org:8080", PJ_SUCCESS, "", "", "pjsip.org", 8080, "/"},
278
{"http://127.0.0.1:280/Joomla/index.php?option=com_content&task=view&id=5&Itemid=6",
279
PJ_SUCCESS, "", "", "127.0.0.1", 280,
280
"/Joomla/index.php?option=com_content&task=view&id=5&Itemid=6"},
282
/* URL with port and path */
283
{"http://pjsip.org:81/about-us/", PJ_SUCCESS, "", "", "pjsip.org", 81, "/about-us/"},
285
/* unsupported protocol */
286
{"ftp://www.pjsip.org", PJ_ENOTSUP, "", "", "", 80, ""},
289
{"http:/pjsip.org/about-us/", PJLIB_UTIL_EHTTPINURL, "", "", "", 80, ""},
291
/* invalid port number */
292
{"http://pjsip.org:xyz/", PJLIB_UTIL_EHTTPINPORT, "", "", "", 80, ""},
294
/* with username and password */
295
{"http://user:pass@pjsip.org", PJ_SUCCESS, "user", "pass", "pjsip.org", 80, "/"},
298
{"http://:pass@pjsip.org", PJ_SUCCESS, "", "pass", "pjsip.org", 80, "/"},
301
{"http://user:@pjsip.org", PJ_SUCCESS, "user", "", "pjsip.org", 80, "/"},
303
/* empty username and passwd*/
304
{"http://:@pjsip.org", PJ_SUCCESS, "", "", "pjsip.org", 80, "/"},
306
/* '@' character in username and path */
307
{"http://user@pjsip.org/@", PJ_SUCCESS, "user", "", "pjsip.org", 80, "/@"},
309
/* '@' character in path */
310
{"http://pjsip.org/@", PJ_SUCCESS, "", "", "pjsip.org", 80, "/@"},
312
/* '@' character in path */
313
{"http://pjsip.org/one@", PJ_SUCCESS, "", "", "pjsip.org", 80, "/one@"},
316
{"http://:", PJ_EINVAL, "", "", "", 0, ""},
319
{"http://@", PJ_EINVAL, "", "", "", 0, ""},
322
{"http", PJ_EINVAL, "", "", "", 0, ""},
325
{"http:/", PJ_EINVAL, "", "", "", 0, ""},
328
{"http://", PJ_EINVAL, "", "", "", 0, ""},
331
{"http:///", PJ_EINVAL, "", "", "", 0, ""},
334
{"http://@/", PJ_EINVAL, "", "", "", 0, ""},
337
{"http:///@", PJ_EINVAL, "", "", "", 0, ""},
340
{"http://:::", PJ_EINVAL, "", "", "", 0, ""},
344
for (i=0; i<PJ_ARRAY_SIZE(test_data); ++i) {
345
struct test_data *ptd;
351
PJ_LOG(3, (THIS_FILE, ".. %s", ptd->url));
352
status = parse_url(ptd->url, &hurl);
354
if (status != ptd->result) {
355
PJ_LOG(3,(THIS_FILE, "%d", status));
358
if (status != PJ_SUCCESS)
360
if (pj_strcmp2(&hurl.username, ptd->username))
362
if (pj_strcmp2(&hurl.passwd, ptd->passwd))
364
if (pj_strcmp2(&hurl.host, ptd->host))
366
if (hurl.port != ptd->port)
368
if (pj_strcmp2(&hurl.path, ptd->path))
376
* GET request scenario 1: using on_response() and on_data_read()
377
* Server replies with content-length. Application cancels the
378
* request upon receiving the response, then start it again.
380
int http_client_test1()
383
pj_http_req_callback hcb;
384
pj_http_req_param param;
387
pj_bzero(&hcb, sizeof(hcb));
388
hcb.on_complete = &on_complete;
389
hcb.on_data_read = &on_data_read;
390
hcb.on_response = &on_response;
391
pj_http_req_param_default(¶m);
393
/* Create pool, timer, and ioqueue */
394
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
395
if (pj_timer_heap_create(pool, 16, &timer_heap))
397
if (pj_ioqueue_create(pool, 16, &ioqueue))
400
#ifdef USE_LOCAL_SERVER
402
thread_quit = PJ_FALSE;
403
g_server.action = ACTION_REPLY;
404
g_server.send_content_length = PJ_TRUE;
405
g_server.data_size = 2970;
406
g_server.buf_size = 1024;
408
sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
410
if (sstatus != PJ_SUCCESS)
413
pj_sockaddr_in_init(&addr, NULL, 0);
415
sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
416
if (sstatus != PJ_SUCCESS)
421
int addr_len = sizeof(addr);
422
sstatus = pj_sock_getsockname(g_server.sock, &addr, &addr_len);
423
if (sstatus != PJ_SUCCESS)
425
g_server.port = pj_sockaddr_in_get_port(&addr);
426
pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
427
"http://127.0.0.1:%d/about-us/",
429
url = pj_str(urlbuf);
432
sstatus = pj_sock_listen(g_server.sock, 8);
433
if (sstatus != PJ_SUCCESS)
436
sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
437
0, 0, &g_server.thread);
438
if (sstatus != PJ_SUCCESS)
442
pj_cstr(&url, "http://www.teluu.com/about-us/");
445
if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
446
¶m, &hcb, &http_req))
449
test_cancel = PJ_TRUE;
450
if (pj_http_req_start(http_req))
453
while (pj_http_req_is_running(http_req)) {
454
pj_time_val delay = {0, 50};
455
pj_ioqueue_poll(ioqueue, &delay);
456
pj_timer_heap_poll(timer_heap, NULL);
459
if (pj_http_req_start(http_req))
462
while (pj_http_req_is_running(http_req)) {
463
pj_time_val delay = {0, 50};
464
pj_ioqueue_poll(ioqueue, &delay);
465
pj_timer_heap_poll(timer_heap, NULL);
468
#ifdef USE_LOCAL_SERVER
469
thread_quit = PJ_TRUE;
470
pj_thread_join(g_server.thread);
471
pj_sock_close(g_server.sock);
474
pj_http_req_destroy(http_req);
475
pj_ioqueue_destroy(ioqueue);
476
pj_timer_heap_destroy(timer_heap);
477
pj_pool_release(pool);
483
* GET request scenario 2: using on_complete() to get the
484
* complete data. Server does not reply with content-length.
485
* Request timed out, application sets a longer timeout, then
486
* then restart the request.
488
int http_client_test2()
491
pj_http_req_callback hcb;
492
pj_http_req_param param;
496
pj_bzero(&hcb, sizeof(hcb));
497
hcb.on_complete = &on_complete;
498
hcb.on_response = &on_response;
499
pj_http_req_param_default(¶m);
501
/* Create pool, timer, and ioqueue */
502
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
503
if (pj_timer_heap_create(pool, 16, &timer_heap))
505
if (pj_ioqueue_create(pool, 16, &ioqueue))
508
#ifdef USE_LOCAL_SERVER
510
pj_cstr(&url, "http://127.0.0.1:380");
511
param.timeout.sec = 0;
512
param.timeout.msec = 2000;
514
thread_quit = PJ_FALSE;
515
g_server.action = ACTION_IGNORE;
516
g_server.send_content_length = PJ_FALSE;
517
g_server.data_size = 4173;
518
g_server.buf_size = 1024;
520
sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
522
if (sstatus != PJ_SUCCESS)
525
pj_sockaddr_in_init(&addr, NULL, 0);
527
sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
528
if (sstatus != PJ_SUCCESS)
533
int addr_len = sizeof(addr);
534
sstatus = pj_sock_getsockname(g_server.sock, &addr, &addr_len);
535
if (sstatus != PJ_SUCCESS)
537
g_server.port = pj_sockaddr_in_get_port(&addr);
538
pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
539
"http://127.0.0.1:%d",
541
url = pj_str(urlbuf);
544
sstatus = pj_sock_listen(g_server.sock, 8);
545
if (sstatus != PJ_SUCCESS)
548
sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
549
0, 0, &g_server.thread);
550
if (sstatus != PJ_SUCCESS)
554
pj_cstr(&url, "http://www.google.com.sg");
555
param.timeout.sec = 0;
556
param.timeout.msec = 50;
559
pj_http_headers_add_elmt2(¶m.headers, (char*)"Accept",
560
(char*)"image/gif, image/x-xbitmap, image/jpeg, "
561
"image/pjpeg, application/x-ms-application,"
562
" application/vnd.ms-xpsdocument, "
563
"application/xaml+xml, "
564
"application/x-ms-xbap, "
565
"application/x-shockwave-flash, "
566
"application/vnd.ms-excel, "
567
"application/vnd.ms-powerpoint, "
568
"application/msword, */*");
569
pj_http_headers_add_elmt2(¶m.headers, (char*)"Accept-Language",
571
pj_http_headers_add_elmt2(¶m.headers, (char*)"User-Agent",
572
(char*)"Mozilla/4.0 (compatible; MSIE 7.0; "
573
"Windows NT 6.0; SLCC1; "
574
".NET CLR 2.0.50727; "
575
".NET CLR 3.0.04506)");
576
if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
577
¶m, &hcb, &http_req))
580
if (pj_http_req_start(http_req))
583
while (pj_http_req_is_running(http_req)) {
584
pj_time_val delay = {0, 50};
585
pj_ioqueue_poll(ioqueue, &delay);
586
pj_timer_heap_poll(timer_heap, NULL);
589
#ifdef USE_LOCAL_SERVER
590
g_server.action = ACTION_REPLY;
593
timeout.sec = 0; timeout.msec = 10000;
594
pj_http_req_set_timeout(http_req, &timeout);
595
if (pj_http_req_start(http_req))
598
while (pj_http_req_is_running(http_req)) {
599
pj_time_val delay = {0, 50};
600
pj_ioqueue_poll(ioqueue, &delay);
601
pj_timer_heap_poll(timer_heap, NULL);
604
#ifdef USE_LOCAL_SERVER
605
thread_quit = PJ_TRUE;
606
pj_thread_join(g_server.thread);
607
pj_sock_close(g_server.sock);
610
pj_http_req_destroy(http_req);
611
pj_ioqueue_destroy(ioqueue);
612
pj_timer_heap_destroy(timer_heap);
613
pj_pool_release(pool);
619
* PUT request scenario 1: sending the whole data at once
621
int http_client_test_put1()
624
pj_http_req_callback hcb;
625
pj_http_req_param param;
630
pj_bzero(&hcb, sizeof(hcb));
631
hcb.on_complete = &on_complete;
632
hcb.on_data_read = &on_data_read;
633
hcb.on_response = &on_response;
635
/* Create pool, timer, and ioqueue */
636
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
637
if (pj_timer_heap_create(pool, 16, &timer_heap))
639
if (pj_ioqueue_create(pool, 16, &ioqueue))
642
#ifdef USE_LOCAL_SERVER
643
thread_quit = PJ_FALSE;
644
g_server.action = ACTION_REPLY;
645
g_server.send_content_length = PJ_TRUE;
646
g_server.data_size = 0;
647
g_server.buf_size = 4096;
649
sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
651
if (sstatus != PJ_SUCCESS)
654
pj_sockaddr_in_init(&addr, NULL, 0);
656
sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
657
if (sstatus != PJ_SUCCESS)
662
int addr_len = sizeof(addr);
663
sstatus = pj_sock_getsockname(g_server.sock, &addr, &addr_len);
664
if (sstatus != PJ_SUCCESS)
666
g_server.port = pj_sockaddr_in_get_port(&addr);
667
pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
668
"http://127.0.0.1:%d/test/test.txt",
670
url = pj_str(urlbuf);
673
sstatus = pj_sock_listen(g_server.sock, 8);
674
if (sstatus != PJ_SUCCESS)
677
sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
678
0, 0, &g_server.thread);
679
if (sstatus != PJ_SUCCESS)
683
pj_cstr(&url, "http://127.0.0.1:280/test/test.txt");
687
pj_http_req_param_default(¶m);
688
pj_strset2(¶m.method, (char*)"PUT");
689
data = (char*)pj_pool_alloc(pool, length);
690
pj_create_random_string(data, length);
691
pj_ansi_sprintf(data, "PUT test\n");
692
param.reqdata.data = data;
693
param.reqdata.size = length;
694
if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
695
¶m, &hcb, &http_req))
698
if (pj_http_req_start(http_req))
701
while (pj_http_req_is_running(http_req)) {
702
pj_time_val delay = {0, 50};
703
pj_ioqueue_poll(ioqueue, &delay);
704
pj_timer_heap_poll(timer_heap, NULL);
707
#ifdef USE_LOCAL_SERVER
708
thread_quit = PJ_TRUE;
709
pj_thread_join(g_server.thread);
710
pj_sock_close(g_server.sock);
713
pj_http_req_destroy(http_req);
714
pj_ioqueue_destroy(ioqueue);
715
pj_timer_heap_destroy(timer_heap);
716
pj_pool_release(pool);
722
* PUT request scenario 2: using on_send_data() callback to
723
* sending the data in chunks
725
int http_client_test_put2()
728
pj_http_req_callback hcb;
729
pj_http_req_param param;
732
pj_bzero(&hcb, sizeof(hcb));
733
hcb.on_complete = &on_complete;
734
hcb.on_send_data = &on_send_data;
735
hcb.on_data_read = &on_data_read;
736
hcb.on_response = &on_response;
738
/* Create pool, timer, and ioqueue */
739
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
740
if (pj_timer_heap_create(pool, 16, &timer_heap))
742
if (pj_ioqueue_create(pool, 16, &ioqueue))
745
#ifdef USE_LOCAL_SERVER
746
thread_quit = PJ_FALSE;
747
g_server.action = ACTION_REPLY;
748
g_server.send_content_length = PJ_TRUE;
749
g_server.data_size = 0;
750
g_server.buf_size = 16384;
752
sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
754
if (sstatus != PJ_SUCCESS)
757
pj_sockaddr_in_init(&addr, NULL, 0);
759
sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
760
if (sstatus != PJ_SUCCESS)
765
int addr_len = sizeof(addr);
766
sstatus = pj_sock_getsockname(g_server.sock, &addr, &addr_len);
767
if (sstatus != PJ_SUCCESS)
769
g_server.port = pj_sockaddr_in_get_port(&addr);
770
pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
771
"http://127.0.0.1:%d/test/test2.txt",
773
url = pj_str(urlbuf);
776
sstatus = pj_sock_listen(g_server.sock, 8);
777
if (sstatus != PJ_SUCCESS)
780
sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
781
0, 0, &g_server.thread);
782
if (sstatus != PJ_SUCCESS)
786
pj_cstr(&url, "http://127.0.0.1:280/test/test2.txt");
790
pj_http_req_param_default(¶m);
791
pj_strset2(¶m.method, (char*)"PUT");
794
param.reqdata.total_size = total_size;
795
if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
796
¶m, &hcb, &http_req))
799
if (pj_http_req_start(http_req))
802
while (pj_http_req_is_running(http_req)) {
803
pj_time_val delay = {0, 50};
804
pj_ioqueue_poll(ioqueue, &delay);
805
pj_timer_heap_poll(timer_heap, NULL);
808
#ifdef USE_LOCAL_SERVER
809
thread_quit = PJ_TRUE;
810
pj_thread_join(g_server.thread);
811
pj_sock_close(g_server.sock);
814
pj_http_req_destroy(http_req);
815
pj_ioqueue_destroy(ioqueue);
816
pj_timer_heap_destroy(timer_heap);
817
pj_pool_release(pool);
822
int http_client_test_delete()
825
pj_http_req_callback hcb;
826
pj_http_req_param param;
829
pj_bzero(&hcb, sizeof(hcb));
830
hcb.on_complete = &on_complete;
831
hcb.on_response = &on_response;
833
/* Create pool, timer, and ioqueue */
834
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
835
if (pj_timer_heap_create(pool, 16, &timer_heap))
837
if (pj_ioqueue_create(pool, 16, &ioqueue))
840
#ifdef USE_LOCAL_SERVER
841
thread_quit = PJ_FALSE;
842
g_server.action = ACTION_REPLY;
843
g_server.send_content_length = PJ_TRUE;
844
g_server.data_size = 0;
845
g_server.buf_size = 1024;
847
sstatus = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0,
849
if (sstatus != PJ_SUCCESS)
852
pj_sockaddr_in_init(&addr, NULL, 0);
854
sstatus = pj_sock_bind(g_server.sock, &addr, sizeof(addr));
855
if (sstatus != PJ_SUCCESS)
860
int addr_len = sizeof(addr);
861
sstatus = pj_sock_getsockname(g_server.sock, &addr, &addr_len);
862
if (sstatus != PJ_SUCCESS)
864
g_server.port = pj_sockaddr_in_get_port(&addr);
865
pj_ansi_snprintf(urlbuf, sizeof(urlbuf),
866
"http://127.0.0.1:%d/test/test2.txt",
868
url = pj_str(urlbuf);
871
sstatus = pj_sock_listen(g_server.sock, 8);
872
if (sstatus != PJ_SUCCESS)
875
sstatus = pj_thread_create(pool, NULL, &server_thread, &g_server,
876
0, 0, &g_server.thread);
877
if (sstatus != PJ_SUCCESS)
881
pj_cstr(&url, "http://127.0.0.1:280/test/test2.txt");
884
pj_http_req_param_default(¶m);
885
pj_strset2(¶m.method, (char*)"DELETE");
886
if (pj_http_req_create(pool, &url, timer_heap, ioqueue,
887
¶m, &hcb, &http_req))
890
if (pj_http_req_start(http_req))
893
while (pj_http_req_is_running(http_req)) {
894
pj_time_val delay = {0, 50};
895
pj_ioqueue_poll(ioqueue, &delay);
896
pj_timer_heap_poll(timer_heap, NULL);
899
#ifdef USE_LOCAL_SERVER
900
thread_quit = PJ_TRUE;
901
pj_thread_join(g_server.thread);
902
pj_sock_close(g_server.sock);
905
pj_http_req_destroy(http_req);
906
pj_ioqueue_destroy(ioqueue);
907
pj_timer_heap_destroy(timer_heap);
908
pj_pool_release(pool);
913
int http_client_test()
917
PJ_LOG(3, (THIS_FILE, "..Testing URL parsing"));
918
rc = parse_url_test();
922
PJ_LOG(3, (THIS_FILE, "..Testing GET request scenario 1"));
923
rc = http_client_test1();
927
PJ_LOG(3, (THIS_FILE, "..Testing GET request scenario 2"));
928
rc = http_client_test2();
932
PJ_LOG(3, (THIS_FILE, "..Testing PUT request scenario 1"));
933
rc = http_client_test_put1();
937
PJ_LOG(3, (THIS_FILE, "..Testing PUT request scenario 2"));
938
rc = http_client_test_put2();
942
PJ_LOG(3, (THIS_FILE, "..Testing DELETE request"));
943
rc = http_client_test_delete();
951
/* To prevent warning about "translation unit is empty"
952
* when this test is disabled.
954
int dummy_http_client_test;
955
#endif /* INCLUDE_HTTP_CLIENT_TEST */