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_version.h>
26 #define SERF_VERSION_STRING "0.01"
29 const char *resp_file;
33 static serf_bucket_t* accept_response(void *acceptor_baton,
34 serf_bucket_alloc_t *bkt_alloc,
37 accept_baton_t *ctx = acceptor_baton;
42 status = apr_file_open(&file, ctx->resp_file,
43 APR_READ, APR_OS_DEFAULT, pool);
48 c = ctx->bkt = serf_bucket_file_create(file, bkt_alloc);
50 c = serf_bucket_barrier_create(c, bkt_alloc);
52 return serf_bucket_response_create(c, bkt_alloc);
56 #if APR_MAJOR_VERSION > 0
57 apr_uint32_t requests_outstanding;
59 apr_atomic_t requests_outstanding;
63 /* Kludges for APR 0.9 support. */
64 #if APR_MAJOR_VERSION == 0
65 #define apr_atomic_inc32 apr_atomic_inc
66 #define apr_atomic_dec32 apr_atomic_dec
67 #define apr_atomic_read32 apr_atomic_read
70 static apr_status_t handle_response(serf_request_t *request,
71 serf_bucket_t *response,
79 handler_baton_t *ctx = handler_baton;
81 status = serf_bucket_response_status(response, &sl);
83 if (APR_STATUS_IS_EAGAIN(status)) {
89 status = serf_bucket_read(response, 2048, &data, &len);
91 if (!status || APR_STATUS_IS_EOF(status)) {
93 s = apr_pstrmemdup(pool, data, len);
97 else if (APR_STATUS_IS_EAGAIN(status)) {
100 if (APR_STATUS_IS_EOF(status)) {
104 hdrs = serf_bucket_response_get_headers(response);
105 v = serf_bucket_headers_get(hdrs, "Trailer-Test");
107 printf("Trailer-Test: %s\n", v);
110 apr_atomic_dec32(&ctx->requests_outstanding);
116 int main(int argc, const char **argv)
120 serf_bucket_t *resp_bkt;
121 accept_baton_t accept_ctx;
122 handler_baton_t handler_ctx;
123 serf_bucket_alloc_t *allocator;
126 printf("%s: [Resp. File]\n", argv[0]);
129 accept_ctx.resp_file = argv[1];
132 atexit(apr_terminate);
134 apr_pool_create(&pool, NULL);
135 apr_atomic_init(pool);
136 /* serf_initialize(); */
138 allocator = serf_bucket_allocator_create(pool, NULL, NULL);
140 handler_ctx.requests_outstanding = 0;
141 apr_atomic_inc32(&handler_ctx.requests_outstanding);
143 resp_bkt = accept_response(&accept_ctx, allocator, pool);
145 status = handle_response(NULL, resp_bkt, &handler_ctx, pool);
146 if (APR_STATUS_IS_TIMEUP(status))
148 if (SERF_BUCKET_READ_ERROR(status)) {
149 printf("Error running context: %d\n", status);
152 if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
156 serf_bucket_destroy(resp_bkt);
157 serf_bucket_destroy(accept_ctx.bkt);
159 apr_pool_destroy(pool);