mirror of
https://github.com/owntone/owntone-server.git
synced 2025-02-10 05:08:11 -05:00
[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)
This commit is contained in:
parent
733a521df8
commit
9b8bff45ab
@ -206,7 +206,7 @@ static struct artwork_source artwork_item_source[] =
|
|||||||
.name = "Spotify web api",
|
.name = "Spotify web api",
|
||||||
.handler = source_item_spotifywebapi_get,
|
.handler = source_item_spotifywebapi_get,
|
||||||
.data_kinds = (1 << DATA_KIND_SPOTIFY),
|
.data_kinds = (1 << DATA_KIND_SPOTIFY),
|
||||||
.cache = ON_SUCCESS,
|
.cache = ON_SUCCESS | ON_FAILURE,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "playlist own",
|
.name = "playlist own",
|
||||||
@ -261,13 +261,14 @@ artwork_url_read(struct evbuffer *evbuf, const char *url)
|
|||||||
if (http_client_request(&client) < 0)
|
if (http_client_request(&client) < 0)
|
||||||
goto out_kv;
|
goto out_kv;
|
||||||
|
|
||||||
|
if (client.response_code != HTTP_OK)
|
||||||
|
goto out_kv;
|
||||||
|
|
||||||
content_type = keyval_get(kv, "Content-Type");
|
content_type = keyval_get(kv, "Content-Type");
|
||||||
if (content_type && (strcmp(content_type, "image/jpeg") == 0))
|
if (content_type && (strcmp(content_type, "image/jpeg") == 0))
|
||||||
ret = ART_FMT_JPEG;
|
ret = ART_FMT_JPEG;
|
||||||
else if (content_type && (strcmp(content_type, "image/png") == 0))
|
else if (content_type && (strcmp(content_type, "image/png") == 0))
|
||||||
ret = ART_FMT_PNG;
|
ret = ART_FMT_PNG;
|
||||||
else
|
|
||||||
ret = ART_FMT_JPEG;
|
|
||||||
|
|
||||||
out_kv:
|
out_kv:
|
||||||
keyval_clear(kv);
|
keyval_clear(kv);
|
||||||
|
31
src/http.c
31
src/http.c
@ -100,7 +100,6 @@ request_cb(struct evhttp_request *req, void *arg)
|
|||||||
{
|
{
|
||||||
struct http_client_ctx *ctx;
|
struct http_client_ctx *ctx;
|
||||||
const char *response_code_line;
|
const char *response_code_line;
|
||||||
int response_code;
|
|
||||||
|
|
||||||
ctx = (struct http_client_ctx *)arg;
|
ctx = (struct http_client_ctx *)arg;
|
||||||
|
|
||||||
@ -119,21 +118,21 @@ request_cb(struct evhttp_request *req, void *arg)
|
|||||||
goto connection_error;
|
goto connection_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
response_code = evhttp_request_get_response_code(req);
|
ctx->response_code = evhttp_request_get_response_code(req);
|
||||||
#ifndef HAVE_LIBEVENT2_OLD
|
#ifndef HAVE_LIBEVENT2_OLD
|
||||||
response_code_line = evhttp_request_get_response_code_line(req);
|
response_code_line = evhttp_request_get_response_code_line(req);
|
||||||
#else
|
#else
|
||||||
response_code_line = "no error text";
|
response_code_line = "no error text";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (response_code == 0)
|
if (ctx->response_code == 0)
|
||||||
{
|
{
|
||||||
DPRINTF(E_WARN, L_HTTP, "Connection to %s failed: Connection refused\n", ctx->url);
|
DPRINTF(E_WARN, L_HTTP, "Connection to %s failed: Connection refused\n", ctx->url);
|
||||||
goto connection_error;
|
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;
|
goto connection_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,6 +308,23 @@ http_client_request_impl(struct http_client_ctx *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_LIBCURL
|
#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
|
static size_t
|
||||||
curl_request_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
|
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;
|
struct onekeyval *okv;
|
||||||
const char *user_agent;
|
const char *user_agent;
|
||||||
char header[1024];
|
char header[1024];
|
||||||
|
long response_code;
|
||||||
|
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
if (!curl)
|
if (!curl)
|
||||||
@ -384,6 +401,10 @@ https_client_request_impl(struct http_client_ctx *ctx)
|
|||||||
return -1;
|
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);
|
curl_easy_cleanup(curl);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -29,6 +29,9 @@ struct http_client_ctx
|
|||||||
*/
|
*/
|
||||||
int headers_only;
|
int headers_only;
|
||||||
|
|
||||||
|
/* HTTP Response code */
|
||||||
|
int response_code;
|
||||||
|
|
||||||
/* Private */
|
/* Private */
|
||||||
int ret;
|
int ret;
|
||||||
void *evbase;
|
void *evbase;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user