OSDN Git Service

* Added serf library.
[modchxj/mod_chxj.git] / src / serf / test / serf_get.c
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdlib.h>
17
18 #include <apr.h>
19 #include <apr_uri.h>
20 #include <apr_strings.h>
21 #include <apr_atomic.h>
22 #include <apr_base64.h>
23 #include <apr_getopt.h>
24 #include <apr_version.h>
25
26 #include "serf.h"
27
28 #define SERF_VERSION_STRING "0.01"
29
30 typedef struct {
31     int using_ssl;
32     serf_ssl_context_t *ssl_ctx;
33     serf_bucket_alloc_t *bkt_alloc;
34 } app_baton_t;
35
36 static void closed_connection(serf_connection_t *conn,
37                               void *closed_baton,
38                               apr_status_t why,
39                               apr_pool_t *pool)
40 {
41     if (why) {
42         abort();
43     }
44 }
45
46 static serf_bucket_t* conn_setup(apr_socket_t *skt,
47                                 void *setup_baton,
48                                 apr_pool_t *pool)
49 {
50     serf_bucket_t *c;
51     app_baton_t *ctx = setup_baton;
52
53     c = serf_bucket_socket_create(skt, ctx->bkt_alloc);
54     if (ctx->using_ssl) {
55         c = serf_bucket_ssl_decrypt_create(c, ctx->ssl_ctx, ctx->bkt_alloc);
56         if (!ctx->ssl_ctx) {
57             ctx->ssl_ctx = serf_bucket_ssl_decrypt_context_get(c);
58         }
59     }
60
61     return c;
62 }
63
64 static serf_bucket_t* accept_response(serf_request_t *request,
65                                       serf_bucket_t *stream,
66                                       void *acceptor_baton,
67                                       apr_pool_t *pool)
68 {
69     serf_bucket_t *c;
70     serf_bucket_alloc_t *bkt_alloc;
71
72     /* get the per-request bucket allocator */
73     bkt_alloc = serf_request_get_alloc(request);
74
75     /* Create a barrier so the response doesn't eat us! */
76     c = serf_bucket_barrier_create(stream, bkt_alloc);
77
78     return serf_bucket_response_create(c, bkt_alloc);
79 }
80
81 typedef struct {
82 #if APR_MAJOR_VERSION > 0
83     apr_uint32_t requests_outstanding;
84 #else
85     apr_atomic_t requests_outstanding;
86 #endif
87     int print_headers;
88
89     serf_response_acceptor_t acceptor;
90     app_baton_t *acceptor_baton;
91
92     serf_response_handler_t handler;
93
94     const char *host;
95     const char *method;
96     const char *path;
97     const char *req_body_path;
98     const char *authn;
99 } handler_baton_t;
100
101 /* Kludges for APR 0.9 support. */
102 #if APR_MAJOR_VERSION == 0
103 #define apr_atomic_inc32 apr_atomic_inc
104 #define apr_atomic_dec32 apr_atomic_dec
105 #define apr_atomic_read32 apr_atomic_read
106 #endif
107
108 static apr_status_t handle_response(serf_request_t *request,
109                                     serf_bucket_t *response,
110                                     void *handler_baton,
111                                     apr_pool_t *pool)
112 {
113     const char *data;
114     apr_size_t len;
115     serf_status_line sl;
116     apr_status_t status;
117     handler_baton_t *ctx = handler_baton;
118
119     status = serf_bucket_response_status(response, &sl);
120     if (status) {
121         if (APR_STATUS_IS_EAGAIN(status)) {
122             return status;
123         }
124         abort();
125     }
126
127     while (1) {
128         status = serf_bucket_read(response, 2048, &data, &len);
129         if (SERF_BUCKET_READ_ERROR(status))
130             return status;
131
132         /* got some data. print it out. */
133         fwrite(data, 1, len, stdout);
134
135         /* are we done yet? */
136         if (APR_STATUS_IS_EOF(status)) {
137             if (ctx->print_headers) {
138                 serf_bucket_t *hdrs;
139                 hdrs = serf_bucket_response_get_headers(response);
140                 while (1) {
141                     status = serf_bucket_read(hdrs, 2048, &data, &len);
142                     if (SERF_BUCKET_READ_ERROR(status))
143                         return status;
144
145                     fwrite(data, 1, len, stdout);
146                     if (APR_STATUS_IS_EOF(status)) {
147                         break;
148                     }
149                 }
150             }
151
152             apr_atomic_dec32(&ctx->requests_outstanding);
153             return APR_EOF;
154         }
155
156         /* have we drained the response so far? */
157         if (APR_STATUS_IS_EAGAIN(status))
158             return status;
159
160         /* loop to read some more. */
161     }
162     /* NOTREACHED */
163 }
164
165 static apr_status_t setup_request(serf_request_t *request,
166                                   void *setup_baton,
167                                   serf_bucket_t **req_bkt,
168                                   serf_response_acceptor_t *acceptor,                                             void **acceptor_baton,
169                                   serf_response_handler_t *handler,
170                                   void **handler_baton,
171                                   apr_pool_t *pool)
172 {
173     handler_baton_t *ctx = setup_baton;
174     serf_bucket_t *hdrs_bkt;
175     serf_bucket_t *body_bkt;
176
177     if (ctx->req_body_path) {
178         apr_file_t *file;
179         apr_status_t status;
180
181         status = apr_file_open(&file, ctx->req_body_path, APR_READ,
182                                APR_OS_DEFAULT, pool);
183
184         if (status) {
185             printf("Error opening file (%s)\n", ctx->req_body_path);
186             return status;
187         }
188
189         body_bkt = serf_bucket_file_create(file,
190                                            serf_request_get_alloc(request));
191     }
192     else {
193         body_bkt = NULL;
194     }
195
196     *req_bkt = serf_bucket_request_create(ctx->method, ctx->path, body_bkt,
197                                           serf_request_get_alloc(request));
198
199     hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
200
201     /* FIXME: Shouldn't we be able to figure out the host ourselves? */
202     serf_bucket_headers_setn(hdrs_bkt, "Host", ctx->host);
203     serf_bucket_headers_setn(hdrs_bkt, "User-Agent",
204                              "Serf/" SERF_VERSION_STRING);
205     /* Shouldn't serf do this for us? */
206     serf_bucket_headers_setn(hdrs_bkt, "Accept-Encoding", "gzip");
207
208     if (ctx->authn != NULL) {
209         serf_bucket_headers_setn(hdrs_bkt, "Authorization", ctx->authn);
210     }
211
212     apr_atomic_inc32(&(ctx->requests_outstanding));
213
214     if (ctx->acceptor_baton->using_ssl) {
215         serf_bucket_alloc_t *req_alloc;
216         app_baton_t *app_ctx = ctx->acceptor_baton;
217
218         req_alloc = serf_request_get_alloc(request);
219
220         if (app_ctx->ssl_ctx == NULL) {
221             *req_bkt =
222                 serf_bucket_ssl_encrypt_create(*req_bkt, NULL,
223                                                app_ctx->bkt_alloc);
224             app_ctx->ssl_ctx =
225                 serf_bucket_ssl_encrypt_context_get(*req_bkt);
226         }
227         else {
228             *req_bkt =
229                 serf_bucket_ssl_encrypt_create(*req_bkt, app_ctx->ssl_ctx,
230                                                app_ctx->bkt_alloc);
231         }
232     }
233
234     *acceptor = ctx->acceptor;
235     *acceptor_baton = ctx->acceptor_baton;
236     *handler = ctx->handler;
237     *handler_baton = ctx;
238
239     return APR_SUCCESS;
240 }
241
242 void print_usage(apr_pool_t *pool)
243 {
244     puts("serf_get [options] URL");
245     puts("-h\tDisplay this help");
246     puts("-v\tDisplay version");
247     puts("-H\tPrint response headers");
248     puts("-n <count> Fetch URL <count> times");
249     puts("-a <user:password> Present Basic authentication credentials");
250     puts("-m <method> Use the <method> HTTP Method");
251     puts("-f <file> Use the <file> as the request body");
252 }
253
254 int main(int argc, const char **argv)
255 {
256     apr_status_t status;
257     apr_pool_t *pool;
258     apr_sockaddr_t *address;
259     serf_context_t *context;
260     serf_connection_t *connection;
261     serf_request_t *request;
262     app_baton_t app_ctx;
263     handler_baton_t handler_ctx;
264     apr_uri_t url;
265     const char *raw_url, *method, *req_body_path = NULL;
266     int count;
267     int i;
268     int print_headers;
269     char *authn = NULL;
270     apr_getopt_t *opt;
271     char opt_c;
272     const char *opt_arg;
273
274     apr_initialize();
275     atexit(apr_terminate);
276
277     apr_pool_create(&pool, NULL);
278     apr_atomic_init(pool);
279     /* serf_initialize(); */
280
281     /* Default to one round of fetching. */
282     count = 1;
283     /* Default to GET. */
284     method = "GET";
285     /* Do not print headers by default. */
286     print_headers = 0;
287
288     apr_getopt_init(&opt, pool, argc, argv);
289
290     while ((status = apr_getopt(opt, "a:f:hHm:n:v", &opt_c, &opt_arg)) ==
291            APR_SUCCESS) {
292         int srclen, enclen;
293
294         switch (opt_c) {
295         case 'a':
296             srclen = strlen(opt_arg);
297             enclen = apr_base64_encode_len(srclen);
298             authn = apr_palloc(pool, enclen + 6);
299             strcpy(authn, "Basic ");
300             (void) apr_base64_encode(&authn[6], opt_arg, srclen);
301             break;
302         case 'f':
303             req_body_path = opt_arg;
304             break;
305         case 'h':
306             print_usage(pool);
307             exit(0);
308             break;
309         case 'H':
310             print_headers = 1;
311             break;
312         case 'm':
313             method = opt_arg;
314             break;
315         case 'n':
316             errno = 0;
317             count = apr_strtoi64(opt_arg, NULL, 10);
318             if (errno) {
319                 printf("Problem converting number of times to fetch URL (%d)\n",
320                        errno);
321                 return errno;
322             }
323             break;
324         case 'v':
325             puts("Serf version: " SERF_VERSION_STRING);
326             exit(0);
327         default:
328             break;
329         }
330     }
331
332     if (opt->ind != opt->argc - 1) {
333         print_usage(pool);
334         exit(-1);
335     }
336
337     raw_url = argv[opt->ind];
338
339     apr_uri_parse(pool, raw_url, &url);
340     if (!url.port) {
341         url.port = apr_uri_port_of_scheme(url.scheme);
342     }
343     if (!url.path) {
344         url.path = "/";
345     }
346
347     if (strcasecmp(url.scheme, "https") == 0) {
348         app_ctx.using_ssl = 1;
349     }
350     else {
351         app_ctx.using_ssl = 0;
352     }
353
354     status = apr_sockaddr_info_get(&address,
355                                    url.hostname, APR_UNSPEC, url.port, 0,
356                                    pool);
357     if (status) {
358         printf("Error creating address: %d\n", status);
359         exit(1);
360     }
361
362     context = serf_context_create(pool);
363
364     /* ### Connection or Context should have an allocator? */
365     app_ctx.bkt_alloc = serf_bucket_allocator_create(pool, NULL, NULL);
366     app_ctx.ssl_ctx = NULL;
367
368     connection = serf_connection_create(context, address,
369                                         conn_setup, &app_ctx,
370                                         closed_connection, &app_ctx,
371                                         pool);
372
373     handler_ctx.requests_outstanding = 0;
374     handler_ctx.print_headers = print_headers;
375
376     handler_ctx.host = url.hostinfo;
377     handler_ctx.method = method;
378     handler_ctx.path = url.path;
379     handler_ctx.authn = authn;
380
381     handler_ctx.req_body_path = req_body_path;
382
383     handler_ctx.acceptor = accept_response;
384     handler_ctx.acceptor_baton = &app_ctx;
385     handler_ctx.handler = handle_response;
386
387     for (i = 0; i < count; i++) {
388         request = serf_connection_request_create(connection, setup_request,
389                                                  &handler_ctx);
390     }
391
392     while (1) {
393         status = serf_context_run(context, SERF_DURATION_FOREVER, pool);
394         if (APR_STATUS_IS_TIMEUP(status))
395             continue;
396         if (status) {
397             char buf[200];
398
399             printf("Error running context: (%d) %s\n", status,
400                    apr_strerror(status, buf, sizeof(buf)));
401             exit(1);
402         }
403         if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
404             break;
405         }
406         /* Debugging purposes only! */
407         serf_debug__closed_conn(app_ctx.bkt_alloc);
408     }
409
410     serf_connection_close(connection);
411
412     apr_pool_destroy(pool);
413     return 0;
414 }