server 0.0.1
basicserverinfrastructure

src/http/request/parser.c

Go to the documentation of this file.
00001 #include <string.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 
00005 #include "class.h"
00006 #include "http/request_parser.h"
00007 #include "interface/class.h"
00008 #include "interface/stream_reader.h"
00009 #include "http/request.h"
00010 #include "http/request_queue.h"
00011 
00012 void httpRequestParserParse(HttpRequestParser);
00013 
00014 static
00015 void
00016 ctor(void * _this, va_list * params)
00017 {
00018         HttpRequestParser this = _this;
00019 
00020         this->request_queue = new(HttpRequestQueue);
00021 
00022         this->buffer = malloc(HTTP_REQUEST_PARSER_READ_CHUNK);
00023         this->buffer[0] = 0;
00024 }
00025 
00026 static
00027 void
00028 dtor(void * _this)
00029 {
00030         HttpRequestParser this = _this;
00031 
00032         free(this->buffer);
00033         delete(&(this->request_queue));
00034 } 
00035 
00036 static
00037 void
00038 _clone(void * _this, void * _base)
00039 {
00040         HttpRequestParser this = _this;
00041         HttpRequestParser base = _base;
00042         size_t            chunks;
00043 
00047         this->request_queue = new(HttpRequestQueue);
00048         this->buffer_used   = base->buffer_used;
00049 
00050         chunks = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK;
00051         chunks++;
00052 
00053         this->buffer = malloc(chunks * HTTP_REQUEST_PARSER_READ_CHUNK);
00054         memcpy(this->buffer, base->buffer, this->buffer_used);
00055 }
00056 
00057 static
00058 size_t
00059 get_data(void * _this, int fd)
00060 {
00061         HttpRequestParser this = _this;
00062         size_t            remaining, chunks;
00063         char              buffer[1024];
00064 
00065         size_t size = read(fd, buffer, 1024);
00066 
00067         if (0 < size) {
00068                 remaining = this->buffer_used % HTTP_REQUEST_PARSER_READ_CHUNK;
00069                 chunks    = this->buffer_used / HTTP_REQUEST_PARSER_READ_CHUNK;
00070 
00079                 chunks++;
00080 
00081                 if (size >= remaining) {
00082                         this->buffer =
00083                                 realloc(this->buffer, chunks * HTTP_REQUEST_PARSER_READ_CHUNK);
00084                 }
00085 
00086                 memcpy(this->buffer + this->buffer_used, buffer, size);
00087                 this->buffer_used += size;
00088                 this->buffer[this->buffer_used] = 0;
00089 
00090                 httpRequestParserParse(this);
00091         }
00092 
00093         return size;
00094 }
00095 
00096 INIT_IFACE(Class, ctor, dtor, _clone);
00097 INIT_IFACE(StreamReader, get_data);
00098 CREATE_CLASS(HttpRequestParser, NULL, IFACE(Class), IFACE(StreamReader));
00099 
00100 // vim: set ts=4 sw=4:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines