| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#ifndef _NOIT_HTTP_H |
|---|
| 34 |
#define _NOIT_HTTP_H |
|---|
| 35 |
|
|---|
| 36 |
#include "noit_defines.h" |
|---|
| 37 |
#include "eventer/eventer.h" |
|---|
| 38 |
#include "utils/noit_hash.h" |
|---|
| 39 |
#include "utils/noit_atomic.h" |
|---|
| 40 |
|
|---|
| 41 |
typedef enum { |
|---|
| 42 |
NOIT_HTTP_OTHER, NOIT_HTTP_GET, NOIT_HTTP_HEAD |
|---|
| 43 |
} noit_http_method; |
|---|
| 44 |
typedef enum { |
|---|
| 45 |
NOIT_HTTP09, NOIT_HTTP10, NOIT_HTTP11 |
|---|
| 46 |
} noit_http_protocol; |
|---|
| 47 |
|
|---|
| 48 |
#define NOIT_HTTP_CHUNKED 0x0001 |
|---|
| 49 |
#define NOIT_HTTP_CLOSE 0x0002 |
|---|
| 50 |
#define NOIT_HTTP_GZIP 0x0010 |
|---|
| 51 |
#define NOIT_HTTP_DEFLATE 0x0020 |
|---|
| 52 |
|
|---|
| 53 |
#define DEFAULT_BCHAINSIZE ((1 << 15)-(3*sizeof(size_t))-(2*sizeof(void *))) |
|---|
| 54 |
/* 64k - delta */ |
|---|
| 55 |
#define DEFAULT_BCHAINMINREAD (DEFAULT_BCHAINSIZE/4) |
|---|
| 56 |
|
|---|
| 57 |
struct bchain { |
|---|
| 58 |
struct bchain *next, *prev; |
|---|
| 59 |
size_t start; /* where data starts (buff + start) */ |
|---|
| 60 |
size_t size; /* data length (past start) */ |
|---|
| 61 |
size_t allocd;/* total allocation */ |
|---|
| 62 |
char buff[1]; /* over allocate as needed */ |
|---|
| 63 |
}; |
|---|
| 64 |
|
|---|
| 65 |
#define BCHAIN_SPACE(a) ((a)->allocd - (a)->size - (a)->start) |
|---|
| 66 |
|
|---|
| 67 |
API_EXPORT(struct bchain *) bchain_alloc(size_t size); |
|---|
| 68 |
API_EXPORT(void) bchain_free(struct bchain *); |
|---|
| 69 |
|
|---|
| 70 |
typedef struct { |
|---|
| 71 |
eventer_t e; |
|---|
| 72 |
int needs_close; |
|---|
| 73 |
} noit_http_connection; |
|---|
| 74 |
|
|---|
| 75 |
typedef struct { |
|---|
| 76 |
struct bchain *first_input; /* The start of the input chain */ |
|---|
| 77 |
struct bchain *last_input; /* The end of the input chain */ |
|---|
| 78 |
struct bchain *current_input; /* The point of the input where we */ |
|---|
| 79 |
size_t current_offset; /* analyzing. */ |
|---|
| 80 |
|
|---|
| 81 |
struct bchain *current_request_chain; |
|---|
| 82 |
char *method_str; |
|---|
| 83 |
char *uri_str; |
|---|
| 84 |
char *protocol_str; |
|---|
| 85 |
u_int32_t opts; |
|---|
| 86 |
noit_http_method method; |
|---|
| 87 |
noit_http_protocol protocol; |
|---|
| 88 |
noit_hash_table headers; |
|---|
| 89 |
noit_boolean complete; |
|---|
| 90 |
} noit_http_request; |
|---|
| 91 |
|
|---|
| 92 |
typedef struct { |
|---|
| 93 |
noit_http_protocol protocol; |
|---|
| 94 |
int status_code; |
|---|
| 95 |
char *status_reason; |
|---|
| 96 |
|
|---|
| 97 |
noit_hash_table headers; |
|---|
| 98 |
struct bchain *leader; /* serialization of status line and headers */ |
|---|
| 99 |
|
|---|
| 100 |
u_int32_t output_options; |
|---|
| 101 |
struct bchain *output; /* data is pushed in here */ |
|---|
| 102 |
struct bchain *output_raw; /* internally transcoded here for output */ |
|---|
| 103 |
size_t output_raw_offset; /* tracks our offset */ |
|---|
| 104 |
noit_boolean output_started; /* locks the options and leader */ |
|---|
| 105 |
/* and possibly output. */ |
|---|
| 106 |
noit_boolean closed; /* set by _end() */ |
|---|
| 107 |
noit_boolean complete; /* complete, drained and disposable */ |
|---|
| 108 |
} noit_http_response; |
|---|
| 109 |
|
|---|
| 110 |
struct noit_http_session_ctx; |
|---|
| 111 |
typedef int (*noit_http_dispatch_func) (struct noit_http_session_ctx *); |
|---|
| 112 |
|
|---|
| 113 |
typedef struct noit_http_session_ctx { |
|---|
| 114 |
noit_atomic32_t ref_cnt; |
|---|
| 115 |
noit_http_connection conn; |
|---|
| 116 |
noit_http_request req; |
|---|
| 117 |
noit_http_response res; |
|---|
| 118 |
noit_http_dispatch_func dispatcher; |
|---|
| 119 |
void *dispatcher_closure; |
|---|
| 120 |
eventer_func_t drive; |
|---|
| 121 |
} noit_http_session_ctx; |
|---|
| 122 |
|
|---|
| 123 |
API_EXPORT(noit_http_session_ctx *) |
|---|
| 124 |
noit_http_session_ctx_new(noit_http_dispatch_func, void *, eventer_t); |
|---|
| 125 |
API_EXPORT(void) |
|---|
| 126 |
noit_http_session_ctx_release(noit_http_session_ctx *); |
|---|
| 127 |
|
|---|
| 128 |
API_EXPORT(int) |
|---|
| 129 |
noit_http_session_drive(eventer_t, int, void *, struct timeval *); |
|---|
| 130 |
|
|---|
| 131 |
API_EXPORT(noit_boolean) |
|---|
| 132 |
noit_http_response_status_set(noit_http_session_ctx *, int, const char *); |
|---|
| 133 |
API_EXPORT(noit_boolean) |
|---|
| 134 |
noit_http_response_header_set(noit_http_session_ctx *, |
|---|
| 135 |
const char *, const char *); |
|---|
| 136 |
API_EXPORT(noit_boolean) |
|---|
| 137 |
noit_http_response_option_set(noit_http_session_ctx *, u_int32_t); |
|---|
| 138 |
API_EXPORT(noit_boolean) |
|---|
| 139 |
noit_http_response_append(noit_http_session_ctx *, const void *, size_t); |
|---|
| 140 |
API_EXPORT(noit_boolean) |
|---|
| 141 |
noit_http_response_append_bchain(noit_http_session_ctx *, struct bchain *); |
|---|
| 142 |
API_EXPORT(noit_boolean) |
|---|
| 143 |
noit_http_response_flush(noit_http_session_ctx *, noit_boolean); |
|---|
| 144 |
API_EXPORT(noit_boolean) noit_http_response_end(noit_http_session_ctx *); |
|---|
| 145 |
|
|---|
| 146 |
#endif |
|---|