summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@lavabit.com>2009-08-14 22:16:16 +0200
committerLars-Dominik Braun <PromyLOPh@lavabit.com>2009-08-14 22:16:16 +0200
commitf43699482a0a5921cb4cdc4bf110242278209ada (patch)
treeecef2ac0bbca45787e327c8fb8016ed393faae9b
parent0ca626237aef1c8ae30c31aa5ec2de09d2f31c42 (diff)
downloadpianobar-f43699482a0a5921cb4cdc4bf110242278209ada.tar.gz
pianobar-f43699482a0a5921cb4cdc4bf110242278209ada.tar.bz2
pianobar-f43699482a0a5921cb4cdc4bf110242278209ada.zip
waitress: Use dynamic allocated buffer for FetchBuf
-rw-r--r--libpiano/src/http.c9
-rw-r--r--libpiano/src/http.h4
-rw-r--r--libpiano/src/main.c107
-rw-r--r--libwaitress/src/main.c46
-rw-r--r--libwaitress/src/waitress.h2
-rw-r--r--libwardrobe/src/main.c11
6 files changed, 100 insertions, 79 deletions
diff --git a/libpiano/src/http.c b/libpiano/src/http.c
index f21b839..98b5e11 100644
--- a/libpiano/src/http.c
+++ b/libpiano/src/http.c
@@ -39,7 +39,7 @@ THE SOFTWARE.
* @return _RET_OK or _RET_NET_ERROR
*/
PianoReturn_t PianoHttpPost (WaitressHandle_t *waith, const char *postData,
- char *retData, size_t retDataSize) {
+ char **retData) {
PianoReturn_t pRet = PIANO_RET_NET_ERROR;
char *reqPostData = PianoEncryptString (postData);
@@ -51,7 +51,7 @@ PianoReturn_t PianoHttpPost (WaitressHandle_t *waith, const char *postData,
waith->postData = reqPostData;
waith->method = WAITRESS_METHOD_POST;
- if (WaitressFetchBuf (waith, retData, retDataSize) == WAITRESS_RET_OK) {
+ if (WaitressFetchBuf (waith, retData) == WAITRESS_RET_OK) {
pRet = PIANO_RET_OK;
}
@@ -66,13 +66,12 @@ PianoReturn_t PianoHttpPost (WaitressHandle_t *waith, const char *postData,
* @param buffer size
* @return _RET_OK or _RET_NET_ERROR
*/
-PianoReturn_t PianoHttpGet (WaitressHandle_t *waith, char *retData,
- size_t retDataSize) {
+PianoReturn_t PianoHttpGet (WaitressHandle_t *waith, char **retData) {
waith->extraHeaders = NULL;
waith->postData = NULL;
waith->method = WAITRESS_METHOD_GET;
- if (WaitressFetchBuf (waith, retData, retDataSize) == WAITRESS_RET_OK) {
+ if (WaitressFetchBuf (waith, retData) == WAITRESS_RET_OK) {
return PIANO_RET_OK;
}
return PIANO_RET_NET_ERROR;
diff --git a/libpiano/src/http.h b/libpiano/src/http.h
index c56ef8b..de9fdad 100644
--- a/libpiano/src/http.h
+++ b/libpiano/src/http.h
@@ -27,7 +27,7 @@ THE SOFTWARE.
#include <waitress.h>
#include "piano.h"
-PianoReturn_t PianoHttpPost (WaitressHandle_t *, const char *, char *, size_t);
-PianoReturn_t PianoHttpGet (WaitressHandle_t *, char *, size_t);
+PianoReturn_t PianoHttpPost (WaitressHandle_t *, const char *, char **);
+PianoReturn_t PianoHttpGet (WaitressHandle_t *, char **);
#endif /* _HTTP_H */
diff --git a/libpiano/src/main.c b/libpiano/src/main.c
index 844d757..d108c09 100644
--- a/libpiano/src/main.c
+++ b/libpiano/src/main.c
@@ -38,8 +38,6 @@ THE SOFTWARE.
#define PIANO_RPC_PORT "80"
#define PIANO_RPC_PATH "/radio/xmlrpc/v" PIANO_PROTOCOL_VERSION "?"
#define PIANO_SEND_BUFFER_SIZE 10000
-/* station responses are _huge_... */
-#define PIANO_RECV_BUFFER 100*1024
/* prototypes */
static PianoReturn_t PianoAddFeedback (PianoHandle_t *, const char *, const char *,
@@ -189,7 +187,7 @@ void PianoDestroy (PianoHandle_t *ph) {
*/
PianoReturn_t PianoConnect (PianoHandle_t *ph, const char *user,
const char *password) {
- char retStr[PIANO_RECV_BUFFER], xmlSendBuf[PIANO_SEND_BUFFER_SIZE];
+ char *retStr, xmlSendBuf[PIANO_SEND_BUFFER_SIZE];
PianoReturn_t ret;
/* sync and throw away result (it's an encrypted timestamp, decrypt with
@@ -199,7 +197,8 @@ PianoReturn_t PianoConnect (PianoHandle_t *ph, const char *user,
"<params></params></methodCall>");
snprintf (ph->waith.path, sizeof (ph->waith.path), PIANO_RPC_PATH
"rid=%s&method=sync", ph->routeId);
- ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr, sizeof (retStr));
+ ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr);
+ PianoFree (retStr, 0);
if (ret != PIANO_RET_OK) {
return ret;
@@ -216,9 +215,10 @@ PianoReturn_t PianoConnect (PianoHandle_t *ph, const char *user,
snprintf (ph->waith.path, sizeof (ph->waith.path), PIANO_RPC_PATH
"rid=%s&method=authenticateListener", ph->routeId);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseUserinfo (ph, retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -229,7 +229,7 @@ PianoReturn_t PianoConnect (PianoHandle_t *ph, const char *user,
* @param piano handle filled with some authentication data by PianoConnect
*/
PianoReturn_t PianoGetStations (PianoHandle_t *ph) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -241,9 +241,10 @@ PianoReturn_t PianoGetStations (PianoHandle_t *ph) {
"rid=%s&lid=%s&method=getStations", ph->routeId,
ph->user.listenerId);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseStations (ph, retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -255,7 +256,7 @@ PianoReturn_t PianoGetStations (PianoHandle_t *ph) {
*/
PianoReturn_t PianoGetPlaylist (PianoHandle_t *ph, const char *stationId,
PianoAudioFormat_t format) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
/* FIXME: remove static numbers */
@@ -276,9 +277,10 @@ PianoReturn_t PianoGetPlaylist (PianoHandle_t *ph, const char *stationId,
ph->user.listenerId, stationId,
PianoAudioFormatToString (format));
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParsePlaylist (ph, retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -339,7 +341,7 @@ static PianoReturn_t PianoAddFeedback (PianoHandle_t *ph, const char *stationId,
const char *songMusicId, const char *songMatchingSeed,
const char *songUserSeed, const char *songFocusTraitId,
PianoSongRating_t rating) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret = PIANO_RET_ERR;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -369,9 +371,10 @@ static PianoReturn_t PianoAddFeedback (PianoHandle_t *ph, const char *stationId,
(songFocusTraitId == NULL) ? "" : songFocusTraitId,
(rating == PIANO_RATE_LOVE) ? "true" : "false");
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseSimple (retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -386,7 +389,7 @@ static PianoReturn_t PianoAddFeedback (PianoHandle_t *ph, const char *stationId,
*/
PianoReturn_t PianoRenameStation (PianoHandle_t *ph, PianoStation_t *station,
const char *newName) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
char *urlencodedNewName, *xmlencodedNewName;
PianoReturn_t ret = PIANO_RET_ERR;
@@ -408,12 +411,13 @@ PianoReturn_t PianoRenameStation (PianoHandle_t *ph, PianoStation_t *station,
"rid=%s&lid=%s&method=setStationName&arg1=%s&arg2=%s",
ph->routeId, ph->user.listenerId, station->id, urlencodedNewName);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
if ((ret = PianoXmlParseSimple (retStr)) == PIANO_RET_OK) {
PianoFree (station->name, 0);
station->name = strdup (newName);
}
+ PianoFree (retStr, 0);
}
PianoFree (urlencodedNewName, 0);
@@ -428,7 +432,7 @@ PianoReturn_t PianoRenameStation (PianoHandle_t *ph, PianoStation_t *station,
* @param station you want to delete
*/
PianoReturn_t PianoDeleteStation (PianoHandle_t *ph, PianoStation_t *station) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret = PIANO_RET_ERR;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -442,8 +446,8 @@ PianoReturn_t PianoDeleteStation (PianoHandle_t *ph, PianoStation_t *station) {
snprintf (ph->waith.path, sizeof (ph->waith.path), PIANO_RPC_PATH
"rid=%s&lid=%s&method=removeStation&arg1=%s", ph->routeId,
ph->user.listenerId, station->id);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
if ((ret = PianoXmlParseSimple (retStr)) == PIANO_RET_OK) {
/* delete station from local station list */
PianoStation_t *curStation = ph->stations, *lastStation = NULL;
@@ -478,7 +482,7 @@ PianoReturn_t PianoDeleteStation (PianoHandle_t *ph, PianoStation_t *station) {
*/
PianoReturn_t PianoSearchMusic (PianoHandle_t *ph,
const char *searchStr, PianoSearchResult_t *searchResult) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
char *xmlencodedSearchStr, *urlencodedSearchStr;
PianoReturn_t ret;
@@ -499,9 +503,10 @@ PianoReturn_t PianoSearchMusic (PianoHandle_t *ph,
"rid=%s&lid=%s&method=search&arg1=%s", ph->routeId,
ph->user.listenerId, urlencodedSearchStr);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseSearch (retStr, searchResult);
+ PianoFree (retStr, 0);
}
PianoFree (urlencodedSearchStr, 0);
@@ -519,7 +524,7 @@ PianoReturn_t PianoSearchMusic (PianoHandle_t *ph,
*/
PianoReturn_t PianoCreateStation (PianoHandle_t *ph, const char *type,
const char *id) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -534,9 +539,10 @@ PianoReturn_t PianoCreateStation (PianoHandle_t *ph, const char *type,
"rid=%s&lid=%s&method=createStation&arg1=%s%s", ph->routeId,
ph->user.listenerId, type, id);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseCreateStation (ph, retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -553,7 +559,7 @@ PianoReturn_t PianoCreateStation (PianoHandle_t *ph, const char *type,
*/
PianoReturn_t PianoStationAddMusic (PianoHandle_t *ph,
PianoStation_t *station, const char *musicId) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -569,9 +575,10 @@ PianoReturn_t PianoStationAddMusic (PianoHandle_t *ph,
"rid=%s&lid=%s&method=addSeed&arg1=%s&arg2=%s", ph->routeId,
ph->user.listenerId, station->id, musicId);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseAddSeed (ph, retStr, station);
+ PianoFree (retStr, 0);
}
return ret;
@@ -583,7 +590,7 @@ PianoReturn_t PianoStationAddMusic (PianoHandle_t *ph,
* @return _OK or error
*/
PianoReturn_t PianoSongTired (PianoHandle_t *ph, const PianoSong_t *song) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -598,9 +605,10 @@ PianoReturn_t PianoSongTired (PianoHandle_t *ph, const PianoSong_t *song) {
"rid=%s&lid=%s&method=addTiredSong&arg1=%s", ph->routeId,
ph->user.listenerId, song->identity);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseSimple (retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -612,7 +620,7 @@ PianoReturn_t PianoSongTired (PianoHandle_t *ph, const PianoSong_t *song) {
*/
PianoReturn_t PianoSetQuickmix (PianoHandle_t *ph) {
char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], valueBuf[1000], urlArgBuf[1000],
- retStr[PIANO_RECV_BUFFER];
+ *retStr;
PianoReturn_t ret;
PianoStation_t *curStation = ph->stations;
@@ -652,9 +660,10 @@ PianoReturn_t PianoSetQuickmix (PianoHandle_t *ph) {
"rid=%s&lid=%s&method=setQuickMix&arg1=RANDOM&arg2=%s",
ph->routeId, ph->user.listenerId, urlArgBuf);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseSimple (retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -681,15 +690,16 @@ PianoStation_t *PianoFindStationById (PianoStation_t *stations,
* @return _OK or error
*/
PianoReturn_t PianoGetGenreStations (PianoHandle_t *ph) {
- char retStr[PIANO_RECV_BUFFER];
+ char *retStr;
PianoReturn_t ret;
snprintf (ph->waith.path, sizeof (ph->waith.path), "/xml/genre?r=%li",
time (NULL));
- if ((ret = PianoHttpGet (&ph->waith, retStr, sizeof (retStr))) ==
+ if ((ret = PianoHttpGet (&ph->waith, &retStr)) ==
PIANO_RET_OK) {
ret = PianoXmlParseGenreExplorer (ph, retStr);
+ PianoFree (retStr, 0);
}
return ret;
@@ -703,7 +713,7 @@ PianoReturn_t PianoGetGenreStations (PianoHandle_t *ph) {
*/
PianoReturn_t PianoTransformShared (PianoHandle_t *ph,
PianoStation_t *station) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -718,14 +728,15 @@ PianoReturn_t PianoTransformShared (PianoHandle_t *ph,
"rid=%s&lid=%s&method=transformShared&arg1=%s", ph->routeId,
ph->user.listenerId, station->id);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseTranformStation (retStr);
/* though this call returns a bunch of "new" data only this one is
* changed and important (at the moment) */
if (ret == PIANO_RET_OK) {
station->isCreator = 1;
}
+ PianoFree (retStr, 0);
}
return ret;
@@ -739,7 +750,7 @@ PianoReturn_t PianoTransformShared (PianoHandle_t *ph,
*/
PianoReturn_t PianoExplain (PianoHandle_t *ph, const PianoSong_t *song,
char **retExplain) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -755,9 +766,10 @@ PianoReturn_t PianoExplain (PianoHandle_t *ph, const PianoSong_t *song,
"rid=%s&lid=%s&method=method=narrative&arg1=%s&arg2=%s",
ph->routeId, ph->user.listenerId, song->stationId, song->musicId);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseNarrative (retStr, retExplain);
+ PianoFree (retStr, 0);
}
return ret;
@@ -771,7 +783,7 @@ PianoReturn_t PianoExplain (PianoHandle_t *ph, const PianoSong_t *song,
*/
PianoReturn_t PianoSeedSuggestions (PianoHandle_t *ph, const char *musicId,
unsigned int max, PianoSearchResult_t *searchResult) {
- char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], retStr[PIANO_RECV_BUFFER];
+ char xmlSendBuf[PIANO_SEND_BUFFER_SIZE], *retStr;
PianoReturn_t ret;
snprintf (xmlSendBuf, sizeof (xmlSendBuf), "<?xml version=\"1.0\"?>"
@@ -787,9 +799,10 @@ PianoReturn_t PianoSeedSuggestions (PianoHandle_t *ph, const char *musicId,
"rid=%s&lid=%s&method=method=getSeedSuggestions&arg1=%s&arg2=%u",
ph->routeId, ph->user.listenerId, musicId, max);
- if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, retStr,
- sizeof (retStr))) == PIANO_RET_OK) {
+ if ((ret = PianoHttpPost (&ph->waith, xmlSendBuf, &retStr)) ==
+ PIANO_RET_OK) {
ret = PianoXmlParseSeedSuggestions (retStr, searchResult);
+ PianoFree (retStr, 0);
}
return ret;
diff --git a/libwaitress/src/main.c b/libwaitress/src/main.c
index dbf1a74..cc06ec6 100644
--- a/libwaitress/src/main.c
+++ b/libwaitress/src/main.c
@@ -37,8 +37,7 @@ THE SOFTWARE.
typedef struct {
char *buf;
- char *pos;
- size_t max;
+ size_t pos;
} WaitressFetchBufCbBuffer_t;
inline void WaitressInit (WaitressHandle_t *waith) {
@@ -178,40 +177,49 @@ inline void WaitressSetHPP (WaitressHandle_t *waith, const char *host,
* @param data size
* @param buffer structure
*/
-static char WaitressFetchBufCb (void *recvData, size_t recvDataSize, void *extraData) {
+static char WaitressFetchBufCb (void *recvData, size_t recvDataSize,
+ void *extraData) {
char *recvBytes = recvData;
WaitressFetchBufCbBuffer_t *buffer = extraData;
- if (buffer->pos - buffer->buf + recvDataSize < buffer->max) {
- memcpy (buffer->pos, recvBytes, recvDataSize);
- buffer->pos += recvDataSize;
- *buffer->pos = '\0';
+ if (buffer->buf == NULL) {
+ if ((buffer->buf = malloc (sizeof (*buffer->buf) *
+ (recvDataSize + 1))) == NULL) {
+ return 0;
+ }
} else {
- printf (PACKAGE ": Buffer overflow!\n");
- return 0;
+ char *newbuf;
+ if ((newbuf = realloc (buffer->buf,
+ sizeof (*buffer->buf) *
+ (buffer->pos + recvDataSize + 1))) == NULL) {
+ free (buffer->buf);
+ return 0;
+ }
+ buffer->buf = newbuf;
}
+ memcpy (buffer->buf + buffer->pos, recvBytes, recvDataSize);
+ buffer->pos += recvDataSize;
+ *(buffer->buf+buffer->pos) = '\0';
return 1;
}
/* Fetch string. Beware! This overwrites your waith->data pointer
* @param waitress handle
- * @param Connect to host
- * @param Port or service
- * @param Resource path
- * @param result buffer
- * @param result buffer size
+ * @param result buffer, malloced (don't forget to free it yourself)
*/
-WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char *buf,
- size_t bufSize) {
+WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char **buf) {
WaitressFetchBufCbBuffer_t buffer;
+ WaitressReturn_t wRet;
+
+ memset (&buffer, 0, sizeof (buffer));
- buffer.buf = buffer.pos = buf;
- buffer.max = bufSize;
waith->data = &buffer;
waith->callback = WaitressFetchBufCb;
- return WaitressFetchCall (waith);
+ wRet = WaitressFetchCall (waith);
+ *buf = buffer.buf;
+ return wRet;
}
/* write () wrapper with poll () timeout
diff --git a/libwaitress/src/waitress.h b/libwaitress/src/waitress.h
index 3bcf524..d8c379d 100644
--- a/libwaitress/src/waitress.h
+++ b/libwaitress/src/waitress.h
@@ -67,7 +67,7 @@ char WaitressSplitUrl (const char *, char *, size_t, char *, size_t, char *,
inline char WaitressSetUrl (WaitressHandle_t *, const char *);
inline void WaitressSetHPP (WaitressHandle_t *, const char *, const char *,
const char *);
-WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *, char *, size_t);
+WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *, char **);
WaitressReturn_t WaitressFetchCall (WaitressHandle_t *);
#endif /* _WAITRESS_H */
diff --git a/libwardrobe/src/main.c b/libwardrobe/src/main.c
index a734c26..e13c09d 100644
--- a/libwardrobe/src/main.c
+++ b/libwardrobe/src/main.c
@@ -32,7 +32,6 @@ THE SOFTWARE.
#include "config.h"
#define WARDROBE_HTTP_SEND_SIZE 10*1024
-#define WARDROBE_HTTP_RECV_SIZE 1024
#define WARDROBE_URL_SIZE 1024
/* Initialize song structure
@@ -90,7 +89,7 @@ void WardrobeDestroy (WardrobeHandle_t *wh) {
static WardrobeReturn_t WardrobeHandshake (WardrobeHandle_t *wh) {
/* md5 hash length + long integer max + NULL */
char url[WARDROBE_URL_SIZE], tmp[32+55+1], *tmpDigest, *pwDigest,
- ret[WARDROBE_HTTP_RECV_SIZE], postUrl[1024];
+ *ret, postUrl[1024];
WardrobeReturn_t fRet = WARDROBE_RET_ERR;
time_t currTStamp = time (NULL);
@@ -104,7 +103,7 @@ static WardrobeReturn_t WardrobeHandshake (WardrobeHandle_t *wh) {
wh->waith.method = WAITRESS_METHOD_GET;
wh->waith.postData = NULL;
wh->waith.extraHeaders = NULL;
- if (WaitressFetchBuf (&wh->waith, ret, sizeof (ret)) != WAITRESS_RET_OK) {
+ if (WaitressFetchBuf (&wh->waith, &ret) != WAITRESS_RET_OK) {
return WARDROBE_RET_CONNECT_ERR;
}
@@ -145,6 +144,7 @@ static WardrobeReturn_t WardrobeHandshake (WardrobeHandle_t *wh) {
WardrobeFree (tmpDigest, WARDROBE_MD5_DIGEST_LEN);
WardrobeFree (pwDigest, WARDROBE_MD5_DIGEST_LEN);
+ WardrobeFree (ret, 0);
return fRet;
}
@@ -157,7 +157,7 @@ static WardrobeReturn_t WardrobeHandshake (WardrobeHandle_t *wh) {
static WardrobeReturn_t WardrobeSendSong (WardrobeHandle_t *wh,
const WardrobeSong_t *ws) {
char postContent[WARDROBE_HTTP_SEND_SIZE];
- char *urlencArtist, *urlencTitle, *urlencAlbum, ret[WARDROBE_HTTP_RECV_SIZE];
+ char *urlencArtist, *urlencTitle, *urlencAlbum, *ret;
WardrobeReturn_t fRet = WARDROBE_RET_ERR;
urlencArtist = WaitressUrlEncode (ws->artist);
@@ -173,7 +173,7 @@ static WardrobeReturn_t WardrobeSendSong (WardrobeHandle_t *wh,
wh->waith.method = WAITRESS_METHOD_POST;
wh->waith.postData = postContent;
wh->waith.extraHeaders = "Content-Type: application/x-www-form-urlencoded\r\n";
- if (WaitressFetchBuf (&wh->waith, ret, sizeof (ret)) != WAITRESS_RET_OK) {
+ if (WaitressFetchBuf (&wh->waith, &ret) != WAITRESS_RET_OK) {
return WARDROBE_RET_CONNECT_ERR;
}
@@ -186,6 +186,7 @@ static WardrobeReturn_t WardrobeSendSong (WardrobeHandle_t *wh,
WardrobeFree (urlencArtist, 0);
WardrobeFree (urlencTitle, 0);
WardrobeFree (urlencAlbum, 0);
+ WardrobeFree (ret, 0);
return fRet;
}