Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2010 Teluu Inc. (http://www.teluu.com) |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * \page page_httpdemo_c Samples: HTTP Client demo |
| 22 | * |
| 23 | * This file is pjsip-apps/src/samples/httpdemo.c |
| 24 | * |
| 25 | * \includelineno httpdemo.c |
| 26 | */ |
| 27 | |
| 28 | #include <pjlib.h> |
| 29 | #include <pjlib-util.h> |
| 30 | #include <pjlib-util/http_client.h> |
| 31 | #include <pjsip.h> |
| 32 | #include <pjmedia.h> |
| 33 | #include <pjnath.h> |
| 34 | #include <pjsip_simple.h> |
| 35 | |
| 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; |
| 42 | |
| 43 | //#define VERBOSE |
| 44 | #define THIS_FILE "http_demo" |
| 45 | |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 46 | static void on_response(pj_http_req *http_req, const pj_http_resp *resp) |
| 47 | { |
Benny Prijono | 0828055 | 2010-08-01 15:25:04 +0000 | [diff] [blame^] | 48 | PJ_UNUSED_ARG(http_req); |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 49 | PJ_LOG(3,(THIS_FILE, "%.*s %d %.*s", (int)resp->version.slen, resp->version.ptr, |
| 50 | resp->status_code, |
| 51 | (int)resp->reason.slen, resp->reason.ptr)); |
| 52 | } |
| 53 | |
| 54 | static void on_send_data(pj_http_req *http_req, void **data, pj_size_t *size) |
| 55 | { |
Benny Prijono | 0828055 | 2010-08-01 15:25:04 +0000 | [diff] [blame^] | 56 | PJ_UNUSED_ARG(http_req); |
| 57 | PJ_UNUSED_ARG(size); |
| 58 | PJ_UNUSED_ARG(data); |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 61 | static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size) |
| 62 | { |
| 63 | PJ_UNUSED_ARG(hreq); |
| 64 | |
| 65 | if (size > 0) { |
| 66 | fwrite(data, 1, size, f); |
| 67 | fflush(f); |
| 68 | #ifdef VERBOSE |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 69 | PJ_LOG(3, (THIS_FILE, "Data received: %d bytes", size)); |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 70 | printf("%.*s\n", (int)size, (char *)data); |
| 71 | #endif |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void on_complete(pj_http_req *hreq, pj_status_t status, |
| 76 | const pj_http_resp *resp) |
| 77 | { |
| 78 | PJ_UNUSED_ARG(hreq); |
| 79 | |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 80 | if (status != PJ_SUCCESS) { |
| 81 | PJ_PERROR(1, (THIS_FILE, status, "HTTP request completed with error")); |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 84 | PJ_LOG(3, (THIS_FILE, "Data completed: %d bytes", resp->size)); |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 85 | if (resp->size > 0 && resp->data) { |
| 86 | #ifdef VERBOSE |
| 87 | printf("%.*s\n", (int)resp->size, (char *)resp->data); |
| 88 | #endif |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pj_status_t getURL(const char *curl) |
| 93 | { |
| 94 | pj_str_t url; |
| 95 | pj_http_req_callback hcb; |
| 96 | pj_status_t status; |
| 97 | |
| 98 | pj_bzero(&hcb, sizeof(hcb)); |
| 99 | hcb.on_complete = &on_complete; |
| 100 | hcb.on_data_read = &on_data_read; |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 101 | hcb.on_send_data = &on_send_data; |
| 102 | hcb.on_response = &on_response; |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 103 | |
| 104 | /* Create pool, timer, and ioqueue */ |
| 105 | pool = pj_pool_create(mem, NULL, 8192, 4096, NULL); |
| 106 | if (pj_timer_heap_create(pool, 16, &timer_heap)) |
| 107 | return -31; |
| 108 | if (pj_ioqueue_create(pool, 16, &ioqueue)) |
| 109 | return -32; |
| 110 | |
| 111 | pj_strdup2(pool, &url, curl); |
| 112 | |
| 113 | if ((status = pj_http_req_create(pool, &url, timer_heap, ioqueue, |
| 114 | NULL, &hcb, &http_req)) != PJ_SUCCESS) |
| 115 | return status; |
| 116 | |
| 117 | if ((status = pj_http_req_start(http_req)) != PJ_SUCCESS) |
| 118 | return status; |
| 119 | |
| 120 | while (pj_http_req_is_running(http_req)) { |
| 121 | pj_time_val delay = {0, 50}; |
| 122 | pj_ioqueue_poll(ioqueue, &delay); |
| 123 | pj_timer_heap_poll(timer_heap, NULL); |
| 124 | } |
| 125 | |
| 126 | pj_http_req_destroy(http_req); |
| 127 | pj_ioqueue_destroy(ioqueue); |
| 128 | pj_timer_heap_destroy(timer_heap); |
| 129 | pj_pool_release(pool); |
| 130 | |
| 131 | return PJ_SUCCESS; |
| 132 | } |
| 133 | /* |
| 134 | * main() |
| 135 | */ |
| 136 | int main(int argc, char *argv[]) |
| 137 | { |
| 138 | pj_caching_pool cp; |
| 139 | pj_status_t status; |
| 140 | |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 141 | if (argc < 2 || argc > 3) { |
| 142 | puts("Usage: httpdemo URL [output-filename]"); |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 143 | return 1; |
| 144 | } |
| 145 | |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 146 | pj_log_set_level(5); |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 147 | |
| 148 | pj_init(); |
| 149 | pj_caching_pool_init(&cp, NULL, 0); |
| 150 | mem = &cp.factory; |
| 151 | pjlib_util_init(); |
| 152 | |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 153 | if (argc > 2) |
| 154 | f = fopen(argv[2], "wb"); |
| 155 | else |
| 156 | f = stdout; |
| 157 | |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 158 | status = getURL(argv[1]); |
| 159 | if (status != PJ_SUCCESS) { |
| 160 | PJ_PERROR(1, (THIS_FILE, status, "Error")); |
| 161 | } |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 162 | |
Sauw Ming | 63236bb | 2010-02-05 16:03:29 +0000 | [diff] [blame] | 163 | if (f != stdout) |
| 164 | fclose(f); |
| 165 | |
| 166 | pj_caching_pool_destroy(&cp); |
Sauw Ming | d8435e6 | 2010-02-04 18:29:16 +0000 | [diff] [blame] | 167 | pj_shutdown(); |
| 168 | return 0; |
| 169 | } |