lib/fetcher: Factor out HTTP status code handling from soup and curl

Use the same G_IO_ERROR_* values for HTTP status codes in both fetchers.
The libsoup fetcher still handles a few more internal error codes than
the libcurl one; this could be built on in future.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Closes: #1594
Approved by: jlebon
This commit is contained in:
Philip Withnall 2018-05-30 12:20:49 +01:00 committed by Atomic Bot
parent 78f40136db
commit 197644c406
4 changed files with 26 additions and 22 deletions

View File

@ -337,19 +337,7 @@ check_multi_info (OstreeFetcher *fetcher)
curl_easy_getinfo (easy, CURLINFO_RESPONSE_CODE, &response);
if (!is_file && !(response >= 200 && response < 300))
{
GIOErrorEnum giocode;
/* TODO - share with soup */
switch (response)
{
case 404:
case 403:
case 410:
giocode = G_IO_ERROR_NOT_FOUND;
break;
default:
giocode = G_IO_ERROR_FAILED;
}
GIOErrorEnum giocode = _ostree_fetcher_http_status_code_to_io_error (response);
if (req->idx + 1 == req->mirrorlist->len)
{

View File

@ -1070,19 +1070,13 @@ on_request_sent (GObject *object,
soup_uri_to_string (soup_request_get_uri (pending->request), FALSE);
GIOErrorEnum code;
switch (msg->status_code)
{
case SOUP_STATUS_NOT_FOUND:
case SOUP_STATUS_FORBIDDEN:
case SOUP_STATUS_GONE:
code = G_IO_ERROR_NOT_FOUND;
break;
/* These statuses are internal to libsoup, and not standard HTTP ones: */
case SOUP_STATUS_CANCELLED:
code = G_IO_ERROR_CANCELLED;
break;
case SOUP_STATUS_REQUEST_TIMEOUT:
code = G_IO_ERROR_TIMED_OUT;
break;
case SOUP_STATUS_CANT_RESOLVE:
case SOUP_STATUS_CANT_CONNECT:
code = G_IO_ERROR_HOST_NOT_FOUND;
@ -1095,7 +1089,8 @@ on_request_sent (GObject *object,
#endif
break;
default:
code = G_IO_ERROR_FAILED;
code = _ostree_fetcher_http_status_code_to_io_error (msg->status_code);
break;
}
{

View File

@ -218,3 +218,22 @@ _ostree_fetcher_should_retry_request (const GError *error,
return FALSE;
}
/* Convert a HTTP status code representing an error from libsoup or libcurl to
* a #GIOError. This will return %G_IO_ERROR_FAILED if the status code is
* unknown or otherwise unhandled. */
GIOError
_ostree_fetcher_http_status_code_to_io_error (guint status_code)
{
switch (status_code)
{
case 403: /* SOUP_STATUS_FORBIDDEN */
case 404: /* SOUP_STATUS_NOT_FOUND */
case 410: /* SOUP_STATUS_GONE */
return G_IO_ERROR_NOT_FOUND;
case 408: /* SOUP_STATUS_REQUEST_TIMEOUT */
return G_IO_ERROR_TIMED_OUT;
default:
return G_IO_ERROR_FAILED;
}
}

View File

@ -78,6 +78,8 @@ void _ostree_fetcher_journal_failure (const char *remote_name,
gboolean _ostree_fetcher_should_retry_request (const GError *error,
guint n_retries_remaining);
GIOError _ostree_fetcher_http_status_code_to_io_error (guint status_code);
G_END_DECLS
#endif