Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: potyl/camarero
base: 5bb1cb21db
...
head fork: potyl/camarero
compare: 1fcd755a06
Checking mergeability… Don't worry, you can still create the pull request.
  • 16 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
1  .gitignore
@@ -1,3 +1,4 @@
camarero
*.dSYM
src/*.o
+gen
View
41 Makefile
@@ -2,45 +2,62 @@ CC=cc
CC_OPT_FLAGS=-O0 -g3
PKG_LIBS=libsoup-2.4 glib-2.0 gthread-2.0
CFLAGS_RAW=$(shell pkg-config --cflags $(PKG_LIBS))
-LIBS=$(shell pkg-config --libs $(PKG_LIBS))
-COMPILER=$(CC) --std=c99 $(CC_OPT_FLAGS) $(CFLAGS)
-LINKER=$(CC) $(LIBS)
-
ifdef DEBUG
CFLAGS=-DDEBUG=1 $(CFLAGS_RAW)
else
CFLAGS=$(CFLAGS_RAW)
endif
+COMPILER=$(CC) --std=c99 $(CC_OPT_FLAGS) $(CFLAGS) -Igen
+
+
+RESOURCES=res/favicon.ico res/index.html
.PHONY: all
all: camarero
-src/camarero-mime-types.o: src/camarero-mime-types.c src/camarero-mime-types.h
+gen/camarero-resources.c: res/camarero.gresource.xml $(RESOURCES)
+ @-[ -d gen ] || mkdir gen
+ glib-compile-resources --target=$@ --generate-source --c-name camarero $<
+
+gen/camarero-resources.h: res/camarero.gresource.xml $(RESOURCES)
+ @-[ -d gen ] || mkdir gen
+ glib-compile-resources --target=$@ --generate-header --c-name camarero $<
+
+gen/camarero-mime-types.o: src/camarero-mime-types.c src/camarero-mime-types.h
+ @-[ -d gen ] || mkdir gen
$(COMPILER) -Isrc -c -o $@ $<
+gen/camarero.o: src/camarero.c src/config.h gen/camarero-resources.h
+ @-[ -d gen ] || mkdir gen
+ $(COMPILER) -Isrc -c -o $@ $<
-src/camarero.o: src/camarero.c src/config.h
+gen/camarero-resources.o: gen/camarero-resources.c gen/camarero-resources.h
+ @-[ -d gen ] || mkdir gen
$(COMPILER) -Isrc -c -o $@ $<
-camarero: src/camarero.o src/camarero-mime-types.o
- $(LINKER) -o $@ $^
+.PHONY: camarero
+camarero: gen/camarero
+gen/camarero: gen/camarero.o gen/camarero-mime-types.o gen/camarero-resources.o
+ $(CC) -o $@ $^ `pkg-config --libs $(PKG_LIBS)`
.PHONY: gdb
-gdb: camarero
+gdb: gen/camarero
G_DEBUG=fatal_warnings gdb ./$<
-
ifneq ($(shell uname),Darwin)
-camarero-static: src/camarero.o src/camarero-mime-types.o
- $(LINKER) -static -static-libgcc -o $@ $^ `pkg-config --static --libs $(PKG_LIBS)` -lpcre -lselinux
+.PHONY: static
+static: gen/camarero-static
+gen/camarero-static: gen/camarero.o gen/camarero-mime-types.o gen/camarero-resources.o
+ $(CC) -static -static-libgcc -o $@ $^ `pkg-config --static --libs $(PKG_LIBS)` -lpcre -lselinux
endif
.PHONY: clean
clean:
rm -f camarero camarero-static src/*.o
+ -[ -d gen ] && rm -rf gen/*
View
7 res/camarero.gresource.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+ <gresource prefix="/camarero">
+ <file alias="icons/favicon.ico">res/favicon.ico</file>
+ <file alias="html/index.html">res/index.html</file>
+ </gresource>
+</gresources>
View
BIN  res/favicon.ico
Binary file not shown
View
10 res/index.html
@@ -0,0 +1,10 @@
+<html>
+<head>
+ <title>Dir $path</title>
+ <link rel='shortcut icon' href='/favicon.ico'>
+</head>
+<body>
+<h1>Dir $path</h1>
+$body
+</body>
+</html>
View
76 src/camarero.c
@@ -44,6 +44,7 @@
#include "config.h"
#include "camarero-mime-types.h"
+#include "camarero-resources.h"
#if !GLIB_CHECK_VERSION(2, 30, 0)
@@ -86,6 +87,7 @@ typedef struct _CamareroApp {
gchar *username;
gchar *password;
GHashTable *mime_types;
+ GResource *gresource;
} CamareroApp;
CamareroApp APP = {0,};
@@ -113,6 +115,12 @@ camarero_app_free () {
g_hash_table_unref(APP.mime_types);
APP.mime_types = NULL;
}
+
+
+ if (APP.gresource != NULL) {
+ g_resource_unref(APP.gresource);
+ APP.gresource = NULL;
+ }
}
@@ -139,8 +147,24 @@ camarero_favicon_callback (
const char *path, GHashTable *query,
SoupClientContext *context, gpointer data
) {
+ GError *error = NULL;
+ GBytes *bytes = g_resource_lookup_data(APP.gresource, "/camarero/icons/favicon.ico", G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
+ if (bytes != NULL) {
+ soup_message_set_status(msg, SOUP_STATUS_OK);
+ gsize size;
+ gconstpointer data = g_bytes_get_data(bytes, &size);
+ soup_message_body_append(msg->response_body, SOUP_MEMORY_COPY, data, size);
+ g_bytes_unref(bytes);
+
+ //gchar *size_str = g_format_size(size);
+ //g_printf("%3d %s (%s)\n", SOUP_STATUS_OK, path, size_str);
+ //g_free(size_str);
+ return;
+ }
+
soup_message_set_status(msg, SOUP_STATUS_NOT_FOUND);
soup_message_body_append(msg->response_body, SOUP_MEMORY_STATIC, "", 0);
+ //g_printf("%3d %s (0 bytes)\n", SOUP_STATUS_NOT_FOUND, path);
}
@@ -172,6 +196,18 @@ camarero_memmap_message_write (SoupMessage *msg, SoupBuffer *buffer, gpointer da
}
+static gboolean
+camarero_regexp_callback (const GMatchInfo *info, GString *buffer, gpointer data) {
+ gchar *match = g_match_info_fetch(info, 0);
+ gchar *value = g_hash_table_lookup((GHashTable *)data, match);
+ if (value == NULL) value = "";
+ g_string_append(buffer, value);
+ g_free(match);
+
+ return FALSE;
+}
+
+
static void
camarero_server_callback (
SoupServer *server, SoupMessage *msg,
@@ -296,11 +332,8 @@ camarero_server_callback (
// Build an HTML page with the folder contents
- GString *buffer = g_string_new_len(NULL, 4096);
- content_type = "text/html";
- g_string_append_printf(buffer, "<html><head><title>Dir %s</title></head><body>\n", path);
- g_string_append_printf(buffer, "<h1>Dir %s</h1>\n", path);
+ GString *buffer = g_string_sized_new(4096);
if (array->len) {
g_string_append_printf(buffer, "<p>%d files</p>\n<ul>\n", array->len);
for (guint i = 0; i < array->len; ++i) {
@@ -329,12 +362,37 @@ camarero_server_callback (
}
g_ptr_array_free(array, TRUE);
- g_string_append(buffer, "</body></html>\n");
- soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, buffer->str, buffer->len);
- len = buffer->len;
- status = SOUP_STATUS_OK;
+ GBytes *bytes = g_resource_lookup_data(APP.gresource, "/camarero/html/index.html", G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
+ if (bytes == NULL) {
+ g_string_free(buffer, TRUE);
+ error_str = g_strdup_printf("Can't find document template: %s", "/camarero/html/index.html");
+ status = SOUP_STATUS_INTERNAL_SERVER_ERROR;
+ goto DONE;
+ }
+
+ content_type = "text/html";
+ gchar *template = NULL;
+ gsize size;
+ gconstpointer data = g_bytes_get_data(bytes, &size);
+ template = g_strndup(data, size);
+ g_bytes_unref(bytes);
+
+ // Create the document's body with all variable interpolated
+ GHashTable *vars = g_hash_table_new(g_str_hash, g_str_equal);
+ g_hash_table_insert(vars, "$body", buffer->str);
+ g_hash_table_insert(vars, "$path", (gpointer) path);
+
+ GRegex *regex = g_regex_new("\\$\\w+", 0, 0, NULL);
+ gchar *doc = g_regex_replace_eval(regex, template, -1, 0, 0, camarero_regexp_callback, vars, NULL);
+ g_free(template);
+ g_hash_table_destroy(vars);
g_string_free(buffer, FALSE);
+ g_regex_unref(regex);
+
+ len = strlen(doc);
+ soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, doc, len);
+ status = SOUP_STATUS_OK;
goto DONE;
}
@@ -647,6 +705,8 @@ main (int argc, char ** argv) {
g_thread_init(NULL);
g_type_init();
+ APP.gresource = camarero_get_resource();
+
signal(SIGTERM, camarero_signal_end);
signal(SIGQUIT, camarero_signal_end);
signal(SIGINT, camarero_signal_end);

No commit comments for this range

Something went wrong with that request. Please try again.