1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
28 #define SERF_VERSION_STRING "0.01"
32 serf_ssl_context_t *ssl_ctx;
33 serf_bucket_alloc_t *bkt_alloc;
36 static void closed_connection(serf_connection_t *conn,
46 static serf_bucket_t* conn_setup(apr_socket_t *skt,
51 app_baton_t *ctx = setup_baton;
53 c = serf_bucket_socket_create(skt, ctx->bkt_alloc);
55 c = serf_bucket_ssl_decrypt_create(c, ctx->ssl_ctx, ctx->bkt_alloc);
57 ctx->ssl_ctx = serf_bucket_ssl_decrypt_context_get(c);
64 static serf_bucket_t* accept_response(serf_request_t *request,
65 serf_bucket_t *stream,
70 serf_bucket_alloc_t *bkt_alloc;
72 /* get the per-request bucket allocator */
73 bkt_alloc = serf_request_get_alloc(request);
75 /* Create a barrier so the response doesn't eat us! */
76 c = serf_bucket_barrier_create(stream, bkt_alloc);
78 return serf_bucket_response_create(c, bkt_alloc);
82 #if APR_MAJOR_VERSION > 0
83 apr_uint32_t requests_outstanding;
85 apr_atomic_t requests_outstanding;
89 serf_response_acceptor_t acceptor;
90 app_baton_t *acceptor_baton;
92 serf_response_handler_t handler;
97 const char *req_body_path;
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
108 static apr_status_t handle_response(serf_request_t *request,
109 serf_bucket_t *response,
117 handler_baton_t *ctx = handler_baton;
119 status = serf_bucket_response_status(response, &sl);
121 if (APR_STATUS_IS_EAGAIN(status)) {
128 status = serf_bucket_read(response, 2048, &data, &len);
129 if (SERF_BUCKET_READ_ERROR(status))
132 /* got some data. print it out. */
133 fwrite(data, 1, len, stdout);
135 /* are we done yet? */
136 if (APR_STATUS_IS_EOF(status)) {
137 if (ctx->print_headers) {
139 hdrs = serf_bucket_response_get_headers(response);
141 status = serf_bucket_read(hdrs, 2048, &data, &len);
142 if (SERF_BUCKET_READ_ERROR(status))
145 fwrite(data, 1, len, stdout);
146 if (APR_STATUS_IS_EOF(status)) {
152 apr_atomic_dec32(&ctx->requests_outstanding);
156 /* have we drained the response so far? */
157 if (APR_STATUS_IS_EAGAIN(status))
160 /* loop to read some more. */
165 static apr_status_t setup_request(serf_request_t *request,
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,
173 handler_baton_t *ctx = setup_baton;
174 serf_bucket_t *hdrs_bkt;
175 serf_bucket_t *body_bkt;
177 if (ctx->req_body_path) {
181 status = apr_file_open(&file, ctx->req_body_path, APR_READ,
182 APR_OS_DEFAULT, pool);
185 printf("Error opening file (%s)\n", ctx->req_body_path);
189 body_bkt = serf_bucket_file_create(file,
190 serf_request_get_alloc(request));
196 *req_bkt = serf_bucket_request_create(ctx->method, ctx->path, body_bkt,
197 serf_request_get_alloc(request));
199 hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
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");
208 if (ctx->authn != NULL) {
209 serf_bucket_headers_setn(hdrs_bkt, "Authorization", ctx->authn);
212 apr_atomic_inc32(&(ctx->requests_outstanding));
214 if (ctx->acceptor_baton->using_ssl) {
215 serf_bucket_alloc_t *req_alloc;
216 app_baton_t *app_ctx = ctx->acceptor_baton;
218 req_alloc = serf_request_get_alloc(request);
220 if (app_ctx->ssl_ctx == NULL) {
222 serf_bucket_ssl_encrypt_create(*req_bkt, NULL,
225 serf_bucket_ssl_encrypt_context_get(*req_bkt);
229 serf_bucket_ssl_encrypt_create(*req_bkt, app_ctx->ssl_ctx,
234 *acceptor = ctx->acceptor;
235 *acceptor_baton = ctx->acceptor_baton;
236 *handler = ctx->handler;
237 *handler_baton = ctx;
242 void print_usage(apr_pool_t *pool)
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");
254 int main(int argc, const char **argv)
258 apr_sockaddr_t *address;
259 serf_context_t *context;
260 serf_connection_t *connection;
261 serf_request_t *request;
263 handler_baton_t handler_ctx;
265 const char *raw_url, *method, *req_body_path = NULL;
275 atexit(apr_terminate);
277 apr_pool_create(&pool, NULL);
278 apr_atomic_init(pool);
279 /* serf_initialize(); */
281 /* Default to one round of fetching. */
283 /* Default to GET. */
285 /* Do not print headers by default. */
288 apr_getopt_init(&opt, pool, argc, argv);
290 while ((status = apr_getopt(opt, "a:f:hHm:n:v", &opt_c, &opt_arg)) ==
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);
303 req_body_path = opt_arg;
317 count = apr_strtoi64(opt_arg, NULL, 10);
319 printf("Problem converting number of times to fetch URL (%d)\n",
325 puts("Serf version: " SERF_VERSION_STRING);
332 if (opt->ind != opt->argc - 1) {
337 raw_url = argv[opt->ind];
339 apr_uri_parse(pool, raw_url, &url);
341 url.port = apr_uri_port_of_scheme(url.scheme);
347 if (strcasecmp(url.scheme, "https") == 0) {
348 app_ctx.using_ssl = 1;
351 app_ctx.using_ssl = 0;
354 status = apr_sockaddr_info_get(&address,
355 url.hostname, APR_UNSPEC, url.port, 0,
358 printf("Error creating address: %d\n", status);
362 context = serf_context_create(pool);
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;
368 connection = serf_connection_create(context, address,
369 conn_setup, &app_ctx,
370 closed_connection, &app_ctx,
373 handler_ctx.requests_outstanding = 0;
374 handler_ctx.print_headers = print_headers;
376 handler_ctx.host = url.hostinfo;
377 handler_ctx.method = method;
378 handler_ctx.path = url.path;
379 handler_ctx.authn = authn;
381 handler_ctx.req_body_path = req_body_path;
383 handler_ctx.acceptor = accept_response;
384 handler_ctx.acceptor_baton = &app_ctx;
385 handler_ctx.handler = handle_response;
387 for (i = 0; i < count; i++) {
388 request = serf_connection_request_create(connection, setup_request,
393 status = serf_context_run(context, SERF_DURATION_FOREVER, pool);
394 if (APR_STATUS_IS_TIMEUP(status))
399 printf("Error running context: (%d) %s\n", status,
400 apr_strerror(status, buf, sizeof(buf)));
403 if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
406 /* Debugging purposes only! */
407 serf_debug__closed_conn(app_ctx.bkt_alloc);
410 serf_connection_close(connection);
412 apr_pool_destroy(pool);