summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAndres Gomez <agomez@igalia.com>2013-06-27 17:53:45 +0300
committerAndres Gomez <agomez@igalia.com>2013-07-03 10:53:29 +0300
commit09727545304e22b3c31df344ca58ef979e43f8e5 (patch)
treed8ec1436a3de9de25e889cb8e9e8011dfbcab6c5 /examples
parent037c1f92678735c24e337ad568463141710d5679 (diff)
downloadlibsoup-09727545304e22b3c31df344ca58ef979e43f8e5.tar.gz
[examples] "get" uses -o to save to a file
The "get" example is now able to handle a new "-o" parameter specifying the path of a file in which to write the received body of a "GET" operation. If the file exists, it will be overwritten. https://bugzilla.gnome.org/show_bug.cgi?id=703229
Diffstat (limited to 'examples')
-rw-r--r--examples/get.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/examples/get.c b/examples/get.c
index a2307bfd..ad1446e3 100644
--- a/examples/get.c
+++ b/examples/get.c
@@ -12,6 +12,7 @@
static SoupSession *session;
static GMainLoop *loop;
static gboolean debug, head, quiet;
+static const gchar *output_file_path = NULL;
static void
get_url (const char *url)
@@ -19,6 +20,7 @@ get_url (const char *url)
const char *name;
SoupMessage *msg;
const char *header;
+ FILE *output_file = NULL;
msg = soup_message_new (head ? "HEAD" : "GET", url);
soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
@@ -55,9 +57,23 @@ get_url (const char *url)
g_free (uri_string);
soup_uri_free (uri);
}
- } else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
- fwrite (msg->response_body->data, 1,
- msg->response_body->length, stdout);
+ } else if (!head && SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
+ if (output_file_path) {
+ output_file = fopen (output_file_path, "w");
+ if (!output_file)
+ g_printerr ("Error trying to create file %s.\n", output_file_path);
+ } else if (!quiet)
+ output_file = stdout;
+
+ if (output_file) {
+ fwrite (msg->response_body->data,
+ 1,
+ msg->response_body->length,
+ output_file);
+
+ if (output_file_path)
+ fclose (output_file);
+ }
}
}
@@ -77,6 +93,9 @@ static GOptionEntry entries[] = {
{ "ntlm", 'n', 0,
G_OPTION_ARG_NONE, &ntlm,
"Use NTLM authentication", NULL },
+ { "output", 'o', 0,
+ G_OPTION_ARG_STRING, &output_file_path,
+ "Write the received data to FILE instead of stdout", "FILE" },
{ "proxy", 'p', 0,
G_OPTION_ARG_STRING, &proxy,
"Use URL as an HTTP proxy", "URL" },
@@ -146,7 +165,7 @@ main (int argc, char **argv)
exit (1);
}
- g_object_set (G_OBJECT (session),
+ g_object_set (G_OBJECT (session),
SOUP_SESSION_PROXY_URI, proxy_uri,
NULL);
soup_uri_free (proxy_uri);