#include <glib.h>
#include <atompub/atom.h>
+#include <atompub/atom-glib.h>
static gchar *
giochannel_iri_to_filename (AtomCtx *ctx, IRI *iri)
g_free (filename);
if (channel == NULL)
{
- atom_error_set (ctx, error);
+ AtomError *aerr = atom_error_new_from_gerror (error);
+ atom_error_set (ctx, aerr);
+ g_error_free (error);
return NULL;
}
error = NULL;
if (g_io_channel_read_to_end (channel, &data, &len, &error) !=
G_IO_STATUS_NORMAL)
{
+ AtomError *aerr = atom_error_new_from_gerror (error);
g_io_channel_unref (channel);
- atom_error_set (ctx, error);
+ atom_error_set (ctx, aerr);
+ g_error_free (error);
return NULL;
}
g_io_channel_unref (channel);
#include <glib.h>
#include <gio/gio.h>
#include <atompub/atom.h>
+#include <atompub/atom-glib.h>
static GFile *
gio_iri_to_file (AtomCtx *ctx, IRI *iri)
error = NULL;
if (!g_file_load_contents (file, NULL, &data, &len, NULL, &error))
{
+ AtomError *aerr = atom_error_new_from_gerror (error);
g_object_unref (file);
- atom_error_set (ctx, error);
+ atom_error_set (ctx, aerr);
+ g_error_free (error);
return NULL;
}
g_object_unref (file);
value = g_key_file_get_string (keyfile, group, key, &error);
if (value == NULL)
{
- atom_error_set (ctx, error);
+ AtomError *aerr = atom_error_new ();
+ atom_error_code_set (aerr, 500);
+ atom_error_message_set (aerr, error->message);
+ atom_error_set (ctx, aerr);
+ g_error_free (error);
}
return value;
}
if (!g_key_file_load_from_file (keyfile, "atompub.conf",
G_KEY_FILE_NONE, &error))
{
- atom_error_set (ctx, error);
+ AtomError *aerr = atom_error_new ();
+ atom_error_code_set (aerr, 500);
+ atom_error_message_set (aerr, error->message);
+ atom_error_set (ctx, aerr);
+ g_error_free (error);
return;
}
atom_config_data_set (ctx, keyfile);
{
IRI *iri = iri_new ();
Atom *atom;
- GError *error;
+ AtomError *error;
iri_set_path (iri, path);
atom = atom_retrieve_resource (ctx, iri);
iri_delete (iri);
}
else if ((error = atom_error_get (ctx)) != NULL)
{
- if (error->domain == G_FILE_ERROR &&
- (error->code == G_FILE_ERROR_EXIST ||
- error->code == G_FILE_ERROR_ACCES ||
- error->code == G_FILE_ERROR_PERM))
- fprintf (stdout, "Status: 403 %s\n\n%s\n", error->message,
- error->message);
- else if (error->domain == G_FILE_ERROR &&
- error->code == G_FILE_ERROR_NOENT)
- fprintf (stdout, "Status: 404 %s\n\n%s\n", error->message,
- error->message);
- else
- fprintf (stdout, "Status: 500 %s\n\n%s\n", error->message,
- error->message);
+ int code = atom_error_code (error);
+ char *message = atom_error_message (error);
+ fprintf (stdout, "Status: %d %s\n\n%s\n", code, message, message);
+ }
+ else
+ {
+ fprintf (stdout, "Status: 500 Server error\n\nServer error\n");
}
}
else
-include_HEADERS = atom.h config.h ctx.h entry.h error.h feed.h iri.h backend.h
+include_HEADERS = atom.h config.h ctx.h entry.h error.h feed.h \
+ iri.h backend.h atom-glib.h error-glib.h
--- /dev/null
+/*
+ * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#ifndef ATOMPUB_ATOM_GLIB_H
+#define ATOMPUB_ATOM_GLIB_H
+
+#include <atompub/error-glib.h>
+
+#endif
#ifndef ATOMPUB_ENTRY_H
#define ATOMPUB_ENTRY_H
-#include <atompub/ctx.h>
+#include <sys/types.h>
typedef struct _atom_entry Atom;
--- /dev/null
+/*
+ * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#ifndef ATOMPUB_ERROR_GLIB_H
+#define ATOMPUB_ERROR_GLIB_H
+
+#include <atompub/error.h>
+#include <glib.h>
+
+AtomError * atom_error_new_from_gerror (GError *);
+
+#endif
#define ATOMPUB_ERROR_H
#include <atompub/ctx.h>
-#include <glib.h>
-GError *atom_error_get (AtomCtx *);
-void atom_error_set (AtomCtx *, GError *);
+typedef struct _atom_error AtomError;
+
+AtomError * atom_error_get (AtomCtx *);
+void atom_error_set (AtomCtx *, AtomError *);
+AtomError * atom_error_new (void);
+void atom_error_delete (AtomError *);
+int atom_error_code (AtomError *);
+void atom_error_code_set (AtomError *, int);
+char * atom_error_message (AtomError *);
+void atom_error_message_set (AtomError *, char *);
#endif
bin_PROGRAMS = atompub
-atompub_SOURCES = main.c config.c ctx.c backend.c
+atompub_SOURCES = main.c config.c ctx.c backend.c error.c
atompub_CFLAGS = -I$(top_srcdir)/include
atompub_CFLAGS += $(GLIB_CFLAGS)
atompub_LDADD = -L$(top_builddir)/backend/files -lgiochannel
#include <atompub/atom.h>
+#include <glib.h>
struct _atom_backend
{
struct _atom_ctx
{
- GError *error;
+ AtomError *error;
gpointer config_data;
AtomBackend *backend;
};
atom_ctx_delete (AtomCtx *ctx)
{
if (ctx->error)
- g_error_free (ctx->error);
+ atom_error_delete (ctx->error);
if (ctx->backend)
atom_backend_delete (ctx->backend);
g_slice_free (AtomCtx, ctx);
}
void
-atom_error_set (AtomCtx *ctx, GError *error)
+atom_error_set (AtomCtx *ctx, AtomError *error)
{
if (ctx->error)
- g_error_free (ctx->error);
+ atom_error_delete (ctx->error);
ctx->error = error;
}
-GError *
+AtomError *
atom_error_get (AtomCtx *ctx)
{
return ctx->error;
--- /dev/null
+/*
+ * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <atompub/atom.h>
+
+#include <glib.h>
+
+struct _atom_error
+{
+ int code;
+ char * message;
+};
+
+AtomError *
+atom_error_new ()
+{
+ AtomError *error;
+ error = g_slice_new (AtomError);
+ error->code = 0;
+ error->message = NULL;
+ return error;
+}
+
+void
+atom_error_delete (AtomError *error)
+{
+ if (error->message)
+ g_free (error->message);
+ g_slice_free (AtomError, error);
+}
+
+int
+atom_error_code (AtomError *error)
+{
+ return error->code;
+}
+
+void
+atom_error_code_set (AtomError *error, int code)
+{
+ error->code = code;
+}
+
+char *
+atom_error_message (AtomError *error)
+{
+ return error->message;
+}
+
+void
+atom_error_message_set (AtomError *error, char *message)
+{
+ error->message = g_strdup (message);
+}
+
+AtomError *
+atom_error_new_from_gerror (GError *error)
+{
+ AtomError *aerr;
+ aerr = atom_error_new ();
+ if (error->domain == G_FILE_ERROR)
+ {
+ switch (error->code)
+ {
+ case G_FILE_ERROR_EXIST:
+ case G_FILE_ERROR_ACCES:
+ case G_FILE_ERROR_PERM:
+ aerr->code = 403;
+ break;
+ case G_FILE_ERROR_NOENT:
+ aerr->code = 404;
+ break;
+ default:
+ aerr->code = 500;
+ break;
+ }
+ }
+ else
+ {
+ aerr->code = 500;
+ }
+ aerr->message = error->message;
+ return aerr;
+}
AtomCtx *ctx;
IRI *iri;
Atom *atom;
- GError *error;
+ AtomError *error;
g_type_init ();
ctx = atom_ctx_new ();
atom_config_init (ctx);
atom_backend_set (ctx, giochannel_backend ());
if ((error = atom_error_get (ctx)) != NULL)
{
- g_message ("%s\n", error->message);
+ g_message ("%s\n", atom_error_message (error));
exit (1);
}
cgi_serve_request (ctx);