summaryrefslogtreecommitdiff
path: root/src/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui.c')
-rw-r--r--src/ui.c116
1 files changed, 10 insertions, 106 deletions
diff --git a/src/ui.c b/src/ui.c
index 023a849..aaa83ae 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -29,6 +29,7 @@ THE SOFTWARE.
#include "ui_readline.h"
#include "console.h"
#include <assert.h>
+#include <stdio.h>
typedef int (*BarSortFunc_t) (const void *, const void *);
@@ -147,103 +148,12 @@ void BarUiMsg (const BarSettings_t *settings, const BarUiMsg_t type,
fflush (stdout);
}
-typedef struct {
- char *data;
- size_t pos;
-} buffer;
-
-static size_t httpFetchCb (char *ptr, size_t size, size_t nmemb,
- void *userdata) {
- buffer * const buffer = userdata;
- size_t recvSize = size * nmemb;
-
- if (buffer->data == NULL) {
- if ((buffer->data = malloc (sizeof (*buffer->data) *
- (recvSize + 1))) == NULL) {
- return 0;
- }
- } else {
- char *newbuf;
- if ((newbuf = realloc (buffer->data, sizeof (*buffer->data) *
- (buffer->pos + recvSize + 1))) == NULL) {
- free (buffer->data);
- return 0;
- }
- buffer->data = newbuf;
- }
- memcpy (buffer->data + buffer->pos, ptr, recvSize);
- buffer->pos += recvSize;
- buffer->data[buffer->pos] = '\0';
-
- return recvSize;
-}
-
-#define setAndCheck(k,v) \
- httpret = curl_easy_setopt (http, k, v); \
- assert (httpret == CURLE_OK);
-
-static CURLcode BarPianoHttpRequest (CURL * const http,
- const BarSettings_t * const settings, PianoRequest_t * const req) {
- buffer buffer = {NULL, 0};
- char url[2048];
- int ret = snprintf (url, sizeof (url), "%s://%s:%s%s",
- req->secure ? "https" : "http",
- settings->rpcHost,
- req->secure ? settings->rpcTlsPort : "80",
- req->urlPath);
- assert (ret >= 0 && ret <= (int) sizeof (url));
-
- curl_easy_reset (http);
- CURLcode httpret;
- setAndCheck (CURLOPT_URL, url);
- setAndCheck (CURLOPT_USERAGENT, PACKAGE "-" VERSION);
- setAndCheck (CURLOPT_POSTFIELDS, req->postData);
- setAndCheck (CURLOPT_WRITEFUNCTION, httpFetchCb);
- setAndCheck (CURLOPT_WRITEDATA, &buffer);
- setAndCheck (CURLOPT_POST, 1);
- setAndCheck (CURLOPT_TIMEOUT, 30);
- if (settings->caBundle != NULL) {
- setAndCheck (CURLOPT_CAINFO, settings->caBundle);
- }
-
- /* set up proxy (control proxy for non-us citizen or global proxy for poor
- * firewalled fellows) */
- if (settings->controlProxy != NULL) {
- /* control proxy overrides global proxy */
- if (curl_easy_setopt (http, CURLOPT_PROXY,
- settings->controlProxy) != CURLE_OK) {
- /* if setting proxy fails, url is invalid */
- BarUiMsg (settings, MSG_ERR, "Control proxy (%s) is invalid!\n",
- settings->controlProxy);
- }
- } else if (settings->proxy != NULL && strlen (settings->proxy) > 0) {
- if (curl_easy_setopt (http, CURLOPT_PROXY,
- settings->proxy) != CURLE_OK) {
- /* if setting proxy fails, url is invalid */
- BarUiMsg (settings, MSG_ERR, "Proxy (%s) is invalid!\n",
- settings->proxy);
- }
- }
-
- struct curl_slist *list = NULL;
- list = curl_slist_append (list, "Content-Type: text/plain");
- setAndCheck (CURLOPT_HTTPHEADER, list);
-
- httpret = curl_easy_perform (http);
-
- curl_slist_free_all (list);
-
- req->responseData = buffer.data;
-
- return httpret;
-}
-
/* piano wrapper: prepare/execute http request and pass result back to
* libpiano (updates data structures)
* @return 1 on success, 0 otherwise
*/
int BarUiPianoCall (BarApp_t * const app, PianoRequestType_t type,
- void *data, PianoReturn_t *pRet, CURLcode *wRet) {
+ void *data, PianoReturn_t *pRet) {
PianoRequest_t req;
memset (&req, 0, sizeof (req));
@@ -259,14 +169,13 @@ int BarUiPianoCall (BarApp_t * const app, PianoRequestType_t type,
return 0;
}
- *wRet = BarPianoHttpRequest (app->http, &app->settings, &req);
- if (*wRet != CURLE_OK) {
- BarUiMsg (&app->settings, MSG_NONE, "Network error: %s\n",
- curl_easy_strerror (*wRet));
+ if (!HttpRequest(app->http2, &req)) {
+ BarUiMsg(&app->settings, MSG_NONE, "Network error: %s\n",
+ HttpGetError(app->http2));
if (req.responseData != NULL) {
- free (req.responseData);
+ free(req.responseData);
}
- PianoDestroyRequest (&req);
+ PianoDestroyRequest(&req);
return 0;
}
@@ -277,17 +186,14 @@ int BarUiPianoCall (BarApp_t * const app, PianoRequestType_t type,
type != PIANO_REQUEST_LOGIN) {
/* reauthenticate */
PianoReturn_t authpRet;
- CURLcode authwRet;
PianoRequestDataLogin_t reqData;
reqData.user = app->settings.username;
reqData.password = app->settings.password;
reqData.step = 0;
BarUiMsg (&app->settings, MSG_NONE, "Reauthentication required... ");
- if (!BarUiPianoCall (app, PIANO_REQUEST_LOGIN, &reqData, &authpRet,
- &authwRet)) {
+ if (!BarUiPianoCall (app, PIANO_REQUEST_LOGIN, &reqData, &authpRet)) {
*pRet = authpRet;
- *wRet = authwRet;
if (req.responseData != NULL) {
free (req.responseData);
}
@@ -569,14 +475,12 @@ char *BarUiSelectMusicId (BarApp_t *app, PianoStation_t *station,
if (BarReadlineStr (lineBuf, sizeof (lineBuf), app->rl,
BAR_RL_DEFAULT) > 0) {
PianoReturn_t pRet;
- CURLcode wRet;
PianoRequestDataSearch_t reqData;
reqData.searchStr = lineBuf;
BarUiMsg (&app->settings, MSG_INFO, "Searching... ");
- if (!BarUiPianoCall (app, PIANO_REQUEST_SEARCH, &reqData, &pRet,
- &wRet)) {
+ if (!BarUiPianoCall (app, PIANO_REQUEST_SEARCH, &reqData, &pRet)) {
return NULL;
}
memcpy (&searchResult, &reqData.searchResult, sizeof (searchResult));
@@ -776,7 +680,7 @@ size_t BarUiListSongs (const BarSettings_t *settings,
void BarUiStartEventCmd (const BarSettings_t *settings, const char *type,
const PianoStation_t *curStation, const PianoSong_t *curSong,
const player2_t * const player, PianoStation_t *stations,
- PianoReturn_t pRet, CURLcode wRet) {
+ PianoReturn_t pRet) {
//pid_t chld;
//int pipeFd[2];