Commit 1d238aa55d48674707773d95b848a2bca5693749
1 parent
a0ec357e
as i have a lot of static string initialization with the length of that sting a …
…created and used a macro to prevent errors
Showing
14 changed files
with
60 additions
and
167 deletions
... | ... | @@ -23,8 +23,12 @@ |
23 | 23 | #ifndef __UTILS_MEMORY_H__ |
24 | 24 | #define __UTILS_MEMORY_H__ |
25 | 25 | |
26 | +#define CSTRA(val) val, sizeof(val)-1 //!< Const STRing Argument | |
27 | + | |
26 | 28 | #define FREE(val) (ffree((void**)&(val))) |
27 | 29 | |
28 | 30 | void ffree(void **); |
29 | 31 | |
30 | 32 | #endif // __UTILS_MEMORY_H__ |
33 | + | |
34 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -28,6 +28,8 @@ |
28 | 28 | #include "http/request.h" |
29 | 29 | #include "http/header.h" |
30 | 30 | |
31 | +#include "utils/memory.h" | |
32 | + | |
31 | 33 | #ifndef TRUE |
32 | 34 | #define TRUE 1 |
33 | 35 | #endif |
... | ... | @@ -43,10 +45,7 @@ httpMessageHasKeepAlive(HttpMessage message) |
43 | 45 | size_t size; |
44 | 46 | char * value; |
45 | 47 | |
46 | - header = httpHeaderGet( | |
47 | - &(message->header), | |
48 | - "connection", | |
49 | - sizeof("connection")-1); | |
48 | + header = httpHeaderGet(&(message->header), CSTRA("connection")); | |
50 | 49 | |
51 | 50 | if (NULL == header) { |
52 | 51 | return 0; | ... | ... |
... | ... | @@ -29,6 +29,7 @@ |
29 | 29 | #include "http/message.h" |
30 | 30 | #include "http/header.h" |
31 | 31 | |
32 | +#include "utils/memory.h" | |
32 | 33 | |
33 | 34 | HttpResponse |
34 | 35 | httpResponse304( |
... | ... | @@ -47,23 +48,11 @@ httpResponse304( |
47 | 48 | message->body = NULL; |
48 | 49 | |
49 | 50 | httpHeaderAdd(&(message->header), |
50 | - new(HttpHeader, | |
51 | - "Content-Type", | |
52 | - sizeof("Content-Type")-1, | |
53 | - mime, | |
54 | - nmime)); | |
51 | + new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); | |
55 | 52 | httpHeaderAdd(&(message->header), |
56 | - new(HttpHeader, | |
57 | - "ETag", | |
58 | - sizeof("ETag")-1, | |
59 | - etag, | |
60 | - netag)); | |
53 | + new(HttpHeader, CSTRA("ETag"), etag, netag)); | |
61 | 54 | httpHeaderAdd(&(message->header), |
62 | - new(HttpHeader, | |
63 | - "Last-Modified", | |
64 | - sizeof("Last-Modified")-1, | |
65 | - mtime, | |
66 | - nmtime)); | |
55 | + new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); | |
67 | 56 | |
68 | 57 | return response; |
69 | 58 | } | ... | ... |
... | ... | @@ -38,8 +38,6 @@ httpResponse403() |
38 | 38 | { |
39 | 39 | HttpResponse response; |
40 | 40 | HttpMessage message; |
41 | - char buffer[200]; | |
42 | - size_t nbuf; | |
43 | 41 | |
44 | 42 | response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden"); |
45 | 43 | message = (HttpMessage)response; |
... | ... | @@ -48,15 +46,6 @@ httpResponse403() |
48 | 46 | message->nbody = 0; |
49 | 47 | message->body = NULL; |
50 | 48 | |
51 | - nbuf = sprintf(buffer, "%d", message->nbody); | |
52 | - | |
53 | - httpHeaderAdd(&(message->header), | |
54 | - new(HttpHeader, | |
55 | - "Content-Length", | |
56 | - sizeof("Content-Length")-1, | |
57 | - buffer, | |
58 | - nbuf)); | |
59 | - | |
60 | 49 | return response; |
61 | 50 | } |
62 | 51 | ... | ... |
... | ... | @@ -32,6 +32,7 @@ |
32 | 32 | #include "http/message.h" |
33 | 33 | #include "http/header.h" |
34 | 34 | |
35 | +#include "utils/memory.h" | |
35 | 36 | |
36 | 37 | #define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \ |
37 | 38 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \ |
... | ... | @@ -45,35 +46,20 @@ |
45 | 46 | HttpResponse |
46 | 47 | httpResponse404() |
47 | 48 | { |
48 | - char buffer[200]; | |
49 | 49 | HttpResponse response; |
50 | 50 | HttpMessage message; |
51 | - size_t nbuf; | |
52 | 51 | |
53 | 52 | response = new(HttpResponse, "HTTP/1.1", 404, "Not Found"); |
54 | 53 | message = (HttpMessage)response; |
55 | 54 | |
56 | 55 | httpHeaderAdd(&(message->header), |
57 | - new(HttpHeader, | |
58 | - "Content-Type", | |
59 | - sizeof("Content-Type")-1, | |
60 | - "text/html", | |
61 | - sizeof("text/html")-1)); | |
56 | + new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); | |
62 | 57 | |
63 | 58 | message->type = HTTP_MESSAGE_BUFFERED; |
64 | 59 | message->nbody = sizeof(RESP_DATA) - 1; |
65 | 60 | message->body = malloc(sizeof(RESP_DATA)); |
66 | 61 | memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)); |
67 | 62 | |
68 | - nbuf = sprintf(buffer, "%d", message->nbody); | |
69 | - | |
70 | - httpHeaderAdd(&(message->header), | |
71 | - new(HttpHeader, | |
72 | - "Content-Length", | |
73 | - sizeof("Content-Length")-1, | |
74 | - buffer, | |
75 | - nbuf)); | |
76 | - | |
77 | 63 | return response; |
78 | 64 | } |
79 | 65 | ... | ... |
... | ... | @@ -33,6 +33,8 @@ |
33 | 33 | #include "http/message.h" |
34 | 34 | #include "http/header.h" |
35 | 35 | |
36 | +#include "utils/memory.h" | |
37 | + | |
36 | 38 | |
37 | 39 | HttpResponse |
38 | 40 | httpResponseAsset( |
... | ... | @@ -47,8 +49,6 @@ httpResponseAsset( |
47 | 49 | size_t netag; |
48 | 50 | char mtime[200]; |
49 | 51 | size_t nmtime; |
50 | - char clen[200]; | |
51 | - size_t nclen; | |
52 | 52 | struct stat st; |
53 | 53 | HttpResponse response; |
54 | 54 | HttpMessage message; |
... | ... | @@ -72,32 +72,12 @@ httpResponseAsset( |
72 | 72 | message->handle = handle; |
73 | 73 | message->nbody = st.st_size; |
74 | 74 | |
75 | - nclen = sprintf(clen, "%d", message->nbody); | |
76 | - | |
77 | - httpHeaderAdd(&(message->header), | |
78 | - new(HttpHeader, | |
79 | - "Content-Type", | |
80 | - sizeof("Content-Type")-1, | |
81 | - mime, | |
82 | - nmime)); | |
83 | 75 | httpHeaderAdd(&(message->header), |
84 | - new(HttpHeader, | |
85 | - "Content-Length", | |
86 | - sizeof("Content-Length")-1, | |
87 | - clen, | |
88 | - nclen)); | |
76 | + new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); | |
89 | 77 | httpHeaderAdd(&(message->header), |
90 | - new(HttpHeader, | |
91 | - "ETag", | |
92 | - sizeof("ETag")-1, | |
93 | - etag, | |
94 | - netag)); | |
78 | + new(HttpHeader, CSTRA("ETag"), etag, netag)); | |
95 | 79 | httpHeaderAdd(&(message->header), |
96 | - new(HttpHeader, | |
97 | - "Last-Modified", | |
98 | - sizeof("Last-Modified")-1, | |
99 | - mtime, | |
100 | - nmtime)); | |
80 | + new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); | |
101 | 81 | |
102 | 82 | return response; |
103 | 83 | } | ... | ... |
... | ... | @@ -33,6 +33,8 @@ |
33 | 33 | #include "http/message.h" |
34 | 34 | #include "http/header.h" |
35 | 35 | |
36 | +#include "utils/memory.h" | |
37 | + | |
36 | 38 | #define RESP_DATA "<form action=\"/me/\" method=\"POST\">" \ |
37 | 39 | "<input name=\"username\" type=\"text\" />" \ |
38 | 40 | "<input type=\"submit\">" \ |
... | ... | @@ -41,20 +43,14 @@ |
41 | 43 | HttpResponse |
42 | 44 | httpResponseLoginForm() |
43 | 45 | { |
44 | - char buffer[200]; | |
45 | 46 | HttpResponse response; |
46 | 47 | HttpMessage message; |
47 | - size_t nbuf; | |
48 | 48 | |
49 | 49 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
50 | 50 | message = (HttpMessage)response; |
51 | 51 | |
52 | 52 | httpHeaderAdd(&(message->header), |
53 | - new(HttpHeader, | |
54 | - "Content-Type", | |
55 | - sizeof("Content-Type")-1, | |
56 | - "text/html", | |
57 | - sizeof("text/html")-1)); | |
53 | + new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); | |
58 | 54 | |
59 | 55 | message->type = HTTP_MESSAGE_BUFFERED; |
60 | 56 | |
... | ... | @@ -62,15 +58,6 @@ httpResponseLoginForm() |
62 | 58 | message->body = malloc(message->nbody); |
63 | 59 | memcpy(message->body, RESP_DATA, message->nbody); |
64 | 60 | |
65 | - nbuf = sprintf(buffer, "%d", message->nbody); | |
66 | - | |
67 | - httpHeaderAdd(&(message->header), | |
68 | - new(HttpHeader, | |
69 | - "Content-Length", | |
70 | - sizeof("Content-Length")-1, | |
71 | - buffer, | |
72 | - nbuf)); | |
73 | - | |
74 | 61 | return response; |
75 | 62 | } |
76 | 63 | ... | ... |
... | ... | @@ -33,6 +33,8 @@ |
33 | 33 | #include "http/message.h" |
34 | 34 | #include "http/header.h" |
35 | 35 | |
36 | +#include "utils/memory.h" | |
37 | + | |
36 | 38 | |
37 | 39 | #define RESP_DATA "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" \ |
38 | 40 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" \ |
... | ... | @@ -124,32 +126,18 @@ |
124 | 126 | HttpResponse |
125 | 127 | httpResponseMe(char * uname) |
126 | 128 | { |
127 | - char buffer[200]; | |
128 | 129 | HttpResponse response; |
129 | 130 | HttpMessage message; |
130 | - size_t nbuf; | |
131 | 131 | |
132 | 132 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
133 | 133 | message = (HttpMessage)response; |
134 | 134 | |
135 | 135 | httpHeaderAdd(&(message->header), |
136 | - new(HttpHeader, | |
137 | - "Content-Type", | |
138 | - sizeof("Content-Type")-1, | |
139 | - "text/html", | |
140 | - sizeof("text/html")-1)); | |
136 | + new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html"))); | |
141 | 137 | httpHeaderAdd(&(message->header), |
142 | - new(HttpHeader, | |
143 | - "Set-Cookie", | |
144 | - sizeof("Set-Cookie")-1, | |
145 | - "name=\"Georg Hopp\"", | |
146 | - sizeof("name=\"Georg Hopp\"")-1)); | |
138 | + new(HttpHeader, CSTRA("Set-Cookie"), CSTRA("name=Georg+Hopp"))); | |
147 | 139 | httpHeaderAdd(&(message->header), |
148 | - new(HttpHeader, | |
149 | - "Set-Cookie", | |
150 | - sizeof("Set-Cookie")-1, | |
151 | - "profession=\"coder\"", | |
152 | - sizeof("profession=\"coder\"")-1)); | |
140 | + new(HttpHeader, CSTRA("Set-Cookie"), CSTRA("profession=coder"))); | |
153 | 141 | |
154 | 142 | message->type = HTTP_MESSAGE_BUFFERED; |
155 | 143 | message->nbody = sizeof(RESP_DATA)-1-2+strlen(uname); //!< the two are the %s |
... | ... | @@ -157,15 +145,6 @@ httpResponseMe(char * uname) |
157 | 145 | sprintf(message->body, RESP_DATA, uname); |
158 | 146 | //memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)-1); |
159 | 147 | |
160 | - nbuf = sprintf(buffer, "%d", message->nbody); | |
161 | - | |
162 | - httpHeaderAdd(&(message->header), | |
163 | - new(HttpHeader, | |
164 | - "Content-Length", | |
165 | - sizeof("Content-Length")-1, | |
166 | - buffer, | |
167 | - nbuf)); | |
168 | - | |
169 | 148 | return response; |
170 | 149 | } |
171 | 150 | ... | ... |
... | ... | @@ -33,6 +33,8 @@ |
33 | 33 | #include "http/message.h" |
34 | 34 | #include "http/header.h" |
35 | 35 | |
36 | +#include "utils/memory.h" | |
37 | + | |
36 | 38 | #define RESP_DATA "{\"ctime\":%ld,\"vnext\":%ld,\"value\":\"%02d\"}" |
37 | 39 | |
38 | 40 | HttpResponse |
... | ... | @@ -48,11 +50,7 @@ httpResponseRandval(time_t ctime, int value) |
48 | 50 | message = (HttpMessage)response; |
49 | 51 | |
50 | 52 | httpHeaderAdd(&(message->header), |
51 | - new(HttpHeader, | |
52 | - "Content-Type", | |
53 | - sizeof("Content-Type")-1, | |
54 | - "application/json", | |
55 | - sizeof("application/json")-1)); | |
53 | + new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json"))); | |
56 | 54 | |
57 | 55 | message->type = HTTP_MESSAGE_BUFFERED; |
58 | 56 | |
... | ... | @@ -64,15 +62,6 @@ httpResponseRandval(time_t ctime, int value) |
64 | 62 | message->body = malloc(nbuf); |
65 | 63 | memcpy(message->body, buffer, nbuf); |
66 | 64 | |
67 | - nbuf = sprintf(buffer, "%d", message->nbody); | |
68 | - | |
69 | - httpHeaderAdd(&(message->header), | |
70 | - new(HttpHeader, | |
71 | - "Content-Length", | |
72 | - sizeof("Content-Length")-1, | |
73 | - buffer, | |
74 | - nbuf)); | |
75 | - | |
76 | 65 | return response; |
77 | 66 | } |
78 | 67 | ... | ... |
... | ... | @@ -7,49 +7,45 @@ |
7 | 7 | #include "http/message.h" |
8 | 8 | #include "http/response.h" |
9 | 9 | |
10 | +#include "utils/memory.h" | |
11 | + | |
10 | 12 | void |
11 | 13 | httpWorkerAddCommonHeader(HttpMessage request, HttpMessage response) |
12 | 14 | { |
13 | 15 | time_t t; |
14 | 16 | struct tm * tmp; |
15 | 17 | char buffer[200]; |
16 | - size_t ndate; | |
18 | + size_t nbuf; | |
17 | 19 | |
18 | 20 | if (httpMessageHasKeepAlive(request)) { |
19 | 21 | httpHeaderAdd( |
20 | 22 | &(response->header), |
21 | - new(HttpHeader, | |
22 | - "Connection", | |
23 | - sizeof("Connection")-1, | |
24 | - "Keep-Alive", | |
25 | - sizeof("Keep-Alive")-1)); | |
23 | + new(HttpHeader, CSTRA("Connection"), CSTRA("Keep-Alive"))); | |
26 | 24 | } |
27 | 25 | else { |
28 | 26 | httpHeaderAdd( |
29 | 27 | &(response->header), |
30 | - new(HttpHeader, | |
31 | - "Connection", | |
32 | - sizeof("Connection")-1, | |
33 | - "Close", | |
34 | - sizeof("Close")-1)); | |
28 | + new(HttpHeader, CSTRA("Connection"), CSTRA("Close"))); | |
35 | 29 | } |
36 | 30 | |
37 | 31 | httpHeaderAdd(&(response->header), |
38 | - new(HttpHeader, | |
39 | - "Server", | |
40 | - sizeof("Server")-1, | |
41 | - "testserver", | |
42 | - sizeof("testserver")-1)); | |
43 | - | |
44 | - t = time(NULL); | |
45 | - tmp = localtime(&t); | |
46 | - ndate = strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); | |
32 | + new(HttpHeader, CSTRA("Server"), CSTRA("testserver"))); | |
33 | + | |
34 | + switch(((HttpResponse)response)->status) { | |
35 | + case 304: | |
36 | + break; | |
37 | + | |
38 | + default: | |
39 | + nbuf = sprintf(buffer, "%d", response->nbody); | |
40 | + httpHeaderAdd(&(response->header), | |
41 | + new(HttpHeader, CSTRA("Content-Length"), buffer, nbuf)); | |
42 | + } | |
43 | + | |
44 | + t = time(NULL); | |
45 | + tmp = localtime(&t); | |
46 | + nbuf = strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); | |
47 | 47 | httpHeaderAdd(&(response->header), |
48 | - new(HttpHeader, | |
49 | - "Date", | |
50 | - sizeof("Date")-1, | |
51 | - buffer, | |
52 | - ndate)); | |
48 | + new(HttpHeader, CSTRA("Date"), buffer, nbuf)); | |
53 | 49 | } |
54 | 50 | |
55 | 51 | // vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -5,6 +5,8 @@ |
5 | 5 | #include "http/request.h" |
6 | 6 | #include "http/response.h" |
7 | 7 | |
8 | +#include "utils/memory.h" | |
9 | + | |
8 | 10 | HttpMessage |
9 | 11 | httpWorkerGetAsset( |
10 | 12 | HttpRequest request, |
... | ... | @@ -18,8 +20,7 @@ httpWorkerGetAsset( |
18 | 20 | |
19 | 21 | header = httpHeaderGet( |
20 | 22 | &(((HttpMessage)request)->header), |
21 | - "If-None-Match", | |
22 | - sizeof("If-None-Match")-1); | |
23 | + CSTRA("If-None-Match")); | |
23 | 24 | |
24 | 25 | if (NULL == header) { |
25 | 26 | match = ""; | ... | ... |
... | ... | @@ -36,6 +36,8 @@ |
36 | 36 | #include "http/parser.h" |
37 | 37 | #include "session.h" |
38 | 38 | |
39 | +#include "utils/memory.h" | |
40 | + | |
39 | 41 | HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); |
40 | 42 | void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); |
41 | 43 | |
... | ... | @@ -58,8 +60,7 @@ httpWorkerProcess(HttpWorker this, int fd) |
58 | 60 | HttpMessage response = NULL; |
59 | 61 | HttpHeader cookie = httpHeaderGet( |
60 | 62 | &(rmessage->header), |
61 | - "cookie", | |
62 | - sizeof("Cookie")-1); | |
63 | + CSTRA("cookie")); | |
63 | 64 | |
64 | 65 | if (NULL == this->session && NULL != cookie) { |
65 | 66 | int i; |
... | ... | @@ -111,11 +112,7 @@ httpWorkerProcess(HttpWorker this, int fd) |
111 | 112 | |
112 | 113 | httpHeaderAdd( |
113 | 114 | &(response->header), |
114 | - new(HttpHeader, | |
115 | - "Set-Cookie", | |
116 | - sizeof("Set-Cookie")-1, | |
117 | - buffer, | |
118 | - nbuf)); | |
115 | + new(HttpHeader, CSTRA("Set-Cookie"), buffer, nbuf)); | |
119 | 116 | } |
120 | 117 | } |
121 | 118 | |
... | ... | @@ -144,16 +141,14 @@ httpWorkerProcess(HttpWorker this, int fd) |
144 | 141 | response = httpWorkerGetAsset( |
145 | 142 | request, |
146 | 143 | "./assets/waldschrat.jpg", |
147 | - "image/jpeg", | |
148 | - sizeof("image/jpeg")-1); | |
144 | + CSTRA("image/jpeg")); | |
149 | 145 | } |
150 | 146 | |
151 | 147 | if (0 == strcmp("/jquery/", request->uri)) { |
152 | 148 | response = httpWorkerGetAsset( |
153 | 149 | request, |
154 | 150 | "./assets/jquery-1.7.1.min.js", |
155 | - "text/javascript", | |
156 | - sizeof("text/javascript")-1); | |
151 | + CSTRA("text/javascript")); | |
157 | 152 | } |
158 | 153 | } |
159 | 154 | ... | ... |
... | ... | @@ -56,7 +56,7 @@ socketCtor(void * _this, va_list * params) |
56 | 56 | } |
57 | 57 | |
58 | 58 | //! Make the socket REUSE a TIME_WAIT socket |
59 | - setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse)); | |
59 | + setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof(reUse)); | |
60 | 60 | |
61 | 61 | return 0; |
62 | 62 | } | ... | ... |
Please
register
or
login
to post a comment