]> Pileus Git - grits/blob - src/data/gis-http.c
Convert self to real names
[grits] / src / data / gis-http.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <glib.h>
20 #include <glib/gstdio.h>
21 #include <libsoup/soup.h>
22
23 #include "gis-http.h"
24
25 GisHttp *gis_http_new(const gchar *prefix)
26 {
27         g_debug("GisHttp: new - %s", prefix);
28         GisHttp *http = g_new0(GisHttp, 1);
29         http->soup = soup_session_sync_new();
30         http->prefix = g_strdup(prefix);
31         g_object_set(http->soup, "user-agent", PACKAGE_STRING, NULL);
32         return http;
33 }
34
35 void gis_http_free(GisHttp *http)
36 {
37         g_debug("GisHttp: free - %s", http->prefix);
38         soup_session_abort(http->soup);
39         g_object_unref(http->soup);
40         g_free(http->prefix);
41         g_free(http);
42 }
43
44 /* For passing data to the chunck callback */
45 struct _cache_info {
46         FILE  *fp;
47         gchar *path;
48         GisChunkCallback callback;
49         gpointer user_data;
50 };
51
52 /**
53  * Append data to the file and call the users callback if they supplied one.
54  */
55 static void _chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
56 {
57         struct _cache_info *info = _info;
58
59         if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
60                 g_warning("GisHttp: _chunk_cb - soup failed with %d",
61                                 message->status_code);
62                 return;
63         }
64
65         if (!fwrite(chunk->data, chunk->length, 1, info->fp))
66                 g_error("GisHttp: _chunk_cb - Unable to write data");
67
68         if (info->callback) {
69                 goffset cur = ftell(info->fp);
70                 goffset st=0, end=0, total=0;
71                 soup_message_headers_get_content_range(message->response_headers,
72                                 &st, &end, &total);
73                 info->callback(info->path, cur, total, info->user_data);
74         }
75 }
76
77 /* TODO: use .part extentions and continue even when using GIS_ONCE */
78 gchar *gis_http_fetch(GisHttp *http, const gchar *uri, const char *local,
79                 GisCacheType mode, GisChunkCallback callback, gpointer user_data)
80 {
81         g_debug("GisHttp: fetch - %.20s... >> %s/%s  mode=%d",
82                         uri, http->prefix, local, mode);
83
84         gchar *path = g_build_filename(g_get_user_cache_dir(), PACKAGE,
85                         http->prefix, local, NULL);
86
87         /* Unlink the file if we're refreshing it */
88         if (mode == GIS_REFRESH)
89                 g_remove(path);
90
91         /* Do the cache if necessasairy */
92         if (!(mode == GIS_ONCE && g_file_test(path, G_FILE_TEST_EXISTS)) &&
93                         mode != GIS_LOCAL) {
94                 g_debug("GisHttp: do_cache - Caching file %s", local);
95
96                 /* Open the file for writting */
97                 FILE *fp = fopen_p(path, "a");
98
99                 /* Make temp data */
100                 struct _cache_info info = {
101                         .fp        = fp,
102                         .path      = path,
103                         .callback  = callback,
104                         .user_data = user_data,
105                 };
106
107                 /* Download the file */
108                 SoupMessage *message = soup_message_new("GET", uri);
109                 if (message == NULL)
110                         g_error("message is null, cannot parse uri");
111                 g_signal_connect(message, "got-chunk", G_CALLBACK(_chunk_cb), &info);
112                 soup_message_headers_set_range(message->request_headers, ftell(fp), -1);
113                 soup_session_send_message(http->soup, message);
114
115                 /* Finished */
116                 if (message->status_code == 416) {
117                         /* Range unsatisfiable, file already complete */
118                 } else if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
119                         g_warning("GisHttp: done_cb - error copying file, status=%d\n"
120                                         "\tsrc=%s\n"
121                                         "\tdst=%s",
122                                         message->status_code, uri, path);
123         }
124
125         /* TODO: free everything.. */
126         return path;
127 }