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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * ====================================================================
21 #include <apr_pools.h>
22 #include <apr_strings.h>
27 #include "serf_bucket_util.h"
28 #include "serf_bucket_types.h"
32 /* This is an implementation of Bidirectional Web Transfer Protocol (BWTP)
34 * http://bwtp.wikidot.com/
40 int type; /* 0 = header, 1 = message */ /* TODO enum? */
42 serf_bucket_t *headers;
48 serf_bucket_t *stream;
49 serf_bucket_t *body; /* Pointer to the stream wrapping the body. */
50 serf_bucket_t *headers; /* holds parsed headers */
53 STATE_STATUS_LINE, /* reading status line */
54 STATE_HEADERS, /* reading headers */
55 STATE_BODY, /* reading body */
56 STATE_DONE /* we've sent EOF */
59 /* Buffer for accumulating a line from the response. */
60 serf_linebuf_t linebuf;
62 int type; /* 0 = header, 1 = message */ /* TODO enum? */
69 serf_bucket_t *serf_bucket_bwtp_channel_close(
71 serf_bucket_alloc_t *allocator)
75 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
78 ctx->channel = channel;
79 ctx->phrase = "CLOSED";
80 ctx->headers = serf_bucket_headers_create(allocator);
82 return serf_bucket_create(&serf_bucket_type_bwtp_frame, allocator, ctx);
85 serf_bucket_t *serf_bucket_bwtp_channel_open(
88 serf_bucket_alloc_t *allocator)
92 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
95 ctx->channel = channel;
97 ctx->headers = serf_bucket_headers_create(allocator);
99 return serf_bucket_create(&serf_bucket_type_bwtp_frame, allocator, ctx);
102 serf_bucket_t *serf_bucket_bwtp_header_create(
105 serf_bucket_alloc_t *allocator)
107 frame_context_t *ctx;
109 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
112 ctx->channel = channel;
113 ctx->phrase = phrase;
114 ctx->headers = serf_bucket_headers_create(allocator);
116 return serf_bucket_create(&serf_bucket_type_bwtp_frame, allocator, ctx);
119 serf_bucket_t *serf_bucket_bwtp_message_create(
122 serf_bucket_alloc_t *allocator)
124 frame_context_t *ctx;
126 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
129 ctx->channel = channel;
130 ctx->phrase = "MESSAGE";
131 ctx->headers = serf_bucket_headers_create(allocator);
133 return serf_bucket_create(&serf_bucket_type_bwtp_frame, allocator, ctx);
136 int serf_bucket_bwtp_frame_get_channel(
137 serf_bucket_t *bucket)
139 if (SERF_BUCKET_IS_BWTP_FRAME(bucket)) {
140 frame_context_t *ctx = bucket->data;
144 else if (SERF_BUCKET_IS_BWTP_INCOMING_FRAME(bucket)) {
145 incoming_context_t *ctx = bucket->data;
153 int serf_bucket_bwtp_frame_get_type(
154 serf_bucket_t *bucket)
156 if (SERF_BUCKET_IS_BWTP_FRAME(bucket)) {
157 frame_context_t *ctx = bucket->data;
161 else if (SERF_BUCKET_IS_BWTP_INCOMING_FRAME(bucket)) {
162 incoming_context_t *ctx = bucket->data;
170 const char *serf_bucket_bwtp_frame_get_phrase(
171 serf_bucket_t *bucket)
173 if (SERF_BUCKET_IS_BWTP_FRAME(bucket)) {
174 frame_context_t *ctx = bucket->data;
178 else if (SERF_BUCKET_IS_BWTP_INCOMING_FRAME(bucket)) {
179 incoming_context_t *ctx = bucket->data;
187 serf_bucket_t *serf_bucket_bwtp_frame_get_headers(
188 serf_bucket_t *bucket)
190 if (SERF_BUCKET_IS_BWTP_FRAME(bucket)) {
191 frame_context_t *ctx = bucket->data;
195 else if (SERF_BUCKET_IS_BWTP_INCOMING_FRAME(bucket)) {
196 incoming_context_t *ctx = bucket->data;
204 static int count_size(void *baton, const char *key, const char *value)
206 apr_size_t *c = baton;
207 /* TODO Deal with folding. Yikes. */
209 /* Add in ": " and CRLF - so an extra four bytes. */
210 *c += strlen(key) + strlen(value) + 4;
215 static apr_size_t calc_header_size(serf_bucket_t *hdrs)
219 serf_bucket_headers_do(hdrs, count_size, &size);
224 static void serialize_data(serf_bucket_t *bucket)
226 frame_context_t *ctx = bucket->data;
227 serf_bucket_t *new_bucket;
230 /* Serialize the request-line and headers into one mother string,
231 * and wrap a bucket around it.
233 req_len = apr_snprintf(ctx->req_line, sizeof(ctx->req_line),
234 "%s %d " "%" APR_UINT64_T_HEX_FMT " %s%s\r\n",
235 (ctx->type ? "BWM" : "BWH"),
236 ctx->channel, calc_header_size(ctx->headers),
237 (ctx->open ? "OPEN " : ""),
239 new_bucket = serf_bucket_simple_copy_create(ctx->req_line, req_len,
242 /* Build up the new bucket structure.
244 * Note that self needs to become an aggregate bucket so that a
245 * pointer to self still represents the "right" data.
247 serf_bucket_aggregate_become(bucket);
249 /* Insert the two buckets. */
250 serf_bucket_aggregate_append(bucket, new_bucket);
251 serf_bucket_aggregate_append(bucket, ctx->headers);
253 /* Our private context is no longer needed, and is not referred to by
254 * any existing bucket. Toss it.
256 serf_bucket_mem_free(bucket->allocator, ctx);
259 static apr_status_t serf_bwtp_frame_read(serf_bucket_t *bucket,
260 apr_size_t requested,
261 const char **data, apr_size_t *len)
263 /* Seralize our private data into a new aggregate bucket. */
264 serialize_data(bucket);
266 /* Delegate to the "new" aggregate bucket to do the read. */
267 return serf_bucket_read(bucket, requested, data, len);
270 static apr_status_t serf_bwtp_frame_readline(serf_bucket_t *bucket,
271 int acceptable, int *found,
272 const char **data, apr_size_t *len)
274 /* Seralize our private data into a new aggregate bucket. */
275 serialize_data(bucket);
277 /* Delegate to the "new" aggregate bucket to do the readline. */
278 return serf_bucket_readline(bucket, acceptable, found, data, len);
281 static apr_status_t serf_bwtp_frame_read_iovec(serf_bucket_t *bucket,
282 apr_size_t requested,
287 /* Seralize our private data into a new aggregate bucket. */
288 serialize_data(bucket);
290 /* Delegate to the "new" aggregate bucket to do the read. */
291 return serf_bucket_read_iovec(bucket, requested,
292 vecs_size, vecs, vecs_used);
295 static apr_status_t serf_bwtp_frame_peek(serf_bucket_t *bucket,
299 /* Seralize our private data into a new aggregate bucket. */
300 serialize_data(bucket);
302 /* Delegate to the "new" aggregate bucket to do the peek. */
303 return serf_bucket_peek(bucket, data, len);
306 const serf_bucket_type_t serf_bucket_type_bwtp_frame = {
308 serf_bwtp_frame_read,
309 serf_bwtp_frame_readline,
310 serf_bwtp_frame_read_iovec,
311 serf_default_read_for_sendfile,
312 serf_default_read_bucket,
313 serf_bwtp_frame_peek,
314 serf_default_destroy_and_data,
318 serf_bucket_t *serf_bucket_bwtp_incoming_frame_create(
319 serf_bucket_t *stream,
320 serf_bucket_alloc_t *allocator)
322 incoming_context_t *ctx;
324 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
325 ctx->stream = stream;
327 ctx->headers = serf_bucket_headers_create(allocator);
328 ctx->state = STATE_STATUS_LINE;
333 serf_linebuf_init(&ctx->linebuf);
335 return serf_bucket_create(&serf_bucket_type_bwtp_incoming_frame, allocator, ctx);
338 static void bwtp_incoming_destroy_and_data(serf_bucket_t *bucket)
340 incoming_context_t *ctx = bucket->data;
342 if (ctx->state != STATE_STATUS_LINE && ctx->phrase) {
343 serf_bucket_mem_free(bucket->allocator, (void*)ctx->phrase);
346 serf_bucket_destroy(ctx->stream);
347 if (ctx->body != NULL)
348 serf_bucket_destroy(ctx->body);
349 serf_bucket_destroy(ctx->headers);
351 serf_default_destroy_and_data(bucket);
354 static apr_status_t fetch_line(incoming_context_t *ctx, int acceptable)
356 return serf_linebuf_fetch(&ctx->linebuf, ctx->stream, acceptable);
359 static apr_status_t parse_status_line(incoming_context_t *ctx,
360 serf_bucket_alloc_t *allocator)
363 char *reason; /* ### stupid APR interface makes this non-const */
365 /* ctx->linebuf.line should be of form: BW* */
366 res = apr_date_checkmask(ctx->linebuf.line, "BW*");
368 /* Not an BWTP response? Well, at least we won't understand it. */
372 if (ctx->linebuf.line[2] == 'H') {
375 else if (ctx->linebuf.line[2] == 'M') {
382 ctx->channel = apr_strtoi64(ctx->linebuf.line + 3, &reason, 16);
384 /* Skip leading spaces for the reason string. */
385 if (apr_isspace(*reason)) {
389 ctx->length = apr_strtoi64(reason, &reason, 16);
391 /* Skip leading spaces for the reason string. */
392 if (reason - ctx->linebuf.line < ctx->linebuf.used) {
393 if (apr_isspace(*reason)) {
397 ctx->phrase = serf_bstrmemdup(allocator, reason,
399 - (reason - ctx->linebuf.line));
407 /* This code should be replaced with header buckets. */
408 static apr_status_t fetch_headers(serf_bucket_t *bkt, incoming_context_t *ctx)
412 /* RFC 2616 says that CRLF is the only line ending, but we can easily
413 * accept any kind of line ending.
415 status = fetch_line(ctx, SERF_NEWLINE_ANY);
416 if (SERF_BUCKET_READ_ERROR(status)) {
419 /* Something was read. Process it. */
421 if (ctx->linebuf.state == SERF_LINEBUF_READY && ctx->linebuf.used) {
425 end_key = c = memchr(ctx->linebuf.line, ':', ctx->linebuf.used);
431 /* Skip over initial : and spaces. */
432 while (apr_isspace(*++c))
435 /* Always copy the headers (from the linebuf into new mem). */
436 /* ### we should be able to optimize some mem copies */
437 serf_bucket_headers_setx(
439 ctx->linebuf.line, end_key - ctx->linebuf.line, 1,
440 c, ctx->linebuf.line + ctx->linebuf.used - c, 1);
446 /* Perform one iteration of the state machine.
448 * Will return when one the following conditions occurred:
451 * 3) the stream is not ready or at EOF
452 * 4) APR_SUCCESS, meaning the machine can be run again immediately
454 static apr_status_t run_machine(serf_bucket_t *bkt, incoming_context_t *ctx)
456 apr_status_t status = APR_SUCCESS; /* initialize to avoid gcc warnings */
458 switch (ctx->state) {
459 case STATE_STATUS_LINE:
460 /* RFC 2616 says that CRLF is the only line ending, but we can easily
461 * accept any kind of line ending.
463 status = fetch_line(ctx, SERF_NEWLINE_ANY);
464 if (SERF_BUCKET_READ_ERROR(status))
467 if (ctx->linebuf.state == SERF_LINEBUF_READY && ctx->linebuf.used) {
468 /* The Status-Line is in the line buffer. Process it. */
469 status = parse_status_line(ctx, bkt->allocator);
475 serf_bucket_barrier_create(ctx->stream, bkt->allocator);
476 ctx->body = serf_bucket_limit_create(ctx->body, ctx->length,
479 ctx->state = STATE_HEADERS;
481 ctx->state = STATE_BODY;
484 ctx->state = STATE_DONE;
488 /* The connection closed before we could get the next
489 * response. Treat the request as lost so that our upper
490 * end knows the server never tried to give us a response.
492 if (APR_STATUS_IS_EOF(status)) {
493 return SERF_ERROR_REQUEST_LOST;
498 status = fetch_headers(ctx->body, ctx);
499 if (SERF_BUCKET_READ_ERROR(status))
502 /* If an empty line was read, then we hit the end of the headers.
503 * Move on to the body.
505 if (ctx->linebuf.state == SERF_LINEBUF_READY && !ctx->linebuf.used) {
506 /* Advance the state. */
507 ctx->state = STATE_DONE;
511 /* Don't do anything. */
523 static apr_status_t wait_for_body(serf_bucket_t *bkt, incoming_context_t *ctx)
527 /* Keep reading and moving through states if we aren't at the BODY */
528 while (ctx->state != STATE_BODY) {
529 status = run_machine(bkt, ctx);
531 /* Anything other than APR_SUCCESS means that we cannot immediately
532 * read again (for now).
542 apr_status_t serf_bucket_bwtp_incoming_frame_wait_for_headers(
543 serf_bucket_t *bucket)
545 incoming_context_t *ctx = bucket->data;
547 return wait_for_body(bucket, ctx);
550 static apr_status_t bwtp_incoming_read(serf_bucket_t *bucket,
551 apr_size_t requested,
552 const char **data, apr_size_t *len)
554 incoming_context_t *ctx = bucket->data;
557 rv = wait_for_body(bucket, ctx);
559 /* It's not possible to have read anything yet! */
560 if (APR_STATUS_IS_EOF(rv) || APR_STATUS_IS_EAGAIN(rv)) {
566 rv = serf_bucket_read(ctx->body, requested, data, len);
567 if (APR_STATUS_IS_EOF(rv)) {
568 ctx->state = STATE_DONE;
573 static apr_status_t bwtp_incoming_readline(serf_bucket_t *bucket,
574 int acceptable, int *found,
575 const char **data, apr_size_t *len)
577 incoming_context_t *ctx = bucket->data;
580 rv = wait_for_body(bucket, ctx);
585 /* Delegate to the stream bucket to do the readline. */
586 return serf_bucket_readline(ctx->body, acceptable, found, data, len);
589 /* ### need to implement */
590 #define bwtp_incoming_peek NULL
592 const serf_bucket_type_t serf_bucket_type_bwtp_incoming_frame = {
595 bwtp_incoming_readline,
596 serf_default_read_iovec,
597 serf_default_read_for_sendfile,
598 serf_default_read_bucket,
600 bwtp_incoming_destroy_and_data,