X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=frontend%2Fcgi%2Fcgi.c;h=9b1b8d079b2852ef1b8a6c3a9ce11bde025c3385;hb=66c442f8a3717c265e7d41185b490aed0beedb9c;hp=76380bab4a541eda43e2dff2d10c89977c39dc5c;hpb=1998069911275d08a542e365928e52ba133e7664;p=cascardo%2Fatompub.git diff --git a/frontend/cgi/cgi.c b/frontend/cgi/cgi.c index 76380ba..9b1b8d0 100644 --- a/frontend/cgi/cgi.c +++ b/frontend/cgi/cgi.c @@ -19,19 +19,97 @@ #include +#include + +#define _GNU_SOURCE /* for asprintf */ #include #include #include #include +static int +uri_is_absolute (char *uri) +{ + return (*uri != '/'); +} + +static char * +getbaseurl (void) +{ + char *uri = NULL; + char *request_uri = getenv ("REQUEST_URI"); + char *path = getenv ("PATH_INFO"); + char *host = getenv ("HTTP_HOST"); + char *server = getenv ("SERVER_NAME"); + char *sport = getenv ("SERVER_PORT"); + int port; + if (sport) + { + port = strtol (sport, NULL, 0); + } + else + { + port = 0; + } + if (request_uri == NULL) + return NULL; + if (uri_is_absolute (request_uri)) + { + uri = strdup (request_uri); + } + else if (host) + { + asprintf (&uri, "http://%s%s", host, request_uri); + } + else if (server && port != 0 && port != 80) + { + asprintf (&uri, "http://%s:%d%s", server, port, request_uri); + } + else if (server) + { + asprintf (&uri, "http://%s%s", server, request_uri); + } + if (path && uri) + { + size_t pathl = strlen (path); + size_t uril = strlen (uri); + if (!strncmp (uri + uril - pathl, path, pathl)) + { + *(uri + uril - pathl) = 0; + } + } + return uri; +} + +static void +cgi_request_content_set (AtomCtx *ctx, AtomRequest *request) +{ + GIOChannel *channel; + GError *error = NULL; + gchar *data; + gsize len; + channel = g_io_channel_unix_new (0); + 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, aerr); + g_error_free (error); + return; + } + atom_request_content_set (request, data, len); + g_io_channel_unref (channel); + g_free (data); +} + static AtomRequest * cgi_get_request (AtomCtx *ctx) { AtomError *error; char *method = getenv ("REQUEST_METHOD"); char *path = getenv ("PATH_INFO"); - int type; - char *request; + AtomRequest *request; if (method == NULL) { error = atom_error_new (); @@ -47,6 +125,17 @@ cgi_get_request (AtomCtx *ctx) /* Remove the leading slash before mapping */ return atom_request_new (ATOM_REQUEST_GET, path + 1); } + else if (!strcmp (method, "POST")) + { + request = atom_request_new (ATOM_REQUEST_POST, path + 1); + cgi_request_content_set (ctx, request); + if (atom_error_get (ctx) != NULL) + { + atom_request_delete (request); + return NULL; + } + return request; + } error = atom_error_new (); atom_error_code_set (error, 501); atom_error_message_set (error, "Not Implemented");