diff options
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r-- | src/daemon/daemon.c | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index ef6ffe9b..8854d2bd 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -161,23 +161,26 @@ MHD_add_response_header(struct MHD_Response * response, | |||
161 | if(response == NULL || header == NULL || content == NULL || strlen(header) == 0 || strlen(content) == 0) { | 161 | if(response == NULL || header == NULL || content == NULL || strlen(header) == 0 || strlen(content) == 0) { |
162 | return MHD_NO; | 162 | return MHD_NO; |
163 | } | 163 | } |
164 | 164 | /* CG: use linked list to avoid limitation and over-allocation! */ | |
165 | if(response->firstFreeHeader >= MHD_MAX_HEADERS) { | 165 | if(response->firstFreeHeader >= MHD_MAX_HEADERS) { |
166 | return MHD_NO; | 166 | return MHD_NO; |
167 | } | 167 | } |
168 | 168 | ||
169 | newHeader = (char *)malloc(strlen(header)+1); | 169 | newHeader = (char *)malloc(strlen(header)+1); |
170 | newContent = (char *)malloc(strlen(content)+1); | 170 | newContent = (char *)malloc(strlen(content)+1); |
171 | 171 | /* CG: useless check! */ | |
172 | if(newHeader == NULL || newContent == NULL) { | 172 | if(newHeader == NULL || newContent == NULL) { |
173 | /* CG: printf! */ | ||
173 | fprintf(stderr, "Error allocating memory!\n"); | 174 | fprintf(stderr, "Error allocating memory!\n"); |
174 | return MHD_NO; | 175 | return MHD_NO; |
175 | } | 176 | } |
176 | 177 | ||
178 | /* CG: do you mean strcpy/strdup? defer allocation | ||
179 | until you need to (after malformed checks!) */ | ||
177 | sprintf(newHeader, "%s", header); | 180 | sprintf(newHeader, "%s", header); |
178 | sprintf(newContent, "%s", content); | 181 | sprintf(newContent, "%s", content); |
179 | 182 | ||
180 | 183 | /* CG: why not use strstr? */ | |
181 | if(strtok_r(newHeader, " \t\r\n", &saveptr) != NULL) { | 184 | if(strtok_r(newHeader, " \t\r\n", &saveptr) != NULL) { |
182 | fprintf(stderr, "Malformed header!\n"); | 185 | fprintf(stderr, "Malformed header!\n"); |
183 | free(newContent); | 186 | free(newContent); |
@@ -185,6 +188,7 @@ MHD_add_response_header(struct MHD_Response * response, | |||
185 | return MHD_NO; | 188 | return MHD_NO; |
186 | } | 189 | } |
187 | 190 | ||
191 | /* CG: why not use strstr? */ | ||
188 | if(strtok_r(newContent, "\n", &saveptr) != NULL) { | 192 | if(strtok_r(newContent, "\n", &saveptr) != NULL) { |
189 | fprintf(stderr, "Malformed content!\n"); | 193 | fprintf(stderr, "Malformed content!\n"); |
190 | free(newContent); | 194 | free(newContent); |
@@ -192,21 +196,30 @@ MHD_add_response_header(struct MHD_Response * response, | |||
192 | return MHD_NO; | 196 | return MHD_NO; |
193 | } | 197 | } |
194 | 198 | ||
199 | /* CG: this is not C++ -- no need to cast after malloc! */ | ||
195 | struct MHD_HTTP_Header * newHTTPHeader = (struct MHD_HTTP_Header *)malloc(sizeof(struct MHD_HTTP_Header)); | 200 | struct MHD_HTTP_Header * newHTTPHeader = (struct MHD_HTTP_Header *)malloc(sizeof(struct MHD_HTTP_Header)); |
196 | 201 | ||
197 | if(newHTTPHeader == NULL) { | 202 | if(newHTTPHeader == NULL) { |
203 | /* CG: useless check, printf */ | ||
198 | fprintf(stderr, "Error allocating memory!\n"); | 204 | fprintf(stderr, "Error allocating memory!\n"); |
199 | free(newContent); | 205 | free(newContent); |
200 | free(newHeader); | 206 | free(newHeader); |
201 | return MHD_NO; | 207 | return MHD_NO; |
202 | } | 208 | } |
203 | 209 | ||
210 | /* CG: strdup here, avoids free's above! */ | ||
204 | response->headers[response->firstFreeHeader]->header = newHeader; | 211 | response->headers[response->firstFreeHeader]->header = newHeader; |
205 | response->headers[response->firstFreeHeader]->headerContent = newContent; | 212 | response->headers[response->firstFreeHeader]->headerContent = newContent; |
206 | 213 | ||
207 | //For now, everything is a HTTP Header... this needs to be improved! | 214 | //For now, everything is a HTTP Header... this needs to be improved! |
215 | /* CG: what else are you thinking about? Cookies? | ||
216 | sounds like you are proposing an API change!!! */ | ||
217 | |||
208 | response->headers[response->firstFreeHeader]->kind = MHD_HEADER_KIND; | 218 | response->headers[response->firstFreeHeader]->kind = MHD_HEADER_KIND; |
209 | 219 | ||
220 | /* CG: YUCK! Yet another reason for linked lists... | ||
221 | Why bother with the firstFreeHandler field if | ||
222 | you're O(n) anyway!? */ | ||
210 | response->firstFreeHeader=MHD_MAX_HEADERS; | 223 | response->firstFreeHeader=MHD_MAX_HEADERS; |
211 | for(i = 0; i < MHD_MAX_HEADERS; i++) { | 224 | for(i = 0; i < MHD_MAX_HEADERS; i++) { |
212 | if(response->headers[i] == NULL) { | 225 | if(response->headers[i] == NULL) { |
@@ -241,9 +254,12 @@ MHD_create_connection(struct MHD_Daemon * daemon) { | |||
241 | if(first_free == -1) | 254 | if(first_free == -1) |
242 | return -1; | 255 | return -1; |
243 | 256 | ||
257 | /* CG: delay allocation until at accept has succeeded! */ | ||
244 | daemon->connections[first_free] = (struct MHD_Session *)malloc(sizeof(struct MHD_Session)); | 258 | daemon->connections[first_free] = (struct MHD_Session *)malloc(sizeof(struct MHD_Session)); |
245 | 259 | ||
246 | if(daemon->connections[first_free] == NULL) { | 260 | if(daemon->connections[first_free] == NULL) { |
261 | /* CG: use MACRO or (static) helper function | ||
262 | instead of writing this option check everywhere! */ | ||
247 | if((daemon->options & MHD_USE_DEBUG) != 0) | 263 | if((daemon->options & MHD_USE_DEBUG) != 0) |
248 | fprintf(stderr, "Error allocating memory!\n"); | 264 | fprintf(stderr, "Error allocating memory!\n"); |
249 | return -1; | 265 | return -1; |
@@ -251,7 +267,7 @@ MHD_create_connection(struct MHD_Daemon * daemon) { | |||
251 | 267 | ||
252 | size = sizeof(struct sockaddr); | 268 | size = sizeof(struct sockaddr); |
253 | daemon->connections[first_free]->socket_fd = | 269 | daemon->connections[first_free]->socket_fd = |
254 | 270 | ||
255 | accept(daemon->socket_fd, (struct sockaddr *)&daemon->connections[first_free]->addr, | 271 | accept(daemon->socket_fd, (struct sockaddr *)&daemon->connections[first_free]->addr, |
256 | (socklen_t *)&size); | 272 | (socklen_t *)&size); |
257 | 273 | ||
@@ -291,7 +307,9 @@ MHD_create_connection(struct MHD_Daemon * daemon) { | |||
291 | for(i = 0; i < MHD_MAX_RESPONSE; i++) { | 307 | for(i = 0; i < MHD_MAX_RESPONSE; i++) { |
292 | daemon->connections[first_free]->currentResponses[i] = NULL; | 308 | daemon->connections[first_free]->currentResponses[i] = NULL; |
293 | } | 309 | } |
294 | 310 | /* CG: maybe better to re-compute max_fd closer to select; | |
311 | also handles deletion mo re graceful, need to iterate over | ||
312 | all connections anyway for FD_SET... */ | ||
295 | if(daemon->max_fd < daemon->connections[first_free]->socket_fd) { | 313 | if(daemon->max_fd < daemon->connections[first_free]->socket_fd) { |
296 | daemon->max_fd = daemon->connections[first_free]->socket_fd; | 314 | daemon->max_fd = daemon->connections[first_free]->socket_fd; |
297 | } | 315 | } |
@@ -320,12 +338,14 @@ MHD_create_response_from_callback(size_t size, | |||
320 | 338 | ||
321 | 339 | ||
322 | if(crc == NULL) { | 340 | if(crc == NULL) { |
341 | /* CG: printf! */ | ||
323 | fprintf(stderr, "A ContentReaderCallback must be provided to MHD_create_response_from_callback!\n"); | 342 | fprintf(stderr, "A ContentReaderCallback must be provided to MHD_create_response_from_callback!\n"); |
324 | return NULL; | 343 | return NULL; |
325 | } | 344 | } |
326 | 345 | ||
327 | retVal = (struct MHD_Response *) malloc(sizeof(struct MHD_Response)); | 346 | retVal = (struct MHD_Response *) malloc(sizeof(struct MHD_Response)); |
328 | if(retVal == NULL) { | 347 | if(retVal == NULL) { |
348 | /* CG: printf, useless check, useless cast */ | ||
329 | fprintf(stderr, "Error allocating memory!\n"); | 349 | fprintf(stderr, "Error allocating memory!\n"); |
330 | return NULL; | 350 | return NULL; |
331 | } | 351 | } |
@@ -353,7 +373,7 @@ MHD_create_response_from_callback(size_t size, | |||
353 | free(retVal); | 373 | free(retVal); |
354 | return NULL; | 374 | return NULL; |
355 | } | 375 | } |
356 | 376 | /* CG: use memset? linked list!!? */ | |
357 | for(i = 0; i < MHD_MAX_HEADERS; i++) { | 377 | for(i = 0; i < MHD_MAX_HEADERS; i++) { |
358 | retVal->headers[i] = NULL; | 378 | retVal->headers[i] = NULL; |
359 | } | 379 | } |