From ec8ce91baab40b74c4512e08514ba11e3fc18778 Mon Sep 17 00:00:00 2001 From: chme Date: Mon, 16 Mar 2020 21:03:17 +0100 Subject: [PATCH] [jsonapi] Use synchronous pairing request command --- src/httpd_jsonapi.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/httpd_jsonapi.c b/src/httpd_jsonapi.c index c6af3c89..9df5b4b0 100644 --- a/src/httpd_jsonapi.c +++ b/src/httpd_jsonapi.c @@ -1330,11 +1330,12 @@ jsonapi_reply_lastfm_logout(struct httpd_request *hreq) * } */ static int -jsonapi_reply_pairing_kickoff(struct httpd_request *hreq) +jsonapi_reply_pairing_pair(struct httpd_request *hreq) { struct evbuffer *evbuf; json_object* request; - const char* message; + const char* pin; + int ret; evbuf = evhttp_request_get_input_buffer(hreq->req); request = jparse_obj_from_evbuffer(evbuf); @@ -1346,15 +1347,25 @@ jsonapi_reply_pairing_kickoff(struct httpd_request *hreq) DPRINTF(E_DBG, L_WEB, "Received pairing post request: %s\n", json_object_to_json_string(request)); - message = jparse_str_from_obj(request, "pin"); - if (message) - remote_pairing_kickoff((char **)&message); + pin = jparse_str_from_obj(request, "pin"); + if (pin) + { + ret = remote_pairing_pair(pin); + } else - DPRINTF(E_LOG, L_WEB, "Missing pin in request body: %s\n", json_object_to_json_string(request)); + { + DPRINTF(E_LOG, L_WEB, "Missing pin in request body: %s\n", json_object_to_json_string(request)); + ret = REMOTE_INVALID_PIN; + } jparse_free(request); - return HTTP_NOCONTENT; + if (ret == 0) + return HTTP_NOCONTENT; + else if (ret == REMOTE_INVALID_PIN) + return HTTP_BADREQUEST; + + return HTTP_INTERNAL; } /* @@ -3946,7 +3957,7 @@ static struct httpd_uri_map adm_handlers[] = { EVHTTP_REQ_POST, "^/api/spotify-login$", jsonapi_reply_spotify_login }, { EVHTTP_REQ_GET, "^/api/spotify$", jsonapi_reply_spotify }, { EVHTTP_REQ_GET, "^/api/pairing$", jsonapi_reply_pairing_get }, - { EVHTTP_REQ_POST, "^/api/pairing$", jsonapi_reply_pairing_kickoff }, + { EVHTTP_REQ_POST, "^/api/pairing$", jsonapi_reply_pairing_pair }, { EVHTTP_REQ_POST, "^/api/lastfm-login$", jsonapi_reply_lastfm_login }, { EVHTTP_REQ_GET, "^/api/lastfm-logout$", jsonapi_reply_lastfm_logout }, { EVHTTP_REQ_GET, "^/api/lastfm$", jsonapi_reply_lastfm },