From 96c11453268aae65cb21cb8c566dfc3628e381f0 Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Mon, 27 Feb 2017 20:51:59 +0100 Subject: [PATCH] [feat] add get.c file with not working async http downloader(get) --- src/get.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/get.c diff --git a/src/get.c b/src/get.c new file mode 100644 index 0000000..d64bc66 --- /dev/null +++ b/src/get.c @@ -0,0 +1,109 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2001-2003, Ximian, Inc. + * Copyright (C) 2013 Igalia, S.L. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include + +static SoupSession *session; +static GMainLoop *loop; +static gboolean debug; + + static void +//finished (SoupSession *session, SoupMessage *msg, gpointer loop) +finished (GObject *session, GAsyncResult *res, gpointer loop) +{ + SoupMessage *msg = g_async_result_get_user_data(res); + g_print("Output %s", soup_message_get_uri (msg)->path); + //g_main_loop_quit (loop); +} + + char* +concat(const char *s1, const char *s2) +{ + char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator + //in real code you would check for errors in malloc here + strcpy(result, s1); + strcat(result, s2); + return result; +} + + int +get (char *url, const gchar *output_file_path) +{ + GError *error = NULL; + SoupLogger *logger = NULL; + + if (!soup_uri_new (url)) { + g_printerr ("Could not parse '%s' as a URL\n", url); + return (1); + } + + session = g_object_new (SOUP_TYPE_SESSION, + SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER, + SOUP_SESSION_USER_AGENT, "teleport ", + SOUP_SESSION_ACCEPT_LANGUAGE_AUTO, TRUE, + NULL); + + if (debug) { + logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1); + soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger)); + g_object_unref (logger); + } + + loop = g_main_loop_new (NULL, TRUE); + + const char *name; + SoupMessage *msg; + const char *header; + FILE *output_file = NULL; + + msg = soup_message_new ("GET", url); + soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT); + + g_object_ref (msg); + //instate of NULL it should have a gcancellable object + soup_session_send_async (session, msg, NULL, finished, loop); + g_main_loop_run (loop); + + name = soup_message_get_uri (msg)->path; + + if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) + g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase); + + if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) { + g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase); + } else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) { + output_file = fopen (concat(output_file_path, name), "w"); + if (!output_file) + g_printerr ("Error trying to create file %s.\n", output_file_path); + + if (output_file) { + fwrite (msg->response_body->data, + 1, + msg->response_body->length, + output_file); + + fclose (output_file); + } + } + + g_main_loop_unref (loop); + + return 0; +} + +int main () +{ + get ("http://juliansparber.com/index.html", "./test_download"); + return 0; +}