BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

Samples: HTTP Client demo

This file is pjsip-apps/src/samples/httpdemo.c

00001 /* $Id: httpdemo.c 3553 2011-05-05 06:14:19Z nanang $ */
00002 /* 
00003  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00018  */
00019 
00028 #include <pjlib.h>
00029 #include <pjlib-util.h>
00030 #include <pjlib-util/http_client.h>
00031 #include <pjsip.h>
00032 #include <pjmedia.h>
00033 #include <pjnath.h>
00034 #include <pjsip_simple.h>
00035 
00036 static pj_timer_heap_t *timer_heap;
00037 static pj_ioqueue_t *ioqueue;
00038 static pj_pool_t *pool;
00039 static pj_http_req *http_req;
00040 static pj_pool_factory *mem;
00041 static FILE *f = NULL;
00042 
00043 //#define VERBOSE
00044 #define THIS_FILE           "http_demo"
00045 
00046 static void on_response(pj_http_req *http_req, const pj_http_resp *resp)
00047 {
00048     unsigned i;
00049 
00050     PJ_UNUSED_ARG(http_req);
00051     PJ_LOG(3,(THIS_FILE, "%.*s %d %.*s", (int)resp->version.slen, resp->version.ptr,
00052                                            resp->status_code,
00053                                            (int)resp->reason.slen, resp->reason.ptr));
00054 
00055     for (i=0; i<resp->headers.count; ++i) {
00056         const pj_http_header_elmt *h = &resp->headers.header[i];
00057 
00058         if (!pj_stricmp2(&h->name, "Content-Length") ||
00059             !pj_stricmp2(&h->name, "Content-Type"))
00060         {
00061             PJ_LOG(3,(THIS_FILE, "%.*s: %.*s",
00062                       (int)h->name.slen, h->name.ptr,
00063                       (int)h->value.slen, h->value.ptr));
00064         }
00065     }
00066 }
00067 
00068 static void on_send_data(pj_http_req *http_req, void **data, pj_size_t *size)
00069 {
00070         PJ_UNUSED_ARG(http_req);
00071         PJ_UNUSED_ARG(size);
00072         PJ_UNUSED_ARG(data);
00073 }
00074 
00075 static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size)
00076 {
00077     PJ_UNUSED_ARG(hreq);
00078 
00079     if (size > 0) {
00080         fwrite(data, 1, size, f);
00081         fflush(f);
00082 #ifdef VERBOSE
00083         PJ_LOG(3, (THIS_FILE, "Data received: %d bytes", size));
00084         printf("%.*s\n", (int)size, (char *)data);
00085 #endif
00086     }
00087 }
00088 
00089 static void on_complete(pj_http_req *hreq, pj_status_t status,
00090                         const pj_http_resp *resp)
00091 {
00092     PJ_UNUSED_ARG(hreq);
00093 
00094     if (status != PJ_SUCCESS) {
00095         PJ_PERROR(1, (THIS_FILE, status, "HTTP request completed with error"));
00096         return;
00097     }
00098     PJ_LOG(3, (THIS_FILE, "Data completed: %d bytes", resp->size));
00099     if (resp->size > 0 && resp->data) {
00100 #ifdef VERBOSE
00101         printf("%.*s\n", (int)resp->size, (char *)resp->data);
00102 #endif
00103     }
00104 }
00105 
00106 pj_status_t getURL(const char *curl)
00107 {
00108     pj_str_t url;
00109     pj_http_req_callback hcb;
00110     pj_status_t status;
00111 
00112     pj_bzero(&hcb, sizeof(hcb));
00113     hcb.on_complete = &on_complete;
00114     hcb.on_data_read = &on_data_read;
00115     hcb.on_send_data = &on_send_data;
00116     hcb.on_response = &on_response;
00117 
00118     /* Create pool, timer, and ioqueue */
00119     pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
00120     if (pj_timer_heap_create(pool, 16, &timer_heap))
00121         return -31;
00122     if (pj_ioqueue_create(pool, 16, &ioqueue))
00123         return -32;
00124 
00125     pj_strdup2(pool, &url, curl);
00126 
00127     if ((status = pj_http_req_create(pool, &url, timer_heap, ioqueue, 
00128                            NULL, &hcb, &http_req)) != PJ_SUCCESS)
00129         return status;
00130 
00131     if ((status = pj_http_req_start(http_req)) != PJ_SUCCESS)
00132         return status;
00133 
00134     while (pj_http_req_is_running(http_req)) {
00135         pj_time_val delay = {0, 50};
00136         pj_ioqueue_poll(ioqueue, &delay);
00137         pj_timer_heap_poll(timer_heap, NULL);
00138     }
00139 
00140     pj_http_req_destroy(http_req);
00141     pj_ioqueue_destroy(ioqueue);
00142     pj_timer_heap_destroy(timer_heap);
00143     pj_pool_release(pool);
00144 
00145     return PJ_SUCCESS;
00146 }
00147 /*
00148  * main()
00149  */
00150 int main(int argc, char *argv[])
00151 {
00152     pj_caching_pool cp;
00153     pj_status_t status;
00154 
00155     if (argc < 2 || argc > 3) {
00156         puts("Usage: httpdemo URL [output-filename]");
00157         return 1;
00158     }
00159 
00160     pj_log_set_level(5);
00161 
00162     pj_init();
00163     pj_caching_pool_init(&cp, NULL, 0);
00164     mem = &cp.factory;
00165     pjlib_util_init();
00166 
00167     if (argc > 2)
00168         f = fopen(argv[2], "wb");
00169     else
00170         f = stdout;
00171 
00172     status = getURL(argv[1]);
00173     if (status != PJ_SUCCESS) {
00174         PJ_PERROR(1, (THIS_FILE, status, "Error"));
00175     }
00176 
00177     if (f != stdout)
00178         fclose(f);
00179 
00180     pj_caching_pool_destroy(&cp);
00181     pj_shutdown();
00182     return 0;
00183 }

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.