diff --git a/src/http/request_parser.c b/src/http/request_parser.c index 04f71e5..f1b899d 100644 --- a/src/http/request_parser.c +++ b/src/http/request_parser.c @@ -101,49 +101,28 @@ CREATE_CLASS(HttpRequestParser, NULL, IFACE(Class), IFACE(StreamReader)); static inline -int -httpRequestLineGet(HttpRequestParser this, char ** line) +char * +httpRequestLineGet(char ** data) { - char * line_end = strstr(this->buffer, "\r\n"); + char * line_end = strstr(*data, "\r\n"); + char * ret = *data; if (NULL == line_end) { - *line = NULL; - return -1; + return NULL; } *line_end = 0; - *line = malloc(strlen(this->buffer) + 1); - strcpy(*line, this->buffer); + *data = line_end + 2; - line_end+=2; - - memmove(this->buffer, - line_end, - this->buffer_used - (line_end-this->buffer) + 1); - - this->buffer_used -= line_end-this->buffer; - - return line_end-this->buffer-2; + return ret; } static inline void -httpRequestSkip(HttpRequestParser this) +httpRequestSkip(char ** data) { - size_t skip; - - for (skip = 0; - 0 != this->buffer[skip] - && ! isalpha(this->buffer[skip]) - && this->buffer_used > skip; - skip++); - - memmove(this->buffer, - &this->buffer[skip], - this->buffer_used - skip + 1); - - this->buffer_used -= skip; + for (; 0 != **data && ! isalpha(**data); *data++); } static @@ -151,9 +130,9 @@ void httpRequestParserParse(HttpRequestParser this) { //static HttpRequest request = NULL; + char * data = this->buffer; char * line; int cont = 1; - int length; //if(NULL == HttpRequest) { // request = new(HttpRequest); @@ -163,38 +142,35 @@ httpRequestParserParse(HttpRequestParser this) switch(this->state) { case HTTP_REQUEST_GARBAGE: puts("==skip garbage=="); - httpRequestSkip(this); + httpRequestSkip(&data); this->state = HTTP_REQUEST_START; break; case HTTP_REQUEST_START: puts("==request line=="); - if (-1 == httpRequestLineGet(this, &line)) { + if (NULL == (line = httpRequestLineGet(&data))) { cont = 0; break; } printf("%s\n", line); - free(line); this->state = HTTP_REQUEST_REQEUST_LINE_DONE; break; case HTTP_REQUEST_REQEUST_LINE_DONE: puts("==read header=="); - if (-1 == (length = httpRequestLineGet(this, &line))) { + if (NULL == (line = httpRequestLineGet(&data))) { cont = 0; break; } - if (0 == length) { - free(line); + if (0 == strlen(line)) { this->state = HTTP_REQUEST_HEADERS_DONE; break; } printf("%s\n", line); - free(line); break; case HTTP_REQUEST_HEADERS_DONE: @@ -205,10 +181,18 @@ httpRequestParserParse(HttpRequestParser this) case HTTP_REQUEST_DONE: puts("==request done=="); + memmove(this->buffer, + data, + this->buffer_used - (data - this->buffer) + 1); + + this->buffer_used -= data - this->buffer; + if (0 == this->buffer_used) { cont = 0; } + this->state = HTTP_REQUEST_GARBAGE; + break; default: