| 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 <libxml/tree.h> |
|---|
| 38 |
#include "eventer/eventer.h" |
|---|
| 39 |
#include "utils/noit_hash.h" |
|---|
| 40 |
#include "utils/noit_atomic.h" |
|---|
| 41 |
#include "noit_listener.h" |
|---|
| 42 |
|
|---|
| 43 |
typedef enum { |
|---|
| 44 |
NOIT_HTTP_OTHER, NOIT_HTTP_GET, NOIT_HTTP_HEAD, NOIT_HTTP_POST |
|---|
| 45 |
} noit_http_method; |
|---|
| 46 |
typedef enum { |
|---|
| 47 |
NOIT_HTTP09, NOIT_HTTP10, NOIT_HTTP11 |
|---|
| 48 |
} noit_http_protocol; |
|---|
| 49 |
|
|---|
| 50 |
#define NOIT_HTTP_CHUNKED 0x0001 |
|---|
| 51 |
#define NOIT_HTTP_CLOSE 0x0002 |
|---|
| 52 |
#define NOIT_HTTP_GZIP 0x0010 |
|---|
| 53 |
#define NOIT_HTTP_DEFLATE 0x0020 |
|---|
| 54 |
|
|---|
| 55 |
typedef enum { |
|---|
| 56 |
BCHAIN_INLINE = 0, |
|---|
| 57 |
BCHAIN_MMAP |
|---|
| 58 |
} bchain_type_t; |
|---|
| 59 |
|
|---|
| 60 |
struct bchain; |
|---|
| 61 |
|
|---|
| 62 |
#define DEFAULT_MAXWRITE 1<<14 /* 32k */ |
|---|
| 63 |
#define DEFAULT_BCHAINSIZE ((1 << 15)-(offsetof(struct bchain, _buff))) |
|---|
| 64 |
/* 64k - delta */ |
|---|
| 65 |
#define DEFAULT_BCHAINMINREAD (DEFAULT_BCHAINSIZE/4) |
|---|
| 66 |
#define BCHAIN_SPACE(a) ((a)->allocd - (a)->size - (a)->start) |
|---|
| 67 |
|
|---|
| 68 |
struct noit_http_connection; |
|---|
| 69 |
typedef struct noit_http_connection noit_http_connection; |
|---|
| 70 |
struct noit_http_request; |
|---|
| 71 |
typedef struct noit_http_request noit_http_request; |
|---|
| 72 |
struct noit_http_response; |
|---|
| 73 |
typedef struct noit_http_response noit_http_response; |
|---|
| 74 |
|
|---|
| 75 |
struct bchain { |
|---|
| 76 |
bchain_type_t type; |
|---|
| 77 |
struct bchain *next, *prev; |
|---|
| 78 |
size_t start; /* where data starts (buff + start) */ |
|---|
| 79 |
size_t size; /* data length (past start) */ |
|---|
| 80 |
size_t allocd;/* total allocation */ |
|---|
| 81 |
char *buff; |
|---|
| 82 |
char _buff[1]; /* over allocate as needed */ |
|---|
| 83 |
}; |
|---|
| 84 |
|
|---|
| 85 |
struct noit_http_session_ctx; |
|---|
| 86 |
typedef struct noit_http_session_ctx noit_http_session_ctx; |
|---|
| 87 |
typedef int (*noit_http_dispatch_func) (noit_http_session_ctx *); |
|---|
| 88 |
|
|---|
| 89 |
API_EXPORT(noit_http_session_ctx *) |
|---|
| 90 |
noit_http_session_ctx_new(noit_http_dispatch_func, void *, eventer_t, |
|---|
| 91 |
acceptor_closure_t *); |
|---|
| 92 |
API_EXPORT(void) |
|---|
| 93 |
noit_http_ctx_session_release(noit_http_session_ctx *ctx); |
|---|
| 94 |
API_EXPORT(uint32_t) |
|---|
| 95 |
noit_http_session_ref_cnt(noit_http_session_ctx *); |
|---|
| 96 |
API_EXPORT(uint32_t) |
|---|
| 97 |
noit_http_session_ref_dec(noit_http_session_ctx *); |
|---|
| 98 |
API_EXPORT(uint32_t) |
|---|
| 99 |
noit_http_session_ref_inc(noit_http_session_ctx *); |
|---|
| 100 |
API_EXPORT(void) |
|---|
| 101 |
noit_http_session_trigger(noit_http_session_ctx *, int state); |
|---|
| 102 |
|
|---|
| 103 |
API_EXPORT(noit_http_request *) |
|---|
| 104 |
noit_http_session_request(noit_http_session_ctx *); |
|---|
| 105 |
API_EXPORT(noit_http_response *) |
|---|
| 106 |
noit_http_session_response(noit_http_session_ctx *); |
|---|
| 107 |
API_EXPORT(noit_http_connection *) |
|---|
| 108 |
noit_http_session_connection(noit_http_session_ctx *); |
|---|
| 109 |
|
|---|
| 110 |
API_EXPORT(void *) |
|---|
| 111 |
noit_http_session_dispatcher_closure(noit_http_session_ctx *); |
|---|
| 112 |
API_EXPORT(void) |
|---|
| 113 |
noit_http_session_set_dispatcher(noit_http_session_ctx *, |
|---|
| 114 |
int (*)(noit_http_session_ctx *), void *); |
|---|
| 115 |
|
|---|
| 116 |
API_EXPORT(eventer_t) |
|---|
| 117 |
noit_http_connection_event(noit_http_connection *); |
|---|
| 118 |
|
|---|
| 119 |
API_EXPORT(const char *) |
|---|
| 120 |
noit_http_request_uri_str(noit_http_request *); |
|---|
| 121 |
API_EXPORT(const char *) |
|---|
| 122 |
noit_http_request_method_str(noit_http_request *); |
|---|
| 123 |
API_EXPORT(const char *) |
|---|
| 124 |
noit_http_request_protocol_str(noit_http_request *); |
|---|
| 125 |
API_EXPORT(size_t) |
|---|
| 126 |
noit_http_request_content_length(noit_http_request *); |
|---|
| 127 |
API_EXPORT(const char *) |
|---|
| 128 |
noit_http_request_querystring(noit_http_request *, const char *); |
|---|
| 129 |
API_EXPORT(noit_hash_table *) |
|---|
| 130 |
noit_http_request_querystring_table(noit_http_request *); |
|---|
| 131 |
API_EXPORT(noit_hash_table *) |
|---|
| 132 |
noit_http_request_headers_table(noit_http_request *); |
|---|
| 133 |
|
|---|
| 134 |
API_EXPORT(noit_boolean) |
|---|
| 135 |
noit_http_response_closed(noit_http_response *); |
|---|
| 136 |
API_EXPORT(noit_boolean) |
|---|
| 137 |
noit_http_response_complete(noit_http_response *); |
|---|
| 138 |
|
|---|
| 139 |
API_EXPORT(void) |
|---|
| 140 |
noit_http_ctx_acceptor_free(void *); /* just calls noit_http_session_ctx_release */ |
|---|
| 141 |
|
|---|
| 142 |
API_EXPORT(void) |
|---|
| 143 |
noit_http_process_querystring(noit_http_request *); |
|---|
| 144 |
|
|---|
| 145 |
API_EXPORT(int) |
|---|
| 146 |
noit_http_session_drive(eventer_t, int, void *, struct timeval *, int *done); |
|---|
| 147 |
|
|---|
| 148 |
API_EXPORT(noit_boolean) |
|---|
| 149 |
noit_http_session_prime_input(noit_http_session_ctx *, const void *, size_t); |
|---|
| 150 |
API_EXPORT(int) |
|---|
| 151 |
noit_http_session_req_consume(noit_http_session_ctx *ctx, |
|---|
| 152 |
void *buf, size_t len, int *mask); |
|---|
| 153 |
API_EXPORT(noit_boolean) |
|---|
| 154 |
noit_http_response_status_set(noit_http_session_ctx *, int, const char *); |
|---|
| 155 |
API_EXPORT(noit_boolean) |
|---|
| 156 |
noit_http_response_header_set(noit_http_session_ctx *, |
|---|
| 157 |
const char *, const char *); |
|---|
| 158 |
API_EXPORT(noit_boolean) |
|---|
| 159 |
noit_http_response_option_set(noit_http_session_ctx *, u_int32_t); |
|---|
| 160 |
API_EXPORT(noit_boolean) |
|---|
| 161 |
noit_http_response_append(noit_http_session_ctx *, const void *, size_t); |
|---|
| 162 |
API_EXPORT(noit_boolean) |
|---|
| 163 |
noit_http_response_append_bchain(noit_http_session_ctx *, struct bchain *); |
|---|
| 164 |
API_EXPORT(noit_boolean) |
|---|
| 165 |
noit_http_response_append_mmap(noit_http_session_ctx *, |
|---|
| 166 |
int fd, size_t len, int flags, off_t offset); |
|---|
| 167 |
API_EXPORT(noit_boolean) |
|---|
| 168 |
noit_http_response_flush(noit_http_session_ctx *, noit_boolean); |
|---|
| 169 |
API_EXPORT(noit_boolean) noit_http_response_end(noit_http_session_ctx *); |
|---|
| 170 |
|
|---|
| 171 |
#define noit_http_response_server_error(ctx, type) \ |
|---|
| 172 |
noit_http_response_standard(ctx, 500, "ERROR", type) |
|---|
| 173 |
#define noit_http_response_ok(ctx, type) \ |
|---|
| 174 |
noit_http_response_standard(ctx, 200, "OK", type) |
|---|
| 175 |
#define noit_http_response_not_found(ctx, type) \ |
|---|
| 176 |
noit_http_response_standard(ctx, 404, "NOT FOUND", type) |
|---|
| 177 |
#define noit_http_response_denied(ctx, type) \ |
|---|
| 178 |
noit_http_response_standard(ctx, 403, "DENIED", type) |
|---|
| 179 |
|
|---|
| 180 |
#define noit_http_response_standard(ctx, code, name, type) do { \ |
|---|
| 181 |
noit_http_response_status_set(ctx, code, name); \ |
|---|
| 182 |
noit_http_response_header_set(ctx, "Content-Type", type); \ |
|---|
| 183 |
if(noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED) == noit_false) \ |
|---|
| 184 |
noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE); \ |
|---|
| 185 |
noit_http_response_option_set(ctx, NOIT_HTTP_GZIP); \ |
|---|
| 186 |
} while(0) |
|---|
| 187 |
|
|---|
| 188 |
API_EXPORT(void) |
|---|
| 189 |
noit_http_response_xml(noit_http_session_ctx *, xmlDocPtr); |
|---|
| 190 |
|
|---|
| 191 |
API_EXPORT(void) |
|---|
| 192 |
noit_http_init(); |
|---|
| 193 |
|
|---|
| 194 |
#endif |
|---|