Blame examples/simple-httpd.c

rpm-build 4f3c61
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
rpm-build 4f3c61
/*
rpm-build 4f3c61
 * Copyright (C) 2001-2003, Ximian, Inc.
rpm-build 4f3c61
 */
rpm-build 4f3c61
rpm-build 4f3c61
#include <errno.h>
rpm-build 4f3c61
#include <stdio.h>
rpm-build 4f3c61
#include <stdlib.h>
rpm-build 4f3c61
#include <string.h>
rpm-build 4f3c61
#include <sys/stat.h>
rpm-build 4f3c61
rpm-build 4f3c61
#include <libsoup/soup.h>
rpm-build 4f3c61
#include <glib/gstdio.h>
rpm-build 4f3c61
rpm-build 4f3c61
static int
rpm-build 4f3c61
compare_strings (gconstpointer a, gconstpointer b)
rpm-build 4f3c61
{
rpm-build 4f3c61
	const char **sa = (const char **)a;
rpm-build 4f3c61
	const char **sb = (const char **)b;
rpm-build 4f3c61
rpm-build 4f3c61
	return strcmp (*sa, *sb);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static GString *
rpm-build 4f3c61
get_directory_listing (const char *path)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GPtrArray *entries;
rpm-build 4f3c61
	GString *listing;
rpm-build 4f3c61
	char *escaped;
rpm-build 4f3c61
	GDir *dir;
rpm-build 4f3c61
	const gchar *d_name;
rpm-build 4f3c61
	int i;
rpm-build 4f3c61
rpm-build 4f3c61
	entries = g_ptr_array_new ();
rpm-build 4f3c61
	dir = g_dir_open (path, 0, NULL);
rpm-build 4f3c61
	if (dir) {
rpm-build 4f3c61
		while ((d_name = g_dir_read_name (dir))) {
rpm-build 4f3c61
			if (!strcmp (d_name, ".") ||
rpm-build 4f3c61
			    (!strcmp (d_name, "..") &&
rpm-build 4f3c61
			     !strcmp (path, "./")))
rpm-build 4f3c61
				continue;
rpm-build 4f3c61
			escaped = g_markup_escape_text (d_name, -1);
rpm-build 4f3c61
			g_ptr_array_add (entries, escaped);
rpm-build 4f3c61
		}
rpm-build 4f3c61
		g_dir_close (dir);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	g_ptr_array_sort (entries, (GCompareFunc)compare_strings);
rpm-build 4f3c61
rpm-build 4f3c61
	listing = g_string_new ("<html>\r\n");
rpm-build 4f3c61
	escaped = g_markup_escape_text (strchr (path, '/'), -1);
rpm-build 4f3c61
	g_string_append_printf (listing, "<head><title>Index of %s</title></head>\r\n", escaped);
rpm-build 4f3c61
	g_string_append_printf (listing, "<body>

Index of %s

\r\n

\r\n", escaped);

rpm-build 4f3c61
	g_free (escaped);
rpm-build 4f3c61
	for (i = 0; i < entries->len; i++) {
rpm-build 4f3c61
		g_string_append_printf (listing, "%s
\r\n",
rpm-build 4f3c61
					(char *)entries->pdata[i], 
rpm-build 4f3c61
					(char *)entries->pdata[i]);
rpm-build 4f3c61
		g_free (entries->pdata[i]);
rpm-build 4f3c61
	}
rpm-build 4f3c61
	g_string_append (listing, "</body>\r\n</html>\r\n");
rpm-build 4f3c61
rpm-build 4f3c61
	g_ptr_array_free (entries, TRUE);
rpm-build 4f3c61
	return listing;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
do_get (SoupServer *server, SoupMessage *msg, const char *path)
rpm-build 4f3c61
{
rpm-build 4f3c61
	char *slash;
rpm-build 4f3c61
	GStatBuf st;
rpm-build 4f3c61
rpm-build 4f3c61
	if (g_stat (path, &st) == -1) {
rpm-build 4f3c61
		if (errno == EPERM)
rpm-build 4f3c61
			soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
rpm-build 4f3c61
		else if (errno == ENOENT)
rpm-build 4f3c61
			soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
rpm-build 4f3c61
		else
rpm-build 4f3c61
			soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
rpm-build 4f3c61
		return;
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
rpm-build 4f3c61
		GString *listing;
rpm-build 4f3c61
		char *index_path;
rpm-build 4f3c61
rpm-build 4f3c61
		slash = strrchr (path, '/');
rpm-build 4f3c61
		if (!slash || slash[1]) {
rpm-build 4f3c61
			char *redir_uri;
rpm-build 4f3c61
rpm-build 4f3c61
			redir_uri = g_strdup_printf ("%s/", soup_message_get_uri (msg)->path);
rpm-build 4f3c61
			soup_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY,
rpm-build 4f3c61
						   redir_uri);
rpm-build 4f3c61
			g_free (redir_uri);
rpm-build 4f3c61
			return;
rpm-build 4f3c61
		}
rpm-build 4f3c61
rpm-build 4f3c61
		index_path = g_strdup_printf ("%s/index.html", path);
rpm-build 4f3c61
		if (g_stat (path, &st) != -1) {
rpm-build 4f3c61
			do_get (server, msg, index_path);
rpm-build 4f3c61
			g_free (index_path);
rpm-build 4f3c61
			return;
rpm-build 4f3c61
		}
rpm-build 4f3c61
		g_free (index_path);
rpm-build 4f3c61
rpm-build 4f3c61
		listing = get_directory_listing (path);
rpm-build 4f3c61
		soup_message_set_response (msg, "text/html",
rpm-build 4f3c61
					   SOUP_MEMORY_TAKE,
rpm-build 4f3c61
					   listing->str, listing->len);
rpm-build 4f3c61
		soup_message_set_status (msg, SOUP_STATUS_OK);
rpm-build 4f3c61
		g_string_free (listing, FALSE);
rpm-build 4f3c61
		return;
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	if (msg->method == SOUP_METHOD_GET) {
rpm-build 4f3c61
		GMappedFile *mapping;
rpm-build 4f3c61
		SoupBuffer *buffer;
rpm-build 4f3c61
rpm-build 4f3c61
		mapping = g_mapped_file_new (path, FALSE, NULL);
rpm-build 4f3c61
		if (!mapping) {
rpm-build 4f3c61
			soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
rpm-build 4f3c61
			return;
rpm-build 4f3c61
		}
rpm-build 4f3c61
rpm-build 4f3c61
		buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping),
rpm-build 4f3c61
						     g_mapped_file_get_length (mapping),
rpm-build 4f3c61
						     mapping, (GDestroyNotify)g_mapped_file_unref);
rpm-build 4f3c61
		soup_message_body_append_buffer (msg->response_body, buffer);
rpm-build 4f3c61
		soup_buffer_free (buffer);
rpm-build 4f3c61
	} else /* msg->method == SOUP_METHOD_HEAD */ {
rpm-build 4f3c61
		char *length;
rpm-build 4f3c61
rpm-build 4f3c61
		/* We could just use the same code for both GET and
rpm-build 4f3c61
		 * HEAD (soup-message-server-io.c will fix things up).
rpm-build 4f3c61
		 * But we'll optimize and avoid the extra I/O.
rpm-build 4f3c61
		 */
rpm-build 4f3c61
		length = g_strdup_printf ("%lu", (gulong)st.st_size);
rpm-build 4f3c61
		soup_message_headers_append (msg->response_headers,
rpm-build 4f3c61
					     "Content-Length", length);
rpm-build 4f3c61
		g_free (length);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	soup_message_set_status (msg, SOUP_STATUS_OK);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
do_put (SoupServer *server, SoupMessage *msg, const char *path)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GStatBuf st;
rpm-build 4f3c61
	FILE *f;
rpm-build 4f3c61
	gboolean created = TRUE;
rpm-build 4f3c61
rpm-build 4f3c61
	if (g_stat (path, &st) != -1) {
rpm-build 4f3c61
		const char *match = soup_message_headers_get_one (msg->request_headers, "If-None-Match");
rpm-build 4f3c61
		if (match && !strcmp (match, "*")) {
rpm-build 4f3c61
			soup_message_set_status (msg, SOUP_STATUS_CONFLICT);
rpm-build 4f3c61
			return;
rpm-build 4f3c61
		}
rpm-build 4f3c61
rpm-build 4f3c61
		if (!g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
rpm-build 4f3c61
			soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
rpm-build 4f3c61
			return;
rpm-build 4f3c61
		}
rpm-build 4f3c61
rpm-build 4f3c61
		created = FALSE;
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	f = fopen (path, "w");
rpm-build 4f3c61
	if (!f) {
rpm-build 4f3c61
		soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
rpm-build 4f3c61
		return;
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	fwrite (msg->request_body->data, 1, msg->request_body->length, f);
rpm-build 4f3c61
	fclose (f);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_message_set_status (msg, created ? SOUP_STATUS_CREATED : SOUP_STATUS_OK);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
server_callback (SoupServer *server, SoupMessage *msg,
rpm-build 4f3c61
		 const char *path, GHashTable *query,
rpm-build 4f3c61
		 SoupClientContext *context, gpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	char *file_path;
rpm-build 4f3c61
	SoupMessageHeadersIter iter;
rpm-build 4f3c61
	const char *name, *value;
rpm-build 4f3c61
rpm-build 4f3c61
	g_print ("%s %s HTTP/1.%d\n", msg->method, path,
rpm-build 4f3c61
		 soup_message_get_http_version (msg));
rpm-build 4f3c61
	soup_message_headers_iter_init (&iter, msg->request_headers);
rpm-build 4f3c61
	while (soup_message_headers_iter_next (&iter, &name, &value))
rpm-build 4f3c61
		g_print ("%s: %s\n", name, value);
rpm-build 4f3c61
	if (msg->request_body->length)
rpm-build 4f3c61
		g_print ("%s\n", msg->request_body->data);
rpm-build 4f3c61
rpm-build 4f3c61
	file_path = g_strdup_printf (".%s", path);
rpm-build 4f3c61
rpm-build 4f3c61
	if (msg->method == SOUP_METHOD_GET || msg->method == SOUP_METHOD_HEAD)
rpm-build 4f3c61
		do_get (server, msg, file_path);
rpm-build 4f3c61
	else if (msg->method == SOUP_METHOD_PUT)
rpm-build 4f3c61
		do_put (server, msg, file_path);
rpm-build 4f3c61
	else
rpm-build 4f3c61
		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
rpm-build 4f3c61
rpm-build 4f3c61
	g_free (file_path);
rpm-build 4f3c61
	g_print ("  -> %d %s\n\n", msg->status_code, msg->reason_phrase);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
quit (int sig)
rpm-build 4f3c61
{
rpm-build 4f3c61
	/* Exit cleanly on ^C in case we're valgrinding. */
rpm-build 4f3c61
	exit (0);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static int port;
rpm-build 4f3c61
static const char *tls_cert_file, *tls_key_file;
rpm-build 4f3c61
rpm-build 4f3c61
static GOptionEntry entries[] = {
rpm-build 4f3c61
	{ "cert-file", 'c', 0,
rpm-build 4f3c61
	  G_OPTION_ARG_STRING, &tls_cert_file,
rpm-build 4f3c61
	  "Use FILE as the TLS certificate file", "FILE" },
rpm-build 4f3c61
	{ "key-file", 'k', 0,
rpm-build 4f3c61
	  G_OPTION_ARG_STRING, &tls_key_file,
rpm-build 4f3c61
	  "Use FILE as the TLS private key file", "FILE" },
rpm-build 4f3c61
	{ "port", 'p', 0,
rpm-build 4f3c61
	  G_OPTION_ARG_INT, &port,
rpm-build 4f3c61
	  "Port to listen on", NULL },
rpm-build 4f3c61
	{ NULL }
rpm-build 4f3c61
};
rpm-build 4f3c61
rpm-build 4f3c61
int
rpm-build 4f3c61
main (int argc, char **argv)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GOptionContext *opts;
rpm-build 4f3c61
	GMainLoop *loop;
rpm-build 4f3c61
	SoupServer *server;
rpm-build 4f3c61
	GSList *uris, *u;
rpm-build 4f3c61
	char *str;
rpm-build 4f3c61
	GTlsCertificate *cert;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	opts = g_option_context_new (NULL);
rpm-build 4f3c61
	g_option_context_add_main_entries (opts, entries, NULL);
rpm-build 4f3c61
	if (!g_option_context_parse (opts, &argc, &argv, &error)) {
rpm-build 4f3c61
		g_printerr ("Could not parse arguments: %s\n",
rpm-build 4f3c61
			    error->message);
rpm-build 4f3c61
		g_printerr ("%s",
rpm-build 4f3c61
			    g_option_context_get_help (opts, TRUE, NULL));
rpm-build 4f3c61
		exit (1);
rpm-build 4f3c61
	}
rpm-build 4f3c61
	if (argc != 1) {
rpm-build 4f3c61
		g_printerr ("%s",
rpm-build 4f3c61
			    g_option_context_get_help (opts, TRUE, NULL));
rpm-build 4f3c61
		exit (1);
rpm-build 4f3c61
	}
rpm-build 4f3c61
	g_option_context_free (opts);
rpm-build 4f3c61
rpm-build 4f3c61
	signal (SIGINT, quit);
rpm-build 4f3c61
rpm-build 4f3c61
	if (tls_cert_file && tls_key_file) {
rpm-build 4f3c61
		cert = g_tls_certificate_new_from_files (tls_cert_file, tls_key_file, &error);
rpm-build 4f3c61
		if (error) {
rpm-build 4f3c61
			g_printerr ("Unable to create server: %s\n", error->message);
rpm-build 4f3c61
			exit (1);
rpm-build 4f3c61
		}
rpm-build 4f3c61
		server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "simple-httpd ",
rpm-build 4f3c61
					  SOUP_SERVER_TLS_CERTIFICATE, cert,
rpm-build 4f3c61
					  NULL);
rpm-build 4f3c61
		g_object_unref (cert);
rpm-build 4f3c61
rpm-build 4f3c61
		soup_server_listen_all (server, port, SOUP_SERVER_LISTEN_HTTPS, &error);
rpm-build 4f3c61
	} else {
rpm-build 4f3c61
		server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "simple-httpd ",
rpm-build 4f3c61
					  NULL);
rpm-build 4f3c61
		soup_server_listen_all (server, port, 0, &error);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	soup_server_add_handler (server, NULL,
rpm-build 4f3c61
				 server_callback, NULL, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	uris = soup_server_get_uris (server);
rpm-build 4f3c61
	for (u = uris; u; u = u->next) {
rpm-build 4f3c61
		str = soup_uri_to_string (u->data, FALSE);
rpm-build 4f3c61
		g_print ("Listening on %s\n", str);
rpm-build 4f3c61
		g_free (str);
rpm-build 4f3c61
		soup_uri_free (u->data);
rpm-build 4f3c61
	}
rpm-build 4f3c61
	g_slist_free (uris);
rpm-build 4f3c61
rpm-build 4f3c61
	g_print ("\nWaiting for requests...\n");
rpm-build 4f3c61
rpm-build 4f3c61
	loop = g_main_loop_new (NULL, TRUE);
rpm-build 4f3c61
	g_main_loop_run (loop);
rpm-build 4f3c61
rpm-build 4f3c61
	return 0;
rpm-build 4f3c61
}