aboutsummaryrefslogtreecommitdiff
path: root/src/examples/fileserver_example.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-06-13 08:47:17 +0000
committerChristian Grothoff <christian@grothoff.org>2010-06-13 08:47:17 +0000
commitf710701c8241822b0869ba5feb95f33e61527efa (patch)
tree62ef074684b0d9fcf35cd97d91feef82399f6f7e /src/examples/fileserver_example.c
parent2dedef2d861073db618bf67360877701c88d683e (diff)
downloadlibmicrohttpd-f710701c8241822b0869ba5feb95f33e61527efa.tar.gz
libmicrohttpd-f710701c8241822b0869ba5feb95f33e61527efa.zip
clean up example code
Diffstat (limited to 'src/examples/fileserver_example.c')
-rw-r--r--src/examples/fileserver_example.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/examples/fileserver_example.c b/src/examples/fileserver_example.c
index 76a10f2f..93604d70 100644
--- a/src/examples/fileserver_example.c
+++ b/src/examples/fileserver_example.c
@@ -38,6 +38,13 @@ file_reader (void *cls, uint64_t pos, char *buf, int max)
38 return fread (buf, 1, max, file); 38 return fread (buf, 1, max, file);
39} 39}
40 40
41static void
42free_callback (void *cls)
43{
44 FILE *file = cls;
45 fclose (file);
46}
47
41static int 48static int
42ahc_echo (void *cls, 49ahc_echo (void *cls,
43 struct MHD_Connection *connection, 50 struct MHD_Connection *connection,
@@ -62,7 +69,10 @@ ahc_echo (void *cls,
62 return MHD_YES; 69 return MHD_YES;
63 } 70 }
64 *ptr = NULL; /* reset when done */ 71 *ptr = NULL; /* reset when done */
65 file = fopen (&url[1], "rb"); 72 if (0 == stat (&url[1], &buf))
73 file = fopen (&url[1], "rb");
74 else
75 file = NULL;
66 if (file == NULL) 76 if (file == NULL)
67 { 77 {
68 response = MHD_create_response_from_data (strlen (PAGE), 78 response = MHD_create_response_from_data (strlen (PAGE),
@@ -73,12 +83,15 @@ ahc_echo (void *cls,
73 } 83 }
74 else 84 else
75 { 85 {
76 stat (&url[1], &buf);
77 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ 86 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
78 &file_reader, 87 &file_reader,
79 file, 88 file,
80 (MHD_ContentReaderFreeCallback) 89 &free_callback);
81 & fclose); 90 if (response == NULL)
91 {
92 fclose (file);
93 return MHD_NO;
94 }
82 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 95 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
83 MHD_destroy_response (response); 96 MHD_destroy_response (response);
84 } 97 }
@@ -90,9 +103,9 @@ main (int argc, char *const *argv)
90{ 103{
91 struct MHD_Daemon *d; 104 struct MHD_Daemon *d;
92 105
93 if (argc != 3) 106 if (argc != 2)
94 { 107 {
95 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); 108 printf ("%s PORT\n", argv[0]);
96 return 1; 109 return 1;
97 } 110 }
98 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 111 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
@@ -100,7 +113,7 @@ main (int argc, char *const *argv)
100 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 113 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
101 if (d == NULL) 114 if (d == NULL)
102 return 1; 115 return 1;
103 sleep (atoi (argv[2])); 116 getc (stdin);
104 MHD_stop_daemon (d); 117 MHD_stop_daemon (d);
105 return 0; 118 return 0;
106} 119}