]> Pileus Git - grits/blob - src/data/grits-http.c
01bc7053b61504f3c6b8981604ec444263b23871
[grits] / src / data / grits-http.c
1 /*
2  * Copyright (C) 2009-2011 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 /**
19  * SECTION:grits-http
20  * @short_description: Hyper Text Transfer Protocol
21  *
22  * #GritsHttp is a small wrapper around libsoup to provide data access using
23  * the Hyper Text Transfer Protocol. Each #GritsHttp should be associated with
24  * a particular server or dataset, all the files downloaded for this dataset
25  * will be cached together in $HOME/.cache/grits/
26  */
27
28 #include <config.h>
29 #include <glib.h>
30 #include <glib/gstdio.h>
31 #include <libsoup/soup.h>
32
33 #include "grits-http.h"
34
35 gchar *_get_cache_path(GritsHttp *http, const gchar *local)
36 {
37         return g_build_filename(g_get_user_cache_dir(), PACKAGE,
38                         http->prefix, local, NULL);
39 }
40
41 /**
42  * grits_http_new:
43  * @prefix: The prefix in the cache to store the downloaded files.
44  *          For example: * "/nexrad/level2/".
45  *
46  * Create a new #GritsHttp for the given prefix
47  *
48  * Returns: the new #GritsHttp
49  */
50 GritsHttp *grits_http_new(const gchar *prefix)
51 {
52         g_debug("GritsHttp: new - %s", prefix);
53         GritsHttp *http = g_new0(GritsHttp, 1);
54         http->soup = soup_session_sync_new();
55         http->prefix = g_strdup(prefix);
56         g_object_set(http->soup, "user-agent", PACKAGE_STRING, NULL);
57         g_object_set(http->soup, "timeout",    10,             NULL);
58         return http;
59 }
60
61 /**
62  * grits_http_free:
63  * @http: the #GritsHttp to free
64  *
65  * Frees resources used by @http and cancels any pending requests.
66  */
67 void grits_http_free(GritsHttp *http)
68 {
69         g_debug("GritsHttp: free - %s", http->prefix);
70         soup_session_abort(http->soup);
71         g_object_unref(http->soup);
72         g_free(http->prefix);
73         g_free(http);
74 }
75
76 /* For passing data to the chunck callback */
77 struct _CacheInfo {
78         FILE  *fp;
79         gchar *path;
80         GritsChunkCallback callback;
81         gpointer user_data;
82 };
83 struct _CacheInfoMain {
84         gchar *path;
85         GritsChunkCallback callback;
86         gpointer user_data;
87         goffset cur, total;
88 };
89
90 /* call the user callback from the main thread,
91  * since it's usually UI updates */
92 static gboolean _chunk_main_cb(gpointer _infomain)
93 {
94         struct _CacheInfoMain *infomain = _infomain;
95         infomain->callback(infomain->path,
96                         infomain->cur, infomain->total,
97                         infomain->user_data);
98         g_free(infomain);
99         return FALSE;
100 }
101
102 /**
103  * Append data to the file and call the users callback if they supplied one.
104  */
105 static void _chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
106 {
107         struct _CacheInfo *info = _info;
108
109         if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
110                 g_warning("GritsHttp: _chunk_cb - soup failed with %d",
111                                 message->status_code);
112                 return;
113         }
114
115         if (!fwrite(chunk->data, chunk->length, 1, info->fp))
116                 g_error("GritsHttp: _chunk_cb - Unable to write data");
117
118         if (info->callback) {
119                 struct _CacheInfoMain *infomain = g_new0(struct _CacheInfoMain, 1);
120                 infomain->path      = info->path;
121                 infomain->callback  = info->callback;
122                 infomain->user_data = info->user_data;
123                 infomain->cur       = ftell(info->fp);
124                 goffset st=0, end=0;
125                 soup_message_headers_get_content_range(message->response_headers,
126                                 &st, &end, &infomain->total);
127                 g_idle_add(_chunk_main_cb, infomain);
128         }
129
130 }
131
132 /**
133  * grits_http_fetch:
134  * @http:      the #GritsHttp connection to use
135  * @uri:       the URI to fetch
136  * @local:     the local name to give to the file
137  * @mode:      the update type to use when fetching data
138  * @callback:  callback to call when a chunk of data is received
139  * @user_data: user data to pass to the callback
140  *
141  * Fetch a file from the cache. Whether the file is actually loaded from the
142  * remote server depends on the value of @mode.
143  *
144  * Returns: The local path to the complete file
145  */
146 /* TODO: use .part extentions and continue even when using GRITS_ONCE */
147 gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local,
148                 GritsCacheType mode, GritsChunkCallback callback, gpointer user_data)
149 {
150         g_debug("GritsHttp: fetch - %s mode=%d", local, mode);
151         gchar *path = _get_cache_path(http, local);
152
153         /* Unlink the file if we're refreshing it */
154         if (mode == GRITS_REFRESH)
155                 g_remove(path);
156
157         /* Do the cache if necessasairy */
158         if (!(mode == GRITS_ONCE && g_file_test(path, G_FILE_TEST_EXISTS)) &&
159                         mode != GRITS_LOCAL) {
160                 g_debug("GritsHttp: fetch - Caching file %s", local);
161
162                 /* Open the file for writting */
163                 gchar *part = path;
164                 if (!g_file_test(path, G_FILE_TEST_EXISTS))
165                         part = g_strdup_printf("%s.part", path);
166                 FILE *fp = fopen_p(part, "ab");
167                 if (!fp) {
168                         g_warning("GritsHttp: fetch - error opening %s", path);
169                         return NULL;
170                 }
171                 fseek(fp, 0, SEEK_END); // "a" is broken on Windows, twice
172
173                 /* Make temp data */
174                 struct _CacheInfo info = {
175                         .fp        = fp,
176                         .path      = path,
177                         .callback  = callback,
178                         .user_data = user_data,
179                 };
180
181                 /* Download the file */
182                 SoupMessage *message = soup_message_new("GET", uri);
183                 if (message == NULL)
184                         g_error("message is null, cannot parse uri");
185                 g_signal_connect(message, "got-chunk", G_CALLBACK(_chunk_cb), &info);
186                 //if (ftell(fp) > 0)
187                         soup_message_headers_set_range(message->request_headers, ftell(fp), -1);
188                 if (mode == GRITS_REFRESH)
189                         soup_message_headers_replace(message->request_headers,
190                                         "Cache-Control", "max-age=0");
191                 soup_session_send_message(http->soup, message);
192
193                 /* Close file */
194                 fclose(fp);
195                 if (path != part) {
196                         if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
197                                 g_rename(part, path);
198                         g_free(part);
199                 }
200
201                 /* Finished */
202                 guint status = message->status_code;
203                 g_object_unref(message);
204                 if (status == SOUP_STATUS_CANCELLED) {
205                         return NULL;
206                 } else if (status == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE) {
207                         /* Range unsatisfiable, file already complete */
208                 } else if (!SOUP_STATUS_IS_SUCCESSFUL(status)) {
209                         g_warning("GritsHttp: done_cb - error copying file, status=%d\n"
210                                         "\tsrc=%s\n"
211                                         "\tdst=%s",
212                                         status, uri, path);
213                         return NULL;
214                 }
215         }
216
217         /* TODO: free everything.. */
218         return path;
219 }
220
221 /**
222  * grits_http_available:
223  * @http:    the #GritsHttp connection to use
224  * @filter:  filter used to extract files from the index, or NULL
225  *           For example: "href=\"([^"]*)\""
226  * @cache:   path to the local cache, or NULL to not search the cache
227  * @extract: regex used to extract filenames from the page, should match the
228  *           filename as $1, or NULL to use /http="([^"])"/
229  * @index:   path to the index page, or NULL to not search online
230  *
231  * Look through the cache and an HTTP index page for a list of available files.
232  * The name of each file that matches the filter is added to the returned list.
233  *
234  * The list as well as the strings contained in it should be freed afterwards.
235  *
236  * Returns the list of matching filenames
237  */
238 GList *grits_http_available(GritsHttp *http,
239                 gchar *filter, gchar *cache,
240                 gchar *extract, gchar *index)
241 {
242         g_debug("GritsHttp: available - %s~=%s %s~=%s",
243                         filter, cache, extract, index);
244         GRegex *filter_re = g_regex_new(filter, 0, 0, NULL);
245         GList  *files = NULL;
246
247         /* Add cached files */
248         if (cache) {
249                 const gchar *file;
250                 gchar *path = _get_cache_path(http, cache);
251                 GDir  *dir  = g_dir_open(path, 0, NULL);
252                 while (dir && (file = g_dir_read_name(dir)))
253                         if (g_regex_match(filter_re, file, 0, NULL))
254                                 files = g_list_prepend(files, g_strdup(file));
255                 g_free(path);
256                 if (dir)
257                         g_dir_close(dir);
258         }
259
260         /* Add online files if online */
261         if (index) {
262                 gchar tmp[32];
263                 g_snprintf(tmp, sizeof(tmp), ".index.%x", g_random_int());
264                 gchar *path = grits_http_fetch(http, index, tmp,
265                                 GRITS_REFRESH, NULL, NULL);
266                 if (!path)
267                         return files;
268                 gchar *html;
269                 g_file_get_contents(path, &html, NULL, NULL);
270                 if (!html)
271                         return files;
272
273                 /* Match hrefs by default, this regex is not very accurate */
274                 GRegex *extract_re = g_regex_new(
275                                 extract ?: "href=\"([^\"]*)\"", 0, 0, NULL);
276                 GMatchInfo *info;
277                 g_regex_match(extract_re, html, 0, &info);
278                 while (g_match_info_matches(info)) {
279                         gchar *file = g_match_info_fetch(info, 1);
280                         if (file) {
281                                 if (g_regex_match(filter_re, file, 0, NULL))
282                                         files = g_list_prepend(files, file);
283                                 else
284                                         g_free(file);
285                         }
286                         g_match_info_next(info, NULL);
287                 }
288
289                 g_regex_unref(extract_re);
290                 g_match_info_free(info);
291                 g_unlink(path);
292                 g_free(path);
293                 g_free(html);
294         }
295
296         g_regex_unref(filter_re);
297
298         return files;
299 }