12 changed files with 233 additions and 88 deletions
-
54ChangeLog
-
17include/http/request.h
-
32include/http/request_parser.h
-
6include/http/request_queue.h
-
19include/interface/stream_reader.h
-
6include/server.h
-
5src/Makefile.am
-
111src/http/request_parser.c
-
19src/interface/stream_reader.c
-
2src/server.c
-
28src/server/run.c
-
22src/testserver.c
@ -1,32 +1,28 @@ |
|||
#ifndef __HTTP_REQUEST_PARSER_H__ |
|||
#define __HTTP_REQUEST_PARSER_H__ |
|||
|
|||
#include "cclass.h" |
|||
#include "server.h" |
|||
#include "http/request.h" |
|||
#include "http/request_queue.h" |
|||
#include "class.h" |
|||
//#include "http/request.h" |
|||
//#include "http/request_queue.h" |
|||
|
|||
#define HTTP_REQUEST_PARSER_READ_CHUNK 1024 |
|||
|
|||
#define HTTP_REQUEST_PARSER_START 0 |
|||
#define HTTP_REQUEST_PARSER_REQUEST_LINE_DONE 1 |
|||
#define HTTP_REQUEST_PARSER_HEADERS_DONE 2 |
|||
#define HTTP_REQUEST_PARSER_DONE 3 |
|||
typedef enum e_HttpRequestState { |
|||
HTTP_REQUEST_START=0, |
|||
HTTP_REQUEST_REQEUST_LINE_DONE, |
|||
HTTP_REQUEST_HEADERS_DONE, |
|||
HTTP_REQUEST_DONE |
|||
} HttpRequestState; |
|||
|
|||
|
|||
CLASS(HTTP_REQUEST_PARSER) { |
|||
server_read_hook get_data; |
|||
CLASS(HttpRequestParser) { |
|||
char * buffer; |
|||
size_t buffer_used; |
|||
|
|||
char * buffer; |
|||
size_t buffer_used; |
|||
|
|||
HTTP_REQUEST_QUEUE request_queue; |
|||
|
|||
unsigned char state; |
|||
//HttpRequestQueue request_queue; |
|||
HttpRequestState state; |
|||
}; |
|||
|
|||
void http_request_parser_parse(const char * buffer, size_t size); |
|||
|
|||
#endif /* __HTTP_REQUEST_PARSER_H__ */ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,19 @@ |
|||
#ifndef __STREAM_READER_H__ |
|||
#define __STREAM_READER_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
typedef size_t (* fptr_streamReaderRead)(void *, int fd); |
|||
|
|||
extern const struct interface i_StreamReader; |
|||
|
|||
struct i_StreamReader { |
|||
const struct interface * const _; |
|||
fptr_streamReaderRead read; |
|||
}; |
|||
|
|||
extern size_t streamReaderRead(void *, int fd); |
|||
|
|||
#endif // __STREAM_READER_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,51 +1,108 @@ |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include <sys/types.h> |
|||
|
|||
#include "cclass.h" |
|||
#include "http/request.h" |
|||
#include "class.h" |
|||
#include "http/request_parser.h" |
|||
|
|||
|
|||
INIT_CLASS(HTTP_REQUEST_PARSER); |
|||
|
|||
|
|||
__construct(LOGGER) |
|||
#include "interface/class.h" |
|||
#include "interface/stream_reader.h" |
|||
//#include "http/request.h" |
|||
//#include "http/request_queue.h" |
|||
|
|||
static |
|||
void |
|||
httpRequestParserParse(char * data, size_t * size); |
|||
|
|||
static |
|||
void |
|||
ctor(void * _this, va_list * params) |
|||
{ |
|||
this->request_queue = va_arg(*params, HTTP_REQUEST_QUEUE); |
|||
HttpRequestParser this = _this; |
|||
|
|||
//this->request_queue = va_arg(*params, HttpRequestQueue); |
|||
|
|||
this->buffer = malloc(HTTP_REQUEST_PARSER_READ_CHUNK); |
|||
} |
|||
|
|||
__destruct(LOGGER) |
|||
static |
|||
void |
|||
dtor(void * _this) |
|||
{ |
|||
HttpRequestParser this = _this; |
|||
|
|||
free(this->buffer); |
|||
} |
|||
|
|||
__jsonConst(LOGGER) {} |
|||
__toJson(LOGGER) {} |
|||
__clear(LOGGER) {} |
|||
|
|||
|
|||
static void |
|||
get_data(HTTP_REQUEST_PARSER this, const char * data, size_t size) |
|||
static |
|||
void |
|||
_clone(void * _this, void * _base) |
|||
{ |
|||
size_t remaining, chunks; |
|||
HttpRequestParser this = _this; |
|||
HttpRequestParser base = _base; |
|||
size_t chunks; |
|||
|
|||
//this->request_queue = base->request_queue; |
|||
this->buffer_used = base->buffer_used; |
|||
|
|||
remaining = this->buffer_used % HTTP_REQUEST_PARSER_READ_CHUNK; |
|||
chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK; |
|||
chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK; |
|||
chunks++; |
|||
|
|||
chunks = (0 == remaining) ? chunks++ : chunks; |
|||
this->buffer = malloc(chunks * HTTP_REQUEST_PARSER_READ_CHUNK); |
|||
memcpy(this->buffer, base->buffer, this->buffer_used); |
|||
} |
|||
|
|||
if (size > remaining) { |
|||
this->buffer = |
|||
realloc(this->buffer, chunks * HTTP_REQUEST_PARSER_READ_CHUNK); |
|||
static |
|||
size_t |
|||
get_data(void * _this, int fd) |
|||
{ |
|||
HttpRequestParser this = _this; |
|||
size_t remaining, chunks; |
|||
char buffer[1024]; |
|||
|
|||
size_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; |
|||
|
|||
httpRequestParserParse(this->buffer, &this->buffer_used); |
|||
} |
|||
|
|||
memcpy(this->buffer + this->buffer_used, data, size); |
|||
this->buffer_used += size; |
|||
return size; |
|||
} |
|||
|
|||
void http_request_parser_parse(const char * data, size_t size) |
|||
INIT_IFACE(Class, ctor, dtor, _clone); |
|||
INIT_IFACE(StreamReader, get_data); |
|||
CREATE_CLASS(HttpRequestParser, NULL, IFACE(Class), IFACE(StreamReader)); |
|||
|
|||
static |
|||
void |
|||
httpRequestParserParse(char * data, size_t * size) |
|||
{ |
|||
data[*size] = 0; |
|||
printf("%s", data); |
|||
*size = 0; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,19 @@ |
|||
#include "class.h" |
|||
#include "interface/stream_reader.h" |
|||
|
|||
const struct interface i_StreamReader = { |
|||
"streamReader", |
|||
1 |
|||
}; |
|||
|
|||
size_t |
|||
streamReaderRead(void * object, int fd) |
|||
{ |
|||
size_t ret; |
|||
|
|||
RETCALL(object, StreamReader, read, ret, fd); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue