diff --git a/src/httpd.c b/src/httpd.c index bc626797..f3dd4185 100644 --- a/src/httpd.c +++ b/src/httpd.c @@ -154,6 +154,8 @@ static int httpd_port; struct stream_ctx *g_st; #endif +static void +redirect_to_admin(struct evhttp_request *req); static void stream_end(struct stream_ctx *st, int failed) @@ -210,24 +212,16 @@ scrobble_cb(void *arg) static void oauth_interface(struct evhttp_request *req, const char *uri) { - struct evbuffer *evbuf; struct evkeyvalq query; const char *req_uri; const char *ptr; char __attribute__((unused)) redirect_uri[256]; + char *errmsg; int ret; +#ifdef HAVE_SPOTIFY_H req_uri = evhttp_request_get_uri(req); - evbuf = evbuffer_new(); - if (!evbuf) - { - DPRINTF(E_LOG, L_HTTPD, "Could not alloc evbuf for oauth\n"); - return; - } - - evbuffer_add_printf(evbuf, "

forked-daapd oauth

\n\n"); - memset(&query, 0, sizeof(struct evkeyvalq)); ptr = strchr(req_uri, '?'); @@ -236,32 +230,38 @@ oauth_interface(struct evhttp_request *req, const char *uri) ret = evhttp_parse_query_str(ptr + 1, &query); if (ret < 0) { - evbuffer_add_printf(evbuf, "OAuth error: Could not parse parameters in callback (%s)\n", req_uri); - - httpd_send_reply(req, HTTP_OK, "OK", evbuf, 0); - evbuffer_free(evbuf); + DPRINTF(E_LOG, L_HTTPD, "OAuth error: Could not parse parameters in callback (%s)\n", req_uri); + httpd_send_error(req, HTTP_BADREQUEST, "Could not parse parameters in callback"); return; } } -#ifdef HAVE_SPOTIFY_H - snprintf(redirect_uri, sizeof(redirect_uri), "http://forked-daapd.local:%d/oauth/spotify", httpd_port); if (strncmp(uri, "/oauth/spotify", strlen("/oauth/spotify")) == 0) - spotify_oauth_callback(evbuf, &query, redirect_uri); + { + snprintf(redirect_uri, sizeof(redirect_uri), "http://forked-daapd.local:%d/oauth/spotify", httpd_port); + ret = spotify_oauth_callback(&query, redirect_uri, &errmsg); + if (ret < 0) + { + DPRINTF(E_LOG, L_HTTPD, "OAuth error: Could not parse parameters in callback (%s)\n", req_uri); + httpd_send_error(req, HTTP_INTERNAL, errmsg); + } + else + { + redirect_to_admin(req); + } + evhttp_clear_headers(&query); + free(errmsg); + } else - spotify_oauth_interface(evbuf, redirect_uri); + { + httpd_send_error(req, HTTP_NOTFOUND, NULL); + } + #else - evbuffer_add_printf(evbuf, "

This version was built without modules requiring OAuth support

\n"); + DPRINTF(E_LOG, L_HTTPD, "This version was built without modules requiring OAuth support\n"); + httpd_send_error(req, HTTP_NOTFOUND, "No modules with OAuth support"); #endif - - evbuffer_add_printf(evbuf, "

(sorry about this ugly interface)

\n"); - - evhttp_clear_headers(&query); - - httpd_send_reply(req, HTTP_OK, "OK", evbuf, 0); - - evbuffer_free(evbuf); } static void @@ -909,6 +909,18 @@ path_is_legal(char *path) return strncmp(WEB_ROOT, path, strlen(WEB_ROOT)); } +/* Thread: httpd */ +static void +redirect_to_admin(struct evhttp_request *req) +{ + struct evkeyvalq *headers; + + headers = evhttp_request_get_output_headers(req); + evhttp_add_header(headers, "Location", "/admin.html"); + + httpd_send_reply(req, HTTP_MOVETEMP, "Moved", NULL, HTTPD_SEND_NO_GZIP); +} + /* Thread: httpd */ static void redirect_to_index(struct evhttp_request *req, char *uri) @@ -1189,9 +1201,9 @@ httpd_gen_cb(struct evhttp_request *req, void *arg) } req_uri = evhttp_request_get_uri(req); - if (!req_uri) + if (!req_uri || strcmp(req_uri, "/") == 0) { - redirect_to_index(req, "/"); + redirect_to_admin(req); return; } diff --git a/src/spotify.c b/src/spotify.c index af1199fc..dca0ec68 100644 --- a/src/spotify.c +++ b/src/spotify.c @@ -1824,30 +1824,30 @@ spotify_oauth_interface(struct evbuffer *evbuf, const char *redirect_uri) } /* Thread: httpd */ -void -spotify_oauth_callback(struct evbuffer *evbuf, struct evkeyvalq *param, const char *redirect_uri) +int +spotify_oauth_callback(struct evkeyvalq *param, const char *redirect_uri, char **errmsg) { const char *code; const char *err; char *user = NULL; int ret; + *errmsg = NULL; + code = evhttp_find_header(param, "code"); if (!code) { - evbuffer_add_printf(evbuf, "Error: Didn't receive a code from Spotify\n"); - return; + *errmsg = safe_asprintf("Error: Didn't receive a code from Spotify"); + return -1; } DPRINTF(E_DBG, L_SPOTIFY, "Received OAuth code: %s\n", code); - evbuffer_add_printf(evbuf, "

Requesting access token from Spotify...\n"); - ret = spotifywebapi_token_get(code, redirect_uri, &user, &err); if (ret < 0) { - evbuffer_add_printf(evbuf, "failed

\n

Error: %s

\n", err); - return; + *errmsg = safe_asprintf("Error: %s", err); + return -1; } // Received a valid access token @@ -1866,11 +1866,9 @@ spotify_oauth_callback(struct evbuffer *evbuf, struct evkeyvalq *param, const ch // Trigger scan after successful access to spotifywebapi library_exec_async(webapi_scan, NULL); - evbuffer_add_printf(evbuf, "ok, all done

\n"); - listener_notify(LISTENER_SPOTIFY); - return; + return 0; } static void diff --git a/src/spotify.h b/src/spotify.h index 6b4197a7..8bbbf06b 100644 --- a/src/spotify.h +++ b/src/spotify.h @@ -45,8 +45,8 @@ spotify_artwork_get(struct evbuffer *evbuf, char *path, int max_w, int max_h); void spotify_oauth_interface(struct evbuffer *evbuf, const char *redirect_uri); -void -spotify_oauth_callback(struct evbuffer *evbuf, struct evkeyvalq *param, const char *redirect_uri); +int +spotify_oauth_callback(struct evkeyvalq *param, const char *redirect_uri, char **errmsg); int spotify_login_user(const char *user, const char *password, char **errmsg);