aboutsummaryrefslogtreecommitdiff
path: root/src/examples/fileserver_example_dirs.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_dirs.c
parent2dedef2d861073db618bf67360877701c88d683e (diff)
downloadlibmicrohttpd-f710701c8241822b0869ba5feb95f33e61527efa.tar.gz
libmicrohttpd-f710701c8241822b0869ba5feb95f33e61527efa.zip
clean up example code
Diffstat (limited to 'src/examples/fileserver_example_dirs.c')
-rw-r--r--src/examples/fileserver_example_dirs.c65
1 files changed, 54 insertions, 11 deletions
diff --git a/src/examples/fileserver_example_dirs.c b/src/examples/fileserver_example_dirs.c
index 04519c63..30749fee 100644
--- a/src/examples/fileserver_example_dirs.c
+++ b/src/examples/fileserver_example_dirs.c
@@ -39,16 +39,32 @@ file_reader (void *cls, uint64_t pos, char *buf, int max)
39 return fread (buf, 1, max, file); 39 return fread (buf, 1, max, file);
40} 40}
41 41
42static void
43file_free_callback (void *cls)
44{
45 FILE *file = cls;
46 fclose (file);
47}
48
49static void
50dir_free_callback (void *cls)
51{
52 DIR *dir = cls;
53 if (dir != NULL)
54 closedir (dir);
55}
42 56
43static int 57static int
44dir_reader (void *cls, uint64_t pos, char *buf, int max) 58dir_reader (void *cls, uint64_t pos, char *buf, int max)
45{ 59{
60 DIR *dir = cls;
46 struct dirent *e; 61 struct dirent *e;
62
47 if (max < 512) 63 if (max < 512)
48 return 0; 64 return 0;
49 do 65 do
50 { 66 {
51 e = readdir (cls); 67 e = readdir (dir);
52 if (e == NULL) 68 if (e == NULL)
53 return -1; 69 return -1;
54 } while (e->d_name[0] == '.'); 70 } while (e->d_name[0] == '.');
@@ -72,7 +88,9 @@ ahc_echo (void *cls,
72 struct MHD_Response *response; 88 struct MHD_Response *response;
73 int ret; 89 int ret;
74 FILE *file; 90 FILE *file;
91 DIR *dir;
75 struct stat buf; 92 struct stat buf;
93 char emsg[1024];
76 94
77 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 95 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
78 return MHD_NO; /* unexpected method */ 96 return MHD_NO; /* unexpected method */
@@ -86,13 +104,39 @@ ahc_echo (void *cls,
86 file = fopen (&url[1], "rb"); 104 file = fopen (&url[1], "rb");
87 if (file == NULL) 105 if (file == NULL)
88 { 106 {
89 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 107 dir = opendir (".");
90 32 * 1024, 108 if (dir == NULL)
91 &dir_reader, 109 {
92 opendir ("."), 110 /* most likely cause: more concurrent requests than
93 (MHD_ContentReaderFreeCallback) &closedir); 111 available file descriptors / 2 */
94 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 112 snprintf (emsg,
95 MHD_destroy_response (response); 113 sizeof (emsg),
114 "Failed to open directory `.': %s\n",
115 strerror (errno));
116 response = MHD_create_response_from_data (strlen (emsg),
117 emsg,
118 MHD_NO,
119 MHD_YES);
120 if (response == NULL)
121 return MHD_NO;
122 ret = MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, response);
123 MHD_destroy_response (response);
124 }
125 else
126 {
127 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
128 32 * 1024,
129 &dir_reader,
130 dir,
131 &dir_free_callback);
132 if (response == NULL)
133 {
134 closedir (dir);
135 return MHD_NO;
136 }
137 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
138 MHD_destroy_response (response);
139 }
96 } 140 }
97 else 141 else
98 { 142 {
@@ -100,8 +144,7 @@ ahc_echo (void *cls,
100 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ 144 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
101 &file_reader, 145 &file_reader,
102 file, 146 file,
103 (MHD_ContentReaderFreeCallback) 147 &file_free_callback);
104 & fclose);
105 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 148 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
106 MHD_destroy_response (response); 149 MHD_destroy_response (response);
107 } 150 }
@@ -123,7 +166,7 @@ main (int argc, char *const *argv)
123 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 166 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
124 if (d == NULL) 167 if (d == NULL)
125 return 1; 168 return 1;
126 while (1) sleep (1); 169 getc (stdin);
127 MHD_stop_daemon (d); 170 MHD_stop_daemon (d);
128 return 0; 171 return 0;
129} 172}