Commit 6591c0a6c7a13fb431b8dd4e154ac7c87d6be67f
1 parent
68978882
changed callocs of memory not needed to be initialized to malloc
Showing
13 changed files
with
37 additions
and
55 deletions
... | ... | @@ -38,13 +38,11 @@ CLASS(HttpHeader) { |
38 | 38 | size_t nname; //!< len of name without \0 |
39 | 39 | size_t nvalue[N_VALUES]; //!< len of value without \0 |
40 | 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 | 44 | HttpHeader httpHeaderAdd(const HttpHeader *, HttpHeader); |
46 | 45 | HttpHeader httpHeaderGet(const HttpHeader *, const char *, size_t); |
47 | -size_t httpHeaderSizeGet(HttpHeader); | |
48 | 46 | size_t httpHeaderToString(HttpHeader, char *); |
49 | 47 | |
50 | 48 | #endif // __HTTP_HEADER_H__ | ... | ... |
... | ... | @@ -63,8 +63,10 @@ CLASS(HttpRequestParser) { |
63 | 63 | ssize_t httpRequestParserParse(HttpRequestParser, int); |
64 | 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 | 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 | 29 | http/worker/get_asset.c http/worker/add_common_header.c |
30 | 30 | WRITER = http/response/writer.c http/response/writer/write.c |
31 | 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 | 33 | PARSER = http/request/parser.c http/request/parser/get_header.c \ |
34 | 34 | http/request/parser/parse.c http/request/parser/get_request_line.c \ |
35 | 35 | http/request/parser/get_body.c | ... | ... |
... | ... | @@ -43,14 +43,17 @@ httpHeaderCtor(void * _this, va_list * params) { |
43 | 43 | value = va_arg(* params, char *); |
44 | 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 | 48 | memcpy(this->name, name, this->nname); |
48 | 49 | |
49 | 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 | 54 | memcpy((this->value)[0], value, (this->nvalue)[0]); |
53 | 55 | this->cvalue = 1; |
56 | + this->size = this->nname + 2 + (this->nvalue)[0] + 2; | |
54 | 57 | |
55 | 58 | return 0; |
56 | 59 | } | ... | ... |
... | ... | @@ -58,6 +58,7 @@ httpHeaderAdd(const HttpHeader * root, HttpHeader header) |
58 | 58 | } |
59 | 59 | (found->nvalue)[found->cvalue] = (header->nvalue)[0]; |
60 | 60 | (found->value)[(found->cvalue)++] = (header->value)[0]; |
61 | + found->size += header->size; | |
61 | 62 | (header->value)[0] = NULL; |
62 | 63 | delete(header); |
63 | 64 | } | ... | ... |
... | ... | @@ -55,7 +55,9 @@ httpRequestParserGetBody(HttpRequestParser this) |
55 | 55 | else { |
56 | 56 | message->type = HTTP_MESSAGE_BUFFERED; |
57 | 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 | 61 | message->dbody = 0; |
60 | 62 | } |
61 | 63 | } | ... | ... |
... | ... | @@ -36,15 +36,15 @@ httpRequestParserGetHeader( |
36 | 36 | const char * line, |
37 | 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 | 41 | size_t nname = (value++) - name; |
42 | 42 | |
43 | 43 | for (; *value == ' ' && *value != 0; value++); |
44 | 44 | |
45 | 45 | httpHeaderAdd( |
46 | 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 | 50 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -27,45 +27,18 @@ |
27 | 27 | #include "http/request.h" |
28 | 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 | 36 | char * method, * uri, * version; |
64 | 37 | HttpMessage message = (HttpMessage)request; |
65 | 38 | int mlen, ulen, vlen; |
66 | 39 | |
67 | 40 | method = line; |
68 | - uri = strchr(line, ' '); | |
41 | + uri = memchr(line, ' ', line_end - line); | |
69 | 42 | |
70 | 43 | if (NULL == uri) |
71 | 44 | return; |
... | ... | @@ -73,7 +46,7 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) |
73 | 46 | mlen = uri - method; |
74 | 47 | for (; *uri == ' ' && *uri != 0; uri++); |
75 | 48 | |
76 | - version = strchr(uri, ' '); | |
49 | + version = memchr(uri, ' ', line_end - uri); | |
77 | 50 | |
78 | 51 | if (NULL == version) |
79 | 52 | return; |
... | ... | @@ -81,13 +54,16 @@ httpRequestParserGetRequestLine(HttpRequestParser this, char * cr) |
81 | 54 | ulen = version - uri; |
82 | 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 | 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 | 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 | 67 | memcpy(message->version, version, vlen); |
92 | 68 | } |
93 | 69 | ... | ... |
... | ... | @@ -86,7 +86,7 @@ httpRequestParserParse(HttpRequestParser this, int fd) |
86 | 86 | break; |
87 | 87 | } |
88 | 88 | |
89 | - httpRequestParserGetRequestLine(this->cur_request, line); | |
89 | + httpRequestParserGetRequestLine(this->cur_request, line, line_end); | |
90 | 90 | if (! httpRequestHasValidMethod(this->cur_request)) { |
91 | 91 | cbufRelease(this->buffer); |
92 | 92 | this->ourLock = FALSE; | ... | ... |
... | ... | @@ -62,7 +62,7 @@ httpResponse404() |
62 | 62 | |
63 | 63 | message->type = HTTP_MESSAGE_BUFFERED; |
64 | 64 | message->nbody = sizeof(RESP_DATA) - 1; |
65 | - message->body = calloc(1, sizeof(RESP_DATA)); | |
65 | + message->body = malloc(sizeof(RESP_DATA)); | |
66 | 66 | memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)); |
67 | 67 | |
68 | 68 | nbuf = sprintf(buffer, "%d", message->nbody); | ... | ... |
... | ... | @@ -90,7 +90,7 @@ httpResponseMe(int value) |
90 | 90 | |
91 | 91 | message->type = HTTP_MESSAGE_BUFFERED; |
92 | 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 | 94 | sprintf(message->body, RESP_DATA, value); |
95 | 95 | |
96 | 96 | nbuf = sprintf(buffer, "%d", message->nbody); | ... | ... |
Please
register
or
login
to post a comment