diff options
Diffstat (limited to 'src/examples/fileserver_example_external_select.c')
-rw-r--r-- | src/examples/fileserver_example_external_select.c | 137 |
1 files changed, 69 insertions, 68 deletions
diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c index 6aea6dbf..187cdf7e 100644 --- a/src/examples/fileserver_example_external_select.c +++ b/src/examples/fileserver_example_external_select.c | |||
@@ -27,7 +27,8 @@ | |||
27 | #include <sys/stat.h> | 27 | #include <sys/stat.h> |
28 | #include <unistd.h> | 28 | #include <unistd.h> |
29 | 29 | ||
30 | #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" | 30 | #define PAGE \ |
31 | "<html><head><title>File not found</title></head><body>File not found</body></html>" | ||
31 | 32 | ||
32 | static ssize_t | 33 | static ssize_t |
33 | file_reader (void *cls, uint64_t pos, char *buf, size_t max) | 34 | file_reader (void *cls, uint64_t pos, char *buf, size_t max) |
@@ -54,7 +55,7 @@ ahc_echo (void *cls, | |||
54 | const char *method, | 55 | const char *method, |
55 | const char *version, | 56 | const char *version, |
56 | const char *upload_data, | 57 | const char *upload_data, |
57 | size_t *upload_data_size, void **ptr) | 58 | size_t *upload_data_size, void **ptr) |
58 | { | 59 | { |
59 | static int aptr; | 60 | static int aptr; |
60 | struct MHD_Response *response; | 61 | struct MHD_Response *response; |
@@ -62,61 +63,61 @@ ahc_echo (void *cls, | |||
62 | FILE *file; | 63 | FILE *file; |
63 | int fd; | 64 | int fd; |
64 | struct stat buf; | 65 | struct stat buf; |
65 | (void)cls; /* Unused. Silent compiler warning. */ | 66 | (void) cls; /* Unused. Silent compiler warning. */ |
66 | (void)version; /* Unused. Silent compiler warning. */ | 67 | (void) version; /* Unused. Silent compiler warning. */ |
67 | (void)upload_data; /* Unused. Silent compiler warning. */ | 68 | (void) upload_data; /* Unused. Silent compiler warning. */ |
68 | (void)upload_data_size; /* Unused. Silent compiler warning. */ | 69 | (void) upload_data_size; /* Unused. Silent compiler warning. */ |
69 | 70 | ||
70 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | 71 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) |
71 | return MHD_NO; /* unexpected method */ | 72 | return MHD_NO; /* unexpected method */ |
72 | if (&aptr != *ptr) | 73 | if (&aptr != *ptr) |
73 | { | 74 | { |
74 | /* do never respond on first call */ | 75 | /* do never respond on first call */ |
75 | *ptr = &aptr; | 76 | *ptr = &aptr; |
76 | return MHD_YES; | 77 | return MHD_YES; |
77 | } | 78 | } |
78 | *ptr = NULL; /* reset when done */ | 79 | *ptr = NULL; /* reset when done */ |
79 | 80 | ||
80 | file = fopen (&url[1], "rb"); | 81 | file = fopen (&url[1], "rb"); |
81 | if (NULL != file) | 82 | if (NULL != file) |
83 | { | ||
84 | fd = fileno (file); | ||
85 | if (-1 == fd) | ||
82 | { | 86 | { |
83 | fd = fileno (file); | 87 | (void) fclose (file); |
84 | if (-1 == fd) | 88 | return MHD_NO; /* internal error */ |
85 | { | ||
86 | (void) fclose (file); | ||
87 | return MHD_NO; /* internal error */ | ||
88 | } | ||
89 | if ( (0 != fstat (fd, &buf)) || | ||
90 | (! S_ISREG (buf.st_mode)) ) | ||
91 | { | ||
92 | /* not a regular file, refuse to serve */ | ||
93 | fclose (file); | ||
94 | file = NULL; | ||
95 | } | ||
96 | } | 89 | } |
97 | 90 | if ( (0 != fstat (fd, &buf)) || | |
98 | if (NULL == file) | 91 | (! S_ISREG (buf.st_mode)) ) |
99 | { | 92 | { |
100 | response = MHD_create_response_from_buffer (strlen (PAGE), | 93 | /* not a regular file, refuse to serve */ |
101 | (void *) PAGE, | 94 | fclose (file); |
102 | MHD_RESPMEM_PERSISTENT); | 95 | file = NULL; |
103 | ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); | ||
104 | MHD_destroy_response (response); | ||
105 | } | 96 | } |
97 | } | ||
98 | |||
99 | if (NULL == file) | ||
100 | { | ||
101 | response = MHD_create_response_from_buffer (strlen (PAGE), | ||
102 | (void *) PAGE, | ||
103 | MHD_RESPMEM_PERSISTENT); | ||
104 | ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); | ||
105 | MHD_destroy_response (response); | ||
106 | } | ||
106 | else | 107 | else |
108 | { | ||
109 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ | ||
110 | &file_reader, | ||
111 | file, | ||
112 | &free_callback); | ||
113 | if (NULL == response) | ||
107 | { | 114 | { |
108 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ | 115 | fclose (file); |
109 | &file_reader, | 116 | return MHD_NO; |
110 | file, | ||
111 | &free_callback); | ||
112 | if (NULL == response) | ||
113 | { | ||
114 | fclose (file); | ||
115 | return MHD_NO; | ||
116 | } | ||
117 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
118 | MHD_destroy_response (response); | ||
119 | } | 117 | } |
118 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
119 | MHD_destroy_response (response); | ||
120 | } | ||
120 | return ret; | 121 | return ret; |
121 | } | 122 | } |
122 | 123 | ||
@@ -135,10 +136,10 @@ main (int argc, char *const *argv) | |||
135 | MHD_UNSIGNED_LONG_LONG mhd_timeout; | 136 | MHD_UNSIGNED_LONG_LONG mhd_timeout; |
136 | 137 | ||
137 | if (argc != 3) | 138 | if (argc != 3) |
138 | { | 139 | { |
139 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | 140 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); |
140 | return 1; | 141 | return 1; |
141 | } | 142 | } |
142 | d = MHD_start_daemon (MHD_USE_ERROR_LOG, | 143 | d = MHD_start_daemon (MHD_USE_ERROR_LOG, |
143 | atoi (argv[1]), | 144 | atoi (argv[1]), |
144 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | 145 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); |
@@ -146,30 +147,30 @@ main (int argc, char *const *argv) | |||
146 | return 1; | 147 | return 1; |
147 | end = time (NULL) + atoi (argv[2]); | 148 | end = time (NULL) + atoi (argv[2]); |
148 | while ((t = time (NULL)) < end) | 149 | while ((t = time (NULL)) < end) |
150 | { | ||
151 | tv.tv_sec = end - t; | ||
152 | tv.tv_usec = 0; | ||
153 | max = 0; | ||
154 | FD_ZERO (&rs); | ||
155 | FD_ZERO (&ws); | ||
156 | FD_ZERO (&es); | ||
157 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | ||
158 | break; /* fatal internal error */ | ||
159 | if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES) | ||
160 | { | ||
161 | if (((MHD_UNSIGNED_LONG_LONG) tv.tv_sec) < mhd_timeout / 1000LL) | ||
162 | { | ||
163 | tv.tv_sec = mhd_timeout / 1000LL; | ||
164 | tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL; | ||
165 | } | ||
166 | } | ||
167 | if (-1 == select (max + 1, &rs, &ws, &es, &tv)) | ||
149 | { | 168 | { |
150 | tv.tv_sec = end - t; | 169 | if (EINTR != errno) |
151 | tv.tv_usec = 0; | 170 | abort (); |
152 | max = 0; | ||
153 | FD_ZERO (&rs); | ||
154 | FD_ZERO (&ws); | ||
155 | FD_ZERO (&es); | ||
156 | if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) | ||
157 | break; /* fatal internal error */ | ||
158 | if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES) | ||
159 | { | ||
160 | if (((MHD_UNSIGNED_LONG_LONG)tv.tv_sec) < mhd_timeout / 1000LL) | ||
161 | { | ||
162 | tv.tv_sec = mhd_timeout / 1000LL; | ||
163 | tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL; | ||
164 | } | ||
165 | } | ||
166 | if (-1 == select (max + 1, &rs, &ws, &es, &tv)) | ||
167 | { | ||
168 | if (EINTR != errno) | ||
169 | abort (); | ||
170 | } | ||
171 | MHD_run (d); | ||
172 | } | 171 | } |
172 | MHD_run (d); | ||
173 | } | ||
173 | MHD_stop_daemon (d); | 174 | MHD_stop_daemon (d); |
174 | return 0; | 175 | return 0; |
175 | } | 176 | } |