24 changed files with 696 additions and 155 deletions
-
6ChangeLog
-
20include/class.h
-
28include/http/message.h
-
10include/http/request.h
-
5include/http/request/parser.h
-
10include/http/response.h
-
13include/http/worker.h
-
5src/Makefile.am
-
47src/http/header/add.c
-
61src/http/message.c
-
30src/http/message/helper.c
-
36src/http/request.c
-
4src/http/request/has_keep_alive.c
-
38src/http/request/parser/get_body.c
-
4src/http/request/parser/get_header.c
-
5src/http/request/parser/get_request_line.c
-
32src/http/request/parser/parse.c
-
44src/http/request/parser/read.c
-
39src/http/response.c
-
20src/http/response/404.c
-
12src/http/response/size_get.c
-
9src/http/response/to_string.c
-
4src/server/run.c
-
333tests/tst-tsearch.c
@ -0,0 +1,28 @@ |
|||
#ifndef __HTTP_MESSAGE__ |
|||
#define __HTTP_MESSAGE__ |
|||
|
|||
#include "class.h" |
|||
#include "http/header.h" |
|||
|
|||
typedef enum e_HttpMessageType { |
|||
HTTP_MESSAGE_BUFFERED=0, |
|||
HTTP_MESSAGE_PIPED |
|||
} HttpMessageType; |
|||
|
|||
|
|||
CLASS(HttpMessage) { |
|||
char * version; |
|||
|
|||
HttpHeader header; |
|||
|
|||
HttpMessageType type; |
|||
union { |
|||
char * buffer; |
|||
int handle; |
|||
} body; |
|||
int nbody; |
|||
}; |
|||
|
|||
#endif // __HTTP_MESSAGE__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,13 @@ |
|||
#ifndef __HTTP_WORKER_H__ |
|||
#define __HTTP_WORKER_H__ |
|||
|
|||
#include "class.h" |
|||
#include "http/request/parser.h" |
|||
|
|||
CLASS(HttpWorker) { |
|||
HttpRequestParser parser; |
|||
}; |
|||
|
|||
#endif // __HTTP_WORKER_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,47 @@ |
|||
#include <search.h> |
|||
#include <stdlib.h> |
|||
#include <ctype.h> |
|||
#include <stdio.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
#include "http/header.h" |
|||
|
|||
static |
|||
inline |
|||
unsigned long |
|||
sdbm(const unsigned char * str) |
|||
{ |
|||
unsigned long hash = 0; |
|||
int c; |
|||
|
|||
while ((c = tolower(*str++))) |
|||
hash = c + (hash << 6) + (hash << 16) - hash; |
|||
|
|||
return hash; |
|||
} |
|||
|
|||
static |
|||
inline |
|||
int |
|||
comp(const void * _a, const void * _b) |
|||
{ |
|||
HttpHeader a = (HttpHeader)_a; |
|||
HttpHeader b = (HttpHeader)_b; |
|||
return (a->hash < b->hash)? -1 : (a->hash > b->hash)? 1 : 0; |
|||
} |
|||
|
|||
void |
|||
httpHeaderAdd(const HttpHeader * root, HttpHeader header) |
|||
{ |
|||
HttpHeader * found = tsearch(header, (void **)root, comp); |
|||
|
|||
if (*found != header) { |
|||
puts("uhh, duplicate header set. " |
|||
"This is not implemented right now. " |
|||
"Keep the first one found."); |
|||
delete(&header); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,61 @@ |
|||
#ifndef _GNU_SOURCE |
|||
#define _GNU_SOURCE |
|||
#endif |
|||
|
|||
#include <search.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdarg.h> |
|||
#include <unistd.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
#include "http/message.h" |
|||
#include "message/helper.c" |
|||
|
|||
|
|||
static |
|||
void |
|||
ctor(void * _this, va_list * params) |
|||
{ |
|||
HttpMessage this = _this; |
|||
char * version = va_arg(* params, char *); |
|||
|
|||
this->version = calloc(1, strlen(version)+1); |
|||
strcpy(this->version, version); |
|||
} |
|||
|
|||
static |
|||
void |
|||
dtor(void * _this) |
|||
{ |
|||
HttpMessage this = _this; |
|||
|
|||
_free((void **)&(this->version)); |
|||
|
|||
/** |
|||
* this is a GNU extension...anyway on most non |
|||
* GNUish systems i would not use tsearch anyway |
|||
* as the trees will be unbalanced. |
|||
*/ |
|||
tdestroy(this->header, tDelete); |
|||
|
|||
switch (this->type) { |
|||
case HTTP_MESSAGE_BUFFERED: |
|||
_free((void **)&((this->body).buffer)); |
|||
break; |
|||
|
|||
case HTTP_MESSAGE_PIPED: |
|||
close((this->body).handle); |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
INIT_IFACE(Class, ctor, dtor, NULL); |
|||
CREATE_CLASS(HttpMessage, NULL, IFACE(Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,30 @@ |
|||
#ifndef __HTTP_MESSAGE_HELPER_C__ |
|||
#define __HTTP_MESSAGE_HELPER_C__ |
|||
|
|||
#include <stdlib.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
|
|||
static |
|||
inline |
|||
void |
|||
_free(void ** data) |
|||
{ |
|||
if (NULL != *data) { |
|||
free(*data); |
|||
} |
|||
} |
|||
|
|||
static |
|||
inline |
|||
void |
|||
tDelete(void * node) |
|||
{ |
|||
delete(&node); |
|||
} |
|||
|
|||
#endif // __HTTP_MESSAGE_HELPER_C__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,38 @@ |
|||
#include <stdlib.h> |
|||
|
|||
#include "http/header.h" |
|||
#include "http/message.h" |
|||
#include "http/request/parser.h" |
|||
|
|||
void |
|||
httpRequestParserGetBody(HttpRequestParser this) |
|||
{ |
|||
HttpMessage message = (HttpMessage)(this->cur_request); |
|||
char * nbody; |
|||
|
|||
if (0 == message->nbody) { |
|||
nbody = httpHeaderGet( |
|||
&(message->header), |
|||
"Content-Length"); |
|||
|
|||
if (NULL == nbody) { |
|||
this->state = HTTP_REQUEST_DONE; |
|||
return; |
|||
} |
|||
else { |
|||
message->type = HTTP_MESSAGE_BUFFERED; |
|||
message->nbody = atoi(nbody); |
|||
} |
|||
} |
|||
|
|||
if (REMAINS(this) >= message->nbody) { |
|||
(message->body).buffer = calloc(1, message->nbody + 1); |
|||
memcpy((message->body).buffer, |
|||
this->cur_data, |
|||
message->nbody); |
|||
this->cur_data += message->nbody; |
|||
this->state = HTTP_REQUEST_DONE; |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,44 @@ |
|||
#include <stdlib.h> |
|||
#include <unistd.h> |
|||
|
|||
#include "http/request/parser.h" |
|||
|
|||
|
|||
size_t |
|||
httpRequestParserRead(HttpRequestParser this, int fd) |
|||
{ |
|||
size_t remaining, chunks; |
|||
char buffer[1024]; |
|||
|
|||
ssize_t size = read(fd, buffer, 1024); |
|||
|
|||
if (0 < size) { |
|||
remaining = this->buffer_used % HTTP_REQUEST_PARSER_READ_CHUNK; |
|||
chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK; |
|||
|
|||
/** |
|||
* because a division always rounds down |
|||
* chunks holds exactly the currently allocated chunks if |
|||
* remaining equals 0 but there is no space left. |
|||
* Else chunks holds the actually allocated amount of chunks |
|||
* minus 1. |
|||
* For this reason chunks always has to be increased by 1. |
|||
*/ |
|||
chunks++; |
|||
|
|||
if (size >= remaining) { |
|||
this->buffer = |
|||
realloc(this->buffer, chunks * HTTP_REQUEST_PARSER_READ_CHUNK); |
|||
} |
|||
|
|||
memcpy(this->buffer + this->buffer_used, buffer, size); |
|||
this->buffer_used += size; |
|||
this->buffer[this->buffer_used] = 0; |
|||
|
|||
httpRequestParserParse(this); |
|||
} |
|||
|
|||
return size; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,333 @@ |
|||
/* Test program for tsearch et al. |
|||
Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc. |
|||
This file is part of the GNU C Library. |
|||
|
|||
The GNU C Library is free software; you can redistribute it and/or |
|||
modify it under the terms of the GNU Lesser General Public |
|||
License as published by the Free Software Foundation; either |
|||
version 2.1 of the License, or (at your option) any later version. |
|||
|
|||
The GNU C Library is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|||
Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public |
|||
License along with the GNU C Library; if not, write to the Free |
|||
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
|||
02111-1307 USA. */ |
|||
|
|||
#ifndef _GNU_SOURCE |
|||
# define _GNU_SOURCE 1 |
|||
#endif |
|||
|
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <search.h> |
|||
|
|||
#define SEED 0 |
|||
#define BALANCED 1 |
|||
#define PASSES 100 |
|||
|
|||
#if BALANCED |
|||
#include <math.h> |
|||
#define SIZE 1000 |
|||
#else |
|||
#define SIZE 100 |
|||
#endif |
|||
|
|||
enum order |
|||
{ |
|||
ascending, |
|||
descending, |
|||
randomorder |
|||
}; |
|||
|
|||
enum action |
|||
{ |
|||
build, |
|||
build_and_del, |
|||
delete, |
|||
find |
|||
}; |
|||
|
|||
/* Set to 1 if a test is flunked. */ |
|||
static int error = 0; |
|||
|
|||
/* The keys we add to the tree. */ |
|||
static int x[SIZE]; |
|||
|
|||
/* Pointers into the key array, possibly permutated, to define an order |
|||
for insertion/removal. */ |
|||
static int y[SIZE]; |
|||
|
|||
/* Flags set for each element visited during a tree walk. */ |
|||
static int z[SIZE]; |
|||
|
|||
/* Depths for all the elements, to check that the depth is constant for |
|||
all three visits. */ |
|||
static int depths[SIZE]; |
|||
|
|||
/* Maximum depth during a tree walk. */ |
|||
static int max_depth; |
|||
|
|||
/* Compare two keys. */ |
|||
static int |
|||
cmp_fn (const void *a, const void *b) |
|||
{ |
|||
return *(const int *) a - *(const int *) b; |
|||
} |
|||
|
|||
/* Permute an array of integers. */ |
|||
static void |
|||
memfry (int *string) |
|||
{ |
|||
int i; |
|||
|
|||
for (i = 0; i < SIZE; ++i) |
|||
{ |
|||
int32_t j; |
|||
int c; |
|||
|
|||
j = random () % SIZE; |
|||
|
|||
c = string[i]; |
|||
string[i] = string[j]; |
|||
string[j] = c; |
|||
} |
|||
} |
|||
|
|||
static void |
|||
walk_action (const void *nodep, const VISIT which, const int depth) |
|||
{ |
|||
int key = **(int **) nodep; |
|||
|
|||
if (depth > max_depth) |
|||
max_depth = depth; |
|||
if (which == leaf || which == preorder) |
|||
{ |
|||
++z[key]; |
|||
depths[key] = depth; |
|||
} |
|||
else |
|||
{ |
|||
if (depths[key] != depth) |
|||
{ |
|||
fputs ("Depth for one element is not constant during tree walk.\n", |
|||
stdout); |
|||
} |
|||
} |
|||
} |
|||
|
|||
static void |
|||
walk_tree (void *root, int expected_count) |
|||
{ |
|||
int i; |
|||
|
|||
memset (z, 0, sizeof z); |
|||
max_depth = 0; |
|||
|
|||
twalk (root, walk_action); |
|||
for (i = 0; i < expected_count; ++i) |
|||
if (z[i] != 1) |
|||
{ |
|||
fputs ("Node was not visited.\n", stdout); |
|||
error = 1; |
|||
} |
|||
|
|||
#if BALANCED |
|||
if (max_depth > log (expected_count) * 2 + 2) |
|||
#else |
|||
if (max_depth > expected_count) |
|||
#endif |
|||
{ |
|||
fputs ("Depth too large during tree walk.\n", stdout); |
|||
error = 1; |
|||
} |
|||
} |
|||
|
|||
/* Perform an operation on a tree. */ |
|||
static void |
|||
mangle_tree (enum order how, enum action what, void **root, int lag) |
|||
{ |
|||
int i; |
|||
|
|||
if (how == randomorder) |
|||
{ |
|||
for (i = 0; i < SIZE; ++i) |
|||
y[i] = i; |
|||
memfry (y); |
|||
} |
|||
|
|||
for (i = 0; i < SIZE + lag; ++i) |
|||
{ |
|||
void *elem; |
|||
int j, k; |
|||
|
|||
switch (how) |
|||
{ |
|||
case randomorder: |
|||
if (i >= lag) |
|||
k = y[i - lag]; |
|||
else |
|||
/* Ensure that the array index is within bounds. */ |
|||
k = y[(SIZE - i - 1 + lag) % SIZE]; |
|||
j = y[i % SIZE]; |
|||
break; |
|||
|
|||
case ascending: |
|||
k = i - lag; |
|||
j = i; |
|||
break; |
|||
|
|||
case descending: |
|||
k = SIZE - i - 1 + lag; |
|||
j = SIZE - i - 1; |
|||
break; |
|||
|
|||
default: |
|||
/* This never should happen, but gcc isn't smart enough to |
|||
recognize it. */ |
|||
abort (); |
|||
} |
|||
|
|||
switch (what) |
|||
{ |
|||
case build_and_del: |
|||
case build: |
|||
if (i < SIZE) |
|||
{ |
|||
if (tfind (x + j, (void *const *) root, cmp_fn) != NULL) |
|||
{ |
|||
fputs ("Found element which is not in tree yet.\n", stdout); |
|||
error = 1; |
|||
} |
|||
elem = tsearch (x + j, root, cmp_fn); |
|||
if (elem == 0 |
|||
|| tfind (x + j, (void *const *) root, cmp_fn) == NULL) |
|||
{ |
|||
fputs ("Couldn't find element after it was added.\n", |
|||
stdout); |
|||
error = 1; |
|||
} |
|||
} |
|||
|
|||
if (what == build || i < lag) |
|||
break; |
|||
|
|||
j = k; |
|||
/* fall through */ |
|||
|
|||
case delete: |
|||
elem = tfind (x + j, (void *const *) root, cmp_fn); |
|||
if (elem == NULL || tdelete (x + j, root, cmp_fn) == NULL) |
|||
{ |
|||
fputs ("Error deleting element.\n", stdout); |
|||
error = 1; |
|||
} |
|||
break; |
|||
|
|||
case find: |
|||
if (tfind (x + j, (void *const *) root, cmp_fn) == NULL) |
|||
{ |
|||
fputs ("Couldn't find element after it was added.\n", stdout); |
|||
error = 1; |
|||
} |
|||
break; |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
int |
|||
main (int argc, char **argv) |
|||
{ |
|||
int total_error = 0; |
|||
static char state[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
|||
void *root = NULL; |
|||
int i, j; |
|||
|
|||
initstate (SEED, state, 8); |
|||
|
|||
for (i = 0; i < SIZE; ++i) |
|||
x[i] = i; |
|||
|
|||
/* Do this loop several times to get different permutations for the |
|||
random case. */ |
|||
fputs ("Series I\n", stdout); |
|||
for (i = 0; i < PASSES; ++i) |
|||
{ |
|||
fprintf (stdout, "Pass %d... ", i + 1); |
|||
fflush (stdout); |
|||
error = 0; |
|||
|
|||
mangle_tree (ascending, build, &root, 0); |
|||
mangle_tree (ascending, find, &root, 0); |
|||
mangle_tree (descending, find, &root, 0); |
|||
mangle_tree (randomorder, find, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (ascending, delete, &root, 0); |
|||
|
|||
mangle_tree (ascending, build, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (descending, delete, &root, 0); |
|||
|
|||
mangle_tree (ascending, build, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (randomorder, delete, &root, 0); |
|||
|
|||
mangle_tree (descending, build, &root, 0); |
|||
mangle_tree (ascending, find, &root, 0); |
|||
mangle_tree (descending, find, &root, 0); |
|||
mangle_tree (randomorder, find, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (descending, delete, &root, 0); |
|||
|
|||
mangle_tree (descending, build, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (descending, delete, &root, 0); |
|||
|
|||
mangle_tree (descending, build, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (randomorder, delete, &root, 0); |
|||
|
|||
mangle_tree (randomorder, build, &root, 0); |
|||
mangle_tree (ascending, find, &root, 0); |
|||
mangle_tree (descending, find, &root, 0); |
|||
mangle_tree (randomorder, find, &root, 0); |
|||
walk_tree (root, SIZE); |
|||
mangle_tree (randomorder, delete, &root, 0); |
|||
|
|||
for (j = 1; j < SIZE; j *= 2) |
|||
{ |
|||
mangle_tree (randomorder, build_and_del, &root, j); |
|||
} |
|||
|
|||
fputs (error ? " failed!\n" : " ok.\n", stdout); |
|||
total_error |= error; |
|||
} |
|||
|
|||
fputs ("Series II\n", stdout); |
|||
for (i = 1; i < SIZE; i *= 2) |
|||
{ |
|||
fprintf (stdout, "For size %d... ", i); |
|||
fflush (stdout); |
|||
error = 0; |
|||
|
|||
mangle_tree (ascending, build_and_del, &root, i); |
|||
mangle_tree (descending, build_and_del, &root, i); |
|||
mangle_tree (ascending, build_and_del, &root, i); |
|||
mangle_tree (descending, build_and_del, &root, i); |
|||
mangle_tree (ascending, build_and_del, &root, i); |
|||
mangle_tree (descending, build_and_del, &root, i); |
|||
mangle_tree (ascending, build_and_del, &root, i); |
|||
mangle_tree (descending, build_and_del, &root, i); |
|||
|
|||
fputs (error ? " failed!\n" : " ok.\n", stdout); |
|||
total_error |= error; |
|||
} |
|||
|
|||
return total_error; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue