]> saetta.ns0.it Git - libzakcgi/commitdiff
ZakCgi::parse_stdin now supports variables with multiple values (closes #943).
authorAndrea Zagli <azagli@libero.it>
Sun, 26 Jul 2015 09:03:08 +0000 (11:03 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 26 Jul 2015 09:03:08 +0000 (11:03 +0200)
src/main.c
tests/env.c
tests/provapostcgi.html

index 11a08f6f33feb01e8f1a2f84eaf49bd0a662bb15..c92302c19dbdb5fc1f6d4340ae2f1bed4a6aff5e 100644 (file)
@@ -408,7 +408,7 @@ GHashTable
                        gval = (GValue *)g_hash_table_lookup (ht, parts[0]);
                        if (gval != NULL)
                                {
-                                       if (!G_VALUE_HOLDS (gval, G_TYPE_BOXED))
+                                       if (!G_VALUE_HOLDS (gval, G_TYPE_PTR_ARRAY))
                                                {
                                                        /* convert to GPtrArray */
                                                        ar = g_ptr_array_new ();
@@ -547,7 +547,7 @@ ZakCgiFile
 
        b = g_slice_new (ZakCgiFile);
        b->name = g_strdup (file->name);
-       b->content = g_strdup (file->content);
+       b->content = g_memdup (file->content, file->size);
        b->size = file->size;
 
        return b;
@@ -653,9 +653,12 @@ GHashTable
                                                        guint file_l;
 
                                                        GValue *gval;
+                                                       GValue *gval_tmp;
 
                                                        ZakCgiFile *zgfile;
 
+                                                       GPtrArray *ar;
+
                                                        content_disposition = zak_cgi_main_read_line (buf, l, &i, TRUE, &bytesread);
 
                                                        v_content = g_strsplit (content_disposition, ";", -1);
@@ -726,8 +729,7 @@ GHashTable
                                                                        param_value = zak_cgi_main_read_line (buf, l, &i, TRUE, &bytesread);
                                                                }
 
-                                                       gval = g_new0 (GValue, 1);
-
+                                                       gval_tmp = g_new0 (GValue, 1);
                                                        if (l_v_content == 3)
                                                                {
                                                                        zgfile = (ZakCgiFile *)g_new0 (ZakCgiFile, 1);
@@ -735,16 +737,46 @@ GHashTable
                                                                        zgfile->content = g_memdup (param_value, file_l - 2);
                                                                        zgfile->size = file_l - 2;
 
-                                                                       g_value_init (gval, ZAK_CGI_TYPE_FILE);
-                                                                       g_value_take_boxed (gval, zgfile);
+                                                                       g_value_init (gval_tmp, ZAK_CGI_TYPE_FILE);
+                                                                       g_value_take_boxed (gval_tmp, zgfile);
                                                                }
                                                        else
                                                                {
-                                                                       g_value_init (gval, G_TYPE_STRING);
-                                                                       g_value_set_string (gval, g_strdup (param_value));
+                                                                       g_value_init (gval_tmp, G_TYPE_STRING);
+                                                                       g_value_set_string (gval_tmp, g_strdup (param_value));
                                                                }
 
-                                                       g_hash_table_replace (ht, g_strdup (param_name), gval);
+                                                       gval = g_hash_table_lookup (ht, param_name);
+                                                       if (gval != NULL)
+                                                               {
+                                                                       if (!G_VALUE_HOLDS (gval, G_TYPE_PTR_ARRAY))
+                                                                               {
+                                                                                       GValue *g;
+
+                                                                                       g = (GValue *)g_new0 (GValue, 1);
+                                                                                       g_value_init (g, G_VALUE_TYPE (gval));
+                                                                                       g_value_copy (gval, g);
+                                                                                       g_value_unset (gval);
+
+                                                                                       /* converto to GPtrArray */
+                                                                                       ar = g_ptr_array_new ();
+
+                                                                                       g_ptr_array_add (ar, g);
+
+                                                                                       g_value_init (gval, G_TYPE_PTR_ARRAY);
+                                                                                       g_value_take_boxed (gval, ar);
+                                                                                       g_hash_table_replace (ht, g_strdup (param_name), gval);
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       ar = (GPtrArray *)g_value_get_boxed (gval);
+                                                                               }
+                                                                       g_ptr_array_add (ar, gval_tmp);
+                                                               }
+                                                       else
+                                                               {
+                                                                       g_hash_table_replace (ht, g_strdup (param_name), gval_tmp);
+                                                               }
 
                                                        g_free (param_name);
                                                        g_free (param_value);
index 30b6dcd5bf509de443cbed18a7ab71e8d498d917..be53b5bcfea17237b850f4a35908dd9454b17d99 100644 (file)
 
 #include <main.h>
 
+gchar
+*get_value (GValue *value)
+{
+       gchar *ret;
+
+       if (G_VALUE_HOLDS (value, ZAK_CGI_TYPE_FILE))
+               {
+                       ZakCgiFile *zgfile = (ZakCgiFile *)g_value_get_boxed ((GValue *)value);
+
+                       ret = g_strdup (zgfile->name);
+
+                       if (g_strcmp0 (zgfile->name, "") != 0)
+                               {
+                                       /* save the file to tmp */
+                                       GFile *gfile;
+                                       GFileIOStream *iostream;
+                                       GOutputStream *ostream;
+
+                                       iostream = NULL;
+                                       gfile = g_file_new_tmp (g_strdup_printf ("cgi-XXXXXX-%s", zgfile->name),
+                                                                                       &iostream,
+                                                                                       NULL);
+
+                                       ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
+                                       g_output_stream_write (ostream,
+                                                                                  zgfile->content,
+                                                                                  zgfile->size,
+                                                                                  NULL,
+                                                                                  NULL);
+                                       g_output_stream_close (ostream, NULL, NULL);
+
+                                       g_object_unref (ostream);
+                                       g_object_unref (gfile);
+                               }
+               }
+       else if (G_VALUE_HOLDS (value, G_TYPE_STRING))
+               {
+                       ret = g_strdup ((gchar *)g_value_get_string ((GValue *)value));
+               }
+       else
+               {
+                       ret = g_strdup ("value of unknown type");
+               }
+
+       return ret;
+}
+
 int
 main (int argc, char *argv[])
 {
        gchar *env;
        GString *str;
        GHashTable *ht;
+       gchar *ret;
 
        env = zak_cgi_main_dump_env (NULL);
 
@@ -62,49 +110,26 @@ main (int argc, char *argv[])
                                        g_hash_table_iter_init (&iter, ht);
                                        while (g_hash_table_iter_next (&iter, &key, &value))
                                                {
-                                                       if (G_VALUE_HOLDS (value, ZAK_CGI_TYPE_FILE))
+                                                       if (G_VALUE_HOLDS (value, G_TYPE_PTR_ARRAY))
                                                                {
-                                                                       ZakCgiFile *zgfile = (ZakCgiFile *)g_value_get_boxed ((GValue *)value);
-
-                                                                       g_string_append_printf (str,
-                                                                                                                       "<tr><td>%s</td><td>%s</td></tr>\n",
-                                                                                                                       (gchar *)key, zgfile->name);
-
-                                                                       if (g_strcmp0 (zgfile->name, "") != 0)
+                                                                       guint i;
+                                                                       GPtrArray *ar = (GPtrArray *)g_value_get_boxed (value);
+                                                                       for (i = 0; i < ar->len; i++)
                                                                                {
-                                                                                       /* save the file to tmp */
-                                                                                       GFile *gfile;
-                                                                                       GFileIOStream *iostream;
-                                                                                       GOutputStream *ostream;
-
-                                                                                       iostream = NULL;
-                                                                                       gfile = g_file_new_tmp (g_strdup_printf ("cgi-XXXXXX-%s", zgfile->name),
-                                                                                                                                       &iostream,
-                                                                                                                                       NULL);
-
-                                                                                       ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
-                                                                                       g_output_stream_write (ostream,
-                                                                                                                                  zgfile->content,
-                                                                                                                                  zgfile->size,
-                                                                                                                                  NULL,
-                                                                                                                                  NULL);
-                                                                                       g_output_stream_close (ostream, NULL, NULL);
-
-                                                                                       g_object_unref (ostream);
-                                                                                   g_object_unref (gfile);
+                                                                                       ret = get_value (g_ptr_array_index (ar, i));
+                                                                                       g_string_append_printf (str,
+                                                                                                                                       "<tr><td>%s[%d]</td><td>%s</td></tr>\n",
+                                                                                                                                       (gchar *)key, i, ret);
+                                                                                       g_free (ret);
                                                                                }
                                                                }
-                                                       else if (G_VALUE_HOLDS (value, G_TYPE_STRING))
-                                                               {
-                                                                       g_string_append_printf (str,
-                                                                                                                       "<tr><td>%s</td><td>%s</td></tr>\n",
-                                                                                                                       (gchar *)key, (gchar *)g_value_get_string ((GValue *)value));
-                                                               }
                                                        else
                                                                {
+                                                                       ret = get_value ((GValue *)value);
                                                                        g_string_append_printf (str,
                                                                                                                        "<tr><td>%s</td><td>%s</td></tr>\n",
-                                                                                                                       (gchar *)key, "value of unknown type");
+                                                                                                                       (gchar *)key, ret);
+                                                                       g_free (ret);
                                                                }
                                                }
 
index 72a2627c5f537e195781628922977b5ddb05619b..80eb24c68cb97d9d8652617537f46f0777d04c58 100644 (file)
@@ -3,11 +3,20 @@
 <form method="POST" action="/cgi-bin/env" enctype="multipart/form-data">
 <!--<form method="POST" action="/cgi-bin/env" enctype="application/x-www-form-urlencoded">-->
 <!--<form method="POST" action="/cgi-bin/env">-->
-       <input name="pippo" />
-       <input name="pluto" />
-       <input name="paperino" />
-       <input type="file" name="ilfile" />
-       <input name="topolino" />
+       pippo: <input name="pippo" /><br/>
+       pluto: <input name="pluto" /><br/>
+       paperino: <input name="paperino" /><br/>
+       file: <input type="file" name="ilfile" /><br/>
+       file: <input type="file" name="ilfile" /><br/>
+       file: <input type="file" name="ilfile" /><br/>
+       topolino: <input name="topolino" /><br/>
+       multiple: <select name="multiple" multiple>
+               <option value="1">One</option>
+               <option value="2">Two</option>
+               <option value="3">Three</option>
+               <option value="4">For</option>
+               <option value="5">Five</option>
+       </select><br/>
        <input type="submit" value="invia" />
 </form>
 </body>