1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2001-2003, Ximian, Inc.
4 */
5
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11
12 #include <libsoup/soup.h>
13 #include <glib/gstdio.h>
14
15 static int
compare_strings(gconstpointer a,gconstpointer b)16 compare_strings (gconstpointer a, gconstpointer b)
17 {
18 const char **sa = (const char **)a;
19 const char **sb = (const char **)b;
20
21 return strcmp (*sa, *sb);
22 }
23
24 static GString *
get_directory_listing(const char * path)25 get_directory_listing (const char *path)
26 {
27 GPtrArray *entries;
28 GString *listing;
29 char *escaped;
30 GDir *dir;
31 const gchar *d_name;
32 int i;
33
34 entries = g_ptr_array_new ();
35 dir = g_dir_open (path, 0, NULL);
36 if (dir) {
37 while ((d_name = g_dir_read_name (dir))) {
38 if (!strcmp (d_name, ".") ||
39 (!strcmp (d_name, "..") &&
40 !strcmp (path, "./")))
41 continue;
42 escaped = g_markup_escape_text (d_name, -1);
43 g_ptr_array_add (entries, escaped);
44 }
45 g_dir_close (dir);
46 }
47
48 g_ptr_array_sort (entries, (GCompareFunc)compare_strings);
49
50 listing = g_string_new ("<html>\r\n");
51 escaped = g_markup_escape_text (strchr (path, '/'), -1);
52 g_string_append_printf (listing, "<head><title>Index of %s</title></head>\r\n", escaped);
53 g_string_append_printf (listing, "<body><h1>Index of %s</h1>\r\n<p>\r\n", escaped);
54 g_free (escaped);
55 for (i = 0; i < entries->len; i++) {
56 g_string_append_printf (listing, "<a href=\"%s\">%s</a><br>\r\n",
57 (char *)entries->pdata[i],
58 (char *)entries->pdata[i]);
59 g_free (entries->pdata[i]);
60 }
61 g_string_append (listing, "</body>\r\n</html>\r\n");
62
63 g_ptr_array_free (entries, TRUE);
64 return listing;
65 }
66
67 static void
do_get(SoupServer * server,SoupMessage * msg,const char * path)68 do_get (SoupServer *server, SoupMessage *msg, const char *path)
69 {
70 char *slash;
71 GStatBuf st;
72
73 if (g_stat (path, &st) == -1) {
74 if (errno == EPERM)
75 soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
76 else if (errno == ENOENT)
77 soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
78 else
79 soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
80 /* In a real program you should also provide some text explaining the
81 * error to the user (via soup_message_set_response), and include in it
82 * the request path, as otherwise some browsers (Firefox, WebKit) only
83 * show a blank page, confusing the user.
84 */
85 return;
86 }
87
88 if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
89 GString *listing;
90 char *index_path;
91
92 slash = strrchr (path, '/');
93 if (!slash || slash[1]) {
94 char *redir_uri;
95
96 redir_uri = g_strdup_printf ("%s/", soup_message_get_uri (msg)->path);
97 soup_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY,
98 redir_uri);
99 g_free (redir_uri);
100 return;
101 }
102
103 index_path = g_strdup_printf ("%s/index.html", path);
104 if (g_stat (index_path, &st) != -1) {
105 do_get (server, msg, index_path);
106 g_free (index_path);
107 return;
108 }
109 g_free (index_path);
110
111 listing = get_directory_listing (path);
112 soup_message_set_response (msg, "text/html",
113 SOUP_MEMORY_TAKE,
114 listing->str, listing->len);
115 soup_message_set_status (msg, SOUP_STATUS_OK);
116 g_string_free (listing, FALSE);
117 return;
118 }
119
120 if (msg->method == SOUP_METHOD_GET) {
121 GMappedFile *mapping;
122 SoupBuffer *buffer;
123
124 mapping = g_mapped_file_new (path, FALSE, NULL);
125 if (!mapping) {
126 soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
127 return;
128 }
129
130 buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping),
131 g_mapped_file_get_length (mapping),
132 mapping, (GDestroyNotify)g_mapped_file_unref);
133 soup_message_body_append_buffer (msg->response_body, buffer);
134 soup_buffer_free (buffer);
135 } else /* msg->method == SOUP_METHOD_HEAD */ {
136 char *length;
137
138 /* We could just use the same code for both GET and
139 * HEAD (soup-message-server-io.c will fix things up).
140 * But we'll optimize and avoid the extra I/O.
141 */
142 length = g_strdup_printf ("%lu", (gulong)st.st_size);
143 soup_message_headers_append (msg->response_headers,
144 "Content-Length", length);
145 g_free (length);
146 }
147
148 soup_message_set_status (msg, SOUP_STATUS_OK);
149 }
150
151 static void
do_put(SoupServer * server,SoupMessage * msg,const char * path)152 do_put (SoupServer *server, SoupMessage *msg, const char *path)
153 {
154 GStatBuf st;
155 FILE *f;
156 gboolean created = TRUE;
157
158 if (g_stat (path, &st) != -1) {
159 const char *match = soup_message_headers_get_one (msg->request_headers, "If-None-Match");
160 if (match && !strcmp (match, "*")) {
161 soup_message_set_status (msg, SOUP_STATUS_CONFLICT);
162 return;
163 }
164
165 if (!g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
166 soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
167 return;
168 }
169
170 created = FALSE;
171 }
172
173 f = fopen (path, "w");
174 if (!f) {
175 soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
176 return;
177 }
178
179 fwrite (msg->request_body->data, 1, msg->request_body->length, f);
180 fclose (f);
181
182 soup_message_set_status (msg, created ? SOUP_STATUS_CREATED : SOUP_STATUS_OK);
183 }
184
185 static void
server_callback(SoupServer * server,SoupMessage * msg,const char * path,GHashTable * query,SoupClientContext * context,gpointer data)186 server_callback (SoupServer *server, SoupMessage *msg,
187 const char *path, GHashTable *query,
188 SoupClientContext *context, gpointer data)
189 {
190 char *file_path;
191 SoupMessageHeadersIter iter;
192 const char *name, *value;
193
194 g_print ("%s %s HTTP/1.%d\n", msg->method, path,
195 soup_message_get_http_version (msg));
196 soup_message_headers_iter_init (&iter, msg->request_headers);
197 while (soup_message_headers_iter_next (&iter, &name, &value))
198 g_print ("%s: %s\n", name, value);
199 if (msg->request_body->length)
200 g_print ("%s\n", msg->request_body->data);
201
202 file_path = g_strdup_printf (".%s", path);
203
204 if (msg->method == SOUP_METHOD_GET || msg->method == SOUP_METHOD_HEAD)
205 do_get (server, msg, file_path);
206 else if (msg->method == SOUP_METHOD_PUT)
207 do_put (server, msg, file_path);
208 else
209 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
210
211 g_free (file_path);
212 g_print (" -> %d %s\n\n", msg->status_code, msg->reason_phrase);
213 }
214
215 static void
quit(int sig)216 quit (int sig)
217 {
218 /* Exit cleanly on ^C in case we're valgrinding. */
219 exit (0);
220 }
221
222 static int port;
223 static const char *tls_cert_file, *tls_key_file;
224
225 static GOptionEntry entries[] = {
226 { "cert-file", 'c', 0,
227 G_OPTION_ARG_STRING, &tls_cert_file,
228 "Use FILE as the TLS certificate file", "FILE" },
229 { "key-file", 'k', 0,
230 G_OPTION_ARG_STRING, &tls_key_file,
231 "Use FILE as the TLS private key file", "FILE" },
232 { "port", 'p', 0,
233 G_OPTION_ARG_INT, &port,
234 "Port to listen on", NULL },
235 { NULL }
236 };
237
238 int
main(int argc,char ** argv)239 main (int argc, char **argv)
240 {
241 GOptionContext *opts;
242 GMainLoop *loop;
243 SoupServer *server;
244 GSList *uris, *u;
245 char *str;
246 GTlsCertificate *cert;
247 GError *error = NULL;
248
249 opts = g_option_context_new (NULL);
250 g_option_context_add_main_entries (opts, entries, NULL);
251 if (!g_option_context_parse (opts, &argc, &argv, &error)) {
252 g_printerr ("Could not parse arguments: %s\n",
253 error->message);
254 g_printerr ("%s",
255 g_option_context_get_help (opts, TRUE, NULL));
256 exit (1);
257 }
258 if (argc != 1) {
259 g_printerr ("%s",
260 g_option_context_get_help (opts, TRUE, NULL));
261 exit (1);
262 }
263 g_option_context_free (opts);
264
265 signal (SIGINT, quit);
266
267 if (tls_cert_file && tls_key_file) {
268 cert = g_tls_certificate_new_from_files (tls_cert_file, tls_key_file, &error);
269 if (error) {
270 g_printerr ("Unable to create server: %s\n", error->message);
271 exit (1);
272 }
273 server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "simple-httpd ",
274 SOUP_SERVER_TLS_CERTIFICATE, cert,
275 NULL);
276 g_object_unref (cert);
277
278 soup_server_listen_all (server, port, SOUP_SERVER_LISTEN_HTTPS, &error);
279 } else {
280 server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "simple-httpd ",
281 NULL);
282 soup_server_listen_all (server, port, 0, &error);
283 }
284
285 soup_server_add_handler (server, NULL,
286 server_callback, NULL, NULL);
287
288 uris = soup_server_get_uris (server);
289 for (u = uris; u; u = u->next) {
290 str = soup_uri_to_string (u->data, FALSE);
291 g_print ("Listening on %s\n", str);
292 g_free (str);
293 soup_uri_free (u->data);
294 }
295 g_slist_free (uris);
296
297 g_print ("\nWaiting for requests...\n");
298
299 loop = g_main_loop_new (NULL, TRUE);
300 g_main_loop_run (loop);
301
302 return 0;
303 }
304