From 9b8bff45ab31fd08332a73e8a9a7765cbcd38a8b Mon Sep 17 00:00:00 2001 From: chme Date: Sat, 9 Feb 2019 08:35:20 +0100 Subject: [PATCH] [artwork/http] Check response code and content type Extends the http_client_ctx to hold the response code for a request. Also adds the content type header, if it was a https request (using libcurl instead of libevent) --- src/artwork.c | 7 ++++--- src/http.c | 31 ++++++++++++++++++++++++++----- src/http.h | 3 +++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/artwork.c b/src/artwork.c index 6735d140..e0858d30 100644 --- a/src/artwork.c +++ b/src/artwork.c @@ -206,7 +206,7 @@ static struct artwork_source artwork_item_source[] = .name = "Spotify web api", .handler = source_item_spotifywebapi_get, .data_kinds = (1 << DATA_KIND_SPOTIFY), - .cache = ON_SUCCESS, + .cache = ON_SUCCESS | ON_FAILURE, }, { .name = "playlist own", @@ -261,13 +261,14 @@ artwork_url_read(struct evbuffer *evbuf, const char *url) if (http_client_request(&client) < 0) goto out_kv; + if (client.response_code != HTTP_OK) + goto out_kv; + content_type = keyval_get(kv, "Content-Type"); if (content_type && (strcmp(content_type, "image/jpeg") == 0)) ret = ART_FMT_JPEG; else if (content_type && (strcmp(content_type, "image/png") == 0)) ret = ART_FMT_PNG; - else - ret = ART_FMT_JPEG; out_kv: keyval_clear(kv); diff --git a/src/http.c b/src/http.c index 0cc85a23..1d48c6f3 100644 --- a/src/http.c +++ b/src/http.c @@ -100,7 +100,6 @@ request_cb(struct evhttp_request *req, void *arg) { struct http_client_ctx *ctx; const char *response_code_line; - int response_code; ctx = (struct http_client_ctx *)arg; @@ -119,21 +118,21 @@ request_cb(struct evhttp_request *req, void *arg) goto connection_error; } - response_code = evhttp_request_get_response_code(req); + ctx->response_code = evhttp_request_get_response_code(req); #ifndef HAVE_LIBEVENT2_OLD response_code_line = evhttp_request_get_response_code_line(req); #else response_code_line = "no error text"; #endif - if (response_code == 0) + if (ctx->response_code == 0) { DPRINTF(E_WARN, L_HTTP, "Connection to %s failed: Connection refused\n", ctx->url); goto connection_error; } - else if (response_code != 200) + else if (ctx->response_code != 200) { - DPRINTF(E_WARN, L_HTTP, "Connection to %s failed: %s (error %d)\n", ctx->url, response_code_line, response_code); + DPRINTF(E_WARN, L_HTTP, "Connection to %s failed: %s (error %d)\n", ctx->url, response_code_line, ctx->response_code); goto connection_error; } @@ -309,6 +308,23 @@ http_client_request_impl(struct http_client_ctx *ctx) } #ifdef HAVE_LIBCURL + +static void +curl_headers_save(struct keyval *kv, CURL *curl) +{ + char *content_type; + int ret; + + if (!kv || !curl) + return; + + ret = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &content_type); + if (ret == CURLE_OK && content_type) + { + keyval_add(kv, "Content-Type", content_type); + } +} + static size_t curl_request_cb(char *ptr, size_t size, size_t nmemb, void *userdata) { @@ -341,6 +357,7 @@ https_client_request_impl(struct http_client_ctx *ctx) struct onekeyval *okv; const char *user_agent; char header[1024]; + long response_code; curl = curl_easy_init(); if (!curl) @@ -384,6 +401,10 @@ https_client_request_impl(struct http_client_ctx *ctx) return -1; } + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); + ctx->response_code = (int) response_code; + curl_headers_save(ctx->input_headers, curl); + curl_easy_cleanup(curl); return 0; diff --git a/src/http.h b/src/http.h index 6ab2485b..faa7ed9a 100644 --- a/src/http.h +++ b/src/http.h @@ -29,6 +29,9 @@ struct http_client_ctx */ int headers_only; + /* HTTP Response code */ + int response_code; + /* Private */ int ret; void *evbase;