summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2011-09-18 17:33:34 +0200
committerLars-Dominik Braun <lars@6xq.net>2011-11-09 20:10:02 +0100
commitc73c89eabd2b1924e1bed851c980e6eb4498d31b (patch)
treef7ad5ddfb694c2dba1216a3c683c159fb2bf26c6
parent7fe04afaffb1cc6ef33f601a8d5c81299dbdb556 (diff)
downloadpianobar-windows-c73c89eabd2b1924e1bed851c980e6eb4498d31b.tar.gz
pianobar-windows-c73c89eabd2b1924e1bed851c980e6eb4498d31b.tar.bz2
pianobar-windows-c73c89eabd2b1924e1bed851c980e6eb4498d31b.zip
waitress: added header handler
-rw-r--r--src/libwaitress/waitress.c26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/libwaitress/waitress.c b/src/libwaitress/waitress.c
index 4179685..0c52f06 100644
--- a/src/libwaitress/waitress.c
+++ b/src/libwaitress/waitress.c
@@ -43,6 +43,8 @@ THE SOFTWARE.
#include "config.h"
#include "waitress.h"
+#define streq(a,b) (strcmp(a,b) == 0)
+
typedef struct {
char *buf;
size_t pos;
@@ -496,6 +498,15 @@ static const char *WaitressDefaultPort (WaitressUrl_t *url) {
return url->port == NULL ? "80" : url->port;
}
+/* handle http header
+ */
+static void WaitressHandleHeader (WaitressHandle_t *waith, const char * const key,
+ const char * const value) {
+ if (streq (key, "Content-Length")) {
+ waith->contentLength = atol (value);
+ }
+}
+
/* Receive data from host and call *callback ()
* @param waitress handle
* @return WaitressReturn_t
@@ -512,7 +523,7 @@ WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) {
/* header parser vars */
char *nextLine = NULL, *thisLine = NULL;
enum {HDRM_HEAD, HDRM_LINES, HDRM_FINISHED} hdrParseMode = HDRM_HEAD;
- char statusCode[3], val[256];
+ char statusCode[3];
unsigned int bufFilled = 0;
/* initialize */
@@ -669,9 +680,16 @@ WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) {
if (*thisLine == '\0') {
hdrParseMode = HDRM_FINISHED;
} else {
- memset (val, 0, sizeof (val));
- if (sscanf (thisLine, "Content-Length: %255c", val) == 1) {
- waith->contentLength = atol (val);
+ /* parse header: "key: value", ignore invalid lines */
+ char *key = thisLine, *val;
+
+ val = strchr (thisLine, ':');
+ if (val != NULL) {
+ *val++ = '\0';
+ while (*val != '\0' && isspace ((unsigned char) *val)) {
+ ++val;
+ }
+ WaitressHandleHeader (waith, key, val);
}
}
break;