From: Andrea Zagli Date: Sun, 26 Jul 2015 06:21:50 +0000 (+0200) Subject: Added struct ZakCgiFile for file upload. X-Git-Tag: v0.0.1~18 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=49447886244d65c2ce12ef1d5dfaf2fbfd7b76d8;p=libzakcgi Added struct ZakCgiFile for file upload. --- diff --git a/src/main.c b/src/main.c index 894bf75..11a08f6 100644 --- a/src/main.c +++ b/src/main.c @@ -540,6 +540,30 @@ static gchar return line; } +ZakCgiFile +*zak_cgi_file_copy (ZakCgiFile *file) +{ + ZakCgiFile *b; + + b = g_slice_new (ZakCgiFile); + b->name = g_strdup (file->name); + b->content = g_strdup (file->content); + b->size = file->size; + + return b; +} + +void +zak_cgi_file_free (ZakCgiFile *file) +{ + g_free (file->name); + g_free (file->content); + g_slice_free (ZakCgiFile, file); +} + +G_DEFINE_BOXED_TYPE (ZakCgiFile, zak_cgi_file, zak_cgi_file_copy, zak_cgi_file_free) + + /** * zak_cgi_main_parse_stdin: * @@ -630,6 +654,8 @@ GHashTable GValue *gval; + ZakCgiFile *zgfile; + content_disposition = zak_cgi_main_read_line (buf, l, &i, TRUE, &bytesread); v_content = g_strsplit (content_disposition, ";", -1); @@ -704,15 +730,13 @@ GHashTable if (l_v_content == 3) { - GPtrArray *ar; - - ar = g_ptr_array_new (); - g_ptr_array_add (ar, g_strdup (param_name_file)); - g_ptr_array_add (ar, g_memdup (param_value, file_l - 2)); - g_ptr_array_add (ar, GSIZE_TO_POINTER (file_l - 2)); + zgfile = (ZakCgiFile *)g_new0 (ZakCgiFile, 1); + zgfile->name = g_strdup (param_name_file); + zgfile->content = g_memdup (param_value, file_l - 2); + zgfile->size = file_l - 2; - g_value_init (gval, G_TYPE_PTR_ARRAY); - g_value_take_boxed (gval, ar); + g_value_init (gval, ZAK_CGI_TYPE_FILE); + g_value_take_boxed (gval, zgfile); } else { diff --git a/src/main.h b/src/main.h index f776155..c163009 100644 --- a/src/main.h +++ b/src/main.h @@ -72,6 +72,21 @@ gchar *zak_cgi_main_set_cookie (const gchar *name, GHashTable *zak_cgi_main_get_parameters (ZakCgiMain *zakcgimain, const gchar *query_string); + +typedef struct _ZakCgiFile ZakCgiFile; + +struct _ZakCgiFile +{ + gchar *name; + gchar *content; + guint size; +}; + +#define ZAK_CGI_TYPE_FILE (zak_cgi_file_get_type ()) + +GType zak_cgi_file_get_type (); + + gchar *zak_cgi_main_get_stdin (ZakCgiMain *zakcgimain); GHashTable *zak_cgi_main_parse_stdin (const gchar *buf, const gchar *boundary); diff --git a/tests/env.c b/tests/env.c index 2e2629b..30b6dcd 100644 --- a/tests/env.c +++ b/tests/env.c @@ -62,15 +62,15 @@ 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, G_TYPE_BOXED)) + if (G_VALUE_HOLDS (value, ZAK_CGI_TYPE_FILE)) { - GPtrArray *ar = (GPtrArray *)g_value_get_boxed ((GValue *)value); + ZakCgiFile *zgfile = (ZakCgiFile *)g_value_get_boxed ((GValue *)value); g_string_append_printf (str, "%s%s\n", - (gchar *)key, (gchar *)g_ptr_array_index (ar, 0)); + (gchar *)key, zgfile->name); - if (g_strcmp0 ((gchar *)g_ptr_array_index (ar, 0), "") != 0) + if (g_strcmp0 (zgfile->name, "") != 0) { /* save the file to tmp */ GFile *gfile; @@ -78,14 +78,14 @@ main (int argc, char *argv[]) GOutputStream *ostream; iostream = NULL; - gfile = g_file_new_tmp (g_strdup_printf ("cgi-XXXXXX-%s", (gchar *)g_ptr_array_index (ar, 0)), + 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, - g_ptr_array_index (ar, 1), - GPOINTER_TO_SIZE (g_ptr_array_index (ar, 2)), + zgfile->content, + zgfile->size, NULL, NULL); g_output_stream_close (ostream, NULL, NULL); @@ -119,4 +119,3 @@ main (int argc, char *argv[]) return 0; } - diff --git a/tests/provapostcgi.html b/tests/provapostcgi.html index 7eb4d39..72a2627 100644 --- a/tests/provapostcgi.html +++ b/tests/provapostcgi.html @@ -1,8 +1,8 @@ - +
- +