| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <pjlib.h> |
| 28 |
#include <pjlib-util.h> |
| 29 |
#include <pjlib-util/http_client.h> |
| 30 |
#include <pjsip.h> |
| 31 |
#include <pjmedia.h> |
| 32 |
#include <pjnath.h> |
| 33 |
#include <pjsip_simple.h> |
| 34 |
|
| 35 |
static pj_timer_heap_t *timer_heap; |
| 36 |
static pj_ioqueue_t *ioqueue; |
| 37 |
static pj_pool_t *pool; |
| 38 |
static pj_http_req *http_req; |
| 39 |
static pj_pool_factory *mem; |
| 40 |
static FILE *f = NULL; |
| 41 |
|
| 42 |
|
| 43 |
#define THIS_FILE "http_demo" |
| 44 |
|
| 45 |
static void on_response(pj_http_req *req, const pj_http_resp *resp) |
| 46 |
{ |
| 47 |
unsigned i; |
| 48 |
|
| 49 |
PJ_UNUSED_ARG(req); |
| 50 |
PJ_LOG(3,(THIS_FILE, "%.*s %d %.*s", (int)resp->version.slen, resp->version.ptr, |
| 51 |
resp->status_code, |
| 52 |
(int)resp->reason.slen, resp->reason.ptr)); |
| 53 |
|
| 54 |
for (i=0; i<resp->headers.count; ++i) { |
| 55 |
const pj_http_header_elmt *h = &resp->headers.header[i]; |
| 56 |
|
| 57 |
if (!pj_stricmp2(&h->name, "Content-Length") || |
| 58 |
!pj_stricmp2(&h->name, "Content-Type")) |
| 59 |
{ |
| 60 |
PJ_LOG(3,(THIS_FILE, "%.*s: %.*s", |
| 61 |
(int)h->name.slen, h->name.ptr, |
| 62 |
(int)h->value.slen, h->value.ptr)); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static void on_send_data(pj_http_req *req, void **data, pj_size_t *size) |
| 68 |
{ |
| 69 |
PJ_UNUSED_ARG(req); |
| 70 |
PJ_UNUSED_ARG(size); |
| 71 |
PJ_UNUSED_ARG(data); |
| 72 |
} |
| 73 |
|
| 74 |
static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size) |
| 75 |
{ |
| 76 |
PJ_UNUSED_ARG(hreq); |
| 77 |
|
| 78 |
if (size > 0) { |
| 79 |
fwrite(data, 1, size, f); |
| 80 |
fflush(f); |
| 81 |
#ifdef VERBOSE |
| 82 |
PJ_LOG(3, (THIS_FILE, "Data received: %d bytes", size)); |
| 83 |
printf("%.*s\n", (int)size, (char *)data); |
| 84 |
#endif |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static void on_complete(pj_http_req *hreq, pj_status_t status, |
| 89 |
const pj_http_resp *resp) |
| 90 |
{ |
| 91 |
PJ_UNUSED_ARG(hreq); |
| 92 |
|
| 93 |
if (status != PJ_SUCCESS) { |
| 94 |
PJ_PERROR(1, (THIS_FILE, status, "HTTP request completed with error")); |
| 95 |
return; |
| 96 |
} |
| 97 |
PJ_LOG(3, (THIS_FILE, "Data completed: %ld bytes", resp->size)); |
| 98 |
if (resp->size > 0 && resp->data) { |
| 99 |
#ifdef VERBOSE |
| 100 |
printf("%.*s\n", (int)resp->size, (char *)resp->data); |
| 101 |
#endif |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
pj_status_t getURL(const char *curl) |
| 106 |
{ |
| 107 |
pj_str_t url; |
| 108 |
pj_http_req_callback hcb; |
| 109 |
pj_status_t status; |
| 110 |
|
| 111 |
pj_bzero(&hcb, sizeof(hcb)); |
| 112 |
hcb.on_complete = &on_complete; |
| 113 |
hcb.on_data_read = &on_data_read; |
| 114 |
hcb.on_send_data = &on_send_data; |
| 115 |
hcb.on_response = &on_response; |
| 116 |
|
| 117 |
|
| 118 |
pool = pj_pool_create(mem, NULL, 8192, 4096, NULL); |
| 119 |
if (pj_timer_heap_create(pool, 16, &timer_heap)) |
| 120 |
return -31; |
| 121 |
if (pj_ioqueue_create(pool, 16, &ioqueue)) |
| 122 |
return -32; |
| 123 |
|
| 124 |
pj_strdup2(pool, &url, curl); |
| 125 |
|
| 126 |
if ((status = pj_http_req_create(pool, &url, timer_heap, ioqueue, |
| 127 |
NULL, &hcb, &http_req)) != PJ_SUCCESS) |
| 128 |
return status; |
| 129 |
|
| 130 |
if ((status = pj_http_req_start(http_req)) != PJ_SUCCESS) |
| 131 |
return status; |
| 132 |
|
| 133 |
while (pj_http_req_is_running(http_req)) { |
| 134 |
pj_time_val delay = {0, 50}; |
| 135 |
pj_ioqueue_poll(ioqueue, &delay); |
| 136 |
pj_timer_heap_poll(timer_heap, NULL); |
| 137 |
} |
| 138 |
|
| 139 |
pj_http_req_destroy(http_req); |
| 140 |
pj_ioqueue_destroy(ioqueue); |
| 141 |
pj_timer_heap_destroy(timer_heap); |
| 142 |
pj_pool_release(pool); |
| 143 |
|
| 144 |
return PJ_SUCCESS; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
int main(int argc, char *argv[]) |
| 150 |
{ |
| 151 |
pj_caching_pool cp; |
| 152 |
pj_status_t status; |
| 153 |
|
| 154 |
if (argc < 2 || argc > 3) { |
| 155 |
puts("Usage: httpdemo URL [output-filename]"); |
| 156 |
return 1; |
| 157 |
} |
| 158 |
|
| 159 |
pj_log_set_level(5); |
| 160 |
|
| 161 |
status = pj_init(); |
| 162 |
if (status != PJ_SUCCESS) { |
| 163 |
puts("pj_init() error"); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
pj_caching_pool_init(&cp, NULL, 0); |
| 167 |
mem = &cp.factory; |
| 168 |
status = pjlib_util_init(); |
| 169 |
if (status != PJ_SUCCESS) { |
| 170 |
puts("pjlib_util_init() error"); |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
if (argc > 2) |
| 175 |
f = fopen(argv[2], "wb"); |
| 176 |
else |
| 177 |
f = stdout; |
| 178 |
|
| 179 |
status = getURL(argv[1]); |
| 180 |
if (status != PJ_SUCCESS) { |
| 181 |
PJ_PERROR(1, (THIS_FILE, status, "Error")); |
| 182 |
} |
| 183 |
|
| 184 |
if (f != stdout) |
| 185 |
fclose(f); |
| 186 |
|
| 187 |
pj_caching_pool_destroy(&cp); |
| 188 |
pj_shutdown(); |
| 189 |
return 0; |
| 190 |
} |