X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fdata%2Fgrits-http.c;h=76e68a4aa2935dd79b039ffe67f027cf0b7b9756;hb=782f6b5a0985384b68f2a4f233eef0541efd4c94;hp=943441dfa55a892521c05c873ceb19c9dc137b6d;hpb=75df11f7088dac5301deec551d7458d9572f1848;p=grits diff --git a/src/data/grits-http.c b/src/data/grits-http.c index 943441d..76e68a4 100644 --- a/src/data/grits-http.c +++ b/src/data/grits-http.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2010 Andy Spencer + * Copyright (C) 2009-2011 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -54,6 +54,7 @@ GritsHttp *grits_http_new(const gchar *prefix) http->soup = soup_session_sync_new(); http->prefix = g_strdup(prefix); g_object_set(http->soup, "user-agent", PACKAGE_STRING, NULL); + g_object_set(http->soup, "timeout", 10, NULL); return http; } @@ -163,6 +164,10 @@ gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local, if (!g_file_test(path, G_FILE_TEST_EXISTS)) part = g_strdup_printf("%s.part", path); FILE *fp = fopen_p(part, "ab"); + if (!fp) { + g_warning("GritsHttp: fetch - error opening %s", path); + return NULL; + } fseek(fp, 0, SEEK_END); // "a" is broken on Windows, twice /* Make temp data */ @@ -178,7 +183,8 @@ gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local, if (message == NULL) g_error("message is null, cannot parse uri"); g_signal_connect(message, "got-chunk", G_CALLBACK(_chunk_cb), &info); - soup_message_headers_set_range(message->request_headers, ftell(fp), -1); + //if (ftell(fp) > 0) + soup_message_headers_set_range(message->request_headers, ftell(fp), -1); if (mode == GRITS_REFRESH) soup_message_headers_replace(message->request_headers, "Cache-Control", "max-age=0"); @@ -186,24 +192,28 @@ gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local, /* Close file */ fclose(fp); - if (path != part && SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) { - g_rename(part, path); + if (path != part) { + if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) + g_rename(part, path); g_free(part); } /* Finished */ - if (message->status_code == 416) { + guint status = message->status_code; + g_object_unref(message); + if (status == SOUP_STATUS_CANCELLED) { + return NULL; + } else if (status == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE) { /* Range unsatisfiable, file already complete */ - } else if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) { + } else if (!SOUP_STATUS_IS_SUCCESSFUL(status)) { g_warning("GritsHttp: done_cb - error copying file, status=%d\n" "\tsrc=%s\n" "\tdst=%s", - message->status_code, uri, path); + status, uri, path); return NULL; } } - /* TODO: free everything.. */ return path; } @@ -239,7 +249,7 @@ GList *grits_http_available(GritsHttp *http, const gchar *file; gchar *path = _get_cache_path(http, cache); GDir *dir = g_dir_open(path, 0, NULL); - while ((file = g_dir_read_name(dir))) + while (dir && (file = g_dir_read_name(dir))) if (g_regex_match(filter_re, file, 0, NULL)) files = g_list_prepend(files, g_strdup(file)); g_free(path);