aboutsummaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2014-12-20 00:36:00 +0000
committerChristian Grothoff <christian@grothoff.org>2014-12-20 00:36:00 +0000
commit6c80b16d18c4a39824dcb19aa968388e9b2b7b15 (patch)
tree65ee51ffd152c4ae0820f2b47027fdc6ef338c59 /src/examples
parenta29e7f538c9029883ea26fb3346cbca15e4158ee (diff)
downloadlibmicrohttpd-6c80b16d18c4a39824dcb19aa968388e9b2b7b15.tar.gz
libmicrohttpd-6c80b16d18c4a39824dcb19aa968388e9b2b7b15.zip
-check malloc() return value
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/spdy_fileserver.c76
1 files changed, 39 insertions, 37 deletions
diff --git a/src/examples/spdy_fileserver.c b/src/examples/spdy_fileserver.c
index adff832a..9f4351b9 100644
--- a/src/examples/spdy_fileserver.c
+++ b/src/examples/spdy_fileserver.c
@@ -24,7 +24,7 @@
24 */ 24 */
25 25
26//for asprintf 26//for asprintf
27#define _GNU_SOURCE 27#define _GNU_SOURCE
28 28
29#include <unistd.h> 29#include <unistd.h>
30#include <stdlib.h> 30#include <stdlib.h>
@@ -36,12 +36,12 @@
36#include <errno.h> 36#include <errno.h>
37#include "microspdy.h" 37#include "microspdy.h"
38#include "time.h" 38#include "time.h"
39 39
40 40
41int run = 1; 41int run = 1;
42char* basedir; 42char* basedir;
43 43
44 44
45#define GET_MIME_TYPE(fname, mime) do {\ 45#define GET_MIME_TYPE(fname, mime) do {\
46 unsigned int __len = strlen(fname);\ 46 unsigned int __len = strlen(fname);\
47 if (__len < 4 || '.' != (fname)[__len - 4]) \ 47 if (__len < 4 || '.' != (fname)[__len - 4]) \
@@ -68,11 +68,11 @@ char* basedir;
68 abort();\ 68 abort();\
69 }\ 69 }\
70 } while (0) 70 } while (0)
71 71
72 72
73static const char *DAY_NAMES[] = 73static const char *DAY_NAMES[] =
74 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 74 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
75 75
76static const char *MONTH_NAMES[] = 76static const char *MONTH_NAMES[] =
77 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 77 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
78 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 78 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
@@ -86,6 +86,8 @@ char *Rfc1123_DateTimeNow()
86 struct tm tm; 86 struct tm tm;
87 char * buf = malloc(RFC1123_TIME_LEN+1); 87 char * buf = malloc(RFC1123_TIME_LEN+1);
88 88
89 if (NULL == buf)
90 return NULL;
89 time(&t); 91 time(&t);
90 gmtime_r( &t, &tm); 92 gmtime_r( &t, &tm);
91 93
@@ -104,13 +106,13 @@ response_callback (void *cls,
104 bool *more) 106 bool *more)
105{ 107{
106 FILE *fd =(FILE*)cls; 108 FILE *fd =(FILE*)cls;
107 109
108 int ret = fread(buffer,1,max,fd); 110 int ret = fread(buffer,1,max,fd);
109 *more = feof(fd) == 0; 111 *more = feof(fd) == 0;
110 112
111 //if(!(*more)) 113 //if(!(*more))
112 // fclose(fd); 114 // fclose(fd);
113 115
114 return ret; 116 return ret;
115} 117}
116 118
@@ -125,12 +127,12 @@ response_done_callback(void *cls,
125 (void)streamopened; 127 (void)streamopened;
126 (void)status; 128 (void)status;
127 //printf("answer for %s was sent\n", (char *)cls); 129 //printf("answer for %s was sent\n", (char *)cls);
128 130
129 /*if(SPDY_RESPONSE_RESULT_SUCCESS != status) 131 /*if(SPDY_RESPONSE_RESULT_SUCCESS != status)
130 { 132 {
131 printf("answer for %s was NOT sent, %i\n", (char *)cls,status); 133 printf("answer for %s was NOT sent, %i\n", (char *)cls,status);
132 }*/ 134 }*/
133 135
134 SPDY_destroy_request(request); 136 SPDY_destroy_request(request);
135 SPDY_destroy_response(response); 137 SPDY_destroy_response(response);
136 if(NULL!=cls)fclose(cls); 138 if(NULL!=cls)fclose(cls);
@@ -157,7 +159,7 @@ standard_request_handler(void *cls,
157 (void)method; 159 (void)method;
158 (void)version; 160 (void)version;
159 (void)more; 161 (void)more;
160 162
161 struct SPDY_Response *response=NULL; 163 struct SPDY_Response *response=NULL;
162 struct SPDY_NameValue *resp_headers; 164 struct SPDY_NameValue *resp_headers;
163 char *fname; 165 char *fname;
@@ -167,7 +169,7 @@ standard_request_handler(void *cls,
167 ssize_t filesize = -666; 169 ssize_t filesize = -666;
168 FILE *fd = NULL; 170 FILE *fd = NULL;
169 int ret = -666; 171 int ret = -666;
170 172
171 //printf("received request for '%s %s %s'\n", method, path, version); 173 //printf("received request for '%s %s %s'\n", method, path, version);
172 if(strlen(path) > 1 && NULL == strstr(path, "..") && '/' == path[0]) 174 if(strlen(path) > 1 && NULL == strstr(path, "..") && '/' == path[0])
173 { 175 {
@@ -189,7 +191,7 @@ standard_request_handler(void *cls,
189 printf("SPDY_name_value_create failed\n"); 191 printf("SPDY_name_value_create failed\n");
190 abort(); 192 abort();
191 } 193 }
192 194
193 date = Rfc1123_DateTimeNow(); 195 date = Rfc1123_DateTimeNow();
194 if(NULL == date 196 if(NULL == date
195 || SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_DATE,date)) 197 || SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_DATE,date))
@@ -198,7 +200,7 @@ standard_request_handler(void *cls,
198 abort(); 200 abort();
199 } 201 }
200 free(date); 202 free(date);
201 203
202 if(-1 == asprintf(&fsize, "%zd", filesize) 204 if(-1 == asprintf(&fsize, "%zd", filesize)
203 || SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_CONTENT_LENGTH,fsize)) 205 || SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_CONTENT_LENGTH,fsize))
204 { 206 {
@@ -206,7 +208,7 @@ standard_request_handler(void *cls,
206 abort(); 208 abort();
207 } 209 }
208 free(fsize); 210 free(fsize);
209 211
210 GET_MIME_TYPE(path,mime); 212 GET_MIME_TYPE(path,mime);
211 if(SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_CONTENT_TYPE,mime)) 213 if(SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_CONTENT_TYPE,mime))
212 { 214 {
@@ -214,48 +216,48 @@ standard_request_handler(void *cls,
214 abort(); 216 abort();
215 } 217 }
216 free(mime); 218 free(mime);
217 219
218 if(SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_SERVER,"libmicrospdy/fileserver")) 220 if(SPDY_YES != SPDY_name_value_add(resp_headers,SPDY_HTTP_HEADER_SERVER,"libmicrospdy/fileserver"))
219 { 221 {
220 printf("SPDY_name_value_add failed\n"); 222 printf("SPDY_name_value_add failed\n");
221 abort(); 223 abort();
222 } 224 }
223 225
224 response = SPDY_build_response_with_callback(200,NULL, 226 response = SPDY_build_response_with_callback(200,NULL,
225 SPDY_HTTP_VERSION_1_1,resp_headers,&response_callback,fd,SPDY_MAX_SUPPORTED_FRAME_SIZE); 227 SPDY_HTTP_VERSION_1_1,resp_headers,&response_callback,fd,SPDY_MAX_SUPPORTED_FRAME_SIZE);
226 SPDY_name_value_destroy(resp_headers); 228 SPDY_name_value_destroy(resp_headers);
227 } 229 }
228 230
229 if(NULL==response){ 231 if(NULL==response){
230 printf("no response obj\n"); 232 printf("no response obj\n");
231 abort(); 233 abort();
232 } 234 }
233 235
234 if(SPDY_queue_response(request,response,true,false,&response_done_callback,fd)!=SPDY_YES) 236 if(SPDY_queue_response(request,response,true,false,&response_done_callback,fd)!=SPDY_YES)
235 { 237 {
236 printf("queue\n"); 238 printf("queue\n");
237 abort(); 239 abort();
238 } 240 }
239 241
240 free(fname); 242 free(fname);
241 return; 243 return;
242 } 244 }
243 free(fname); 245 free(fname);
244 } 246 }
245 247
246 if(strcmp(path,"/close")==0) 248 if(strcmp(path,"/close")==0)
247 { 249 {
248 run = 0; 250 run = 0;
249 } 251 }
250 252
251 response = SPDY_build_response(SPDY_HTTP_NOT_FOUND,NULL,SPDY_HTTP_VERSION_1_1,NULL,NULL,0); 253 response = SPDY_build_response(SPDY_HTTP_NOT_FOUND,NULL,SPDY_HTTP_VERSION_1_1,NULL,NULL,0);
252 printf("Not found %s\n",path); 254 printf("Not found %s\n",path);
253 255
254 if(NULL==response){ 256 if(NULL==response){
255 printf("no response obj\n"); 257 printf("no response obj\n");
256 abort(); 258 abort();
257 } 259 }
258 260
259 if(SPDY_queue_response(request,response,true,false,&response_done_callback,NULL)!=SPDY_YES) 261 if(SPDY_queue_response(request,response,true,false,&response_done_callback,NULL)!=SPDY_YES)
260 { 262 {
261 printf("queue\n"); 263 printf("queue\n");
@@ -265,7 +267,7 @@ standard_request_handler(void *cls,
265 267
266int 268int
267main (int argc, char *const *argv) 269main (int argc, char *const *argv)
268{ 270{
269 unsigned long long timeoutlong=0; 271 unsigned long long timeoutlong=0;
270 struct timeval timeout; 272 struct timeval timeout;
271 int ret; 273 int ret;
@@ -274,15 +276,15 @@ main (int argc, char *const *argv)
274 fd_set except_fd_set; 276 fd_set except_fd_set;
275 int maxfd = -1; 277 int maxfd = -1;
276 struct SPDY_Daemon *daemon; 278 struct SPDY_Daemon *daemon;
277 279
278 if(argc != 5) 280 if(argc != 5)
279 { 281 {
280 printf("Usage: %s cert-file key-file base-dir port\n", argv[0]); 282 printf("Usage: %s cert-file key-file base-dir port\n", argv[0]);
281 return 1; 283 return 1;
282 } 284 }
283 285
284 SPDY_init(); 286 SPDY_init();
285 287
286 daemon = SPDY_start_daemon(atoi(argv[4]), 288 daemon = SPDY_start_daemon(atoi(argv[4]),
287 argv[1], 289 argv[1],
288 argv[2], 290 argv[2],
@@ -294,12 +296,12 @@ main (int argc, char *const *argv)
294 SPDY_DAEMON_OPTION_SESSION_TIMEOUT, 296 SPDY_DAEMON_OPTION_SESSION_TIMEOUT,
295 1800, 297 1800,
296 SPDY_DAEMON_OPTION_END); 298 SPDY_DAEMON_OPTION_END);
297 299
298 if(NULL==daemon){ 300 if(NULL==daemon){
299 printf("no daemon\n"); 301 printf("no daemon\n");
300 return 1; 302 return 1;
301 } 303 }
302 304
303 basedir = argv[3]; 305 basedir = argv[3];
304 306
305 do 307 do
@@ -319,14 +321,14 @@ main (int argc, char *const *argv)
319 timeout.tv_sec = timeoutlong / 1000; 321 timeout.tv_sec = timeoutlong / 1000;
320 timeout.tv_usec = (timeoutlong % 1000) * 1000; 322 timeout.tv_usec = (timeoutlong % 1000) * 1000;
321 } 323 }
322 324
323 maxfd = SPDY_get_fdset (daemon, 325 maxfd = SPDY_get_fdset (daemon,
324 &read_fd_set, 326 &read_fd_set,
325 &write_fd_set, 327 &write_fd_set,
326 &except_fd_set); 328 &except_fd_set);
327 329
328 ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout); 330 ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout);
329 331
330 switch(ret) { 332 switch(ret) {
331 case -1: 333 case -1:
332 printf("select error: %i\n", errno); 334 printf("select error: %i\n", errno);
@@ -343,9 +345,9 @@ main (int argc, char *const *argv)
343 while(run); 345 while(run);
344 346
345 SPDY_stop_daemon(daemon); 347 SPDY_stop_daemon(daemon);
346 348
347 SPDY_deinit(); 349 SPDY_deinit();
348 350
349 return 0; 351 return 0;
350} 352}
351 353