ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/src/stable/0.8/contrib/serf/test/serf_get.c
Revision: 9268
Committed: Mon Feb 20 02:52:22 2017 UTC (7 years, 2 months ago) by laffer1
Content type: text/plain
File size: 21439 byte(s)
Log Message:
merge in serf 1.3.9

File Contents

# Content
1 /* ====================================================================
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 * ====================================================================
19 */
20
21 #include <stdlib.h>
22
23 #include <apr.h>
24 #include <apr_uri.h>
25 #include <apr_strings.h>
26 #include <apr_atomic.h>
27 #include <apr_base64.h>
28 #include <apr_getopt.h>
29 #include <apr_version.h>
30
31 #include "serf.h"
32
33 /* Add Connection: close header to each request. */
34 /* #define CONNECTION_CLOSE_HDR */
35
36 typedef struct {
37 const char *hostinfo;
38 int using_ssl;
39 int head_request;
40 serf_ssl_context_t *ssl_ctx;
41 serf_bucket_alloc_t *bkt_alloc;
42 } app_baton_t;
43
44 static void closed_connection(serf_connection_t *conn,
45 void *closed_baton,
46 apr_status_t why,
47 apr_pool_t *pool)
48 {
49 app_baton_t *ctx = closed_baton;
50
51 ctx->ssl_ctx = NULL;
52
53 if (why) {
54 abort();
55 }
56 }
57
58 static void print_ssl_cert_errors(int failures)
59 {
60 if (failures) {
61 fprintf(stderr, "INVALID CERTIFICATE:\n");
62 if (failures & SERF_SSL_CERT_NOTYETVALID)
63 fprintf(stderr, "* The certificate is not yet valid.\n");
64 if (failures & SERF_SSL_CERT_EXPIRED)
65 fprintf(stderr, "* The certificate expired.\n");
66 if (failures & SERF_SSL_CERT_SELF_SIGNED)
67 fprintf(stderr, "* The certificate is self-signed.\n");
68 if (failures & SERF_SSL_CERT_UNKNOWNCA)
69 fprintf(stderr, "* The CA is unknown.\n");
70 if (failures & SERF_SSL_CERT_UNKNOWN_FAILURE)
71 fprintf(stderr, "* Unknown failure.\n");
72 }
73 }
74
75 static apr_status_t ignore_all_cert_errors(void *data, int failures,
76 const serf_ssl_certificate_t *cert)
77 {
78 print_ssl_cert_errors(failures);
79
80 /* In a real application, you would normally would not want to do this */
81 return APR_SUCCESS;
82 }
83
84 static char *
85 convert_organisation_to_str(apr_hash_t *org, apr_pool_t *pool)
86 {
87 return apr_psprintf(pool, "%s, %s, %s, %s, %s (%s)",
88 (char*)apr_hash_get(org, "OU", APR_HASH_KEY_STRING),
89 (char*)apr_hash_get(org, "O", APR_HASH_KEY_STRING),
90 (char*)apr_hash_get(org, "L", APR_HASH_KEY_STRING),
91 (char*)apr_hash_get(org, "ST", APR_HASH_KEY_STRING),
92 (char*)apr_hash_get(org, "C", APR_HASH_KEY_STRING),
93 (char*)apr_hash_get(org, "E", APR_HASH_KEY_STRING));
94 }
95
96 static apr_status_t print_certs(void *data, int failures, int error_depth,
97 const serf_ssl_certificate_t * const * certs,
98 apr_size_t certs_len)
99 {
100 apr_pool_t *pool;
101 const serf_ssl_certificate_t *current;
102
103 apr_pool_create(&pool, NULL);
104
105 fprintf(stderr, "Received certificate chain with length %d\n",
106 (int)certs_len);
107 print_ssl_cert_errors(failures);
108 if (failures)
109 fprintf(stderr, "Error at depth=%d\n", error_depth);
110 else
111 fprintf(stderr, "Chain provided with depth=%d\n", error_depth);
112
113 while ((current = *certs) != NULL)
114 {
115 apr_hash_t *issuer, *subject, *serf_cert;
116 apr_array_header_t *san;
117
118 subject = serf_ssl_cert_subject(current, pool);
119 issuer = serf_ssl_cert_issuer(current, pool);
120 serf_cert = serf_ssl_cert_certificate(current, pool);
121
122 fprintf(stderr, "\n-----BEGIN CERTIFICATE-----\n");
123 fprintf(stderr, "Hostname: %s\n",
124 (const char *)apr_hash_get(subject, "CN", APR_HASH_KEY_STRING));
125 fprintf(stderr, "Sha1: %s\n",
126 (const char *)apr_hash_get(serf_cert, "sha1", APR_HASH_KEY_STRING));
127 fprintf(stderr, "Valid from: %s\n",
128 (const char *)apr_hash_get(serf_cert, "notBefore", APR_HASH_KEY_STRING));
129 fprintf(stderr, "Valid until: %s\n",
130 (const char *)apr_hash_get(serf_cert, "notAfter", APR_HASH_KEY_STRING));
131 fprintf(stderr, "Issuer: %s\n", convert_organisation_to_str(issuer, pool));
132
133 san = apr_hash_get(serf_cert, "subjectAltName", APR_HASH_KEY_STRING);
134 if (san) {
135 int i;
136 for (i = 0; i < san->nelts; i++) {
137 char *s = APR_ARRAY_IDX(san, i, char*);
138 fprintf(stderr, "SubjectAltName: %s\n", s);
139 }
140 }
141
142 fprintf(stderr, "%s\n", serf_ssl_cert_export(current, pool));
143 fprintf(stderr, "-----END CERTIFICATE-----\n");
144 ++certs;
145 }
146
147 apr_pool_destroy(pool);
148 return APR_SUCCESS;
149 }
150
151 static apr_status_t conn_setup(apr_socket_t *skt,
152 serf_bucket_t **input_bkt,
153 serf_bucket_t **output_bkt,
154 void *setup_baton,
155 apr_pool_t *pool)
156 {
157 serf_bucket_t *c;
158 app_baton_t *ctx = setup_baton;
159
160 c = serf_bucket_socket_create(skt, ctx->bkt_alloc);
161 if (ctx->using_ssl) {
162 c = serf_bucket_ssl_decrypt_create(c, ctx->ssl_ctx, ctx->bkt_alloc);
163 if (!ctx->ssl_ctx) {
164 ctx->ssl_ctx = serf_bucket_ssl_decrypt_context_get(c);
165 }
166 serf_ssl_server_cert_chain_callback_set(ctx->ssl_ctx,
167 ignore_all_cert_errors,
168 print_certs, NULL);
169 serf_ssl_set_hostname(ctx->ssl_ctx, ctx->hostinfo);
170
171 *output_bkt = serf_bucket_ssl_encrypt_create(*output_bkt, ctx->ssl_ctx,
172 ctx->bkt_alloc);
173 }
174
175 *input_bkt = c;
176
177 return APR_SUCCESS;
178 }
179
180 static serf_bucket_t* accept_response(serf_request_t *request,
181 serf_bucket_t *stream,
182 void *acceptor_baton,
183 apr_pool_t *pool)
184 {
185 serf_bucket_t *c;
186 serf_bucket_t *response;
187 serf_bucket_alloc_t *bkt_alloc;
188 app_baton_t *app_ctx = acceptor_baton;
189
190 /* get the per-request bucket allocator */
191 bkt_alloc = serf_request_get_alloc(request);
192
193 /* Create a barrier so the response doesn't eat us! */
194 c = serf_bucket_barrier_create(stream, bkt_alloc);
195
196 response = serf_bucket_response_create(c, bkt_alloc);
197
198 if (app_ctx->head_request)
199 serf_bucket_response_set_head(response);
200
201 return response;
202 }
203
204 typedef struct {
205 #if APR_MAJOR_VERSION > 0
206 apr_uint32_t completed_requests;
207 #else
208 apr_atomic_t completed_requests;
209 #endif
210 int print_headers;
211 apr_file_t *output_file;
212
213 serf_response_acceptor_t acceptor;
214 app_baton_t *acceptor_baton;
215
216 serf_response_handler_t handler;
217
218 const char *host;
219 const char *method;
220 const char *path;
221 const char *req_body_path;
222 const char *username;
223 const char *password;
224 int auth_attempts;
225 serf_bucket_t *req_hdrs;
226 } handler_baton_t;
227
228 /* Kludges for APR 0.9 support. */
229 #if APR_MAJOR_VERSION == 0
230 #define apr_atomic_inc32 apr_atomic_inc
231 #define apr_atomic_dec32 apr_atomic_dec
232 #define apr_atomic_read32 apr_atomic_read
233 #endif
234
235
236 static int append_request_headers(void *baton,
237 const char *key,
238 const char *value)
239 {
240 serf_bucket_t *hdrs_bkt = baton;
241 serf_bucket_headers_setc(hdrs_bkt, key, value);
242 return 0;
243 }
244
245 static apr_status_t setup_request(serf_request_t *request,
246 void *setup_baton,
247 serf_bucket_t **req_bkt,
248 serf_response_acceptor_t *acceptor,
249 void **acceptor_baton,
250 serf_response_handler_t *handler,
251 void **handler_baton,
252 apr_pool_t *pool)
253 {
254 handler_baton_t *ctx = setup_baton;
255 serf_bucket_t *hdrs_bkt;
256 serf_bucket_t *body_bkt;
257
258 if (ctx->req_body_path) {
259 apr_file_t *file;
260 apr_status_t status;
261
262 status = apr_file_open(&file, ctx->req_body_path, APR_READ,
263 APR_OS_DEFAULT, pool);
264
265 if (status) {
266 printf("Error opening file (%s)\n", ctx->req_body_path);
267 return status;
268 }
269
270 body_bkt = serf_bucket_file_create(file,
271 serf_request_get_alloc(request));
272 }
273 else {
274 body_bkt = NULL;
275 }
276
277 *req_bkt = serf_request_bucket_request_create(request, ctx->method,
278 ctx->path, body_bkt,
279 serf_request_get_alloc(request));
280
281 hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
282
283 serf_bucket_headers_setn(hdrs_bkt, "User-Agent",
284 "Serf/" SERF_VERSION_STRING);
285 /* Shouldn't serf do this for us? */
286 serf_bucket_headers_setn(hdrs_bkt, "Accept-Encoding", "gzip");
287 #ifdef CONNECTION_CLOSE_HDR
288 serf_bucket_headers_setn(hdrs_bkt, "Connection", "close");
289 #endif
290
291 /* Add the extra headers from the command line */
292 if (ctx->req_hdrs != NULL) {
293 serf_bucket_headers_do(ctx->req_hdrs, append_request_headers, hdrs_bkt);
294 }
295
296 *acceptor = ctx->acceptor;
297 *acceptor_baton = ctx->acceptor_baton;
298 *handler = ctx->handler;
299 *handler_baton = ctx;
300
301 return APR_SUCCESS;
302 }
303
304 static apr_status_t handle_response(serf_request_t *request,
305 serf_bucket_t *response,
306 void *handler_baton,
307 apr_pool_t *pool)
308 {
309 serf_status_line sl;
310 apr_status_t status;
311 handler_baton_t *ctx = handler_baton;
312
313 if (!response) {
314 /* A NULL response probably means that the connection was closed while
315 this request was already written. Just requeue it. */
316 serf_connection_t *conn = serf_request_get_conn(request);
317
318 serf_connection_request_create(conn, setup_request, handler_baton);
319 return APR_SUCCESS;
320 }
321
322 status = serf_bucket_response_status(response, &sl);
323 if (status) {
324 return status;
325 }
326
327 while (1) {
328 struct iovec vecs[64];
329 int vecs_read;
330 apr_size_t bytes_written;
331
332 status = serf_bucket_read_iovec(response, 8000, 64, vecs, &vecs_read);
333 if (SERF_BUCKET_READ_ERROR(status))
334 return status;
335
336 /* got some data. print it out. */
337 if (vecs_read) {
338 apr_file_writev(ctx->output_file, vecs, vecs_read, &bytes_written);
339 }
340
341 /* are we done yet? */
342 if (APR_STATUS_IS_EOF(status)) {
343 if (ctx->print_headers) {
344 serf_bucket_t *hdrs;
345 hdrs = serf_bucket_response_get_headers(response);
346 while (1) {
347 status = serf_bucket_read_iovec(hdrs, 8000, 64, vecs,
348 &vecs_read);
349
350 if (SERF_BUCKET_READ_ERROR(status))
351 return status;
352
353 if (vecs_read) {
354 apr_file_writev(ctx->output_file, vecs, vecs_read,
355 &bytes_written);
356 }
357 if (APR_STATUS_IS_EOF(status)) {
358 break;
359 }
360 }
361 }
362
363 apr_atomic_inc32(&ctx->completed_requests);
364 return APR_EOF;
365 }
366
367 /* have we drained the response so far? */
368 if (APR_STATUS_IS_EAGAIN(status))
369 return status;
370
371 /* loop to read some more. */
372 }
373 /* NOTREACHED */
374 }
375
376 static apr_status_t
377 credentials_callback(char **username,
378 char **password,
379 serf_request_t *request, void *baton,
380 int code, const char *authn_type,
381 const char *realm,
382 apr_pool_t *pool)
383 {
384 handler_baton_t *ctx = baton;
385
386 if (ctx->auth_attempts > 0)
387 {
388 return SERF_ERROR_AUTHN_FAILED;
389 }
390 else
391 {
392 *username = (char*)ctx->username;
393 *password = (char*)ctx->password;
394 ctx->auth_attempts++;
395
396 return APR_SUCCESS;
397 }
398 }
399
400 static void print_usage(apr_pool_t *pool)
401 {
402 puts("serf_get [options] URL");
403 puts("-h\tDisplay this help");
404 puts("-v\tDisplay version");
405 puts("-H\tPrint response headers");
406 puts("-n <count> Fetch URL <count> times");
407 puts("-x <count> Number of maximum outstanding requests inflight");
408 puts("-U <user> Username for Basic/Digest authentication");
409 puts("-P <password> Password for Basic/Digest authentication");
410 puts("-m <method> Use the <method> HTTP Method");
411 puts("-f <file> Use the <file> as the request body");
412 puts("-p <hostname:port> Use the <host:port> as proxy server");
413 puts("-r <header:value> Use <header:value> as request header");
414 }
415
416 int main(int argc, const char **argv)
417 {
418 apr_status_t status;
419 apr_pool_t *pool;
420 serf_bucket_alloc_t *bkt_alloc;
421 serf_context_t *context;
422 serf_connection_t *connection;
423 serf_request_t *request;
424 app_baton_t app_ctx;
425 handler_baton_t handler_ctx;
426 serf_bucket_t *req_hdrs = NULL;
427 apr_uri_t url;
428 const char *proxy = NULL;
429 const char *raw_url, *method, *req_body_path = NULL;
430 int count, inflight;
431 int i;
432 int print_headers;
433 const char *username = NULL;
434 const char *password = "";
435 apr_getopt_t *opt;
436 char opt_c;
437 const char *opt_arg;
438
439 apr_initialize();
440 atexit(apr_terminate);
441
442 apr_pool_create(&pool, NULL);
443 /* serf_initialize(); */
444 bkt_alloc = serf_bucket_allocator_create(pool, NULL, NULL);
445
446 /* Default to one round of fetching with no limit to max inflight reqs. */
447 count = 1;
448 inflight = 0;
449 /* Default to GET. */
450 method = "GET";
451 /* Do not print headers by default. */
452 print_headers = 0;
453
454 apr_getopt_init(&opt, pool, argc, argv);
455
456 while ((status = apr_getopt(opt, "U:P:f:hHm:n:vp:x:r:", &opt_c, &opt_arg)) ==
457 APR_SUCCESS) {
458
459 switch (opt_c) {
460 case 'U':
461 username = opt_arg;
462 break;
463 case 'P':
464 password = opt_arg;
465 break;
466 case 'f':
467 req_body_path = opt_arg;
468 break;
469 case 'h':
470 print_usage(pool);
471 exit(0);
472 break;
473 case 'H':
474 print_headers = 1;
475 break;
476 case 'm':
477 method = opt_arg;
478 break;
479 case 'n':
480 errno = 0;
481 count = apr_strtoi64(opt_arg, NULL, 10);
482 if (errno) {
483 printf("Problem converting number of times to fetch URL (%d)\n",
484 errno);
485 return errno;
486 }
487 break;
488 case 'x':
489 errno = 0;
490 inflight = apr_strtoi64(opt_arg, NULL, 10);
491 if (errno) {
492 printf("Problem converting number of requests to have outstanding (%d)\n",
493 errno);
494 return errno;
495 }
496 break;
497 case 'p':
498 proxy = opt_arg;
499 break;
500 case 'r':
501 {
502 char *sep;
503 char *hdr_val;
504
505 if (req_hdrs == NULL) {
506 /* first request header, allocate bucket */
507 req_hdrs = serf_bucket_headers_create(bkt_alloc);
508 }
509 sep = strchr(opt_arg, ':');
510 if ((sep == NULL) || (sep == opt_arg) || (strlen(sep) <= 1)) {
511 printf("Invalid request header string (%s)\n", opt_arg);
512 return EINVAL;
513 }
514 hdr_val = sep + 1;
515 while (*hdr_val == ' ') {
516 hdr_val++;
517 }
518 serf_bucket_headers_setx(req_hdrs, opt_arg, (sep - opt_arg), 1,
519 hdr_val, strlen(hdr_val), 1);
520 }
521 break;
522 case 'v':
523 puts("Serf version: " SERF_VERSION_STRING);
524 exit(0);
525 default:
526 break;
527 }
528 }
529
530 if (opt->ind != opt->argc - 1) {
531 print_usage(pool);
532 exit(-1);
533 }
534
535 raw_url = argv[opt->ind];
536
537 apr_uri_parse(pool, raw_url, &url);
538 if (!url.port) {
539 url.port = apr_uri_port_of_scheme(url.scheme);
540 }
541 if (!url.path) {
542 url.path = "/";
543 }
544
545 if (strcasecmp(url.scheme, "https") == 0) {
546 app_ctx.using_ssl = 1;
547 }
548 else {
549 app_ctx.using_ssl = 0;
550 }
551
552 if (strcasecmp(method, "HEAD") == 0) {
553 app_ctx.head_request = 1;
554 }
555 else {
556 app_ctx.head_request = 0;
557 }
558
559 app_ctx.hostinfo = url.hostinfo;
560
561 context = serf_context_create(pool);
562
563 if (proxy)
564 {
565 apr_sockaddr_t *proxy_address = NULL;
566 apr_port_t proxy_port;
567 char *proxy_host;
568 char *proxy_scope;
569
570 status = apr_parse_addr_port(&proxy_host, &proxy_scope, &proxy_port, proxy, pool);
571 if (status)
572 {
573 printf("Cannot parse proxy hostname/port: %d\n", status);
574 apr_pool_destroy(pool);
575 exit(1);
576 }
577
578 if (!proxy_host)
579 {
580 printf("Proxy hostname must be specified\n");
581 apr_pool_destroy(pool);
582 exit(1);
583 }
584
585 if (!proxy_port)
586 {
587 printf("Proxy port must be specified\n");
588 apr_pool_destroy(pool);
589 exit(1);
590 }
591
592 status = apr_sockaddr_info_get(&proxy_address, proxy_host, APR_UNSPEC,
593 proxy_port, 0, pool);
594
595 if (status)
596 {
597 printf("Cannot resolve proxy address '%s': %d\n", proxy_host, status);
598 apr_pool_destroy(pool);
599 exit(1);
600 }
601
602 serf_config_proxy(context, proxy_address);
603 }
604
605 if (username)
606 {
607 serf_config_authn_types(context, SERF_AUTHN_ALL);
608 }
609 else
610 {
611 serf_config_authn_types(context, SERF_AUTHN_NTLM | SERF_AUTHN_NEGOTIATE);
612 }
613
614 serf_config_credentials_callback(context, credentials_callback);
615
616 /* ### Connection or Context should have an allocator? */
617 app_ctx.bkt_alloc = bkt_alloc;
618 app_ctx.ssl_ctx = NULL;
619
620 status = serf_connection_create2(&connection, context, url,
621 conn_setup, &app_ctx,
622 closed_connection, &app_ctx,
623 pool);
624 if (status) {
625 printf("Error creating connection: %d\n", status);
626 apr_pool_destroy(pool);
627 exit(1);
628 }
629
630 handler_ctx.completed_requests = 0;
631 handler_ctx.print_headers = print_headers;
632 apr_file_open_stdout(&handler_ctx.output_file, pool);
633
634 handler_ctx.host = url.hostinfo;
635 handler_ctx.method = method;
636 handler_ctx.path = apr_pstrcat(pool,
637 url.path,
638 url.query ? "?" : "",
639 url.query ? url.query : "",
640 NULL);
641 handler_ctx.username = username;
642 handler_ctx.password = password;
643 handler_ctx.auth_attempts = 0;
644
645 handler_ctx.req_body_path = req_body_path;
646
647 handler_ctx.acceptor = accept_response;
648 handler_ctx.acceptor_baton = &app_ctx;
649 handler_ctx.handler = handle_response;
650 handler_ctx.req_hdrs = req_hdrs;
651
652 serf_connection_set_max_outstanding_requests(connection, inflight);
653
654 for (i = 0; i < count; i++) {
655 request = serf_connection_request_create(connection, setup_request,
656 &handler_ctx);
657 }
658
659 while (1) {
660 status = serf_context_run(context, SERF_DURATION_FOREVER, pool);
661 if (APR_STATUS_IS_TIMEUP(status))
662 continue;
663 if (status) {
664 char buf[200];
665 const char *err_string;
666 err_string = serf_error_string(status);
667 if (!err_string) {
668 err_string = apr_strerror(status, buf, sizeof(buf));
669 }
670
671 printf("Error running context: (%d) %s\n", status, err_string);
672 apr_pool_destroy(pool);
673 exit(1);
674 }
675 if (apr_atomic_read32(&handler_ctx.completed_requests) >= count) {
676 break;
677 }
678 /* Debugging purposes only! */
679 serf_debug__closed_conn(app_ctx.bkt_alloc);
680 }
681
682 serf_connection_close(connection);
683
684 apr_pool_destroy(pool);
685 return 0;
686 }

Properties

Name Value
svn:keywords MidnightBSD=%H