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)
47
/* In a real application, you would normally would not want to do this */
51
static apr_status_t conn_setup(apr_socket_t *skt,
52
serf_bucket_t **input_bkt,
53
serf_bucket_t **output_bkt,
54
63
if (!ctx->ssl_ctx) {
55
64
ctx->ssl_ctx = serf_bucket_ssl_decrypt_context_get(c);
66
serf_ssl_server_cert_callback_set(ctx->ssl_ctx, ignore_all_cert_errors, NULL);
68
*output_bkt = serf_bucket_ssl_encrypt_create(*output_bkt, ctx->ssl_ctx,
62
77
static serf_bucket_t* accept_response(serf_request_t *request,
80
95
#if APR_MAJOR_VERSION > 0
81
apr_uint32_t requests_outstanding;
96
apr_uint32_t completed_requests;
83
apr_atomic_t requests_outstanding;
98
apr_atomic_t completed_requests;
85
100
int print_headers;
114
129
apr_status_t status;
115
130
handler_baton_t *ctx = handler_baton;
133
/* A NULL response can come back if the request failed completely */
117
136
status = serf_bucket_response_status(response, &sl);
119
if (APR_STATUS_IS_EAGAIN(status)) {
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,
213
serf_request_get_alloc(request));
198
215
hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
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);
211
apr_atomic_inc32(&(ctx->requests_outstanding));
213
if (ctx->acceptor_baton->using_ssl) {
214
serf_bucket_alloc_t *req_alloc;
215
app_baton_t *app_ctx = ctx->acceptor_baton;
217
req_alloc = serf_request_get_alloc(request);
219
if (app_ctx->ssl_ctx == NULL) {
221
serf_bucket_ssl_encrypt_create(*req_bkt, NULL,
224
serf_bucket_ssl_encrypt_context_get(*req_bkt);
228
serf_bucket_ssl_encrypt_create(*req_bkt, app_ctx->ssl_ctx,
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");
253
247
int main(int argc, const char **argv)
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;
257
const char *proxy = NULL;
264
258
const char *raw_url, *method, *req_body_path = NULL;
287
280
apr_getopt_init(&opt, pool, argc, argv);
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)) ==
291
284
int srclen, enclen;
350
346
app_ctx.using_ssl = 0;
353
status = apr_sockaddr_info_get(&address,
354
url.hostname, APR_UNSPEC, url.port, 0,
357
printf("Error creating address: %d\n", status);
361
349
context = serf_context_create(pool);
353
apr_sockaddr_t *proxy_address = NULL;
354
apr_port_t proxy_port;
358
status = apr_parse_addr_port(&proxy_host, &proxy_scope, &proxy_port, proxy, pool);
361
printf("Cannot parse proxy hostname/port: %d\n", status);
362
apr_pool_destroy(pool);
368
printf("Proxy hostname must be specified\n");
369
apr_pool_destroy(pool);
375
printf("Proxy port must be specified\n");
376
apr_pool_destroy(pool);
380
status = apr_sockaddr_info_get(&proxy_address, proxy_host, APR_UNSPEC,
381
proxy_port, 0, pool);
385
printf("Cannot resolve proxy address '%s': %d\n", proxy_host, status);
386
apr_pool_destroy(pool);
390
serf_config_proxy(context, proxy_address);
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;
367
connection = serf_connection_create(context, address,
368
conn_setup, &app_ctx,
369
closed_connection, &app_ctx,
397
status = serf_connection_create2(&connection, context, url,
398
conn_setup, &app_ctx,
399
closed_connection, &app_ctx,
402
printf("Error creating connection: %d\n", status);
403
apr_pool_destroy(pool);
372
handler_ctx.requests_outstanding = 0;
407
handler_ctx.completed_requests = 0;
373
408
handler_ctx.print_headers = print_headers;
375
410
handler_ctx.host = url.hostinfo;
398
433
printf("Error running context: (%d) %s\n", status,
399
434
apr_strerror(status, buf, sizeof(buf)));
435
apr_pool_destroy(pool);
402
if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
438
if (apr_atomic_read32(&handler_ctx.completed_requests) >= count) {
405
441
/* Debugging purposes only! */