Renamed chunk sources, added request parser, header management, test file.
parent
503e8d7859
commit
18413d698a
@ -0,0 +1,78 @@
|
||||
|
||||
#include "chunk_parser.h"
|
||||
|
||||
void chunk_parser_init(chunk_parser_ctx *ctx, chunkqueue *cq) {
|
||||
ctx->cq = cq;
|
||||
ctx->bytes_in = 0;
|
||||
ctx->curi.element = NULL;
|
||||
ctx->start = 0;
|
||||
ctx->length = 0;
|
||||
ctx->buf = NULL;
|
||||
}
|
||||
|
||||
handler_t chunk_parser_prepare(chunk_parser_ctx *ctx) {
|
||||
if (NULL == ctx->curi.element) {
|
||||
ctx->curi = chunkqueue_iter(ctx->cq);
|
||||
if (NULL == ctx->curi.element) return HANDLER_WAIT_FOR_EVENT;
|
||||
}
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
handler_t chunk_parser_next(server *srv, connection *con, chunk_parser_ctx *ctx, char **p, char **pe) {
|
||||
off_t l;
|
||||
handler_t res;
|
||||
|
||||
if (NULL == ctx->curi.element) return HANDLER_WAIT_FOR_EVENT;
|
||||
|
||||
while (ctx->start >= (l = chunkiter_length(ctx->curi))) {
|
||||
chunkiter i = ctx->curi;
|
||||
/* Wait at the end of the last chunk if it gets extended */
|
||||
if (!chunkiter_next(&i)) return HANDLER_WAIT_FOR_EVENT;
|
||||
ctx->curi = i;
|
||||
ctx->start = 0;
|
||||
}
|
||||
|
||||
if (NULL == ctx->curi.element) return HANDLER_WAIT_FOR_EVENT;
|
||||
|
||||
if (HANDLER_GO_ON != (res = chunkiter_read(srv, con, ctx->curi, ctx->start, l - ctx->start, &ctx->buf, &ctx->length))) {
|
||||
return res;
|
||||
}
|
||||
|
||||
*p = ctx->buf;
|
||||
*pe = ctx->buf + ctx->length;
|
||||
return HANDLER_GO_ON;
|
||||
}
|
||||
|
||||
void chunk_parser_done(chunk_parser_ctx *ctx, goffset len) {
|
||||
ctx->bytes_in += len;
|
||||
ctx->start += len;
|
||||
}
|
||||
|
||||
GString* chunk_extract(server *srv, connection *con, chunk_parser_mark from, chunk_parser_mark to) {
|
||||
GString *str = g_string_sized_new(0);
|
||||
chunk_parser_mark i;
|
||||
for ( i = from; i.ci.element != to.ci.element; chunkiter_next(&i.ci) ) {
|
||||
goffset len = chunkiter_length(i.ci);
|
||||
while (i.pos < len) {
|
||||
char *buf;
|
||||
off_t we_have;
|
||||
if (HANDLER_GO_ON != chunkiter_read(srv, con, i.ci, i.pos, len - i.pos, &buf, &we_have)) goto error;
|
||||
g_string_append_len(str, buf, we_have);
|
||||
i.pos += we_have;
|
||||
}
|
||||
i.pos = 0;
|
||||
}
|
||||
while (i.pos < to.pos) {
|
||||
char *buf;
|
||||
off_t we_have;
|
||||
if (HANDLER_GO_ON != chunkiter_read(srv, con, i.ci, i.pos, to.pos - i.pos, &buf, &we_have)) goto error;
|
||||
g_string_append_len(str, buf, we_have);
|
||||
i.pos += we_have;
|
||||
}
|
||||
|
||||
return str;
|
||||
|
||||
error:
|
||||
g_string_free(str, TRUE);
|
||||
return NULL;
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
#ifndef _LIGHTTPD_CHUNK_PARSER_H_
|
||||
#define _LIGHTTPD_CHUNK_PARSER_H_
|
||||
|
||||
struct chunk_parser_ctx;
|
||||
typedef struct chunk_parser_ctx chunk_parser_ctx;
|
||||
|
||||
struct chunk_parser_mark;
|
||||
typedef struct chunk_parser_mark chunk_parser_mark;
|
||||
|
||||
#include "chunk.h"
|
||||
|
||||
struct chunk_parser_ctx {
|
||||
chunkqueue *cq;
|
||||
|
||||
goffset bytes_in;
|
||||
|
||||
/* current position
|
||||
* buf is curi[start..start+length)
|
||||
*/
|
||||
chunkiter curi;
|
||||
off_t start, length;
|
||||
char *buf;
|
||||
|
||||
int cs;
|
||||
};
|
||||
|
||||
struct chunk_parser_mark {
|
||||
chunkiter ci;
|
||||
off_t pos;
|
||||
};
|
||||
|
||||
LI_API void chunk_parser_init(chunk_parser_ctx *ctx, chunkqueue *cq);
|
||||
LI_API handler_t chunk_parser_prepare(chunk_parser_ctx *ctx);
|
||||
LI_API handler_t chunk_parser_next(server *srv, connection *con, chunk_parser_ctx *ctx, char **p, char **pe);
|
||||
LI_API void chunk_parser_done(chunk_parser_ctx *ctx, goffset len);
|
||||
|
||||
LI_API GString* chunk_extract(server *srv, connection *con, chunk_parser_mark from, chunk_parser_mark to);
|
||||
|
||||
INLINE chunk_parser_mark chunk_parser_getmark(chunk_parser_ctx *ctx, const char *fpc);
|
||||
|
||||
/********************
|
||||
* Inline functions *
|
||||
********************/
|
||||
|
||||
INLINE chunk_parser_mark chunk_parser_getmark(chunk_parser_ctx *ctx, const char *fpc) {
|
||||
chunk_parser_mark m;
|
||||
m.ci = ctx->curi;
|
||||
m.pos = ctx->start + fpc - ctx->buf;
|
||||
return m;
|
||||
}
|
||||
|
||||
#define GETMARK(FPC) (chunk_parser_getmark(&ctx->chunk_ctx, FPC))
|
||||
|
||||
#endif
|
@ -0,0 +1,95 @@
|
||||
|
||||
#include "http_headers.h"
|
||||
|
||||
static void _string_free(gpointer p) {
|
||||
g_string_free((GString*) p, TRUE);
|
||||
}
|
||||
|
||||
http_headers* http_headers_new() {
|
||||
http_headers* headers = g_slice_new0(http_headers);
|
||||
headers->table = g_hash_table_new_full(
|
||||
(GHashFunc) g_string_hash, (GEqualFunc) g_string_equal,
|
||||
_string_free, _string_free);
|
||||
return headers;
|
||||
}
|
||||
|
||||
void http_headers_free(http_headers* headers) {
|
||||
if (!headers) return;
|
||||
g_hash_table_destroy(headers->table);
|
||||
g_slice_free(http_headers, headers);
|
||||
}
|
||||
|
||||
/* Just insert the header (using lokey)
|
||||
*/
|
||||
static void header_insert(http_headers *headers, GString *lokey, GString *key, GString *value) {
|
||||
GString *newval = g_string_sized_new(key->len + value->len + 2);
|
||||
|
||||
g_string_append_len(newval, key->str, key->len);
|
||||
g_string_append_len(newval, ": ", 2);
|
||||
g_string_append_len(newval, value->str, value->len);
|
||||
|
||||
g_hash_table_insert(headers->table, lokey, newval);
|
||||
}
|
||||
|
||||
/** If header does not exist, just insert normal header. If it exists, append (", %s", value) */
|
||||
void http_header_append(http_headers *headers, GString *key, GString *value) {
|
||||
GString *lokey, *tval;
|
||||
|
||||
lokey = g_string_new_len(key->str, key->len);
|
||||
g_string_ascii_down(lokey);
|
||||
tval = (GString*) g_hash_table_lookup(headers->table, lokey);
|
||||
if (!tval) {
|
||||
header_insert(headers, lokey, key, value);
|
||||
} else {
|
||||
g_string_free(lokey, TRUE);
|
||||
g_string_append_len(tval, ", ", 2);
|
||||
g_string_append_len(tval, value->str, value->len);
|
||||
}
|
||||
}
|
||||
|
||||
/** If header does not exist, just insert normal header. If it exists, append ("\r\n%s: %s", key, value) */
|
||||
void http_header_insert(http_headers *headers, GString *key, GString *value) {
|
||||
GString *lokey, *tval;
|
||||
|
||||
lokey = g_string_new_len(key->str, key->len);
|
||||
g_string_ascii_down(lokey);
|
||||
tval = (GString*) g_hash_table_lookup(headers->table, lokey);
|
||||
if (!tval) {
|
||||
header_insert(headers, lokey, key, value);
|
||||
} else {
|
||||
g_string_free(lokey, TRUE);
|
||||
g_string_append_len(tval, "\r\n", 2);
|
||||
g_string_append_len(tval, key->str, key->len);
|
||||
g_string_append_len(tval, ": ", 2);
|
||||
g_string_append_len(tval, value->str, value->len);
|
||||
}
|
||||
}
|
||||
|
||||
/** If header does not exist, just insert normal header. If it exists, overwrite the value */
|
||||
void http_header_overwrite(http_headers *headers, GString *key, GString *value) {
|
||||
GString *lokey, *tval;
|
||||
|
||||
lokey = g_string_new_len(key->str, key->len);
|
||||
g_string_ascii_down(lokey);
|
||||
tval = (GString*) g_hash_table_lookup(headers->table, lokey);
|
||||
if (!tval) {
|
||||
header_insert(headers, lokey, key, value);
|
||||
} else {
|
||||
g_string_free(lokey, TRUE);
|
||||
g_string_truncate(tval, 0);
|
||||
g_string_append_len(tval, key->str, key->len);
|
||||
g_string_append_len(tval, ": ", 2);
|
||||
g_string_append_len(tval, value->str, value->len);
|
||||
}
|
||||
}
|
||||
|
||||
LI_API gboolean http_header_remove(http_headers *headers, GString *key) {
|
||||
GString *lokey;
|
||||
gboolean res;
|
||||
|
||||
lokey = g_string_new_len(key->str, key->len);
|
||||
g_string_ascii_down(lokey);
|
||||
res = g_hash_table_remove(headers->table, lokey);
|
||||
g_string_free(lokey, TRUE);
|
||||
return res;
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#ifndef _LIGHTTPD_HTTP_HEADERS_H_
|
||||
#define _LIGHTTPD_HTTP_HEADERS_H_
|
||||
|
||||
struct http_headers;
|
||||
typedef struct http_headers http_headers;
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
struct http_headers {
|
||||
/** keys are lowercase header name, values contain the complete header */
|
||||
GHashTable *table;
|
||||
};
|
||||
|
||||
/* strings alweays get copied, so you should free key and value yourself */
|
||||
|
||||
LI_API http_headers* http_headers_new();
|
||||
LI_API void http_headers_free(http_headers* headers);
|
||||
|
||||
/** If header does not exist, just insert normal header. If it exists, append (", %s", value) */
|
||||
LI_API void http_header_append(http_headers *headers, GString *key, GString *value);
|
||||
/** If header does not exist, just insert normal header. If it exists, append ("\r\n%s: %s", key, value) */
|
||||
LI_API void http_header_insert(http_headers *headers, GString *key, GString *value);
|
||||
/** If header does not exist, just insert normal header. If it exists, overwrite the value */
|
||||
LI_API void http_header_overwrite(http_headers *headers, GString *key, GString *value);
|
||||
LI_API gboolean http_header_remove(http_headers *headers, GString *key);
|
||||
|
||||
#endif
|
@ -1,55 +1,109 @@
|
||||
|
||||
#include "http_request_parser.h"
|
||||
|
||||
static chunk_parser_mark getmark(http_request_ctx *ctx, const char *fpc) {
|
||||
chunk_parser_mark m;
|
||||
m.ci = ctx->curi;
|
||||
m.pos = ctx->start + fpc - ctx->buf;
|
||||
return m;
|
||||
}
|
||||
|
||||
/** Machine **/
|
||||
|
||||
#define _getString(M, FPC) (chunk_extract(srv, con, ctx->M, GETMARK(FPC)))
|
||||
#define getString(FPC) _getString(mark, FPC)
|
||||
|
||||
%%{
|
||||
|
||||
machine http_request_parser;
|
||||
variable cs ctx->chunk_ctx.cs;
|
||||
|
||||
action mark { ctx->mark = GETMARK(fpc); }
|
||||
action done { fbreak; }
|
||||
|
||||
action method { ctx->request->http_method_str = getString(fpc); }
|
||||
action uri { ctx->request->uri.uri = getString(fpc); }
|
||||
|
||||
# RFC 2616
|
||||
OCTET = any;
|
||||
CHAR = ascii;
|
||||
UPALPHA = upper;
|
||||
LOALPHA = lower;
|
||||
ALPHA = alpha;
|
||||
DIGIT = digit;
|
||||
CTL = ( 0 .. 31 | 127 );
|
||||
CR = '\r';
|
||||
LF = '\n';
|
||||
SP = ' ';
|
||||
HT = '\t';
|
||||
DQUOTE = '"';
|
||||
|
||||
CRLF = CR LF;
|
||||
LWS = CRLF? (SP | HT)+; # linear white space
|
||||
TEXT = (OCTET - CTL) | LWS;
|
||||
HEX = [a-fA-F0-9];
|
||||
|
||||
Separators = [()<>@,;:\\\"/\[\]?={}] | SP | HT;
|
||||
Token = OCTET - Separators - CTL;
|
||||
|
||||
# original definition
|
||||
# Comment = "(" ( CText | Quoted_Pair | Comment )* ")";
|
||||
# CText = TEXT - [()];
|
||||
|
||||
Quoted_Pair = "\\" CHAR;
|
||||
Comment = ( TEXT | Quoted_Pair )*;
|
||||
QDText = TEXT - DQUOTE;
|
||||
Quoted_String = DQUOTE ( QDText | Quoted_Pair )* DQUOTE;
|
||||
|
||||
HTTP_Version = "HTTP" "/" DIGIT+ "." DIGIT+;
|
||||
#HTTP_URL = "http:" "//" Host ( ":" Port )? ( abs_path ( "?" query )? )?;
|
||||
|
||||
# RFC 2396
|
||||
|
||||
CRLF = "\r\n";
|
||||
Mark = [\-_!~*\'()];
|
||||
Unreserved = alnum | Mark;
|
||||
Escaped = "%" HEX HEX;
|
||||
|
||||
action mark { ctx->mark = getmark(ctx, fpc); }
|
||||
PChar = Unreserved | Escaped | [:@&=+$,];
|
||||
Segment = PChar* ( ";" PChar* )*;
|
||||
Path_Segments = Segment ("/" Segment)*;
|
||||
Abs_Path = "/" Path_Segments;
|
||||
|
||||
main := CRLF ;
|
||||
Method = Token >mark %method;
|
||||
Request_URI = ("*" | ( any - CTL - SP )) >mark %uri;
|
||||
Request_Line = Method " " Request_URI " " HTTP_Version CRLF;
|
||||
|
||||
Field_Content = ( TEXT+ | ( Token | Separators | Quoted_String )+ );
|
||||
Field_Value = " "* (Field_Content+ ( Field_Content | LWS )*)? >mark;
|
||||
Message_Header = Token ":" Field_Value?;
|
||||
|
||||
main := (CRLF)* Request_Line (Message_Header CRLF)* CRLF @ done;
|
||||
}%%
|
||||
|
||||
%% write data;
|
||||
|
||||
void http_request_parse(server *srv, connection *con, http_request_ctx *ctx) {
|
||||
int cs = ctx->cs;
|
||||
while (cs != http_request_parser_error && cs != http_request_parser_first_final) {
|
||||
char *p, *pe;
|
||||
off_t l;
|
||||
static int http_request_parser_has_error(http_request_ctx *ctx) {
|
||||
return ctx->chunk_ctx.cs == http_request_parser_error;
|
||||
}
|
||||
|
||||
l = chunkiter_length(ctx->curi);
|
||||
if (ctx->start >= l) {
|
||||
chunkiter_next(&ctx->curi);
|
||||
continue;
|
||||
}
|
||||
static int http_request_parser_is_finished(http_request_ctx *ctx) {
|
||||
return ctx->chunk_ctx.cs >= http_request_parser_first_final;
|
||||
}
|
||||
|
||||
if (HANDLER_GO_ON != chunkiter_read(srv, con, ctx->curi, ctx->start, l - ctx->start, &ctx->buf, &ctx->length)) {
|
||||
return;
|
||||
}
|
||||
void http_request_parser_init(http_request_ctx *ctx, chunkqueue *cq) {
|
||||
%% write init;
|
||||
chunk_parser_init(&ctx->chunk_ctx, cq);
|
||||
}
|
||||
|
||||
p = ctx->buf;
|
||||
pe = ctx->buf + ctx->length;
|
||||
handler_t http_request_parse(server *srv, connection *con, http_request_ctx *ctx) {
|
||||
handler_t res;
|
||||
|
||||
if (HANDLER_GO_ON != (res = chunk_parser_prepare(&ctx->chunk_ctx))) return res;
|
||||
|
||||
while (!http_request_parser_has_error(ctx) && !http_request_parser_is_finished(ctx)) {
|
||||
char *p, *pe;
|
||||
|
||||
if (HANDLER_GO_ON != (res = chunk_parser_next(srv, con, &ctx->chunk_ctx, &p, &pe))) return res;
|
||||
|
||||
%% write exec;
|
||||
|
||||
ctx->start += pe - p;
|
||||
ctx->bytes_in += pe - p;
|
||||
if (ctx->start >= l) {
|
||||
chunkiter_next(&ctx->curi);
|
||||
ctx->start = 0;
|
||||
}
|
||||
chunk_parser_done(&ctx->chunk_ctx, pe - p);
|
||||
}
|
||||
ctx->cs = cs;
|
||||
|
||||
if (http_request_parser_has_error(ctx)) return HANDLER_ERROR;
|
||||
if (http_request_parser_is_finished(ctx)) return HANDLER_GO_ON;
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
|
||||
#include "request.h"
|
||||
|
||||
request* request_new() {
|
||||
request *req = g_slice_new0(request);
|
||||
}
|
||||
|
||||
void request_free(request *req) {
|
||||
/* TODO */
|
||||
g_slice_free(request, req);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
|
||||
#include "base.h"
|
||||
|
||||
int request_test() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue