forked from Mirrors/flatpak-builder
parent
9d0aa6f42e
commit
aa402ca259
|
@ -1073,137 +1073,6 @@ flatpak_allocate_tmpdir (int tmpdir_dfd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
GMainLoop *loop;
|
|
||||||
GError *error;
|
|
||||||
GOutputStream *out;
|
|
||||||
guint64 downloaded_bytes;
|
|
||||||
GString *content;
|
|
||||||
char buffer[16*1024];
|
|
||||||
FlatpakLoadUriProgress progress;
|
|
||||||
GCancellable *cancellable;
|
|
||||||
gpointer user_data;
|
|
||||||
guint64 last_progress_time;
|
|
||||||
char *etag;
|
|
||||||
} LoadUriData;
|
|
||||||
|
|
||||||
static void
|
|
||||||
stream_closed (GObject *source, GAsyncResult *res, gpointer user_data)
|
|
||||||
{
|
|
||||||
LoadUriData *data = user_data;
|
|
||||||
GInputStream *stream = G_INPUT_STREAM (source);
|
|
||||||
g_autoptr(GError) error = NULL;
|
|
||||||
|
|
||||||
if (!g_input_stream_close_finish (stream, res, &error))
|
|
||||||
g_warning ("Error closing http stream: %s", error->message);
|
|
||||||
|
|
||||||
g_main_loop_quit (data->loop);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
load_uri_read_cb (GObject *source, GAsyncResult *res, gpointer user_data)
|
|
||||||
{
|
|
||||||
LoadUriData *data = user_data;
|
|
||||||
GInputStream *stream = G_INPUT_STREAM (source);
|
|
||||||
gsize nread;
|
|
||||||
|
|
||||||
nread = g_input_stream_read_finish (stream, res, &data->error);
|
|
||||||
if (nread == -1 || nread == 0)
|
|
||||||
{
|
|
||||||
if (data->progress)
|
|
||||||
data->progress (data->downloaded_bytes, data->user_data);
|
|
||||||
g_input_stream_close_async (stream,
|
|
||||||
G_PRIORITY_DEFAULT, NULL,
|
|
||||||
stream_closed, data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data->out != NULL)
|
|
||||||
{
|
|
||||||
gsize n_written;
|
|
||||||
|
|
||||||
if (!g_output_stream_write_all (data->out, data->buffer, nread, &n_written,
|
|
||||||
NULL, &data->error))
|
|
||||||
{
|
|
||||||
data->downloaded_bytes += n_written;
|
|
||||||
g_input_stream_close_async (stream,
|
|
||||||
G_PRIORITY_DEFAULT, NULL,
|
|
||||||
stream_closed, data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
data->downloaded_bytes += n_written;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data->downloaded_bytes += nread;
|
|
||||||
g_string_append_len (data->content, data->buffer, nread);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (g_get_monotonic_time () - data->last_progress_time > 1 * G_USEC_PER_SEC)
|
|
||||||
{
|
|
||||||
if (data->progress)
|
|
||||||
data->progress (data->downloaded_bytes, data->user_data);
|
|
||||||
data->last_progress_time = g_get_monotonic_time ();
|
|
||||||
}
|
|
||||||
|
|
||||||
g_input_stream_read_async (stream, data->buffer, sizeof (data->buffer),
|
|
||||||
G_PRIORITY_DEFAULT, data->cancellable,
|
|
||||||
load_uri_read_cb, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
load_uri_callback (GObject *source_object,
|
|
||||||
GAsyncResult *res,
|
|
||||||
gpointer user_data)
|
|
||||||
{
|
|
||||||
SoupRequestHTTP *request = SOUP_REQUEST_HTTP(source_object);
|
|
||||||
g_autoptr(GInputStream) in = NULL;
|
|
||||||
LoadUriData *data = user_data;
|
|
||||||
|
|
||||||
in = soup_request_send_finish (SOUP_REQUEST (request), res, &data->error);
|
|
||||||
if (in == NULL)
|
|
||||||
{
|
|
||||||
g_main_loop_quit (data->loop);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_autoptr(SoupMessage) msg = soup_request_http_get_message ((SoupRequestHTTP*) request);
|
|
||||||
if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
|
|
||||||
{
|
|
||||||
int code;
|
|
||||||
GQuark domain = G_IO_ERROR;
|
|
||||||
|
|
||||||
switch (msg->status_code)
|
|
||||||
{
|
|
||||||
case 304:
|
|
||||||
//TODO domain = FLATPAK_OCI_ERROR;
|
|
||||||
//TODO code = FLATPAK_OCI_ERROR_NOT_CHANGED;
|
|
||||||
code = G_IO_ERROR_FAILED;
|
|
||||||
break;
|
|
||||||
case 404:
|
|
||||||
case 410:
|
|
||||||
code = G_IO_ERROR_NOT_FOUND;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
code = G_IO_ERROR_FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
data->error = g_error_new (domain, code,
|
|
||||||
"Server returned status %u: %s",
|
|
||||||
msg->status_code,
|
|
||||||
soup_status_get_phrase (msg->status_code));
|
|
||||||
g_main_loop_quit (data->loop);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
data->etag = g_strdup (soup_message_headers_get_one (msg->response_headers, "ETag"));
|
|
||||||
|
|
||||||
g_input_stream_read_async (in, data->buffer, sizeof (data->buffer),
|
|
||||||
G_PRIORITY_DEFAULT, data->cancellable,
|
|
||||||
load_uri_read_cb, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
SoupSession *
|
SoupSession *
|
||||||
flatpak_create_soup_session (const char *user_agent)
|
flatpak_create_soup_session (const char *user_agent)
|
||||||
{
|
{
|
||||||
|
@ -1250,54 +1119,6 @@ flatpak_create_curl_session (const char *user_agent)
|
||||||
return curl_session;
|
return curl_session;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
|
||||||
flatpak_download_http_uri (SoupSession *soup_session,
|
|
||||||
const char *uri,
|
|
||||||
GOutputStream *out,
|
|
||||||
FlatpakLoadUriProgress progress,
|
|
||||||
gpointer user_data,
|
|
||||||
GCancellable *cancellable,
|
|
||||||
GError **error)
|
|
||||||
{
|
|
||||||
g_autoptr(SoupRequestHTTP) request = NULL;
|
|
||||||
g_autoptr(GMainLoop) loop = NULL;
|
|
||||||
g_autoptr(GMainContext) context = NULL;
|
|
||||||
LoadUriData data = { NULL };
|
|
||||||
|
|
||||||
g_debug ("Loading %s using libsoup", uri);
|
|
||||||
|
|
||||||
context = g_main_context_ref_thread_default ();
|
|
||||||
|
|
||||||
loop = g_main_loop_new (context, TRUE);
|
|
||||||
data.loop = loop;
|
|
||||||
data.out = out;
|
|
||||||
data.progress = progress;
|
|
||||||
data.cancellable = cancellable;
|
|
||||||
data.user_data = user_data;
|
|
||||||
data.last_progress_time = g_get_monotonic_time ();
|
|
||||||
|
|
||||||
request = soup_session_request_http (soup_session, "GET",
|
|
||||||
uri, error);
|
|
||||||
if (request == NULL)
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
soup_request_send_async (SOUP_REQUEST(request),
|
|
||||||
cancellable,
|
|
||||||
load_uri_callback, &data);
|
|
||||||
|
|
||||||
g_main_loop_run (loop);
|
|
||||||
|
|
||||||
if (data.error)
|
|
||||||
{
|
|
||||||
g_propagate_error (error, data.error);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_debug ("Received %" G_GUINT64_FORMAT " bytes", data.downloaded_bytes);
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
FLATPAK_POLICY_NONE,
|
FLATPAK_POLICY_NONE,
|
||||||
FLATPAK_POLICY_SEE,
|
FLATPAK_POLICY_SEE,
|
||||||
|
|
|
@ -351,21 +351,6 @@ gboolean flatpak_allocate_tmpdir (int tmpdir_dfd,
|
||||||
|
|
||||||
SoupSession * flatpak_create_soup_session (const char *user_agent);
|
SoupSession * flatpak_create_soup_session (const char *user_agent);
|
||||||
CURL * flatpak_create_curl_session (const char *user_agent);
|
CURL * flatpak_create_curl_session (const char *user_agent);
|
||||||
GBytes * flatpak_load_http_uri (SoupSession *soup_session,
|
|
||||||
const char *uri,
|
|
||||||
const char *etag,
|
|
||||||
char **out_etag,
|
|
||||||
FlatpakLoadUriProgress progress,
|
|
||||||
gpointer user_data,
|
|
||||||
GCancellable *cancellable,
|
|
||||||
GError **error);
|
|
||||||
gboolean flatpak_download_http_uri (SoupSession *soup_session,
|
|
||||||
const char *uri,
|
|
||||||
GOutputStream *out,
|
|
||||||
FlatpakLoadUriProgress progress,
|
|
||||||
gpointer user_data,
|
|
||||||
GCancellable *cancellable,
|
|
||||||
GError **error);
|
|
||||||
|
|
||||||
typedef struct FlatpakContext FlatpakContext;
|
typedef struct FlatpakContext FlatpakContext;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue