Exemple #1
0
static void soup_download_deserialize (GDownloadable * download, GKeyFile * keys, const gchar * group)
{
	SoupDownload *soup_download = SOUP_DOWNLOAD (download);

	if (!g_downloadable_is_completed (download)) {

		soup_download->priv->message  = soup_message_new ("GET", download->priv->url);
		soup_message_connect_signals   (soup_download->priv->message, soup_download);
		soup_message_headers_set_range (soup_download->priv->message->request_headers,
										download->priv->downloaded_size, -1);
		soup_download->priv->prev_downloaded = download->priv->downloaded_size;
	}
}
Exemple #2
0
/* TODO: use .part extentions and continue even when using GRITS_ONCE */
gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local,
		GritsCacheType mode, GritsChunkCallback callback, gpointer user_data)
{
	g_debug("GritsHttp: fetch - %s mode=%d", local, mode);
	gchar *path = _get_cache_path(http, local);

	/* Unlink the file if we're refreshing it */
	if (mode == GRITS_REFRESH)
		g_remove(path);

	/* Do the cache if necessasairy */
	if (!(mode == GRITS_ONCE && g_file_test(path, G_FILE_TEST_EXISTS)) &&
			mode != GRITS_LOCAL) {
		g_debug("GritsHttp: fetch - Caching file %s", local);

		/* Open the file for writting */
		gchar *part = path;
		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 */
		struct _CacheInfo info = {
			.fp        = fp,
			.path      = path,
			.callback  = callback,
			.user_data = user_data,
		};

		/* Download the file */
		SoupMessage *message = soup_message_new("GET", uri);
		if (message == NULL)
			g_error("message is null, cannot parse uri");
		g_signal_connect(message, "got-chunk", G_CALLBACK(_chunk_cb), &info);
		//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");
		soup_session_send_message(http->soup, message);

		/* Close file */
		fclose(fp);
		if (path != part) {
			if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
				g_rename(part, path);
			g_free(part);
		}

		/* Finished */
		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(status)) {
			g_warning("GritsHttp: done_cb - error copying file, status=%d\n"
					"\tsrc=%s\n"
					"\tdst=%s",
					status, uri, path);
			return NULL;
		}
	}