OSDN Git Service

* Added serf library.
[modchxj/mod_chxj.git] / src / serf / test / serf_response.c
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <stdlib.h>
17
18 #include <apr.h>
19 #include <apr_uri.h>
20 #include <apr_strings.h>
21 #include <apr_atomic.h>
22 #include <apr_version.h>
23
24 #include "serf.h"
25
26 #define SERF_VERSION_STRING "0.01"
27
28 typedef struct {
29     const char *resp_file;
30     serf_bucket_t *bkt;
31 } accept_baton_t;
32
33 static serf_bucket_t* accept_response(void *acceptor_baton,
34                                       serf_bucket_alloc_t *bkt_alloc,
35                                       apr_pool_t *pool)
36 {
37     accept_baton_t *ctx = acceptor_baton;
38     serf_bucket_t *c;
39     apr_file_t *file;
40     apr_status_t status;
41
42     status = apr_file_open(&file, ctx->resp_file,
43                            APR_READ, APR_OS_DEFAULT, pool);
44     if (status) {
45         return NULL;
46     }
47
48     c = ctx->bkt = serf_bucket_file_create(file, bkt_alloc);
49
50     c = serf_bucket_barrier_create(c, bkt_alloc);
51
52     return serf_bucket_response_create(c, bkt_alloc);
53 }
54
55 typedef struct {
56 #if APR_MAJOR_VERSION > 0
57     apr_uint32_t requests_outstanding;
58 #else
59     apr_atomic_t requests_outstanding;
60 #endif
61 } handler_baton_t;
62
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
68 #endif
69
70 static apr_status_t handle_response(serf_request_t *request,
71                                     serf_bucket_t *response,
72                                     void *handler_baton,
73                                     apr_pool_t *pool)
74 {
75     const char *data, *s;
76     apr_size_t len;
77     serf_status_line sl;
78     apr_status_t status;
79     handler_baton_t *ctx = handler_baton;
80
81     status = serf_bucket_response_status(response, &sl);
82     if (status) {
83         if (APR_STATUS_IS_EAGAIN(status)) {
84             return APR_SUCCESS;
85         }
86         abort();
87     }
88
89     status = serf_bucket_read(response, 2048, &data, &len);
90
91     if (!status || APR_STATUS_IS_EOF(status)) {
92         if (len) {
93             s = apr_pstrmemdup(pool, data, len);
94             printf("%s", s);
95         }
96     }
97     else if (APR_STATUS_IS_EAGAIN(status)) {
98         status = APR_SUCCESS;
99     }
100     if (APR_STATUS_IS_EOF(status)) {
101         serf_bucket_t *hdrs;
102         const char *v;
103
104         hdrs = serf_bucket_response_get_headers(response);
105         v = serf_bucket_headers_get(hdrs, "Trailer-Test");
106         if (v) {
107             printf("Trailer-Test: %s\n", v);
108         }
109
110         apr_atomic_dec32(&ctx->requests_outstanding);
111     }
112
113     return status;
114 }
115
116 int main(int argc, const char **argv)
117 {
118     apr_status_t status;
119     apr_pool_t *pool;
120     serf_bucket_t *resp_bkt;
121     accept_baton_t accept_ctx;
122     handler_baton_t handler_ctx;
123     serf_bucket_alloc_t *allocator;
124
125     if (argc != 2) {
126         printf("%s: [Resp. File]\n", argv[0]);
127         exit(-1);
128     }
129     accept_ctx.resp_file = argv[1];
130
131     apr_initialize();
132     atexit(apr_terminate);
133
134     apr_pool_create(&pool, NULL);
135     apr_atomic_init(pool);
136     /* serf_initialize(); */
137
138     allocator = serf_bucket_allocator_create(pool, NULL, NULL);
139
140     handler_ctx.requests_outstanding = 0;
141     apr_atomic_inc32(&handler_ctx.requests_outstanding);
142
143     resp_bkt = accept_response(&accept_ctx, allocator, pool);
144     while (1) {
145         status = handle_response(NULL, resp_bkt, &handler_ctx, pool);
146         if (APR_STATUS_IS_TIMEUP(status))
147             continue;
148         if (SERF_BUCKET_READ_ERROR(status)) {
149             printf("Error running context: %d\n", status);
150             exit(1);
151         }
152         if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
153             break;
154         }
155     }
156     serf_bucket_destroy(resp_bkt);
157     serf_bucket_destroy(accept_ctx.bkt);
158
159     apr_pool_destroy(pool);
160
161     return 0;
162 }