Commit 6591c0a6c7a13fb431b8dd4e154ac7c87d6be67f

Authored by Georg Hopp
1 parent 68978882

changed callocs of memory not needed to be initialized to malloc

@@ -38,13 +38,11 @@ CLASS(HttpHeader) { @@ -38,13 +38,11 @@ CLASS(HttpHeader) {
38 size_t nname; //!< len of name without \0 38 size_t nname; //!< len of name without \0
39 size_t nvalue[N_VALUES]; //!< len of value without \0 39 size_t nvalue[N_VALUES]; //!< len of value without \0
40 size_t cvalue; //!< count of values up to N_VALUE 40 size_t cvalue; //!< count of values up to N_VALUE
  41 + size_t size; //!< full size of this header
41 }; 42 };
42 43
43 -HttpHeader httpHeaderParse(char * line); // @INFO: destructive  
44 -  
45 HttpHeader httpHeaderAdd(const HttpHeader *, HttpHeader); 44 HttpHeader httpHeaderAdd(const HttpHeader *, HttpHeader);
46 HttpHeader httpHeaderGet(const HttpHeader *, const char *, size_t); 45 HttpHeader httpHeaderGet(const HttpHeader *, const char *, size_t);
47 -size_t httpHeaderSizeGet(HttpHeader);  
48 size_t httpHeaderToString(HttpHeader, char *); 46 size_t httpHeaderToString(HttpHeader, char *);
49 47
50 #endif // __HTTP_HEADER_H__ 48 #endif // __HTTP_HEADER_H__
@@ -63,8 +63,10 @@ CLASS(HttpRequestParser) { @@ -63,8 +63,10 @@ CLASS(HttpRequestParser) {
63 ssize_t httpRequestParserParse(HttpRequestParser, int); 63 ssize_t httpRequestParserParse(HttpRequestParser, int);
64 void httpRequestParserGetBody(HttpRequestParser); 64 void httpRequestParserGetBody(HttpRequestParser);
65 65
66 -void httpRequestParserGetRequestLine(HttpRequest, char *);  
67 -void httpRequestParserGetHeader(HttpRequest, char *, char *); 66 +void httpRequestParserGetRequestLine(
  67 + HttpRequest, const char *, const char *);
  68 +void httpRequestParserGetHeader(
  69 + HttpRequest, const char *, const char *);
68 70
69 #endif // __HTTP_REQUEST_PARSER_H__ 71 #endif // __HTTP_REQUEST_PARSER_H__
70 72
@@ -29,7 +29,7 @@ WORKER = http/worker.c http/worker/process.c http/worker/write.c \ @@ -29,7 +29,7 @@ WORKER = http/worker.c http/worker/process.c http/worker/write.c \
29 http/worker/get_asset.c http/worker/add_common_header.c 29 http/worker/get_asset.c http/worker/add_common_header.c
30 WRITER = http/response/writer.c http/response/writer/write.c 30 WRITER = http/response/writer.c http/response/writer/write.c
31 HEADER = http/header.c http/header/get.c http/header/add.c \ 31 HEADER = http/header.c http/header/get.c http/header/add.c \
32 - http/header/size_get.c http/header/to_string.c 32 + http/header/to_string.c
33 PARSER = http/request/parser.c http/request/parser/get_header.c \ 33 PARSER = http/request/parser.c http/request/parser/get_header.c \
34 http/request/parser/parse.c http/request/parser/get_request_line.c \ 34 http/request/parser/parse.c http/request/parser/get_request_line.c \
35 http/request/parser/get_body.c 35 http/request/parser/get_body.c
@@ -43,14 +43,17 @@ httpHeaderCtor(void * _this, va_list * params) { @@ -43,14 +43,17 @@ httpHeaderCtor(void * _this, va_list * params) {
43 value = va_arg(* params, char *); 43 value = va_arg(* params, char *);
44 this->nvalue[0] = va_arg(* params, size_t); 44 this->nvalue[0] = va_arg(* params, size_t);
45 45
46 - this->name = malloc(this->nname); 46 + this->name = malloc(this->nname + 1);
  47 + this->name[this->nname] = 0;
47 memcpy(this->name, name, this->nname); 48 memcpy(this->name, name, this->nname);
48 49
49 this->hash = sdbm((unsigned char *)name, this->nname); 50 this->hash = sdbm((unsigned char *)name, this->nname);
50 51
51 - (this->value)[0] = malloc((this->nvalue)[0]); 52 + (this->value)[0] = malloc((this->nvalue)[0] + 1);
  53 + (this->value)[0][(this->nvalue)[0]] = 0;
52 memcpy((this->value)[0], value, (this->nvalue)[0]); 54 memcpy((this->value)[0], value, (this->nvalue)[0]);
53 this->cvalue = 1; 55 this->cvalue = 1;
  56 + this->size = this->nname + 2 + (this->nvalue)[0] + 2;
54 57
55 return 0; 58 return 0;
56 } 59 }
@@ -58,6 +58,7 @@ httpHeaderAdd(const HttpHeader * root, HttpHeader header) @@ -58,6 +58,7 @@ httpHeaderAdd(const HttpHeader * root, HttpHeader header)
58 } 58 }
59 (found->nvalue)[found->cvalue] = (header->nvalue)[0]; 59 (found->nvalue)[found->cvalue] = (header->nvalue)[0];
60 (found->value)[(found->cvalue)++] = (header->value)[0]; 60 (found->value)[(found->cvalue)++] = (header->value)[0];
  61 + found->size += header->size;
61 (header->value)[0] = NULL; 62 (header->value)[0] = NULL;
62 delete(header); 63 delete(header);
63 } 64 }
@@ -28,7 +28,7 @@ @@ -28,7 +28,7 @@
28 size_t 28 size_t
29 httpHeaderToString(HttpHeader header, char * string) 29 httpHeaderToString(HttpHeader header, char * string)
30 { 30 {
31 - size_t size = httpHeaderSizeGet(header); 31 + size_t size = header->size;
32 int i; 32 int i;
33 33
34 for (i=0; i<header->cvalue; i++) { 34 for (i=0; i<header->cvalue; i++) {
@@ -37,7 +37,7 @@ void @@ -37,7 +37,7 @@ void
37 addHeaderSize(const void * node, const VISIT which, const int depth) 37 addHeaderSize(const void * node, const VISIT which, const int depth)
38 { 38 {
39 if (endorder == which || leaf == which) { 39 if (endorder == which || leaf == which) {
40 - size += httpHeaderSizeGet(*(HttpHeader *)node); 40 + size += (*(HttpHeader *)node)->size;
41 } 41 }
42 } 42 }
43 43
@@ -55,7 +55,9 @@ httpRequestParserGetBody(HttpRequestParser this) @@ -55,7 +55,9 @@ httpRequestParserGetBody(HttpRequestParser this)
55 else { 55 else {
56 message->type = HTTP_MESSAGE_BUFFERED; 56 message->type = HTTP_MESSAGE_BUFFERED;
57 message->nbody = atoi((clen->value)[0]); 57 message->nbody = atoi((clen->value)[0]);
58 - message->body = calloc(1, message->nbody); 58 + if (0 < message->nbody) {
  59 + message->body = malloc(message->nbody);
  60 + }
59 message->dbody = 0; 61 message->dbody = 0;
60 } 62 }
61 } 63 }
@@ -36,15 +36,15 @@ httpRequestParserGetHeader( @@ -36,15 +36,15 @@ httpRequestParserGetHeader(
36 const char * line, 36 const char * line,
37 const char * line_end) 37 const char * line_end)
38 { 38 {
39 - char * name = line;  
40 - char * value = strchr(line, ':'); 39 + const char * name = line;
  40 + char * value = memchr(line, ':', line_end - line);
41 size_t nname = (value++) - name; 41 size_t nname = (value++) - name;
42 42
43 for (; *value == ' ' && *value != 0; value++); 43 for (; *value == ' ' && *value != 0; value++);
44 44
45 httpHeaderAdd( 45 httpHeaderAdd(
46 &(message->header), 46 &(message->header),
47 - new(HttpHeader, name, nname, value, line_end-value)); 47 + new(HttpHeader, name, nname, value, line_end - value));
48 } 48 }
49 49
50 // vim: set ts=4 sw=4: 50 // vim: set ts=4 sw=4:
@@ -27,45 +27,18 @@ @@ -27,45 +27,18 @@
27 #include "http/request.h" 27 #include "http/request.h"
28 #include "http/message.h" 28 #include "http/message.h"
29 29
30 -#define MAX(x,y) ((x) > (y) ? (x) : (y))  
31 -#define MIN(x,y) ((x) < (y) ? (x) : (y))  
32 -#define MIN_SIZE(x,y) (MAX(strlen((x)), (y)))  
33 -  
34 -enum e_method {  
35 - OPTIONS=0,  
36 - GET,  
37 - HEAD,  
38 - POST,  
39 - PUT,  
40 - DELETE,  
41 - TRACE,  
42 - CONNECT  
43 -};  
44 -  
45 -#define N_METHODS 8  
46 -  
47 -static  
48 -const  
49 -char * method[N_METHODS] = {  
50 - "OPTIONS",  
51 - "GET",  
52 - "HEAD",  
53 - "POST",  
54 - "PUT",  
55 - "DELETE",  
56 - "TRACE",  
57 - "CONNECT"  
58 -};  
59 -  
60 -ssize_t  
61 -httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) 30 +void
  31 +httpRequestParserGetRequestLine(
  32 + HttpRequest request,
  33 + const char * line,
  34 + const char * line_end)
62 { 35 {
63 char * method, * uri, * version; 36 char * method, * uri, * version;
64 HttpMessage message = (HttpMessage)request; 37 HttpMessage message = (HttpMessage)request;
65 int mlen, ulen, vlen; 38 int mlen, ulen, vlen;
66 39
67 method = line; 40 method = line;
68 - uri = strchr(line, ' '); 41 + uri = memchr(line, ' ', line_end - line);
69 42
70 if (NULL == uri) 43 if (NULL == uri)
71 return; 44 return;
@@ -73,7 +46,7 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) @@ -73,7 +46,7 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr)
73 mlen = uri - method; 46 mlen = uri - method;
74 for (; *uri == ' ' && *uri != 0; uri++); 47 for (; *uri == ' ' && *uri != 0; uri++);
75 48
76 - version = strchr(uri, ' '); 49 + version = memchr(uri, ' ', line_end - uri);
77 50
78 if (NULL == version) 51 if (NULL == version)
79 return; 52 return;
@@ -81,13 +54,16 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) @@ -81,13 +54,16 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr)
81 ulen = version - uri; 54 ulen = version - uri;
82 for (; *version == ' ' && *version != 0; version++); 55 for (; *version == ' ' && *version != 0; version++);
83 56
84 - vlen = strlen(version); 57 + vlen = line_end - uri;
85 58
86 - request->method = calloc(1, mlen + 1); 59 + request->method = malloc(mlen + 1);
  60 + request->method[mlen] = 0;
87 memcpy(request->method, method, mlen); 61 memcpy(request->method, method, mlen);
88 - request->uri = calloc(1, ulen + 1); 62 + request->uri = malloc(ulen + 1);
  63 + request->uri[ulen] = 0;
89 memcpy(request->uri, uri, ulen); 64 memcpy(request->uri, uri, ulen);
90 - message->version = calloc(1, vlen + 1); 65 + message->version = malloc(vlen + 1);
  66 + message->version[vlen] = 0;
91 memcpy(message->version, version, vlen); 67 memcpy(message->version, version, vlen);
92 } 68 }
93 69
@@ -86,7 +86,7 @@ httpRequestParserParse(HttpRequestParser this, int fd) @@ -86,7 +86,7 @@ httpRequestParserParse(HttpRequestParser this, int fd)
86 break; 86 break;
87 } 87 }
88 88
89 - httpRequestParserGetRequestLine(this->cur_request, line); 89 + httpRequestParserGetRequestLine(this->cur_request, line, line_end);
90 if (! httpRequestHasValidMethod(this->cur_request)) { 90 if (! httpRequestHasValidMethod(this->cur_request)) {
91 cbufRelease(this->buffer); 91 cbufRelease(this->buffer);
92 this->ourLock = FALSE; 92 this->ourLock = FALSE;
@@ -62,7 +62,7 @@ httpResponse404() @@ -62,7 +62,7 @@ httpResponse404()
62 62
63 message->type = HTTP_MESSAGE_BUFFERED; 63 message->type = HTTP_MESSAGE_BUFFERED;
64 message->nbody = sizeof(RESP_DATA) - 1; 64 message->nbody = sizeof(RESP_DATA) - 1;
65 - message->body = calloc(1, sizeof(RESP_DATA)); 65 + message->body = malloc(sizeof(RESP_DATA));
66 memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)); 66 memcpy(message->body, RESP_DATA, sizeof(RESP_DATA));
67 67
68 nbuf = sprintf(buffer, "%d", message->nbody); 68 nbuf = sprintf(buffer, "%d", message->nbody);
@@ -90,7 +90,7 @@ httpResponseMe(int value) @@ -90,7 +90,7 @@ httpResponseMe(int value)
90 90
91 message->type = HTTP_MESSAGE_BUFFERED; 91 message->type = HTTP_MESSAGE_BUFFERED;
92 message->nbody = sizeof(RESP_DATA)-1-2; 92 message->nbody = sizeof(RESP_DATA)-1-2;
93 - message->body = calloc(1, sizeof(RESP_DATA)-2); 93 + message->body = malloc(sizeof(RESP_DATA)-2);
94 sprintf(message->body, RESP_DATA, value); 94 sprintf(message->body, RESP_DATA, value);
95 95
96 nbuf = sprintf(buffer, "%d", message->nbody); 96 nbuf = sprintf(buffer, "%d", message->nbody);
Please register or login to post a comment