| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
*/ |
|---|
| 5 |
|
|---|
| 6 |
#ifndef _NOIT_HTTP_H |
|---|
| 7 |
#define _NOIT_HTTP_H |
|---|
| 8 |
|
|---|
| 9 |
#include "noit_defines.h" |
|---|
| 10 |
#include "eventer/eventer.h" |
|---|
| 11 |
#include "utils/noit_hash.h" |
|---|
| 12 |
|
|---|
| 13 |
typedef enum { |
|---|
| 14 |
NOIT_HTTP_OTHER, NOIT_HTTP_GET, NOIT_HTTP_HEAD |
|---|
| 15 |
} noit_http_method; |
|---|
| 16 |
typedef enum { |
|---|
| 17 |
NOIT_HTTP09, NOIT_HTTP10, NOIT_HTTP11 |
|---|
| 18 |
} noit_http_protocol; |
|---|
| 19 |
|
|---|
| 20 |
#define NOIT_HTTP_CHUNKED 0x0001 |
|---|
| 21 |
#define NOIT_HTTP_CLOSE 0x0002 |
|---|
| 22 |
#define NOIT_HTTP_GZIP 0x0010 |
|---|
| 23 |
#define NOIT_HTTP_DEFLATE 0x0020 |
|---|
| 24 |
|
|---|
| 25 |
#define DEFAULT_BCHAINSIZE ((1 << 15)-(3*sizeof(size_t))-(2*sizeof(void *))) |
|---|
| 26 |
/* 64k - delta */ |
|---|
| 27 |
#define DEFAULT_BCHAINMINREAD (DEFAULT_BCHAINSIZE/4) |
|---|
| 28 |
|
|---|
| 29 |
struct bchain { |
|---|
| 30 |
struct bchain *next, *prev; |
|---|
| 31 |
size_t start; /* where data starts (buff + start) */ |
|---|
| 32 |
size_t size; /* data length (past start) */ |
|---|
| 33 |
size_t allocd;/* total allocation */ |
|---|
| 34 |
char buff[1]; /* over allocate as needed */ |
|---|
| 35 |
}; |
|---|
| 36 |
|
|---|
| 37 |
#define BCHAIN_SPACE(a) ((a)->allocd - (a)->size - (a)->start) |
|---|
| 38 |
|
|---|
| 39 |
API_EXPORT(struct bchain *) bchain_alloc(size_t size); |
|---|
| 40 |
API_EXPORT(void) bchain_free(struct bchain *); |
|---|
| 41 |
|
|---|
| 42 |
typedef struct { |
|---|
| 43 |
eventer_t e; |
|---|
| 44 |
int needs_close; |
|---|
| 45 |
} noit_http_connection; |
|---|
| 46 |
|
|---|
| 47 |
typedef struct { |
|---|
| 48 |
struct bchain *first_input; /* The start of the input chain */ |
|---|
| 49 |
struct bchain *last_input; /* The end of the input chain */ |
|---|
| 50 |
struct bchain *current_input; /* The point of the input where we */ |
|---|
| 51 |
size_t current_offset; /* analyzing. */ |
|---|
| 52 |
|
|---|
| 53 |
struct bchain *current_request_chain; |
|---|
| 54 |
char *method_str; |
|---|
| 55 |
char *uri_str; |
|---|
| 56 |
char *protocol_str; |
|---|
| 57 |
noit_http_method method; |
|---|
| 58 |
noit_http_protocol protocol; |
|---|
| 59 |
noit_hash_table headers; |
|---|
| 60 |
noit_boolean complete; |
|---|
| 61 |
} noit_http_request; |
|---|
| 62 |
|
|---|
| 63 |
typedef struct { |
|---|
| 64 |
noit_http_protocol protocol; |
|---|
| 65 |
int status_code; |
|---|
| 66 |
char *status_reason; |
|---|
| 67 |
|
|---|
| 68 |
noit_hash_table headers; |
|---|
| 69 |
struct bchain *leader; /* serialization of status line and headers */ |
|---|
| 70 |
|
|---|
| 71 |
u_int32_t output_options; |
|---|
| 72 |
struct bchain *output; /* data is pushed in here */ |
|---|
| 73 |
struct bchain *output_raw; /* internally transcoded here for output */ |
|---|
| 74 |
size_t output_raw_offset; /* tracks our offset */ |
|---|
| 75 |
noit_boolean output_started; /* locks the options and leader */ |
|---|
| 76 |
/* and possibly output. */ |
|---|
| 77 |
noit_boolean closed; /* set by _end() */ |
|---|
| 78 |
noit_boolean complete; /* complete, drained and disposable */ |
|---|
| 79 |
} noit_http_response; |
|---|
| 80 |
|
|---|
| 81 |
struct noit_http_session_ctx; |
|---|
| 82 |
typedef int (*noit_http_dispatch_func) (struct noit_http_session_ctx *); |
|---|
| 83 |
|
|---|
| 84 |
typedef struct noit_http_session_ctx { |
|---|
| 85 |
noit_http_connection conn; |
|---|
| 86 |
noit_http_request req; |
|---|
| 87 |
noit_http_response res; |
|---|
| 88 |
noit_http_dispatch_func dispatcher; |
|---|
| 89 |
eventer_func_t drive; |
|---|
| 90 |
} noit_http_session_ctx; |
|---|
| 91 |
|
|---|
| 92 |
API_EXPORT(noit_http_session_ctx *) |
|---|
| 93 |
noit_http_session_ctx_new(noit_http_dispatch_func, eventer_t); |
|---|
| 94 |
|
|---|
| 95 |
API_EXPORT(int) |
|---|
| 96 |
noit_http_session_drive(eventer_t, int, void *, struct timeval *); |
|---|
| 97 |
|
|---|
| 98 |
API_EXPORT(noit_boolean) |
|---|
| 99 |
noit_http_response_status_set(noit_http_session_ctx *, int, const char *); |
|---|
| 100 |
API_EXPORT(noit_boolean) |
|---|
| 101 |
noit_http_response_header_set(noit_http_session_ctx *, |
|---|
| 102 |
const char *, const char *); |
|---|
| 103 |
API_EXPORT(noit_boolean) |
|---|
| 104 |
noit_http_response_option_set(noit_http_session_ctx *, u_int32_t); |
|---|
| 105 |
API_EXPORT(noit_boolean) |
|---|
| 106 |
noit_http_response_append(noit_http_session_ctx *, const void *, size_t); |
|---|
| 107 |
API_EXPORT(noit_boolean) |
|---|
| 108 |
noit_http_response_append_bchain(noit_http_session_ctx *, struct bchain *); |
|---|
| 109 |
API_EXPORT(noit_boolean) |
|---|
| 110 |
noit_http_response_flush(noit_http_session_ctx *, noit_boolean); |
|---|
| 111 |
API_EXPORT(noit_boolean) noit_http_response_end(noit_http_session_ctx *); |
|---|
| 112 |
|
|---|
| 113 |
#endif |
|---|