~ubuntu-branches/ubuntu/trusty/serf/trusty-security

« back to all changes in this revision

Viewing changes to test/serf_get.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Samuelson
  • Date: 2011-06-03 03:18:07 UTC
  • mfrom: (1.2.4 upstream)
  • mto: (3.3.1 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20110603031807-3vq82t5lzw692our
Tags: 0.7.2-1
* New upstream release.
  - patches/no-export-vars: delete, now upstream.
* New ABI:
  - patches/abi-0.x: New patch to change upstream SONAME.
  - Rename libserf-0-0 to libserf0.7.
  - Rename libserf-0-0-dev to libserf-dev while we're at it.
* Policy 3.9.1: one instance of s/Conflicts/Breaks/.
* "Upgrade" to source format 1.0.
* Add some Depends: ${misc:Depends}; thanks, Lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
    }
42
42
}
43
43
 
44
 
static serf_bucket_t* conn_setup(apr_socket_t *skt,
 
44
static apr_status_t ignore_all_cert_errors(void *data, int failures,
 
45
                                           const serf_ssl_certificate_t *cert)
 
46
{
 
47
    /* In a real application, you would normally would not want to do this */
 
48
    return APR_SUCCESS;
 
49
}
 
50
 
 
51
static apr_status_t conn_setup(apr_socket_t *skt,
 
52
                                serf_bucket_t **input_bkt,
 
53
                                serf_bucket_t **output_bkt,
45
54
                                void *setup_baton,
46
55
                                apr_pool_t *pool)
47
56
{
54
63
        if (!ctx->ssl_ctx) {
55
64
            ctx->ssl_ctx = serf_bucket_ssl_decrypt_context_get(c);
56
65
        }
 
66
        serf_ssl_server_cert_callback_set(ctx->ssl_ctx, ignore_all_cert_errors, NULL);
 
67
 
 
68
        *output_bkt = serf_bucket_ssl_encrypt_create(*output_bkt, ctx->ssl_ctx,
 
69
                                                    ctx->bkt_alloc);
57
70
    }
58
71
 
59
 
    return c;
 
72
    *input_bkt = c;
 
73
 
 
74
    return APR_SUCCESS;
60
75
}
61
76
 
62
77
static serf_bucket_t* accept_response(serf_request_t *request,
78
93
 
79
94
typedef struct {
80
95
#if APR_MAJOR_VERSION > 0
81
 
    apr_uint32_t requests_outstanding;
 
96
    apr_uint32_t completed_requests;
82
97
#else
83
 
    apr_atomic_t requests_outstanding;
 
98
    apr_atomic_t completed_requests;
84
99
#endif
85
100
    int print_headers;
86
101
 
114
129
    apr_status_t status;
115
130
    handler_baton_t *ctx = handler_baton;
116
131
 
 
132
    if (!response) {
 
133
        /* A NULL response can come back if the request failed completely */
 
134
        return APR_EGENERAL;
 
135
    }
117
136
    status = serf_bucket_response_status(response, &sl);
118
137
    if (status) {
119
 
        if (APR_STATUS_IS_EAGAIN(status)) {
120
 
            return status;
121
 
        }
122
 
        abort();
 
138
        return status;
123
139
    }
124
140
 
125
141
    while (1) {
147
163
                }
148
164
            }
149
165
 
150
 
            apr_atomic_dec32(&ctx->requests_outstanding);
 
166
            apr_atomic_inc32(&ctx->completed_requests);
151
167
            return APR_EOF;
152
168
        }
153
169
 
192
208
        body_bkt = NULL;
193
209
    }
194
210
 
195
 
    *req_bkt = serf_bucket_request_create(ctx->method, ctx->path, body_bkt,
196
 
                                          serf_request_get_alloc(request));
 
211
    *req_bkt = serf_request_bucket_request_create(request, ctx->method,
 
212
                                                  ctx->path, body_bkt,
 
213
                                                  serf_request_get_alloc(request));
197
214
 
198
215
    hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
199
216
 
200
 
    /* FIXME: Shouldn't we be able to figure out the host ourselves? */
201
 
    serf_bucket_headers_setn(hdrs_bkt, "Host", ctx->host);
202
217
    serf_bucket_headers_setn(hdrs_bkt, "User-Agent",
203
218
                             "Serf/" SERF_VERSION_STRING);
204
219
    /* Shouldn't serf do this for us? */
208
223
        serf_bucket_headers_setn(hdrs_bkt, "Authorization", ctx->authn);
209
224
    }
210
225
 
211
 
    apr_atomic_inc32(&(ctx->requests_outstanding));
212
 
 
213
 
    if (ctx->acceptor_baton->using_ssl) {
214
 
        serf_bucket_alloc_t *req_alloc;
215
 
        app_baton_t *app_ctx = ctx->acceptor_baton;
216
 
 
217
 
        req_alloc = serf_request_get_alloc(request);
218
 
 
219
 
        if (app_ctx->ssl_ctx == NULL) {
220
 
            *req_bkt =
221
 
                serf_bucket_ssl_encrypt_create(*req_bkt, NULL,
222
 
                                               app_ctx->bkt_alloc);
223
 
            app_ctx->ssl_ctx =
224
 
                serf_bucket_ssl_encrypt_context_get(*req_bkt);
225
 
        }
226
 
        else {
227
 
            *req_bkt =
228
 
                serf_bucket_ssl_encrypt_create(*req_bkt, app_ctx->ssl_ctx,
229
 
                                               app_ctx->bkt_alloc);
230
 
        }
231
 
    }
232
 
 
233
226
    *acceptor = ctx->acceptor;
234
227
    *acceptor_baton = ctx->acceptor_baton;
235
228
    *handler = ctx->handler;
248
241
    puts("-a <user:password> Present Basic authentication credentials");
249
242
    puts("-m <method> Use the <method> HTTP Method");
250
243
    puts("-f <file> Use the <file> as the request body");
 
244
    puts("-p <hostname:port> Use the <host:port> as proxy server");
251
245
}
252
246
 
253
247
int main(int argc, const char **argv)
254
248
{
255
249
    apr_status_t status;
256
250
    apr_pool_t *pool;
257
 
    apr_sockaddr_t *address;
258
251
    serf_context_t *context;
259
252
    serf_connection_t *connection;
260
253
    serf_request_t *request;
261
254
    app_baton_t app_ctx;
262
255
    handler_baton_t handler_ctx;
263
256
    apr_uri_t url;
 
257
    const char *proxy = NULL;
264
258
    const char *raw_url, *method, *req_body_path = NULL;
265
259
    int count;
266
260
    int i;
274
268
    atexit(apr_terminate);
275
269
 
276
270
    apr_pool_create(&pool, NULL);
277
 
    apr_atomic_init(pool);
278
271
    /* serf_initialize(); */
279
272
 
280
273
    /* Default to one round of fetching. */
286
279
 
287
280
    apr_getopt_init(&opt, pool, argc, argv);
288
281
 
289
 
    while ((status = apr_getopt(opt, "a:f:hHm:n:v", &opt_c, &opt_arg)) ==
 
282
    while ((status = apr_getopt(opt, "a:f:hHm:n:vp:", &opt_c, &opt_arg)) ==
290
283
           APR_SUCCESS) {
291
284
        int srclen, enclen;
292
285
 
320
313
                return errno;
321
314
            }
322
315
            break;
 
316
        case 'p':
 
317
            proxy = opt_arg;
 
318
            break;
323
319
        case 'v':
324
320
            puts("Serf version: " SERF_VERSION_STRING);
325
321
            exit(0);
350
346
        app_ctx.using_ssl = 0;
351
347
    }
352
348
 
353
 
    status = apr_sockaddr_info_get(&address,
354
 
                                   url.hostname, APR_UNSPEC, url.port, 0,
355
 
                                   pool);
356
 
    if (status) {
357
 
        printf("Error creating address: %d\n", status);
358
 
        exit(1);
359
 
    }
360
 
 
361
349
    context = serf_context_create(pool);
362
350
 
 
351
    if (proxy)
 
352
    {
 
353
        apr_sockaddr_t *proxy_address = NULL;
 
354
        apr_port_t proxy_port;
 
355
        char *proxy_host;
 
356
        char *proxy_scope;
 
357
 
 
358
        status = apr_parse_addr_port(&proxy_host, &proxy_scope, &proxy_port, proxy, pool);
 
359
        if (status)
 
360
        {
 
361
            printf("Cannot parse proxy hostname/port: %d\n", status);
 
362
            apr_pool_destroy(pool);
 
363
            exit(1);
 
364
        }
 
365
 
 
366
        if (!proxy_host)
 
367
        {
 
368
            printf("Proxy hostname must be specified\n");
 
369
            apr_pool_destroy(pool);
 
370
            exit(1);
 
371
        }
 
372
 
 
373
        if (!proxy_port)
 
374
        {
 
375
            printf("Proxy port must be specified\n");
 
376
            apr_pool_destroy(pool);
 
377
            exit(1);
 
378
        }
 
379
 
 
380
        status = apr_sockaddr_info_get(&proxy_address, proxy_host, APR_UNSPEC,
 
381
                                       proxy_port, 0, pool);
 
382
 
 
383
        if (status)
 
384
        {
 
385
            printf("Cannot resolve proxy address '%s': %d\n", proxy_host, status);
 
386
            apr_pool_destroy(pool);
 
387
            exit(1);
 
388
        }
 
389
 
 
390
        serf_config_proxy(context, proxy_address);
 
391
    }
 
392
 
363
393
    /* ### Connection or Context should have an allocator? */
364
394
    app_ctx.bkt_alloc = serf_bucket_allocator_create(pool, NULL, NULL);
365
395
    app_ctx.ssl_ctx = NULL;
366
396
 
367
 
    connection = serf_connection_create(context, address,
368
 
                                        conn_setup, &app_ctx,
369
 
                                        closed_connection, &app_ctx,
370
 
                                        pool);
 
397
    status = serf_connection_create2(&connection, context, url,
 
398
                                     conn_setup, &app_ctx,
 
399
                                     closed_connection, &app_ctx,
 
400
                                     pool);
 
401
    if (status) {
 
402
        printf("Error creating connection: %d\n", status);
 
403
        apr_pool_destroy(pool);
 
404
        exit(1);
 
405
    }
371
406
 
372
 
    handler_ctx.requests_outstanding = 0;
 
407
    handler_ctx.completed_requests = 0;
373
408
    handler_ctx.print_headers = print_headers;
374
409
 
375
410
    handler_ctx.host = url.hostinfo;
397
432
 
398
433
            printf("Error running context: (%d) %s\n", status,
399
434
                   apr_strerror(status, buf, sizeof(buf)));
 
435
            apr_pool_destroy(pool);
400
436
            exit(1);
401
437
        }
402
 
        if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
 
438
        if (apr_atomic_read32(&handler_ctx.completed_requests) >= count) {
403
439
            break;
404
440
        }
405
441
        /* Debugging purposes only! */