]> Pileus Git - grits/blobdiff - src/data/gis-http.c
Fix some possible memory issues in GisHttp
[grits] / src / data / gis-http.c
index b5259e47108e15a92f0f95b44c24d4b19828a873..7f7d03be9393978f21aff9039749d745e718cff9 100644 (file)
@@ -79,6 +79,24 @@ struct _CacheInfo {
        GisChunkCallback callback;
        gpointer user_data;
 };
+struct _CacheInfoMain {
+       gchar *path;
+       GisChunkCallback callback;
+       gpointer user_data;
+       goffset cur, total;
+};
+
+/* call the user callback from the main thread,
+ * since it's usually UI updates */
+static gboolean _chunk_main_cb(gpointer _infomain)
+{
+       struct _CacheInfoMain *infomain = _infomain;
+       infomain->callback(infomain->path,
+                       infomain->cur, infomain->total,
+                       infomain->user_data);
+       g_free(infomain);
+       return FALSE;
+}
 
 /**
  * Append data to the file and call the users callback if they supplied one.
@@ -97,12 +115,17 @@ static void _chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
                g_error("GisHttp: _chunk_cb - Unable to write data");
 
        if (info->callback) {
-               goffset cur = ftell(info->fp);
-               goffset st=0, end=0, total=0;
+               struct _CacheInfoMain *infomain = g_new0(struct _CacheInfoMain, 1);
+               infomain->path      = info->path;
+               infomain->callback  = info->callback;
+               infomain->user_data = info->user_data;
+               infomain->cur       = ftell(info->fp);
+               goffset st=0, end=0;
                soup_message_headers_get_content_range(message->response_headers,
-                               &st, &end, &total);
-               info->callback(info->path, cur, total, info->user_data);
+                               &st, &end, &infomain->total);
+               g_idle_add(_chunk_main_cb, infomain);
        }
+
 }
 
 /**
@@ -123,8 +146,7 @@ static void _chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
 gchar *gis_http_fetch(GisHttp *http, const gchar *uri, const char *local,
                GisCacheType mode, GisChunkCallback callback, gpointer user_data)
 {
-       g_debug("GisHttp: fetch - %s... >> %s/%s  mode=%d",
-                       uri, http->prefix, local, mode);
+       g_debug("GisHttp: fetch - %s mode=%d", local, mode);
        gchar *path = _get_cache_path(http, local);
 
        /* Unlink the file if we're refreshing it */
@@ -218,16 +240,21 @@ GList *gis_http_available(GisHttp *http,
                        if (g_regex_match(filter_re, file, 0, NULL))
                                files = g_list_prepend(files, g_strdup(file));
                g_free(path);
+               g_dir_close(dir);
        }
 
        /* Add online files if online */
        if (index) {
-               gchar tmp[16];
+               gchar tmp[32];
                g_snprintf(tmp, sizeof(tmp), ".index.%x", g_random_int());
                gchar *path = gis_http_fetch(http, index, tmp,
                                GIS_REFRESH, NULL, NULL);
+               if (!path)
+                       return files;
                gchar *html;
                g_file_get_contents(path, &html, NULL, NULL);
+               if (!html)
+                       return files;
 
                /* Match hrefs by default, this regex is not very accurate */
                GRegex *extract_re = g_regex_new(
@@ -236,18 +263,23 @@ GList *gis_http_available(GisHttp *http,
                g_regex_match(extract_re, html, 0, &info);
                while (g_match_info_matches(info)) {
                        gchar *file = g_match_info_fetch(info, 1);
-                       if (g_regex_match(filter_re, file, 0, NULL))
-                               files = g_list_prepend(files, file);
-                       else
-                               g_free(file);
+                       if (file) {
+                               if (g_regex_match(filter_re, file, 0, NULL))
+                                       files = g_list_prepend(files, file);
+                               else
+                                       g_free(file);
+                       }
                        g_match_info_next(info, NULL);
                }
 
+               g_regex_unref(extract_re);
                g_match_info_free(info);
                g_unlink(path);
                g_free(path);
                g_free(html);
        }
 
+       g_regex_unref(filter_re);
+
        return files;
 }