]> saetta.ns0.it Git - libzakcgi/commitdiff
Redirect now works with relative urls (not with domain) (closes #942).
authorAndrea Zagli <azagli@libero.it>
Wed, 29 Jul 2015 14:12:41 +0000 (16:12 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 29 Jul 2015 14:12:41 +0000 (16:12 +0200)
src/main.c
src/main.h
tests/redirect.c

index 23f554f3ab54ec31d1b534feebeef8825b435886..75a6f05e4d33b947cc4cb97577ebca4148265fde 100644 (file)
@@ -806,12 +806,62 @@ GHashTable
 }
 
 void
-zak_cgi_main_redirect (const gchar *url)
+zak_cgi_main_redirect (ZakCgiMain *zakcgimain, const gchar *url)
 {
+       GString *_url;
+       GHashTable *ht_env;
+       gchar *value;
+       GRegex *regex;
+
+       _url = g_string_new ("");
+       if (!g_str_has_prefix (url, "http://")
+               && !g_str_has_prefix (url, "https://")
+               && !g_str_has_prefix (url, "ftp://"))
+               {
+                       ht_env = zak_cgi_main_get_env (zakcgimain);
+
+                       value = (gchar *)g_hash_table_lookup (ht_env, "REQUEST_SCHEME");
+                       if (value != NULL)
+                               {
+                                       g_string_append_printf (_url, "%s://", value);
+                               }
+
+                       value = (gchar *)g_hash_table_lookup (ht_env, "SERVER_NAME");
+                       if (value != NULL)
+                               {
+                                       if (!g_str_has_prefix (url, value))
+                                               {
+                                                       g_string_append_printf (_url, value);
+                                               }
+                               }
+
+                       if (!g_str_has_prefix (url, "/"))
+                               {
+                                       /* TODO
+                                        * test if it starts with a domain, ex: www.google.it */
+                                       value = (gchar *)g_hash_table_lookup (ht_env, "CONTEXT_PREFIX");
+                                       if (value != NULL)
+                                               {
+                                                       if (!g_str_has_prefix (url, value))
+                                                               {
+                                                                       g_string_append_printf (_url, value);
+                                                               }
+                                               }
+                               }
+
+                       g_string_append_printf (_url, url);
+               }
+       else
+               {
+                       g_string_assign (_url, url);
+               }
+
        g_printf ("%s%s%c%c\n",
                  "Location: ",
-                 url,
+                 _url->str,
                  13, 10);
+
+       g_string_free (_url, TRUE);
 }
 
 gboolean
index aa15756ec45b7953ba3d567e4770b44c8324502c..1ad3a6e5493ac12780076223a56438602f4ac215 100644 (file)
@@ -91,7 +91,7 @@ gchar *zak_cgi_main_get_stdin (ZakCgiMain *zakcgimain);
 
 GHashTable *zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary);
 
-void zak_cgi_main_redirect (const gchar *url);
+void zak_cgi_main_redirect (ZakCgiMain *zakcgimain, const gchar *url);
 
 gboolean zak_cgi_main_is_request_method (ZakCgiMain *zakcgimain, const gchar *method);
 
index bdc0f199951b6e7d2bf3ebe5e6de06b7010205fa..2069d8b6f1fd6cee65707bfff5aad2ee734ab76b 100644 (file)
 int
 main (int argc, char *argv[])
 {
+       ZakCgiMain *zakcgi_main;
        GHashTable *ht_env;
 
-       ht_env = zak_cgi_main_get_parameters (NULL, NULL);
+       zakcgi_main = zak_cgi_main_new ();
+       ht_env = zak_cgi_main_get_parameters (zakcgi_main, NULL);
 
-       zak_cgi_main_redirect ((gchar *)g_value_get_string ((GValue *)(const gchar *)g_hash_table_lookup (ht_env, "redirectto")));
-
-       g_hash_table_destroy (ht_env);
+       zak_cgi_main_redirect (zakcgi_main, (gchar *)g_value_get_string ((GValue *)(const gchar *)g_hash_table_lookup (ht_env, "redirectto")));
 
        return 0;
 }
-