diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2011-09-18 18:25:49 +0200 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2011-11-09 20:10:03 +0100 |
commit | 9297703298f812361250b67739a102ed63f1222c (patch) | |
tree | 0d6ac3a46c6f7b101859d08ad5225d102875b9c3 | |
parent | 87b4d04dc18a4e13db37ed07d8222992b52fab1a (diff) | |
download | pianobar-9297703298f812361250b67739a102ed63f1222c.tar.gz pianobar-9297703298f812361250b67739a102ed63f1222c.tar.bz2 pianobar-9297703298f812361250b67739a102ed63f1222c.zip |
waitress: status line parser
-rw-r--r-- | src/libwaitress/waitress.c | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/src/libwaitress/waitress.c b/src/libwaitress/waitress.c index 00b775b..a0b4bac 100644 --- a/src/libwaitress/waitress.c +++ b/src/libwaitress/waitress.c @@ -515,6 +515,17 @@ static WaitressCbReturn_t WaitressHandleIdentity (WaitressHandle_t *waith, return waith->callback (buf, size, waith->data); } +/* parse http status line and return status code + */ +static int WaitressParseStatusline (const char * const line) { + char status[4] = "000"; + + if (sscanf (line, "HTTP/1.%*1[0-9] %3[0-9] ", status) == 1) { + return atoi (status); + } + return -1; +} + /* Receive data from host and call *callback () * @param waitress handle * @return WaitressReturn_t @@ -531,7 +542,6 @@ 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]; unsigned int bufFilled = 0; /* initialize */ @@ -666,20 +676,28 @@ WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) { switch (hdrParseMode) { /* Status code */ case HDRM_HEAD: - if (sscanf (thisLine, "HTTP/1.%*1[0-9] %3[0-9] ", - statusCode) == 1) { - if (memcmp (statusCode, "200", 3) == 0 || - memcmp (statusCode, "206", 3) == 0) { - /* everything's fine... */ - } else if (memcmp (statusCode, "403", 3) == 0) { - CLOSE_RET (WAITRESS_RET_FORBIDDEN); - } else if (memcmp (statusCode, "404", 3) == 0) { - CLOSE_RET (WAITRESS_RET_NOTFOUND); - } else { + switch (WaitressParseStatusline (thisLine)) { + case 200: + case 206: + hdrParseMode = HDRM_LINES; + break; + + case 403: + CLOSE_RET(WAITRESS_RET_FORBIDDEN); + break; + + case 404: + CLOSE_RET(WAITRESS_RET_NOTFOUND); + break; + + case -1: + /* ignore invalid line */ + break; + + default: CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN); - } - hdrParseMode = HDRM_LINES; - } /* endif */ + break; + } break; /* Everything else, except status code */ |