WARNING: The online documentation has moved to https://docs.pjsip.org.

Visit the new documentation at https://docs.pjsip.org:

BLOG | DOCUMENTATION | GITHUB

Home --> Documentations --> PJMEDIA Reference

Samples: HTTP Client demo

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

1/*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
27#include <pjlib.h>
28#include <pjlib-util.h>
30#include <pjsip.h>
31#include <pjmedia.h>
32#include <pjnath.h>
33#include <pjsip_simple.h>
34
35static pj_timer_heap_t *timer_heap;
36static pj_ioqueue_t *ioqueue;
37static pj_pool_t *pool;
38static pj_http_req *http_req;
39static pj_pool_factory *mem;
40static FILE *f = NULL;
41
42//#define VERBOSE
43#define THIS_FILE "http_demo"
44
45static 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
67static 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
74static 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
88static 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: %d 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
105pj_status_t getURL(const char *curl)
106{
107 pj_str_t url;
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 /* Create pool, timer, and ioqueue */
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 * main()
148 */
149int main(int argc, char *argv[])
150{
152 pj_status_t status;
153
154 if (argc < 2 || argc > 3) {
155 puts("Usage: httpdemo URL [output-filename]");
156 return 1;
157 }
158
160
161 pj_init();
162 pj_caching_pool_init(&cp, NULL, 0);
163 mem = &cp.factory;
165
166 if (argc > 2)
167 f = fopen(argv[2], "wb");
168 else
169 f = stdout;
170
171 status = getURL(argv[1]);
172 if (status != PJ_SUCCESS) {
173 PJ_PERROR(1, (THIS_FILE, status, "Error"));
174 }
175
176 if (f != stdout)
177 fclose(f);
178
180 pj_shutdown();
181 return 0;
182}
pj_status_t pjlib_util_init(void)
pj_status_t pj_init(void)
struct pj_ioqueue_t pj_ioqueue_t
size_t pj_size_t
int pj_status_t
void pj_shutdown(void)
struct pj_timer_heap_t pj_timer_heap_t
PJ_SUCCESS
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
pj_status_t pj_http_req_start(pj_http_req *http_req)
pj_bool_t pj_http_req_is_running(const pj_http_req *http_req)
pj_status_t pj_http_req_destroy(pj_http_req *http_req)
struct pj_http_req pj_http_req
pj_status_t pj_http_req_create(pj_pool_t *pool, const pj_str_t *url, pj_timer_heap_t *timer, pj_ioqueue_t *ioqueue, const pj_http_req_param *param, const pj_http_req_callback *hcb, pj_http_req **http_req)
pj_status_t pj_ioqueue_destroy(pj_ioqueue_t *ioque)
pj_status_t pj_ioqueue_create(pj_pool_t *pool, pj_size_t max_fd, pj_ioqueue_t **ioqueue)
int pj_ioqueue_poll(pj_ioqueue_t *ioque, const pj_time_val *timeout)
#define PJ_LOG(level, arg)
void pj_log_set_level(int level)
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void pj_pool_release(pj_pool_t *pool)
pj_str_t * pj_strdup2(pj_pool_t *pool, pj_str_t *dst, const char *src)
int pj_stricmp2(const pj_str_t *str1, const char *str2)
void pj_bzero(void *dst, pj_size_t size)
unsigned pj_timer_heap_poll(pj_timer_heap_t *ht, pj_time_val *next_delay)
void pj_timer_heap_destroy(pj_timer_heap_t *ht)
pj_status_t pj_timer_heap_create(pj_pool_t *pool, pj_size_t count, pj_timer_heap_t **ht)
#define PJ_UNUSED_ARG(arg)
#define PJ_PERROR(level, arg)
PJMEDIA main header file.
pj_pool_factory factory
pj_http_header_elmt header[32]
void(* on_response)(pj_http_req *http_req, const pj_http_resp *resp)
void(* on_complete)(pj_http_req *http_req, pj_status_t status, const pj_http_resp *resp)
void(* on_data_read)(pj_http_req *http_req, void *data, pj_size_t size)
void(* on_send_data)(pj_http_req *http_req, void **data, pj_size_t *size)
pj_http_headers headers
pj_uint16_t status_code
pj_str_t reason
pj_size_t size
pj_str_t version
pj_ssize_t slen
char * ptr

 


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