1
/* $Id: httpdemo.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
21
* \page page_httpdemo_c Samples: HTTP Client demo
23
* This file is pjsip-apps/src/samples/httpdemo.c
25
* \includelineno httpdemo.c
29
#include <pjlib-util.h>
30
#include <pjlib-util/http_client.h>
34
#include <pjsip_simple.h>
36
static pj_timer_heap_t *timer_heap;
37
static pj_ioqueue_t *ioqueue;
38
static pj_pool_t *pool;
39
static pj_http_req *http_req;
40
static pj_pool_factory *mem;
41
static FILE *f = NULL;
44
#define THIS_FILE "http_demo"
46
static void on_response(pj_http_req *http_req, const pj_http_resp *resp)
50
PJ_UNUSED_ARG(http_req);
51
PJ_LOG(3,(THIS_FILE, "%.*s %d %.*s", (int)resp->version.slen, resp->version.ptr,
53
(int)resp->reason.slen, resp->reason.ptr));
55
for (i=0; i<resp->headers.count; ++i) {
56
const pj_http_header_elmt *h = &resp->headers.header[i];
58
if (!pj_stricmp2(&h->name, "Content-Length") ||
59
!pj_stricmp2(&h->name, "Content-Type"))
61
PJ_LOG(3,(THIS_FILE, "%.*s: %.*s",
62
(int)h->name.slen, h->name.ptr,
63
(int)h->value.slen, h->value.ptr));
68
static void on_send_data(pj_http_req *http_req, void **data, pj_size_t *size)
70
PJ_UNUSED_ARG(http_req);
75
static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size)
80
fwrite(data, 1, size, f);
83
PJ_LOG(3, (THIS_FILE, "Data received: %d bytes", size));
84
printf("%.*s\n", (int)size, (char *)data);
89
static void on_complete(pj_http_req *hreq, pj_status_t status,
90
const pj_http_resp *resp)
94
if (status != PJ_SUCCESS) {
95
PJ_PERROR(1, (THIS_FILE, status, "HTTP request completed with error"));
98
PJ_LOG(3, (THIS_FILE, "Data completed: %d bytes", resp->size));
99
if (resp->size > 0 && resp->data) {
101
printf("%.*s\n", (int)resp->size, (char *)resp->data);
106
pj_status_t getURL(const char *curl)
109
pj_http_req_callback hcb;
112
pj_bzero(&hcb, sizeof(hcb));
113
hcb.on_complete = &on_complete;
114
hcb.on_data_read = &on_data_read;
115
hcb.on_send_data = &on_send_data;
116
hcb.on_response = &on_response;
118
/* Create pool, timer, and ioqueue */
119
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
120
if (pj_timer_heap_create(pool, 16, &timer_heap))
122
if (pj_ioqueue_create(pool, 16, &ioqueue))
125
pj_strdup2(pool, &url, curl);
127
if ((status = pj_http_req_create(pool, &url, timer_heap, ioqueue,
128
NULL, &hcb, &http_req)) != PJ_SUCCESS)
131
if ((status = pj_http_req_start(http_req)) != PJ_SUCCESS)
134
while (pj_http_req_is_running(http_req)) {
135
pj_time_val delay = {0, 50};
136
pj_ioqueue_poll(ioqueue, &delay);
137
pj_timer_heap_poll(timer_heap, NULL);
140
pj_http_req_destroy(http_req);
141
pj_ioqueue_destroy(ioqueue);
142
pj_timer_heap_destroy(timer_heap);
143
pj_pool_release(pool);
150
int main(int argc, char *argv[])
155
if (argc < 2 || argc > 3) {
156
puts("Usage: httpdemo URL [output-filename]");
163
pj_caching_pool_init(&cp, NULL, 0);
168
f = fopen(argv[2], "wb");
172
status = getURL(argv[1]);
173
if (status != PJ_SUCCESS) {
174
PJ_PERROR(1, (THIS_FILE, status, "Error"));
180
pj_caching_pool_destroy(&cp);