From b813b2c567747c86ebe2c3de24ed6af26119ccf2 Mon Sep 17 00:00:00 2001 From: Liong Sauw Ming Date: Thu, 4 Feb 2010 18:29:16 +0000 Subject: Implemented ticket #1018: Simple HTTP client implementation * pjlib-util: * implement http_client * pjlib-util-test: * unit test for http_client * pjsip-apps/samples: * sample http client implementation * build: * added http_client support on VS6, VS2005, MMP, and Makefile git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3087 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip-apps/src/samples/httpdemo.c | 151 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 pjsip-apps/src/samples/httpdemo.c (limited to 'pjsip-apps/src/samples/httpdemo.c') diff --git a/pjsip-apps/src/samples/httpdemo.c b/pjsip-apps/src/samples/httpdemo.c new file mode 100644 index 00000000..9dc3c0eb --- /dev/null +++ b/pjsip-apps/src/samples/httpdemo.c @@ -0,0 +1,151 @@ +/* $Id$ */ +/* + * Copyright (C) 2008-2010 Teluu Inc. (http://www.teluu.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * \page page_httpdemo_c Samples: HTTP Client demo + * + * This file is pjsip-apps/src/samples/httpdemo.c + * + * \includelineno httpdemo.c + */ + +#include +#include +#include +#include +#include +#include +#include + +static pj_timer_heap_t *timer_heap; +static pj_ioqueue_t *ioqueue; +static pj_pool_t *pool; +static pj_http_req *http_req; +static pj_pool_factory *mem; +static FILE *f = NULL; + +//#define VERBOSE +#define THIS_FILE "http_demo" + +static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size) +{ + PJ_UNUSED_ARG(hreq); + + if (size > 0) { + fwrite(data, 1, size, f); + fflush(f); +#ifdef VERBOSE + PJ_LOG(3, (THIS_FILE, "\nData received: %d bytes\n", size)); + printf("%.*s\n", (int)size, (char *)data); +#endif + } +} + +static void on_complete(pj_http_req *hreq, pj_status_t status, + const pj_http_resp *resp) +{ + PJ_UNUSED_ARG(hreq); + + if (status == PJ_ECANCELLED) { + PJ_LOG(3, (THIS_FILE, "Request cancelled\n")); + return; + } else if (status == PJ_ETIMEDOUT) { + PJ_LOG(3, (THIS_FILE, "Request timed out!\n")); + return; + } else if (status != PJ_SUCCESS && status != PJ_EPENDING) { + PJ_LOG(3, (THIS_FILE, "Error %d\n", status)); + return; + } + PJ_LOG(3, (THIS_FILE, "\nData completed: %d bytes\n", resp->size)); + if (resp->size > 0 && resp->data) { +#ifdef VERBOSE + printf("%.*s\n", (int)resp->size, (char *)resp->data); +#endif + } +} + +pj_status_t getURL(const char *curl) +{ + pj_str_t url; + pj_http_req_callback hcb; + pj_status_t status; + + pj_bzero(&hcb, sizeof(hcb)); + hcb.on_complete = &on_complete; + hcb.on_data_read = &on_data_read; + + /* Create pool, timer, and ioqueue */ + pool = pj_pool_create(mem, NULL, 8192, 4096, NULL); + if (pj_timer_heap_create(pool, 16, &timer_heap)) + return -31; + if (pj_ioqueue_create(pool, 16, &ioqueue)) + return -32; + + pj_strdup2(pool, &url, curl); + + if ((status = pj_http_req_create(pool, &url, timer_heap, ioqueue, + NULL, &hcb, &http_req)) != PJ_SUCCESS) + return status; + + if ((status = pj_http_req_start(http_req)) != PJ_SUCCESS) + return status; + + while (pj_http_req_is_running(http_req)) { + pj_time_val delay = {0, 50}; + pj_ioqueue_poll(ioqueue, &delay); + pj_timer_heap_poll(timer_heap, NULL); + } + + pj_http_req_destroy(http_req); + pj_ioqueue_destroy(ioqueue); + pj_timer_heap_destroy(timer_heap); + pj_pool_release(pool); + + return PJ_SUCCESS; +} +/* + * main() + */ +int main(int argc, char *argv[]) +{ + pj_caching_pool cp; + pj_status_t status; + + if (argc != 3) { + puts("Usage: httpdemo URL filename"); + return 1; + } + + pj_log_set_level(3); + + pj_init(); + pj_caching_pool_init(&cp, NULL, 0); + mem = &cp.factory; + pjlib_util_init(); + + f = fopen(argv[2], "wb"); + status = getURL(argv[1]); + if (status != PJ_SUCCESS) { + PJ_PERROR(1, (THIS_FILE, status, "Error")); + } + fclose(f); + + pj_shutdown(); + return 0; +} -- cgit v1.2.3