From 13106ac8bb95e325cf522817f91ad1f3b0fcecb0 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 23 Nov 2010 21:59:31 +0100 Subject: Better directory layout Removed useless AUTHORS, COPYING and README files. Move manpage to contrib (it's not exactly source code). --- AUTHORS | 2 - Makefile | 13 +- contrib/pianobar.1 | 227 ++++++++ libezxml/COPYING | 20 - libezxml/src/ezxml.c | 715 ------------------------- libezxml/src/ezxml.h | 93 ---- libpiano/AUTHORS | 2 - libpiano/COPYING | 20 - libpiano/README | 5 - libpiano/src/config.h | 1 - libpiano/src/crypt.c | 197 ------- libpiano/src/crypt.h | 30 -- libpiano/src/crypt_key_input.h | 305 ----------- libpiano/src/crypt_key_output.h | 303 ----------- libpiano/src/piano.c | 1114 --------------------------------------- libpiano/src/piano.h | 244 --------- libpiano/src/piano_private.h | 31 -- libpiano/src/xml.c | 826 ----------------------------- libpiano/src/xml.h | 48 -- libwaitress/AUTHORS | 2 - libwaitress/COPYING | 20 - libwaitress/README | 4 - libwaitress/src/config.h | 1 - libwaitress/src/waitress.c | 573 -------------------- libwaitress/src/waitress.h | 78 --- src/libezxml/COPYING | 20 + src/libezxml/ezxml.c | 715 +++++++++++++++++++++++++ src/libezxml/ezxml.h | 93 ++++ src/libpiano/config.h | 1 + src/libpiano/crypt.c | 197 +++++++ src/libpiano/crypt.h | 30 ++ src/libpiano/crypt_key_input.h | 305 +++++++++++ src/libpiano/crypt_key_output.h | 303 +++++++++++ src/libpiano/piano.c | 1114 +++++++++++++++++++++++++++++++++++++++ src/libpiano/piano.h | 244 +++++++++ src/libpiano/piano_private.h | 31 ++ src/libpiano/xml.c | 826 +++++++++++++++++++++++++++++ src/libpiano/xml.h | 48 ++ src/libwaitress/config.h | 1 + src/libwaitress/waitress.c | 573 ++++++++++++++++++++ src/libwaitress/waitress.h | 78 +++ src/pianobar.1 | 227 -------- 42 files changed, 4813 insertions(+), 4867 deletions(-) delete mode 100644 AUTHORS create mode 100644 contrib/pianobar.1 delete mode 100644 libezxml/COPYING delete mode 100644 libezxml/src/ezxml.c delete mode 100644 libezxml/src/ezxml.h delete mode 100644 libpiano/AUTHORS delete mode 100644 libpiano/COPYING delete mode 100644 libpiano/README delete mode 100644 libpiano/src/config.h delete mode 100644 libpiano/src/crypt.c delete mode 100644 libpiano/src/crypt.h delete mode 100644 libpiano/src/crypt_key_input.h delete mode 100644 libpiano/src/crypt_key_output.h delete mode 100644 libpiano/src/piano.c delete mode 100644 libpiano/src/piano.h delete mode 100644 libpiano/src/piano_private.h delete mode 100644 libpiano/src/xml.c delete mode 100644 libpiano/src/xml.h delete mode 100644 libwaitress/AUTHORS delete mode 100644 libwaitress/COPYING delete mode 100644 libwaitress/README delete mode 100644 libwaitress/src/config.h delete mode 100644 libwaitress/src/waitress.c delete mode 100644 libwaitress/src/waitress.h create mode 100644 src/libezxml/COPYING create mode 100644 src/libezxml/ezxml.c create mode 100644 src/libezxml/ezxml.h create mode 100644 src/libpiano/config.h create mode 100644 src/libpiano/crypt.c create mode 100644 src/libpiano/crypt.h create mode 100644 src/libpiano/crypt_key_input.h create mode 100644 src/libpiano/crypt_key_output.h create mode 100644 src/libpiano/piano.c create mode 100644 src/libpiano/piano.h create mode 100644 src/libpiano/piano_private.h create mode 100644 src/libpiano/xml.c create mode 100644 src/libpiano/xml.h create mode 100644 src/libwaitress/config.h create mode 100644 src/libwaitress/waitress.c create mode 100644 src/libwaitress/waitress.h delete mode 100644 src/pianobar.1 diff --git a/AUTHORS b/AUTHORS deleted file mode 100644 index 09787cf..0000000 --- a/AUTHORS +++ /dev/null @@ -1,2 +0,0 @@ -Main code: - Lars-Dominik Braun diff --git a/Makefile b/Makefile index 0fbef58..c6b57e3 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ PIANOBAR_HDR=\ ${PIANOBAR_DIR}/config.h PIANOBAR_OBJ=${PIANOBAR_SRC:.c=.o} -LIBPIANO_DIR=libpiano/src +LIBPIANO_DIR=src/libpiano LIBPIANO_SRC=\ ${LIBPIANO_DIR}/crypt.c \ ${LIBPIANO_DIR}/piano.c \ @@ -43,7 +43,7 @@ LIBPIANO_OBJ=${LIBPIANO_SRC:.c=.o} LIBPIANO_RELOBJ=${LIBPIANO_SRC:.c=.lo} LIBPIANO_INCLUDE=${LIBPIANO_DIR} -LIBWAITRESS_DIR=libwaitress/src +LIBWAITRESS_DIR=src/libwaitress LIBWAITRESS_SRC=${LIBWAITRESS_DIR}/waitress.c LIBWAITRESS_HDR=\ ${LIBWAITRESS_DIR}/config.h \ @@ -52,11 +52,12 @@ LIBWAITRESS_OBJ=${LIBWAITRESS_SRC:.c=.o} LIBWAITRESS_RELOBJ=${LIBWAITRESS_SRC:.c=.lo} LIBWAITRESS_INCLUDE=${LIBWAITRESS_DIR} -LIBEZXML_SRC=libezxml/src/ezxml.c -LIBEZXML_HDR=libezxml/src/ezxml.h +LIBEZXML_DIR=src/libezxml +LIBEZXML_SRC=${LIBEZXML_DIR}/ezxml.c +LIBEZXML_HDR=${LIBEZXML_DIR}/ezxml.h LIBEZXML_OBJ=${LIBEZXML_SRC:.c=.o} LIBEZXML_RELOBJ=${LIBEZXML_SRC:.c=.lo} -LIBEZXML_INCLUDE=libezxml/src +LIBEZXML_INCLUDE=${LIBEZXML_DIR} LIBAO_INCLUDE=/usr/include LIBAO_LIB=-lao @@ -119,7 +120,7 @@ install: pianobar install -d ${DESTDIR}/${BINDIR}/ install -m755 pianobar ${DESTDIR}/${BINDIR}/ install -d ${DESTDIR}/${MANDIR}/man1/ - install -m644 src/pianobar.1 ${DESTDIR}/${MANDIR}/man1/ + install -m644 contrib/pianobar.1 ${DESTDIR}/${MANDIR}/man1/ install-libpiano: libpiano install -d ${DESTDIR}/${LIBDIR}/ diff --git a/contrib/pianobar.1 b/contrib/pianobar.1 new file mode 100644 index 0000000..6a6bd33 --- /dev/null +++ b/contrib/pianobar.1 @@ -0,0 +1,227 @@ +.TH pianobar 1 + +.SH NAME +pianobar \- console pandora.com music player + +.SH SYNOPSIS +.B pianobar + +.SH DESCRIPTION +.B pianobar +is a lightweight console music player for the personalized online radio +pandora.com. + +.SH FILES +.I $XDG_CONFIG_HOME/pianobar/config +or +.I ~/.config/pianobar/config +.RS +Per-user configuration file. See +.B CONFIGURATION. +.RE + +.I /etc/libao.conf +or +.I ~/.libao +.RS +Global/per-user audio output configuration. See libao documentation at +http://xiph.org/ao/doc/config.html +.RE + +.SH CONFIGURATION +The configuration file consists of simple +.B key = value +lines. Each terminated with a newline (\\n) character. Keys and values are both +case sensitive. act_*-keys control +.B pianobar's +key-bindings. + +.TP +.B act_help = ? +Show keybindings. + +.TP +.B act_songlove = + +Love currently played song. + +.TP +.B act_songban = - +Ban current track. It will not be played again and can only removed using the +pandora.com web interface. + +.TP +.B act_stationaddmusic = a +Add more music to current station. You will be asked for a search string. Just +follow the instructions. If you're clueless try '?' (without quotes). + +.TP +.B act_bookmark = b +Bookmark current song or artist. + +.TP +.B act_stationcreate = c +Create new station. You have to enter a search string and select the song or +artist of your choice. + +.TP +.B act_stationdelete = d +Delete current station. + +.TP +.B act_songexplain = e +Explain why this song is played. + +.TP +.B act_stationaddbygenre = g +Add genre station provided by pandora. + +.TP +.B act_history = h +Show history. + +.TP +.B act_songinfo = i +Print information about currently played song/station. + +.TP +.B act_addshared = j +Add shared station by id. id is a very long integer without "sh" at the +beginning. + +.TP +.B act_songmove = m +Move current song to another station + +.TP +.B act_songnext = n +Skip current song. + +.TP +.B act_songpause = p +Pause/Continue + +.TP +.B act_quit = q +Quit +.B pianobar. + +.TP +.B act_stationrename = r +Rename currently played station. + +.TP +.B act_stationchange = s +Select another station. + +.TP +.B act_songtired = t +Ban song for one month. + +.TP +.B act_upcoming = u +Show next songs in playlist. + +.TP +.B act_stationselectquickmix = x +Select quickmix stations. + +.TP +.B audio_format = {aacplus,mp3,mp3-hifi} +Select audio format. aacplus is default if both libraries (faad, mad) are +available. mp3-hifi is available for Pandora One customers only. + +.TP +.B autostart_station = stationid +Play this station when starting up. You can get the +.B stationid +by pressing +.B i +or the key you defined in +.B act_songinfo. + +.TP +.B ban_icon = ~/.config/pianobar/ctl + +.B n +is the keybinding for "next song". If you customized your keybindings you have to use these characters to control +.B pianobar. +.I This behaviour may change in the future! + +Another example: + + while true; do; + nc -l -p 12345 -s localhost localhost > ~/.config/pianobar/ctl; + sleep 1; + done + + echo -ne 'n\\x1a' | nc -q 0 127.0.0.1 12345 + +.SH EVENTCMD + +.B pianobar +can report certain "events" to an external application (see +.B CONFIGURATION +). This application is started with the event name as it's first argument. More +information (artist, title, album, stationName, error code, error description, +song length in milliseconds, rating, album art url) is supplied through stdin. + +Currently supported events are: artistbookmark, songban, songbookmark, +songexplain, songfinish, songlove, songmove, songshelf, songstart, +stationaddmusic, stationaddshared, stationcreate, stationdelete, +stationfetchplaylist, stationquickmixtoggle, stationrename + +An example script can be found in the contrib/ directory of +.B pianobar's +source distribution. + +.SH AUTHOR +Lars-Dominik Braun diff --git a/libezxml/COPYING b/libezxml/COPYING deleted file mode 100644 index 80e4e88..0000000 --- a/libezxml/COPYING +++ /dev/null @@ -1,20 +0,0 @@ -Copyright 2004-2006 Aaron Voisine - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/libezxml/src/ezxml.c b/libezxml/src/ezxml.c deleted file mode 100644 index 967d535..0000000 --- a/libezxml/src/ezxml.c +++ /dev/null @@ -1,715 +0,0 @@ -/* ezxml.c - * - * Copyright 2004-2006 Aaron Voisine - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#define _BSD_SOURCE /* required by strdup() */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include "ezxml.h" - -#define EZXML_WS "\t\r\n " // whitespace -#define EZXML_ERRL 128 // maximum error string length - -typedef struct ezxml_root *ezxml_root_t; -struct ezxml_root { // additional data for the root tag - struct ezxml xml; // is a super-struct built on top of ezxml struct - ezxml_t cur; // current xml tree insertion point - char *m; // original xml string - size_t len; // length of allocated memory for mmap, -1 for malloc - char *u; // UTF-8 conversion of string if original was UTF-16 - char *s; // start of work area - char *e; // end of work area - char **ent; // general entities (ampersand sequences) - char ***attr; // default attributes - char ***pi; // processing instructions - short standalone; // non-zero if - char err[EZXML_ERRL]; // error string -}; - -char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings - -// sets a flag for the given tag and returns the tag -static ezxml_t ezxml_set_flag(ezxml_t xml, short flag) { - if (xml) xml->flags |= flag; - return xml; -} - -// inserts an existing tag into an ezxml structure -static ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off) -{ - ezxml_t cur, prev, head; - - xml->next = xml->sibling = xml->ordered = NULL; - xml->off = off; - xml->parent = dest; - - if ((head = dest->child)) { // already have sub tags - if (head->off <= off) { // not first subtag - for (cur = head; cur->ordered && cur->ordered->off <= off; - cur = cur->ordered); - xml->ordered = cur->ordered; - cur->ordered = xml; - } - else { // first subtag - xml->ordered = head; - dest->child = xml; - } - - for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name); - prev = cur, cur = cur->sibling); // find tag type - if (cur && cur->off <= off) { // not first of type - while (cur->next && cur->next->off <= off) cur = cur->next; - xml->next = cur->next; - cur->next = xml; - } - else { // first tag of this type - if (prev && cur) prev->sibling = cur->sibling; // remove old first - xml->next = cur; // old first tag is now next - for (cur = head, prev = NULL; cur && cur->off <= off; - prev = cur, cur = cur->sibling); // new sibling insert point - xml->sibling = cur; - if (prev) prev->sibling = xml; - } - } - else dest->child = xml; // only sub tag - - return xml; -} - -// Adds a child tag. off is the offset of the child tag relative to the start -// of the parent tag's character content. Returns the child tag. -static ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off) -{ - ezxml_t child; - - if (! xml) return NULL; - child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0', - sizeof(struct ezxml)); - child->name = (char *)name; - child->attr = EZXML_NIL; - child->txt = ""; - - return ezxml_insert(child, xml, off); -} - -// returns the first child tag with the given name or NULL if not found -ezxml_t ezxml_child(ezxml_t xml, const char *name) -{ - xml = (xml) ? xml->child : NULL; - while (xml && strcmp(name, xml->name)) xml = xml->sibling; - return xml; -} - -// returns a new empty ezxml structure with the given root tag name -static ezxml_t ezxml_new(const char *name) -{ - static char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """, - "apos;", "'", "amp;", "&", NULL }; - ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)), - '\0', sizeof(struct ezxml_root)); - root->xml.name = (char *)name; - root->cur = &root->xml; - strcpy(root->err, root->xml.txt = ""); - root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent)); - root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL); - return &root->xml; -} - -// returns the Nth tag with the same name in the same subsection or NULL if not -// found -ezxml_t ezxml_idx(ezxml_t xml, int idx) -{ - for (; xml && idx; idx--) xml = xml->next; - return xml; -} - -// returns the value of the requested tag attribute or NULL if not found -const char *ezxml_attr(ezxml_t xml, const char *attr) -{ - int i = 0, j = 1; - ezxml_root_t root = (ezxml_root_t)xml; - - if (! xml || ! xml->attr) return NULL; - while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2; - if (xml->attr[i]) return xml->attr[i + 1]; // found attribute - - while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag - for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++); - if (! root->attr[i]) return NULL; // no matching default attributes - while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3; - return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default -} - -// same as ezxml_get but takes an already initialized va_list -static ezxml_t ezxml_vget(ezxml_t xml, va_list ap) -{ - char *name = va_arg(ap, char *); - int idx = -1; - - if (name && *name) { - idx = va_arg(ap, int); - xml = ezxml_child(xml, name); - } - return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap); -} - -// Traverses the xml tree to retrieve a specific subtag. Takes a variable -// length list of tag names and indexes. The argument list must be terminated -// by either an index of -1 or an empty string tag name. Example: -// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1); -// This retrieves the title of the 3rd book on the 1st shelf of library. -// Returns NULL if not found. -ezxml_t ezxml_get(ezxml_t xml, ...) -{ - va_list ap; - ezxml_t r; - - va_start(ap, xml); - r = ezxml_vget(xml, ap); - va_end(ap); - return r; -} - -// set an error string and return root -static ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...) -{ - va_list ap; - int line = 1; - char *t, fmt[EZXML_ERRL]; - - for (t = root->s; t < s; t++) if (*t == '\n') line++; - snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err); - - va_start(ap, err); - vsnprintf(root->err, EZXML_ERRL, fmt, ap); - va_end(ap); - - return &root->xml; -} - -// Recursively decodes entity and character references and normalizes new lines -// ent is a null terminated array of alternating entity names and values. set t -// to '&' for general entity decoding, '%' for parameter entity decoding, 'c' -// for cdata sections, ' ' for attribute normalization, or '*' for non-cdata -// attribute normalization. Returns s, or if the decoded string is longer than -// s, returns a malloced string that must be freed. -static char *ezxml_decode(char *s, char **ent, char t) -{ - char *e, *r = s, *m = s; - long b, c, d, l; - - for (; *s; s++) { // normalize line endings - while (*s == '\r') { - *(s++) = '\n'; - if (*s == '\n') memmove(s, (s + 1), strlen(s)); - } - } - - for (s = r; ; ) { - while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++; - - if (! *s) break; - else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference - if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16 - else c = strtol(s + 2, &e, 10); // base 10 - if (! c || *e != ';') { s++; continue; } // not a character ref - - if (c < 0x80) *(s++) = c; // US-ASCII subset - else { // multi-byte UTF-8 sequence - for (b = 0, d = c; d; d /= 2) b++; // number of bits in c - b = (b - 2) / 5; // number of bytes in payload - *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head - while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload - } - - memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';'))); - } - else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) || - (*s == '%' && t == '%')) { // entity reference - for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b])); - b += 2); // find entity in entity list - - if (ent[b++]) { // found a match - if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) { - l = (d = (s - r)) + c + strlen(e); // new length - r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l); - e = strchr((s = r + d), ';'); // fix up pointers - } - - memmove(s + c, e + 1, strlen(e)); // shift rest of string - strncpy(s, ent[b], c); // copy in replacement text - } - else s++; // not a known entity - } - else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' '; - else s++; // no decoding needed - } - - if (t == '*') { // normalize spaces for non-cdata attributes - for (s = r; *s; s++) { - if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1); - while (*s && *s != ' ') s++; - } - if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space - } - return r; -} - -// called when parser finds start of new tag -static void ezxml_open_tag(ezxml_root_t root, char *name, char **attr) -{ - ezxml_t xml = root->cur; - - if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt)); - else xml->name = name; // first open tag - - xml->attr = attr; - root->cur = xml; // update tag insertion point -} - -// called when parser finds character content between open and closing tag -static void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t) -{ - ezxml_t xml = root->cur; - char *m = s; - size_t l; - - if (! xml || ! xml->name || ! len) return; // sanity check - - s[len] = '\0'; // null terminate text (calling functions anticipate this) - len = strlen(s = ezxml_decode(s, root->ent, t)) + 1; - - if (! *(xml->txt)) xml->txt = s; // initial character content - else { // allocate our own memory and make a copy - xml->txt = (xml->flags & EZXML_TXTM) // allocate some space - ? realloc(xml->txt, (l = strlen(xml->txt)) + len) - : strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt); - strcpy(xml->txt + l, s); // add new char content - if (s != m) free(s); // free s if it was malloced by ezxml_decode() - } - - if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM); -} - -// called when parser finds closing tag -static ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s) -{ - if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name)) - return ezxml_err(root, s, "unexpected closing tag ", name); - - root->cur = root->cur->parent; - return NULL; -} - -// checks for circular entity references, returns non-zero if no circular -// references are found, zero otherwise -static int ezxml_ent_ok(char *name, char *s, char **ent) -{ - int i; - - for (; ; s++) { - while (*s && *s != '&') s++; // find next entity reference - if (! *s) return 1; - if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref. - for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2); - if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0; - } -} - -// called when the parser finds a processing instruction -static void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len) -{ - int i = 0, j = 1; - char *target = s; - - s[len] = '\0'; // null terminate instruction - if (*(s += strcspn(s, EZXML_WS))) { - *s = '\0'; // null terminate target - s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target - } - - if (! strcmp(target, "xml")) { // - if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10, - EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1; - return; - } - - if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi - - while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target - if (! root->pi[i]) { // new target - root->pi = realloc(root->pi, sizeof(char **) * (i + 2)); - root->pi[i] = malloc(sizeof(char *) * 3); - root->pi[i][0] = target; - root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list - root->pi[i][2] = strdup(""); // empty document position list - } - - while (root->pi[i][j]) j++; // find end of instruction list for this target - root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3)); - root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1); - strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<"); - root->pi[i][j + 1] = NULL; // null terminate pi list for this target - root->pi[i][j] = s; // set instruction -} - -// called when the parser finds an internal doctype subset -static short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len) -{ - char q, *c, *t, *n = NULL, *v, **ent, **pe; - int i, j; - - pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL)); - - for (s[len] = '\0'; s; ) { - while (*s && *s != '<' && *s != '%') s++; // find next declaration - - if (! *s) break; - else if (! strncmp(s, "'); - continue; - } - - for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++); - ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent - if (*c == '%') pe = ent; - else root->ent = ent; - - *(++s) = '\0'; // null terminate name - if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value - ent[i + 1] = ezxml_decode(v, pe, '%'); // set value - ent[i + 2] = NULL; // null terminate entity list - if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference - if (ent[i + 1] != v) free(ent[i + 1]); - ezxml_err(root, v, "circular entity declaration &%s", n); - break; - } - else ent[i] = n; // set entity name - } - else if (! strncmp(s, "")) == '>') continue; - else *s = '\0'; // null terminate tag name - for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++); - - ++s; // ansi cpr - while (*(n = s + strspn(s, EZXML_WS)) && *n != '>') { - if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name - else { ezxml_err(root, t, "malformed ") - 1; - if (*c == ' ') continue; // cdata is default, nothing to do - v = NULL; - } - else if ((*s == '"' || *s == '\'') && // default value - (s = strchr(v = s + 1, *s))) *s = '\0'; - else { ezxml_err(root, t, "malformed attr[i]) { // new tag name - root->attr = (! i) ? malloc(2 * sizeof(char **)) - : realloc(root->attr, - (i + 2) * sizeof(char **)); - root->attr[i] = malloc(2 * sizeof(char *)); - root->attr[i][0] = t; // set tag name - root->attr[i][1] = (char *)(root->attr[i + 1] = NULL); - } - - for (j = 1; root->attr[i][j]; j += 3); // find end of list - root->attr[i] = realloc(root->attr[i], - (j + 4) * sizeof(char *)); - - root->attr[i][j + 3] = NULL; // null terminate list - root->attr[i][j + 2] = c; // is it cdata? - root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c) - : NULL; - root->attr[i][j] = n; // attribute name - ++s; - } - } - else if (! strncmp(s, ""); // comments - else if (! strncmp(s, ""))) - ezxml_proc_inst(root, c, s++ - c); - } - else if (*s == '<') s = strchr(s, '>'); // skip other declarations - else if (*(s++) == '%' && ! root->standalone) break; - } - - free(pe); - return ! *root->err; -} - -// Converts a UTF-16 string to UTF-8. Returns a new string that must be freed -// or NULL if no conversion was needed. -static char *ezxml_str2utf8(char **s, size_t *len) -{ - char *u; - size_t l = 0, sl, max = *len; - long c, d; - int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1; - - if (be == -1) return NULL; // not UTF-16 - - u = malloc(max); - for (sl = 2; sl < *len - 1; sl += 2) { - c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE - : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE - if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half - d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) - : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); - c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000; - } - - while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE); - if (c < 0x80) u[l++] = c; // US-ASCII subset - else { // multi-byte UTF-8 sequence - for (b = 0, d = c; d; d /= 2) b++; // bits in c - b = (b - 2) / 5; // bytes in payload - u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head - while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload - } - } - return *s = realloc(u, *len = l); -} - -// frees a tag attribute list -static void ezxml_free_attr(char **attr) { - int i = 0; - char *m; - - if (! attr || attr == EZXML_NIL) return; // nothing to free - while (attr[i]) i += 2; // find end of attribute list - m = attr[i + 1]; // list of which names and values are malloced - for (i = 0; m[i]; i++) { - if (m[i] & EZXML_NAMEM) free(attr[i * 2]); - if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]); - } - free(m); - free(attr); -} - -// parse the given xml string and return an ezxml structure -ezxml_t ezxml_parse_str(char *s, size_t len) -{ - ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL); - char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning - int l, i, j; - - root->m = s; - if (! len) return ezxml_err(root, NULL, "root tag missing"); - root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8 - root->e = (root->s = s) + len; // record start and end of work area - - e = s[len - 1]; // save end char - s[len - 1] = '\0'; // turn end char into null terminator - - while (*s && *s != '<') s++; // find first tag - if (! *s) return ezxml_err(root, s, "root tag missing"); - - for (; ; ) { - attr = (char **)EZXML_NIL; - d = ++s; - - if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag - if (! root->cur) - return ezxml_err(root, d, "markup outside of root element"); - - s += strcspn(s, EZXML_WS "/>"); - while (isspace(*s)) *(s++) = '\0'; // null terminate tag name - - if (*s && *s != '/' && *s != '>') // find tag in default attr list - for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++); - - for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib - attr = (l) ? realloc(attr, (l + 4) * sizeof(char *)) - : malloc(4 * sizeof(char *)); // allocate space - attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2) - : malloc(2); // mem for list of maloced vals - strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced - attr[l + 2] = NULL; // null terminate list - attr[l + 1] = ""; // temporary attribute value - attr[l] = s; // set attribute name - - s += strcspn(s, EZXML_WS "=/>"); - if (*s == '=' || isspace(*s)) { - *(s++) = '\0'; // null terminate tag attribute name - q = *(s += strspn(s, EZXML_WS "=")); - if (q == '"' || q == '\'') { // attribute value - attr[l + 1] = ++s; - while (*s && *s != q) s++; - if (*s) *(s++) = '\0'; // null terminate attribute val - else { - ezxml_free_attr(attr); - return ezxml_err(root, d, "missing %c", q); - } - - for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3); - attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a - && a[j]) ? *a[j + 2] : ' '); - if (attr[l + 1] < d || attr[l + 1] > s) - attr[l + 3][l / 2] = EZXML_TXTM; // value malloced - } - } - while (isspace(*s)) s++; - } - - if (*s == '/') { // self closing tag - *(s++) = '\0'; - if ((*s && *s != '>') || (! *s && e != '>')) { - if (l) ezxml_free_attr(attr); - return ezxml_err(root, d, "missing >"); - } - ezxml_open_tag(root, d, attr); - ezxml_close_tag(root, d, s); - } - else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag - *s = '\0'; // temporarily null terminate tag name - ezxml_open_tag(root, d, attr); - *s = q; - } - else { - if (l) ezxml_free_attr(attr); - return ezxml_err(root, d, "missing >"); - } - } - else if (*s == '/') { // close tag - s += strcspn(d = s + 1, EZXML_WS ">") + 1; - if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >"); - *s = '\0'; // temporarily null terminate tag name - if (ezxml_close_tag(root, d, s)) return &root->xml; - if (isspace(*s = q)) s += strspn(s, EZXML_WS); - } - else if (! strncmp(s, "!--", 3)) { // xml comment - if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) || - (! *s && e != '>')) return ezxml_err(root, d, "unclosed - * - * @param xml node named "struct" (or containing a similar structure) - * @param who wants to use this data? callback: content of as - * string, content of as xmlNode (may contain other nodes - * or text), additional data used by callback(); don't forget - * to *copy* data taken from or as they will be - * freed soon - * @param extra data for callback - */ -static void PianoXmlStructParser (const ezxml_t structRoot, - void (*callback) (const char *, const ezxml_t, void *), void *data) { - ezxml_t curNode, keyNode, valueNode; - char *key; - - /* get all nodes */ - for (curNode = ezxml_child (structRoot, "member"); curNode; curNode = curNode->next) { - /* reset variables */ - key = NULL; - valueNode = keyNode = NULL; - - keyNode = ezxml_child (curNode, "name"); - if (keyNode != NULL) { - key = ezxml_txt (keyNode); - } - - valueNode = ezxml_child (curNode, "value"); - /* this will ignore empty nodes, but well... */ - if (*key != '\0' && valueNode != NULL) { - (*callback) ((char *) key, valueNode, data); - } - } -} - -/* create xml parser from string - * @param xml document - * @param returns document pointer (needed to free memory later) - * @param returns document root - * @return _OK or error - */ -static PianoReturn_t PianoXmlInitDoc (char *xmlStr, ezxml_t *xmlDoc) { - PianoReturn_t ret; - - if ((*xmlDoc = ezxml_parse_str (xmlStr, strlen (xmlStr))) == NULL) { - return PIANO_RET_XML_INVALID; - } - - if ((ret = PianoXmlIsFault (*xmlDoc)) != PIANO_RET_OK) { - ezxml_free (*xmlDoc); - return ret; - } - - return PIANO_RET_OK; -} - -/* get text from nodes; some of them have , - * or subnodes, just ignore them - * @param xml node - */ -static char *PianoXmlGetNodeText (const ezxml_t node) { - char *retTxt = NULL; - - retTxt = ezxml_txt (node); - /* no text => empty string */ - if (*retTxt == '\0') { - retTxt = ezxml_txt (node->child); - } - return retTxt; -} - -/* structParser callback; writes userinfo to PianoUserInfo structure - * @param value identifier - * @param value node - * @param pointer to userinfo structure - * @return nothing - */ -static void PianoXmlParseUserinfoCb (const char *key, const ezxml_t value, - void *data) { - PianoUserInfo_t *user = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("webAuthToken", key) == 0) { - user->webAuthToken = strdup (valueStr); - } else if (strcmp ("authToken", key) == 0) { - user->authToken = strdup (valueStr); - } else if (strcmp ("listenerId", key) == 0) { - user->listenerId = strdup (valueStr); - } -} - -static void PianoXmlParseStationsCb (const char *key, const ezxml_t value, - void *data) { - PianoStation_t *station = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("stationName", key) == 0) { - station->name = strdup (valueStr); - } else if (strcmp ("stationId", key) == 0) { - station->id = strdup (valueStr); - } else if (strcmp ("isQuickMix", key) == 0) { - station->isQuickMix = (strcmp (valueStr, "1") == 0); - } else if (strcmp ("isCreator", key) == 0) { - station->isCreator = (strcmp (valueStr, "1") == 0); - } -} - -static void PianoXmlParsePlaylistCb (const char *key, const ezxml_t value, - void *data) { - PianoSong_t *song = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("audioURL", key) == 0) { - /* last 48 chars of audioUrl are encrypted, but they put the key - * into the door's lock... */ - const char urlTailN = 48; - const size_t valueStrN = strlen (valueStr); - char *urlTail = NULL, - *urlTailCrypted = &valueStr[valueStrN - urlTailN]; - - /* don't try to decrypt if string is too short (=> invalid memory - * reads/writes) */ - if (valueStrN > urlTailN && - (urlTail = PianoDecryptString (urlTailCrypted)) != NULL) { - if ((song->audioUrl = calloc (valueStrN + 1, - sizeof (*song->audioUrl))) != NULL) { - memcpy (song->audioUrl, valueStr, valueStrN - urlTailN); - /* FIXME: the key seems to be broken... so ignore 8 x 0x08 - * postfix; urlTailN/2 because the encrypted hex string is now - * decoded */ - memcpy (&song->audioUrl[valueStrN - urlTailN], urlTail, - urlTailN/2 - 8); - } - free (urlTail); - } - } else if (strcmp ("artRadio", key) == 0) { - song->coverArt = strdup (valueStr); - } else if (strcmp ("artistSummary", key) == 0) { - song->artist = strdup (valueStr); - } else if (strcmp ("musicId", key) == 0) { - song->musicId = strdup (valueStr); - } else if (strcmp ("userSeed", key) == 0) { - song->userSeed = strdup (valueStr); - } else if (strcmp ("songTitle", key) == 0) { - song->title = strdup (valueStr); - } else if (strcmp ("rating", key) == 0) { - if (strcmp (valueStr, "1") == 0) { - song->rating = PIANO_RATE_LOVE; - } else { - song->rating = PIANO_RATE_NONE; - } - } else if (strcmp ("stationId", key) == 0) { - song->stationId = strdup (valueStr); - } else if (strcmp ("albumTitle", key) == 0) { - song->album = strdup (valueStr); - } else if (strcmp ("fileGain", key) == 0) { - song->fileGain = atof (valueStr); - } else if (strcmp ("audioEncoding", key) == 0) { - if (strcmp (valueStr, "aacplus") == 0) { - song->audioFormat = PIANO_AF_AACPLUS; - } else if (strcmp (valueStr, "mp3") == 0) { - song->audioFormat = PIANO_AF_MP3; - } else if (strcmp (valueStr, "mp3-hifi") == 0) { - song->audioFormat = PIANO_AF_MP3_HI; - } - } else if (strcmp ("artistMusicId", key) == 0) { - song->artistMusicId = strdup (valueStr); - } else if (strcmp ("testStrategy", key) == 0) { - song->testStrategy = atoi (valueStr); - } else if (strcmp ("songType", key) == 0) { - song->songType = atoi (valueStr); - } -} - -/* parses userinfos sent by pandora as login response - * @param piano handle - * @param utf-8 string - * @return _RET_OK or error - */ -PianoReturn_t PianoXmlParseUserinfo (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, structNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - /* */ - structNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - PianoXmlStructParser (structNode, PianoXmlParseUserinfoCb, &ph->user); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -static void PianoXmlParseQuickMixStationsCb (const char *key, const ezxml_t value, - void *data) { - char ***retIds = data; - char **ids = NULL; - size_t idsN = 0; - ezxml_t curNode; - - if (strcmp ("quickMixStationIds", key) == 0) { - for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); - curNode; curNode = curNode->next) { - idsN++; - if (ids == NULL) { - if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } else { - /* FIXME: memory leak (on failure) */ - if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } - ids[idsN-1] = strdup (PianoXmlGetNodeText (curNode)); - } - /* append NULL: list ends here */ - idsN++; - /* FIXME: copy&waste */ - if (ids == NULL) { - if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } else { - if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } - ids[idsN-1] = NULL; - } - *retIds = ids; -} - -/* parse stations returned by pandora - * @param piano handle - * @param xml returned by pandora - * @return _RET_OK or error - */ -PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - char **quickMixIds = NULL, **curQuickMixId = NULL; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "array", - 0, "data", -1); - - for (dataNode = ezxml_child (dataNode, "value"); dataNode; - dataNode = dataNode->next) { - PianoStation_t *tmpStation; - - if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - PianoXmlStructParser (ezxml_child (dataNode, "struct"), - PianoXmlParseStationsCb, tmpStation); - - /* get stations selected for quickmix */ - if (tmpStation->isQuickMix) { - PianoXmlStructParser (ezxml_child (dataNode, "struct"), - PianoXmlParseQuickMixStationsCb, &quickMixIds); - } - /* start new linked list or append */ - if (ph->stations == NULL) { - ph->stations = tmpStation; - } else { - PianoStation_t *curStation = ph->stations; - while (curStation->next != NULL) { - curStation = curStation->next; - } - curStation->next = tmpStation; - } - } - /* set quickmix flags after all stations are read */ - if (quickMixIds != NULL) { - curQuickMixId = quickMixIds; - while (*curQuickMixId != NULL) { - PianoStation_t *curStation = PianoFindStationById (ph->stations, - *curQuickMixId); - if (curStation != NULL) { - curStation->useQuickMix = 1; - } - free (*curQuickMixId); - curQuickMixId++; - } - free (quickMixIds); - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parse "create station" answer (it returns a new station structure) - * @param piano handle - * @param xml document - * @return nothing yet - */ -PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, dataNode; - PianoStation_t *tmpStation; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - - if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, tmpStation); - /* FIXME: copy & waste */ - /* start new linked list or append */ - if (ph->stations == NULL) { - ph->stations = tmpStation; - } else { - PianoStation_t *curStation = ph->stations; - while (curStation->next != NULL) { - curStation = curStation->next; - } - curStation->next = tmpStation; - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parse "add seed" answer, nearly the same as ParseCreateStation - * @param piano handle - * @param xml document - * @param update this station - */ -PianoReturn_t PianoXmlParseAddSeed (PianoHandle_t *ph, char *xml, - PianoStation_t *station) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - PianoDestroyStation (station); - PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, station); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parses playlist; used when searching too - * @param piano handle - * @param xml document - * @param return: playlist - */ -PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml, - PianoSong_t **retPlaylist) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "array", - 0, "data", -1); - - for (dataNode = ezxml_child (dataNode, "value"); dataNode; - dataNode = dataNode->next) { - PianoSong_t *tmpSong; - - if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - PianoXmlStructParser (ezxml_child (dataNode, "struct"), - PianoXmlParsePlaylistCb, tmpSong); - /* begin linked list or append */ - if (*retPlaylist == NULL) { - *retPlaylist = tmpSong; - } else { - PianoSong_t *curSong = *retPlaylist; - while (curSong->next != NULL) { - curSong = curSong->next; - } - curSong->next = tmpSong; - } - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parse simple answers like this: - * 1 - * - * @param xml string - * @return - */ -PianoReturn_t PianoXmlParseSimple (char *xml) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); - - if (strcmp (ezxml_txt (dataNode), "1") == 0) { - ret = PIANO_RET_OK; - } else { - ret = PIANO_RET_ERR; - } - - ezxml_free (xmlDoc); - - return ret; -} - -/* xml struct parser callback, used in PianoXmlParseSearchCb - */ -static void PianoXmlParseSearchArtistCb (const char *key, const ezxml_t value, - void *data) { - PianoArtist_t *artist = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("artistName", key) == 0) { - artist->name = strdup (valueStr); - } else if (strcmp ("musicId", key) == 0) { - artist->musicId = strdup (valueStr); - } -} - -/* callback for xml struct parser used in PianoXmlParseSearch, "switch" for - * PianoXmlParseSearchArtistCb and PianoXmlParsePlaylistCb - */ -static void PianoXmlParseSearchCb (const char *key, const ezxml_t value, - void *data) { - PianoSearchResult_t *searchResult = data; - ezxml_t curNode; - - if (strcmp ("artists", key) == 0) { - /* skip */ - for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); - curNode; curNode = curNode->next) { - PianoArtist_t *artist; - - if ((artist = calloc (1, sizeof (*artist))) == NULL) { - /* fail silently */ - break; - } - - memset (artist, 0, sizeof (*artist)); - - PianoXmlStructParser (ezxml_child (curNode, "struct"), - PianoXmlParseSearchArtistCb, artist); - - /* add result to linked list */ - if (searchResult->artists == NULL) { - searchResult->artists = artist; - } else { - PianoArtist_t *curArtist = searchResult->artists; - while (curArtist->next != NULL) { - curArtist = curArtist->next; - } - curArtist->next = artist; - } - } - } else if (strcmp ("songs", key) == 0) { - for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); - curNode; curNode = curNode->next) { - /* FIXME: copy & waste */ - PianoSong_t *tmpSong; - - if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { - /* fail silently */ - break; - } - - PianoXmlStructParser (ezxml_child (curNode, "struct"), - PianoXmlParsePlaylistCb, tmpSong); - /* begin linked list or append */ - if (searchResult->songs == NULL) { - searchResult->songs = tmpSong; - } else { - PianoSong_t *curSong = searchResult->songs; - while (curSong->next != NULL) { - curSong = curSong->next; - } - curSong->next = tmpSong; - } - } - } -} - -/* parse search result; searchResult is nulled before use - * @param xml document - * @param returns search result - * @return nothing yet - */ -PianoReturn_t PianoXmlParseSearch (char *xml, - PianoSearchResult_t *searchResult) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - /* we need a "clean" search result (with null pointers) */ - memset (searchResult, 0, sizeof (*searchResult)); - PianoXmlStructParser (dataNode, PianoXmlParseSearchCb, searchResult); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* FIXME: copy&waste (PianoXmlParseSearch) - */ -PianoReturn_t PianoXmlParseSeedSuggestions (char *xml, - PianoSearchResult_t *searchResult) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); - /* we need a "clean" search result (with null pointers) */ - memset (searchResult, 0, sizeof (*searchResult)); - /* reuse seach result parser; structure is nearly the same */ - PianoXmlParseSearchCb ("artists", dataNode, searchResult); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* encode reserved xml chars - * TODO: remove and use ezxml_ampencode - * @param encode this - * @return encoded string or NULL - */ -char *PianoXmlEncodeString (const char *s) { - char *replacements[] = {"&&", "''", "\""", "<<", - ">>", NULL}; - char **r, *sOut, *sOutCurr, found; - - if ((sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut))) == NULL) { - return NULL; - } - - sOutCurr = sOut; - - while (*s != '\0') { - r = replacements; - found = 0; - while (*r != NULL) { - if (*s == *r[0]) { - found = 1; - strcat (sOutCurr, (*r) + 1); - sOutCurr += strlen ((*r) + 1); - break; - } - r++; - } - if (!found) { - *sOutCurr = *s; - sOutCurr++; - } - s++; - } - return sOut; -} - -PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, catNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - /* get all nodes */ - for (catNode = ezxml_child (xmlDoc, "category"); catNode; - catNode = catNode->next) { - PianoGenreCategory_t *tmpGenreCategory; - ezxml_t genreNode; - - if ((tmpGenreCategory = calloc (1, sizeof (*tmpGenreCategory))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - tmpGenreCategory->name = strdup (ezxml_attr (catNode, "categoryName")); - - /* get genre subnodes */ - for (genreNode = ezxml_child (catNode, "genre"); genreNode; - genreNode = genreNode->next) { - PianoGenre_t *tmpGenre; - - if ((tmpGenre = calloc (1, sizeof (*tmpGenre))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - /* get genre attributes */ - tmpGenre->name = strdup (ezxml_attr (genreNode, "name")); - tmpGenre->musicId = strdup (ezxml_attr (genreNode, "musicId")); - - /* append station */ - if (tmpGenreCategory->genres == NULL) { - tmpGenreCategory->genres = tmpGenre; - } else { - PianoGenre_t *curGenre = - tmpGenreCategory->genres; - while (curGenre->next != NULL) { - curGenre = curGenre->next; - } - curGenre->next = tmpGenre; - } - } - /* append category */ - if (ph->genreStations == NULL) { - ph->genreStations = tmpGenreCategory; - } else { - PianoGenreCategory_t *curCat = ph->genreStations; - while (curCat->next != NULL) { - curCat = curCat->next; - } - curCat->next = tmpGenreCategory; - } - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* dummy function, only checks for errors - * @param xml doc - * @return _OK or error - */ -PianoReturn_t PianoXmlParseTranformStation (char *xml) { - ezxml_t xmlDoc; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parses "why did you play ...?" answer - * @param xml - * @param returns the answer - * @return _OK or error - */ -PianoReturn_t PianoXmlParseNarrative (char *xml, char **retNarrative) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - /* $textnode */ - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); - *retNarrative = strdup (ezxml_txt (dataNode)); - - ezxml_free (xmlDoc); - - return ret; -} - diff --git a/libpiano/src/xml.h b/libpiano/src/xml.h deleted file mode 100644 index dc778ca..0000000 --- a/libpiano/src/xml.h +++ /dev/null @@ -1,48 +0,0 @@ -/* -Copyright (c) 2008-2010 - Lars-Dominik Braun - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -#ifndef _XML_H -#define _XML_H - -#include "piano.h" - -PianoReturn_t PianoXmlParseUserinfo (PianoHandle_t *ph, const char *xml); -PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, const char *xml); -PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, const char *xml, - PianoSong_t **); -PianoReturn_t PianoXmlParseSearch (const char *searchXml, - PianoSearchResult_t *searchResult); -PianoReturn_t PianoXmlParseSimple (const char *xml); -PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, - const char *xml); -PianoReturn_t PianoXmlParseAddSeed (PianoHandle_t *ph, const char *xml, - PianoStation_t *station); -PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, - const char *xmlContent); -PianoReturn_t PianoXmlParseTranformStation (const char *searchXml); -PianoReturn_t PianoXmlParseNarrative (const char *xml, char **retNarrative); -PianoReturn_t PianoXmlParseSeedSuggestions (char *, PianoSearchResult_t *); - -char *PianoXmlEncodeString (const char *s); - -#endif /* _XML_H */ diff --git a/libwaitress/AUTHORS b/libwaitress/AUTHORS deleted file mode 100644 index 09787cf..0000000 --- a/libwaitress/AUTHORS +++ /dev/null @@ -1,2 +0,0 @@ -Main code: - Lars-Dominik Braun diff --git a/libwaitress/COPYING b/libwaitress/COPYING deleted file mode 100644 index 2df3345..0000000 --- a/libwaitress/COPYING +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2009-2010 - Lars-Dominik Braun - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/libwaitress/README b/libwaitress/README deleted file mode 100644 index 83b2d02..0000000 --- a/libwaitress/README +++ /dev/null @@ -1,4 +0,0 @@ -README -====== - -libwaitress is pianobar's http communication library. diff --git a/libwaitress/src/config.h b/libwaitress/src/config.h deleted file mode 100644 index e13fe40..0000000 --- a/libwaitress/src/config.h +++ /dev/null @@ -1 +0,0 @@ -#define PACKAGE "libwaitress" diff --git a/libwaitress/src/waitress.c b/libwaitress/src/waitress.c deleted file mode 100644 index 27f7b3d..0000000 --- a/libwaitress/src/waitress.c +++ /dev/null @@ -1,573 +0,0 @@ -/* -Copyright (c) 2009-2010 - Lars-Dominik Braun - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -#define _POSIX_C_SOURCE 1 /* required by getaddrinfo() */ -#define _BSD_SOURCE /* snprintf() */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" -#include "waitress.h" - -typedef struct { - char *buf; - size_t pos; -} WaitressFetchBufCbBuffer_t; - -inline void WaitressInit (WaitressHandle_t *waith) { - memset (waith, 0, sizeof (*waith)); - waith->socktimeout = 30000; -} - -inline void WaitressFree (WaitressHandle_t *waith) { - memset (waith, 0, sizeof (*waith)); -} - -/* Proxy set up? - * @param Waitress handle - * @return true|false - */ -static inline char WaitressProxyEnabled (const WaitressHandle_t *waith) { - return *waith->proxyHost != '\0' && *waith->proxyPort != '\0'; -} - -/* Set http proxy - * @param waitress handle - * @param host - * @param port - */ -inline void WaitressSetProxy (WaitressHandle_t *waith, const char *host, - const char *port) { - strncpy (waith->proxyHost, host, sizeof (waith->proxyHost)-1); - strncpy (waith->proxyPort, port, sizeof (waith->proxyPort)-1); -} - -/* urlencode post-data - * @param encode this - * @return malloc'ed encoded string, don't forget to free it - */ -char *WaitressUrlEncode (const char *in) { - size_t inLen = strlen (in); - /* worst case: encode all characters */ - char *out = calloc (inLen * 3 + 1, sizeof (*in)); - const char *inPos = in; - char *outPos = out; - - while (inPos - in < inLen) { - if (!isalnum (*inPos) && *inPos != '_' && *inPos != '-' && *inPos != '.') { - *outPos++ = '%'; - snprintf (outPos, 3, "%02x", *inPos & 0xff); - outPos += 2; - } else { - /* copy character */ - *outPos++ = *inPos; - } - ++inPos; - } - - return out; -} - -/* Split http url into host, port and path - * @param url - * @param return buffer: host - * @param host buffer size - * @param return buffer: port, defaults to 80 - * @param port buffer size - * @param return buffer: path - * @param path buffer size - * @param 1 = ok, 0 = not a http url; if your buffers are too small horrible - * things will happen... But 1 is returned anyway. - */ -char WaitressSplitUrl (const char *url, char *retHost, size_t retHostSize, - char *retPort, size_t retPortSize, char *retPath, size_t retPathSize) { - size_t urlSize = strlen (url); - const char *urlPos = url, *lastPos; - - if (urlSize > sizeof ("http://")-1 && - memcmp (url, "http://", sizeof ("http://")-1) == 0) { - memset (retHost, 0, retHostSize); - memset (retPort, 0, retPortSize); - strncpy (retPort, "80", retPortSize-1); - memset (retPath, 0, retPathSize); - - urlPos += sizeof ("http://")-1; - lastPos = urlPos; - - /* find host */ - while (*urlPos != '\0' && *urlPos != ':' && *urlPos != '/' && - urlPos - lastPos < retHostSize-1) { - *retHost++ = *urlPos++; - } - lastPos = urlPos; - - /* port, if available */ - if (*urlPos == ':') { - /* skip : */ - ++urlPos; - ++lastPos; - while (*urlPos != '\0' && *urlPos != '/' && - urlPos - lastPos < retPortSize-1) { - *retPort++ = *urlPos++; - } - } - lastPos = urlPos; - - /* path */ - while (*urlPos != '\0' && *urlPos != '#' && - urlPos - lastPos < retPathSize-1) { - *retPath++ = *urlPos++; - } - } else { - return 0; - } - return 1; -} - -/* Parse url and set host, port, path - * @param Waitress handle - * @param url: protocol://host:port/path - */ -inline char WaitressSetUrl (WaitressHandle_t *waith, const char *url) { - return WaitressSplitUrl (url, waith->host, sizeof (waith->host), - waith->port, sizeof (waith->port), waith->path, sizeof (waith->path)); -} - -/* Set host, port, path - * @param Waitress handle - * @param host - * @param port (getaddrinfo () needs a string...) - * @param path, including leading / - */ -inline void WaitressSetHPP (WaitressHandle_t *waith, const char *host, - const char *port, const char *path) { - strncpy (waith->host, host, sizeof (waith->host)-1); - strncpy (waith->port, port, sizeof (waith->port)-1); - strncpy (waith->path, path, sizeof (waith->path)-1); -} - -/* Callback for WaitressFetchBuf, appends received data to NULL-terminated buffer - * @param received data - * @param data size - * @param buffer structure - */ -static WaitressCbReturn_t WaitressFetchBufCb (void *recvData, size_t recvDataSize, - void *extraData) { - char *recvBytes = recvData; - WaitressFetchBufCbBuffer_t *buffer = extraData; - - if (buffer->buf == NULL) { - if ((buffer->buf = malloc (sizeof (*buffer->buf) * - (recvDataSize + 1))) == NULL) { - return WAITRESS_CB_RET_ERR; - } - } else { - char *newbuf; - if ((newbuf = realloc (buffer->buf, - sizeof (*buffer->buf) * - (buffer->pos + recvDataSize + 1))) == NULL) { - free (buffer->buf); - return WAITRESS_CB_RET_ERR; - } - buffer->buf = newbuf; - } - memcpy (buffer->buf + buffer->pos, recvBytes, recvDataSize); - buffer->pos += recvDataSize; - *(buffer->buf+buffer->pos) = '\0'; - - return WAITRESS_CB_RET_OK; -} - -/* Fetch string. Beware! This overwrites your waith->data pointer - * @param waitress handle - * @param result buffer, malloced (don't forget to free it yourself) - */ -WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char **buf) { - WaitressFetchBufCbBuffer_t buffer; - WaitressReturn_t wRet; - - memset (&buffer, 0, sizeof (buffer)); - - waith->data = &buffer; - waith->callback = WaitressFetchBufCb; - - wRet = WaitressFetchCall (waith); - *buf = buffer.buf; - return wRet; -} - -/* poll wrapper that retries after signal interrupts, required for socksify - * wrapper - */ -static int WaitressPollLoop (struct pollfd *fds, nfds_t nfds, int timeout) { - int pollres = -1; - int pollerr = 0; - - do { - pollres = poll (fds, nfds, timeout); - pollerr = errno; - errno = 0; - } while (pollerr == EINTR || pollerr == EINPROGRESS || pollerr == EAGAIN); - - return pollres; -} - -/* write () wrapper with poll () timeout - * @param socket fd - * @param write buffer - * @param write count bytes - * @param reuse existing pollfd structure - * @param timeout (microseconds) - * @return WAITRESS_RET_OK, WAITRESS_RET_TIMEOUT or WAITRESS_RET_ERR - */ -static WaitressReturn_t WaitressPollWrite (int sockfd, const char *buf, size_t count, - struct pollfd *sockpoll, int timeout) { - int pollres = -1; - - sockpoll->events = POLLOUT; - pollres = WaitressPollLoop (sockpoll, 1, timeout); - if (pollres == 0) { - return WAITRESS_RET_TIMEOUT; - } else if (pollres == -1) { - return WAITRESS_RET_ERR; - } - if (write (sockfd, buf, count) == -1) { - return WAITRESS_RET_ERR; - } - return WAITRESS_RET_OK; -} - -/* read () wrapper with poll () timeout - * @param socket fd - * @param write to this buf, not NULL terminated - * @param buffer size - * @param reuse existing pollfd struct - * @param timeout (in microseconds) - * @param read () return value/written bytes - * @return WAITRESS_RET_OK, WAITRESS_RET_TIMEOUT, WAITRESS_RET_ERR - */ -static WaitressReturn_t WaitressPollRead (int sockfd, char *buf, size_t count, - struct pollfd *sockpoll, int timeout, ssize_t *retSize) { - int pollres = -1; - - sockpoll->events = POLLIN; - pollres = WaitressPollLoop (sockpoll, 1, timeout); - if (pollres == 0) { - return WAITRESS_RET_TIMEOUT; - } else if (pollres == -1) { - return WAITRESS_RET_ERR; - } - if ((*retSize = read (sockfd, buf, count)) == -1) { - return WAITRESS_RET_READ_ERR; - } - return WAITRESS_RET_OK; -} - -/* FIXME: compiler macros are ugly... */ -#define CLOSE_RET(ret) close (sockfd); return ret; -#define WRITE_RET(buf, count) \ - if ((wRet = WaitressPollWrite (sockfd, buf, count, \ - &sockpoll, waith->socktimeout)) != WAITRESS_RET_OK) { \ - CLOSE_RET (wRet); \ - } -#define READ_RET(buf, count, size) \ - if ((wRet = WaitressPollRead (sockfd, buf, count, \ - &sockpoll, waith->socktimeout, size)) != WAITRESS_RET_OK) { \ - CLOSE_RET (wRet); \ - } - -/* Receive data from host and call *callback () - * @param waitress handle - * @return WaitressReturn_t - */ -WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) { - struct addrinfo hints, *res; - int sockfd; - char recvBuf[WAITRESS_RECV_BUFFER]; - char writeBuf[2*1024]; - ssize_t recvSize = 0; - WaitressReturn_t wRet = WAITRESS_RET_OK; - struct pollfd sockpoll; - int pollres; - /* header parser vars */ - char *nextLine = NULL, *thisLine = NULL; - enum {HDRM_HEAD, HDRM_LINES, HDRM_FINISHED} hdrParseMode = HDRM_HEAD; - char statusCode[3], val[256]; - unsigned int bufFilled = 0; - - /* initialize */ - waith->contentLength = 0; - waith->contentReceived = 0; - memset (&hints, 0, sizeof hints); - - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - - /* Use proxy? */ - if (WaitressProxyEnabled (waith)) { - if (getaddrinfo (waith->proxyHost, waith->proxyPort, &hints, &res) != 0) { - return WAITRESS_RET_GETADDR_ERR; - } - } else { - if (getaddrinfo (waith->host, waith->port, &hints, &res) != 0) { - return WAITRESS_RET_GETADDR_ERR; - } - } - - if ((sockfd = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { - freeaddrinfo (res); - return WAITRESS_RET_SOCK_ERR; - } - sockpoll.fd = sockfd; - - /* we need shorter timeouts for connect() */ - fcntl (sockfd, F_SETFL, O_NONBLOCK); - - /* increase socket receive buffer */ - const int sockopt = 256*1024; - setsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof (sockopt)); - - /* non-blocking connect will return immediately */ - connect (sockfd, res->ai_addr, res->ai_addrlen); - - sockpoll.events = POLLOUT; - pollres = WaitressPollLoop (&sockpoll, 1, waith->socktimeout); - freeaddrinfo (res); - if (pollres == 0) { - return WAITRESS_RET_TIMEOUT; - } else if (pollres == -1) { - return WAITRESS_RET_ERR; - } - /* check connect () return value */ - socklen_t pollresSize = sizeof (pollres); - getsockopt (sockfd, SOL_SOCKET, SO_ERROR, &pollres, &pollresSize); - if (pollres != 0) { - return WAITRESS_RET_CONNECT_REFUSED; - } - - /* send request */ - if (WaitressProxyEnabled (waith)) { - snprintf (writeBuf, sizeof (writeBuf), - "%s http://%s:%s%s HTTP/1.0\r\n", - (waith->method == WAITRESS_METHOD_GET ? "GET" : "POST"), - waith->host, waith->port, waith->path); - } else { - snprintf (writeBuf, sizeof (writeBuf), - "%s %s HTTP/1.0\r\n", - (waith->method == WAITRESS_METHOD_GET ? "GET" : "POST"), - waith->path); - } - WRITE_RET (writeBuf, strlen (writeBuf)); - - snprintf (writeBuf, sizeof (writeBuf), - "Host: %s\r\nUser-Agent: " PACKAGE "\r\n", waith->host); - WRITE_RET (writeBuf, strlen (writeBuf)); - - if (waith->method == WAITRESS_METHOD_POST && waith->postData != NULL) { - snprintf (writeBuf, sizeof (writeBuf), "Content-Length: %zu\r\n", - strlen (waith->postData)); - WRITE_RET (writeBuf, strlen (writeBuf)); - } - - if (waith->extraHeaders != NULL) { - WRITE_RET (waith->extraHeaders, strlen (waith->extraHeaders)); - } - - WRITE_RET ("\r\n", 2); - - if (waith->method == WAITRESS_METHOD_POST && waith->postData != NULL) { - WRITE_RET (waith->postData, strlen (waith->postData)); - } - - /* receive answer */ - nextLine = recvBuf; - while (hdrParseMode != HDRM_FINISHED) { - READ_RET (recvBuf+bufFilled, sizeof (recvBuf)-1 - bufFilled, &recvSize); - if (recvSize == 0) { - /* connection closed too early */ - CLOSE_RET (WAITRESS_RET_CONNECTION_CLOSED); - } - bufFilled += recvSize; - memset (recvBuf+bufFilled, 0, sizeof (recvBuf) - bufFilled); - thisLine = recvBuf; - - /* split */ - while ((nextLine = strchr (thisLine, '\n')) != NULL && - hdrParseMode != HDRM_FINISHED) { - /* make lines parseable by string routines */ - *nextLine = '\0'; - if (*(nextLine-1) == '\r') { - *(nextLine-1) = '\0'; - } - /* skip \0 */ - ++nextLine; - - 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 { - CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN); - } - hdrParseMode = HDRM_LINES; - } /* endif */ - break; - - /* Everything else, except status code */ - case HDRM_LINES: - /* empty line => content starts here */ - if (*thisLine == '\0') { - hdrParseMode = HDRM_FINISHED; - } else { - memset (val, 0, sizeof (val)); - if (sscanf (thisLine, "Content-Length: %255c", val) == 1) { - waith->contentLength = atol (val); - } - } - break; - - default: - break; - } /* end switch */ - thisLine = nextLine; - } /* end while strchr */ - memmove (recvBuf, thisLine, thisLine-recvBuf); - bufFilled -= (thisLine-recvBuf); - } /* end while hdrParseMode */ - - /* push remaining bytes */ - if (bufFilled > 0) { - waith->contentReceived += bufFilled; - if (waith->callback (thisLine, bufFilled, waith->data) == - WAITRESS_CB_RET_ERR) { - CLOSE_RET (WAITRESS_RET_CB_ABORT); - } - } - - /* receive content */ - do { - READ_RET (recvBuf, sizeof (recvBuf), &recvSize); - if (recvSize > 0) { - waith->contentReceived += recvSize; - if (waith->callback (recvBuf, recvSize, waith->data) == - WAITRESS_CB_RET_ERR) { - wRet = WAITRESS_RET_CB_ABORT; - break; - } - } - } while (recvSize > 0); - - close (sockfd); - - if (wRet == WAITRESS_RET_OK && waith->contentReceived < waith->contentLength) { - return WAITRESS_RET_PARTIAL_FILE; - } - return wRet; -} - -#undef CLOSE_RET -#undef WRITE_RET -#undef READ_RET - -const char *WaitressErrorToStr (WaitressReturn_t wRet) { - switch (wRet) { - case WAITRESS_RET_OK: - return "Everything's fine :)"; - break; - - case WAITRESS_RET_ERR: - return "Unknown."; - break; - - case WAITRESS_RET_STATUS_UNKNOWN: - return "Unknown HTTP status code."; - break; - - case WAITRESS_RET_NOTFOUND: - return "File not found."; - break; - - case WAITRESS_RET_FORBIDDEN: - return "Forbidden."; - break; - - case WAITRESS_RET_CONNECT_REFUSED: - return "Connection refused."; - break; - - case WAITRESS_RET_SOCK_ERR: - return "Socket error."; - break; - - case WAITRESS_RET_GETADDR_ERR: - return "getaddr failed."; - break; - - case WAITRESS_RET_CB_ABORT: - return "Callback aborted request."; - break; - - case WAITRESS_RET_HDR_OVERFLOW: - return "HTTP header overflow."; - break; - - case WAITRESS_RET_PARTIAL_FILE: - return "Partial file."; - break; - - case WAITRESS_RET_TIMEOUT: - return "Timeout."; - break; - - case WAITRESS_RET_READ_ERR: - return "Read error."; - break; - - case WAITRESS_RET_CONNECTION_CLOSED: - return "Connection closed by remote host."; - break; - - default: - return "No error message available."; - break; - } -} - diff --git a/libwaitress/src/waitress.h b/libwaitress/src/waitress.h deleted file mode 100644 index 04333fb..0000000 --- a/libwaitress/src/waitress.h +++ /dev/null @@ -1,78 +0,0 @@ -/* -Copyright (c) 2009-2010 - Lars-Dominik Braun - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -#ifndef _WAITRESS_H -#define _WAITRESS_H - -#include - -#define WAITRESS_HOST_SIZE 100 -/* max: 65,535 */ -#define WAITRESS_PORT_SIZE 6 -#define WAITRESS_PATH_SIZE 1000 -#define WAITRESS_RECV_BUFFER 10*1024 - -typedef enum {WAITRESS_METHOD_GET = 0, WAITRESS_METHOD_POST} WaitressMethod_t; - -typedef enum {WAITRESS_CB_RET_ERR, WAITRESS_CB_RET_OK} WaitressCbReturn_t; - -typedef struct { - char host[WAITRESS_HOST_SIZE]; - char port[WAITRESS_PORT_SIZE]; - char path[WAITRESS_PATH_SIZE]; - WaitressMethod_t method; - const char *extraHeaders; - const char *postData; - size_t contentLength; - size_t contentReceived; - char proxyHost[WAITRESS_HOST_SIZE]; - char proxyPort[WAITRESS_PORT_SIZE]; - /* extra data handed over to callback function */ - void *data; - WaitressCbReturn_t (*callback) (void *, size_t, void *); - int socktimeout; -} WaitressHandle_t; - -typedef enum {WAITRESS_RET_ERR = 0, WAITRESS_RET_OK, WAITRESS_RET_STATUS_UNKNOWN, - WAITRESS_RET_NOTFOUND, WAITRESS_RET_FORBIDDEN, WAITRESS_RET_CONNECT_REFUSED, - WAITRESS_RET_SOCK_ERR, WAITRESS_RET_GETADDR_ERR, - WAITRESS_RET_CB_ABORT, WAITRESS_RET_HDR_OVERFLOW, - WAITRESS_RET_PARTIAL_FILE, WAITRESS_RET_TIMEOUT, WAITRESS_RET_READ_ERR, - WAITRESS_RET_CONNECTION_CLOSED -} WaitressReturn_t; - -void WaitressInit (WaitressHandle_t *); -void WaitressFree (WaitressHandle_t *); -void WaitressSetProxy (WaitressHandle_t *, const char *, const char *); -char *WaitressUrlEncode (const char *); -char WaitressSplitUrl (const char *, char *, size_t, char *, size_t, char *, - size_t); -char WaitressSetUrl (WaitressHandle_t *, const char *); -void WaitressSetHPP (WaitressHandle_t *, const char *, const char *, - const char *); -WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *, char **); -WaitressReturn_t WaitressFetchCall (WaitressHandle_t *); -const char *WaitressErrorToStr (WaitressReturn_t); - -#endif /* _WAITRESS_H */ - diff --git a/src/libezxml/COPYING b/src/libezxml/COPYING new file mode 100644 index 0000000..80e4e88 --- /dev/null +++ b/src/libezxml/COPYING @@ -0,0 +1,20 @@ +Copyright 2004-2006 Aaron Voisine + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/libezxml/ezxml.c b/src/libezxml/ezxml.c new file mode 100644 index 0000000..967d535 --- /dev/null +++ b/src/libezxml/ezxml.c @@ -0,0 +1,715 @@ +/* ezxml.c + * + * Copyright 2004-2006 Aaron Voisine + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#define _BSD_SOURCE /* required by strdup() */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "ezxml.h" + +#define EZXML_WS "\t\r\n " // whitespace +#define EZXML_ERRL 128 // maximum error string length + +typedef struct ezxml_root *ezxml_root_t; +struct ezxml_root { // additional data for the root tag + struct ezxml xml; // is a super-struct built on top of ezxml struct + ezxml_t cur; // current xml tree insertion point + char *m; // original xml string + size_t len; // length of allocated memory for mmap, -1 for malloc + char *u; // UTF-8 conversion of string if original was UTF-16 + char *s; // start of work area + char *e; // end of work area + char **ent; // general entities (ampersand sequences) + char ***attr; // default attributes + char ***pi; // processing instructions + short standalone; // non-zero if + char err[EZXML_ERRL]; // error string +}; + +char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings + +// sets a flag for the given tag and returns the tag +static ezxml_t ezxml_set_flag(ezxml_t xml, short flag) { + if (xml) xml->flags |= flag; + return xml; +} + +// inserts an existing tag into an ezxml structure +static ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off) +{ + ezxml_t cur, prev, head; + + xml->next = xml->sibling = xml->ordered = NULL; + xml->off = off; + xml->parent = dest; + + if ((head = dest->child)) { // already have sub tags + if (head->off <= off) { // not first subtag + for (cur = head; cur->ordered && cur->ordered->off <= off; + cur = cur->ordered); + xml->ordered = cur->ordered; + cur->ordered = xml; + } + else { // first subtag + xml->ordered = head; + dest->child = xml; + } + + for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name); + prev = cur, cur = cur->sibling); // find tag type + if (cur && cur->off <= off) { // not first of type + while (cur->next && cur->next->off <= off) cur = cur->next; + xml->next = cur->next; + cur->next = xml; + } + else { // first tag of this type + if (prev && cur) prev->sibling = cur->sibling; // remove old first + xml->next = cur; // old first tag is now next + for (cur = head, prev = NULL; cur && cur->off <= off; + prev = cur, cur = cur->sibling); // new sibling insert point + xml->sibling = cur; + if (prev) prev->sibling = xml; + } + } + else dest->child = xml; // only sub tag + + return xml; +} + +// Adds a child tag. off is the offset of the child tag relative to the start +// of the parent tag's character content. Returns the child tag. +static ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off) +{ + ezxml_t child; + + if (! xml) return NULL; + child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0', + sizeof(struct ezxml)); + child->name = (char *)name; + child->attr = EZXML_NIL; + child->txt = ""; + + return ezxml_insert(child, xml, off); +} + +// returns the first child tag with the given name or NULL if not found +ezxml_t ezxml_child(ezxml_t xml, const char *name) +{ + xml = (xml) ? xml->child : NULL; + while (xml && strcmp(name, xml->name)) xml = xml->sibling; + return xml; +} + +// returns a new empty ezxml structure with the given root tag name +static ezxml_t ezxml_new(const char *name) +{ + static char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """, + "apos;", "'", "amp;", "&", NULL }; + ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)), + '\0', sizeof(struct ezxml_root)); + root->xml.name = (char *)name; + root->cur = &root->xml; + strcpy(root->err, root->xml.txt = ""); + root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent)); + root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL); + return &root->xml; +} + +// returns the Nth tag with the same name in the same subsection or NULL if not +// found +ezxml_t ezxml_idx(ezxml_t xml, int idx) +{ + for (; xml && idx; idx--) xml = xml->next; + return xml; +} + +// returns the value of the requested tag attribute or NULL if not found +const char *ezxml_attr(ezxml_t xml, const char *attr) +{ + int i = 0, j = 1; + ezxml_root_t root = (ezxml_root_t)xml; + + if (! xml || ! xml->attr) return NULL; + while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2; + if (xml->attr[i]) return xml->attr[i + 1]; // found attribute + + while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag + for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++); + if (! root->attr[i]) return NULL; // no matching default attributes + while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3; + return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default +} + +// same as ezxml_get but takes an already initialized va_list +static ezxml_t ezxml_vget(ezxml_t xml, va_list ap) +{ + char *name = va_arg(ap, char *); + int idx = -1; + + if (name && *name) { + idx = va_arg(ap, int); + xml = ezxml_child(xml, name); + } + return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap); +} + +// Traverses the xml tree to retrieve a specific subtag. Takes a variable +// length list of tag names and indexes. The argument list must be terminated +// by either an index of -1 or an empty string tag name. Example: +// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1); +// This retrieves the title of the 3rd book on the 1st shelf of library. +// Returns NULL if not found. +ezxml_t ezxml_get(ezxml_t xml, ...) +{ + va_list ap; + ezxml_t r; + + va_start(ap, xml); + r = ezxml_vget(xml, ap); + va_end(ap); + return r; +} + +// set an error string and return root +static ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...) +{ + va_list ap; + int line = 1; + char *t, fmt[EZXML_ERRL]; + + for (t = root->s; t < s; t++) if (*t == '\n') line++; + snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err); + + va_start(ap, err); + vsnprintf(root->err, EZXML_ERRL, fmt, ap); + va_end(ap); + + return &root->xml; +} + +// Recursively decodes entity and character references and normalizes new lines +// ent is a null terminated array of alternating entity names and values. set t +// to '&' for general entity decoding, '%' for parameter entity decoding, 'c' +// for cdata sections, ' ' for attribute normalization, or '*' for non-cdata +// attribute normalization. Returns s, or if the decoded string is longer than +// s, returns a malloced string that must be freed. +static char *ezxml_decode(char *s, char **ent, char t) +{ + char *e, *r = s, *m = s; + long b, c, d, l; + + for (; *s; s++) { // normalize line endings + while (*s == '\r') { + *(s++) = '\n'; + if (*s == '\n') memmove(s, (s + 1), strlen(s)); + } + } + + for (s = r; ; ) { + while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++; + + if (! *s) break; + else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference + if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16 + else c = strtol(s + 2, &e, 10); // base 10 + if (! c || *e != ';') { s++; continue; } // not a character ref + + if (c < 0x80) *(s++) = c; // US-ASCII subset + else { // multi-byte UTF-8 sequence + for (b = 0, d = c; d; d /= 2) b++; // number of bits in c + b = (b - 2) / 5; // number of bytes in payload + *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head + while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload + } + + memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';'))); + } + else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) || + (*s == '%' && t == '%')) { // entity reference + for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b])); + b += 2); // find entity in entity list + + if (ent[b++]) { // found a match + if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) { + l = (d = (s - r)) + c + strlen(e); // new length + r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l); + e = strchr((s = r + d), ';'); // fix up pointers + } + + memmove(s + c, e + 1, strlen(e)); // shift rest of string + strncpy(s, ent[b], c); // copy in replacement text + } + else s++; // not a known entity + } + else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' '; + else s++; // no decoding needed + } + + if (t == '*') { // normalize spaces for non-cdata attributes + for (s = r; *s; s++) { + if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1); + while (*s && *s != ' ') s++; + } + if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space + } + return r; +} + +// called when parser finds start of new tag +static void ezxml_open_tag(ezxml_root_t root, char *name, char **attr) +{ + ezxml_t xml = root->cur; + + if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt)); + else xml->name = name; // first open tag + + xml->attr = attr; + root->cur = xml; // update tag insertion point +} + +// called when parser finds character content between open and closing tag +static void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t) +{ + ezxml_t xml = root->cur; + char *m = s; + size_t l; + + if (! xml || ! xml->name || ! len) return; // sanity check + + s[len] = '\0'; // null terminate text (calling functions anticipate this) + len = strlen(s = ezxml_decode(s, root->ent, t)) + 1; + + if (! *(xml->txt)) xml->txt = s; // initial character content + else { // allocate our own memory and make a copy + xml->txt = (xml->flags & EZXML_TXTM) // allocate some space + ? realloc(xml->txt, (l = strlen(xml->txt)) + len) + : strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt); + strcpy(xml->txt + l, s); // add new char content + if (s != m) free(s); // free s if it was malloced by ezxml_decode() + } + + if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM); +} + +// called when parser finds closing tag +static ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s) +{ + if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name)) + return ezxml_err(root, s, "unexpected closing tag ", name); + + root->cur = root->cur->parent; + return NULL; +} + +// checks for circular entity references, returns non-zero if no circular +// references are found, zero otherwise +static int ezxml_ent_ok(char *name, char *s, char **ent) +{ + int i; + + for (; ; s++) { + while (*s && *s != '&') s++; // find next entity reference + if (! *s) return 1; + if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref. + for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2); + if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0; + } +} + +// called when the parser finds a processing instruction +static void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len) +{ + int i = 0, j = 1; + char *target = s; + + s[len] = '\0'; // null terminate instruction + if (*(s += strcspn(s, EZXML_WS))) { + *s = '\0'; // null terminate target + s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target + } + + if (! strcmp(target, "xml")) { // + if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10, + EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1; + return; + } + + if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi + + while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target + if (! root->pi[i]) { // new target + root->pi = realloc(root->pi, sizeof(char **) * (i + 2)); + root->pi[i] = malloc(sizeof(char *) * 3); + root->pi[i][0] = target; + root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list + root->pi[i][2] = strdup(""); // empty document position list + } + + while (root->pi[i][j]) j++; // find end of instruction list for this target + root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3)); + root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1); + strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<"); + root->pi[i][j + 1] = NULL; // null terminate pi list for this target + root->pi[i][j] = s; // set instruction +} + +// called when the parser finds an internal doctype subset +static short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len) +{ + char q, *c, *t, *n = NULL, *v, **ent, **pe; + int i, j; + + pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL)); + + for (s[len] = '\0'; s; ) { + while (*s && *s != '<' && *s != '%') s++; // find next declaration + + if (! *s) break; + else if (! strncmp(s, "'); + continue; + } + + for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++); + ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent + if (*c == '%') pe = ent; + else root->ent = ent; + + *(++s) = '\0'; // null terminate name + if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value + ent[i + 1] = ezxml_decode(v, pe, '%'); // set value + ent[i + 2] = NULL; // null terminate entity list + if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference + if (ent[i + 1] != v) free(ent[i + 1]); + ezxml_err(root, v, "circular entity declaration &%s", n); + break; + } + else ent[i] = n; // set entity name + } + else if (! strncmp(s, "")) == '>') continue; + else *s = '\0'; // null terminate tag name + for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++); + + ++s; // ansi cpr + while (*(n = s + strspn(s, EZXML_WS)) && *n != '>') { + if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name + else { ezxml_err(root, t, "malformed ") - 1; + if (*c == ' ') continue; // cdata is default, nothing to do + v = NULL; + } + else if ((*s == '"' || *s == '\'') && // default value + (s = strchr(v = s + 1, *s))) *s = '\0'; + else { ezxml_err(root, t, "malformed attr[i]) { // new tag name + root->attr = (! i) ? malloc(2 * sizeof(char **)) + : realloc(root->attr, + (i + 2) * sizeof(char **)); + root->attr[i] = malloc(2 * sizeof(char *)); + root->attr[i][0] = t; // set tag name + root->attr[i][1] = (char *)(root->attr[i + 1] = NULL); + } + + for (j = 1; root->attr[i][j]; j += 3); // find end of list + root->attr[i] = realloc(root->attr[i], + (j + 4) * sizeof(char *)); + + root->attr[i][j + 3] = NULL; // null terminate list + root->attr[i][j + 2] = c; // is it cdata? + root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c) + : NULL; + root->attr[i][j] = n; // attribute name + ++s; + } + } + else if (! strncmp(s, ""); // comments + else if (! strncmp(s, ""))) + ezxml_proc_inst(root, c, s++ - c); + } + else if (*s == '<') s = strchr(s, '>'); // skip other declarations + else if (*(s++) == '%' && ! root->standalone) break; + } + + free(pe); + return ! *root->err; +} + +// Converts a UTF-16 string to UTF-8. Returns a new string that must be freed +// or NULL if no conversion was needed. +static char *ezxml_str2utf8(char **s, size_t *len) +{ + char *u; + size_t l = 0, sl, max = *len; + long c, d; + int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1; + + if (be == -1) return NULL; // not UTF-16 + + u = malloc(max); + for (sl = 2; sl < *len - 1; sl += 2) { + c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE + : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE + if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half + d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) + : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); + c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000; + } + + while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE); + if (c < 0x80) u[l++] = c; // US-ASCII subset + else { // multi-byte UTF-8 sequence + for (b = 0, d = c; d; d /= 2) b++; // bits in c + b = (b - 2) / 5; // bytes in payload + u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head + while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload + } + } + return *s = realloc(u, *len = l); +} + +// frees a tag attribute list +static void ezxml_free_attr(char **attr) { + int i = 0; + char *m; + + if (! attr || attr == EZXML_NIL) return; // nothing to free + while (attr[i]) i += 2; // find end of attribute list + m = attr[i + 1]; // list of which names and values are malloced + for (i = 0; m[i]; i++) { + if (m[i] & EZXML_NAMEM) free(attr[i * 2]); + if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]); + } + free(m); + free(attr); +} + +// parse the given xml string and return an ezxml structure +ezxml_t ezxml_parse_str(char *s, size_t len) +{ + ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL); + char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning + int l, i, j; + + root->m = s; + if (! len) return ezxml_err(root, NULL, "root tag missing"); + root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8 + root->e = (root->s = s) + len; // record start and end of work area + + e = s[len - 1]; // save end char + s[len - 1] = '\0'; // turn end char into null terminator + + while (*s && *s != '<') s++; // find first tag + if (! *s) return ezxml_err(root, s, "root tag missing"); + + for (; ; ) { + attr = (char **)EZXML_NIL; + d = ++s; + + if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag + if (! root->cur) + return ezxml_err(root, d, "markup outside of root element"); + + s += strcspn(s, EZXML_WS "/>"); + while (isspace(*s)) *(s++) = '\0'; // null terminate tag name + + if (*s && *s != '/' && *s != '>') // find tag in default attr list + for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++); + + for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib + attr = (l) ? realloc(attr, (l + 4) * sizeof(char *)) + : malloc(4 * sizeof(char *)); // allocate space + attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2) + : malloc(2); // mem for list of maloced vals + strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced + attr[l + 2] = NULL; // null terminate list + attr[l + 1] = ""; // temporary attribute value + attr[l] = s; // set attribute name + + s += strcspn(s, EZXML_WS "=/>"); + if (*s == '=' || isspace(*s)) { + *(s++) = '\0'; // null terminate tag attribute name + q = *(s += strspn(s, EZXML_WS "=")); + if (q == '"' || q == '\'') { // attribute value + attr[l + 1] = ++s; + while (*s && *s != q) s++; + if (*s) *(s++) = '\0'; // null terminate attribute val + else { + ezxml_free_attr(attr); + return ezxml_err(root, d, "missing %c", q); + } + + for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3); + attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a + && a[j]) ? *a[j + 2] : ' '); + if (attr[l + 1] < d || attr[l + 1] > s) + attr[l + 3][l / 2] = EZXML_TXTM; // value malloced + } + } + while (isspace(*s)) s++; + } + + if (*s == '/') { // self closing tag + *(s++) = '\0'; + if ((*s && *s != '>') || (! *s && e != '>')) { + if (l) ezxml_free_attr(attr); + return ezxml_err(root, d, "missing >"); + } + ezxml_open_tag(root, d, attr); + ezxml_close_tag(root, d, s); + } + else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag + *s = '\0'; // temporarily null terminate tag name + ezxml_open_tag(root, d, attr); + *s = q; + } + else { + if (l) ezxml_free_attr(attr); + return ezxml_err(root, d, "missing >"); + } + } + else if (*s == '/') { // close tag + s += strcspn(d = s + 1, EZXML_WS ">") + 1; + if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >"); + *s = '\0'; // temporarily null terminate tag name + if (ezxml_close_tag(root, d, s)) return &root->xml; + if (isspace(*s = q)) s += strspn(s, EZXML_WS); + } + else if (! strncmp(s, "!--", 3)) { // xml comment + if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) || + (! *s && e != '>')) return ezxml_err(root, d, "unclosed + * + * @param xml node named "struct" (or containing a similar structure) + * @param who wants to use this data? callback: content of as + * string, content of as xmlNode (may contain other nodes + * or text), additional data used by callback(); don't forget + * to *copy* data taken from or as they will be + * freed soon + * @param extra data for callback + */ +static void PianoXmlStructParser (const ezxml_t structRoot, + void (*callback) (const char *, const ezxml_t, void *), void *data) { + ezxml_t curNode, keyNode, valueNode; + char *key; + + /* get all nodes */ + for (curNode = ezxml_child (structRoot, "member"); curNode; curNode = curNode->next) { + /* reset variables */ + key = NULL; + valueNode = keyNode = NULL; + + keyNode = ezxml_child (curNode, "name"); + if (keyNode != NULL) { + key = ezxml_txt (keyNode); + } + + valueNode = ezxml_child (curNode, "value"); + /* this will ignore empty nodes, but well... */ + if (*key != '\0' && valueNode != NULL) { + (*callback) ((char *) key, valueNode, data); + } + } +} + +/* create xml parser from string + * @param xml document + * @param returns document pointer (needed to free memory later) + * @param returns document root + * @return _OK or error + */ +static PianoReturn_t PianoXmlInitDoc (char *xmlStr, ezxml_t *xmlDoc) { + PianoReturn_t ret; + + if ((*xmlDoc = ezxml_parse_str (xmlStr, strlen (xmlStr))) == NULL) { + return PIANO_RET_XML_INVALID; + } + + if ((ret = PianoXmlIsFault (*xmlDoc)) != PIANO_RET_OK) { + ezxml_free (*xmlDoc); + return ret; + } + + return PIANO_RET_OK; +} + +/* get text from nodes; some of them have , + * or subnodes, just ignore them + * @param xml node + */ +static char *PianoXmlGetNodeText (const ezxml_t node) { + char *retTxt = NULL; + + retTxt = ezxml_txt (node); + /* no text => empty string */ + if (*retTxt == '\0') { + retTxt = ezxml_txt (node->child); + } + return retTxt; +} + +/* structParser callback; writes userinfo to PianoUserInfo structure + * @param value identifier + * @param value node + * @param pointer to userinfo structure + * @return nothing + */ +static void PianoXmlParseUserinfoCb (const char *key, const ezxml_t value, + void *data) { + PianoUserInfo_t *user = data; + char *valueStr = PianoXmlGetNodeText (value); + + if (strcmp ("webAuthToken", key) == 0) { + user->webAuthToken = strdup (valueStr); + } else if (strcmp ("authToken", key) == 0) { + user->authToken = strdup (valueStr); + } else if (strcmp ("listenerId", key) == 0) { + user->listenerId = strdup (valueStr); + } +} + +static void PianoXmlParseStationsCb (const char *key, const ezxml_t value, + void *data) { + PianoStation_t *station = data; + char *valueStr = PianoXmlGetNodeText (value); + + if (strcmp ("stationName", key) == 0) { + station->name = strdup (valueStr); + } else if (strcmp ("stationId", key) == 0) { + station->id = strdup (valueStr); + } else if (strcmp ("isQuickMix", key) == 0) { + station->isQuickMix = (strcmp (valueStr, "1") == 0); + } else if (strcmp ("isCreator", key) == 0) { + station->isCreator = (strcmp (valueStr, "1") == 0); + } +} + +static void PianoXmlParsePlaylistCb (const char *key, const ezxml_t value, + void *data) { + PianoSong_t *song = data; + char *valueStr = PianoXmlGetNodeText (value); + + if (strcmp ("audioURL", key) == 0) { + /* last 48 chars of audioUrl are encrypted, but they put the key + * into the door's lock... */ + const char urlTailN = 48; + const size_t valueStrN = strlen (valueStr); + char *urlTail = NULL, + *urlTailCrypted = &valueStr[valueStrN - urlTailN]; + + /* don't try to decrypt if string is too short (=> invalid memory + * reads/writes) */ + if (valueStrN > urlTailN && + (urlTail = PianoDecryptString (urlTailCrypted)) != NULL) { + if ((song->audioUrl = calloc (valueStrN + 1, + sizeof (*song->audioUrl))) != NULL) { + memcpy (song->audioUrl, valueStr, valueStrN - urlTailN); + /* FIXME: the key seems to be broken... so ignore 8 x 0x08 + * postfix; urlTailN/2 because the encrypted hex string is now + * decoded */ + memcpy (&song->audioUrl[valueStrN - urlTailN], urlTail, + urlTailN/2 - 8); + } + free (urlTail); + } + } else if (strcmp ("artRadio", key) == 0) { + song->coverArt = strdup (valueStr); + } else if (strcmp ("artistSummary", key) == 0) { + song->artist = strdup (valueStr); + } else if (strcmp ("musicId", key) == 0) { + song->musicId = strdup (valueStr); + } else if (strcmp ("userSeed", key) == 0) { + song->userSeed = strdup (valueStr); + } else if (strcmp ("songTitle", key) == 0) { + song->title = strdup (valueStr); + } else if (strcmp ("rating", key) == 0) { + if (strcmp (valueStr, "1") == 0) { + song->rating = PIANO_RATE_LOVE; + } else { + song->rating = PIANO_RATE_NONE; + } + } else if (strcmp ("stationId", key) == 0) { + song->stationId = strdup (valueStr); + } else if (strcmp ("albumTitle", key) == 0) { + song->album = strdup (valueStr); + } else if (strcmp ("fileGain", key) == 0) { + song->fileGain = atof (valueStr); + } else if (strcmp ("audioEncoding", key) == 0) { + if (strcmp (valueStr, "aacplus") == 0) { + song->audioFormat = PIANO_AF_AACPLUS; + } else if (strcmp (valueStr, "mp3") == 0) { + song->audioFormat = PIANO_AF_MP3; + } else if (strcmp (valueStr, "mp3-hifi") == 0) { + song->audioFormat = PIANO_AF_MP3_HI; + } + } else if (strcmp ("artistMusicId", key) == 0) { + song->artistMusicId = strdup (valueStr); + } else if (strcmp ("testStrategy", key) == 0) { + song->testStrategy = atoi (valueStr); + } else if (strcmp ("songType", key) == 0) { + song->songType = atoi (valueStr); + } +} + +/* parses userinfos sent by pandora as login response + * @param piano handle + * @param utf-8 string + * @return _RET_OK or error + */ +PianoReturn_t PianoXmlParseUserinfo (PianoHandle_t *ph, char *xml) { + ezxml_t xmlDoc, structNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + /* */ + structNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); + PianoXmlStructParser (structNode, PianoXmlParseUserinfoCb, &ph->user); + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +static void PianoXmlParseQuickMixStationsCb (const char *key, const ezxml_t value, + void *data) { + char ***retIds = data; + char **ids = NULL; + size_t idsN = 0; + ezxml_t curNode; + + if (strcmp ("quickMixStationIds", key) == 0) { + for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); + curNode; curNode = curNode->next) { + idsN++; + if (ids == NULL) { + if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } + } else { + /* FIXME: memory leak (on failure) */ + if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } + } + ids[idsN-1] = strdup (PianoXmlGetNodeText (curNode)); + } + /* append NULL: list ends here */ + idsN++; + /* FIXME: copy&waste */ + if (ids == NULL) { + if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } + } else { + if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { + *retIds = NULL; + return; + } + } + ids[idsN-1] = NULL; + } + *retIds = ids; +} + +/* parse stations returned by pandora + * @param piano handle + * @param xml returned by pandora + * @return _RET_OK or error + */ +PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, char *xml) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + char **quickMixIds = NULL, **curQuickMixId = NULL; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "array", + 0, "data", -1); + + for (dataNode = ezxml_child (dataNode, "value"); dataNode; + dataNode = dataNode->next) { + PianoStation_t *tmpStation; + + if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + + PianoXmlStructParser (ezxml_child (dataNode, "struct"), + PianoXmlParseStationsCb, tmpStation); + + /* get stations selected for quickmix */ + if (tmpStation->isQuickMix) { + PianoXmlStructParser (ezxml_child (dataNode, "struct"), + PianoXmlParseQuickMixStationsCb, &quickMixIds); + } + /* start new linked list or append */ + if (ph->stations == NULL) { + ph->stations = tmpStation; + } else { + PianoStation_t *curStation = ph->stations; + while (curStation->next != NULL) { + curStation = curStation->next; + } + curStation->next = tmpStation; + } + } + /* set quickmix flags after all stations are read */ + if (quickMixIds != NULL) { + curQuickMixId = quickMixIds; + while (*curQuickMixId != NULL) { + PianoStation_t *curStation = PianoFindStationById (ph->stations, + *curQuickMixId); + if (curStation != NULL) { + curStation->useQuickMix = 1; + } + free (*curQuickMixId); + curQuickMixId++; + } + free (quickMixIds); + } + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* parse "create station" answer (it returns a new station structure) + * @param piano handle + * @param xml document + * @return nothing yet + */ +PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, char *xml) { + ezxml_t xmlDoc, dataNode; + PianoStation_t *tmpStation; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); + + if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, tmpStation); + /* FIXME: copy & waste */ + /* start new linked list or append */ + if (ph->stations == NULL) { + ph->stations = tmpStation; + } else { + PianoStation_t *curStation = ph->stations; + while (curStation->next != NULL) { + curStation = curStation->next; + } + curStation->next = tmpStation; + } + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* parse "add seed" answer, nearly the same as ParseCreateStation + * @param piano handle + * @param xml document + * @param update this station + */ +PianoReturn_t PianoXmlParseAddSeed (PianoHandle_t *ph, char *xml, + PianoStation_t *station) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); + PianoDestroyStation (station); + PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, station); + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* parses playlist; used when searching too + * @param piano handle + * @param xml document + * @param return: playlist + */ +PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml, + PianoSong_t **retPlaylist) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "array", + 0, "data", -1); + + for (dataNode = ezxml_child (dataNode, "value"); dataNode; + dataNode = dataNode->next) { + PianoSong_t *tmpSong; + + if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + + PianoXmlStructParser (ezxml_child (dataNode, "struct"), + PianoXmlParsePlaylistCb, tmpSong); + /* begin linked list or append */ + if (*retPlaylist == NULL) { + *retPlaylist = tmpSong; + } else { + PianoSong_t *curSong = *retPlaylist; + while (curSong->next != NULL) { + curSong = curSong->next; + } + curSong->next = tmpSong; + } + } + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* parse simple answers like this: + * 1 + * + * @param xml string + * @return + */ +PianoReturn_t PianoXmlParseSimple (char *xml) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); + + if (strcmp (ezxml_txt (dataNode), "1") == 0) { + ret = PIANO_RET_OK; + } else { + ret = PIANO_RET_ERR; + } + + ezxml_free (xmlDoc); + + return ret; +} + +/* xml struct parser callback, used in PianoXmlParseSearchCb + */ +static void PianoXmlParseSearchArtistCb (const char *key, const ezxml_t value, + void *data) { + PianoArtist_t *artist = data; + char *valueStr = PianoXmlGetNodeText (value); + + if (strcmp ("artistName", key) == 0) { + artist->name = strdup (valueStr); + } else if (strcmp ("musicId", key) == 0) { + artist->musicId = strdup (valueStr); + } +} + +/* callback for xml struct parser used in PianoXmlParseSearch, "switch" for + * PianoXmlParseSearchArtistCb and PianoXmlParsePlaylistCb + */ +static void PianoXmlParseSearchCb (const char *key, const ezxml_t value, + void *data) { + PianoSearchResult_t *searchResult = data; + ezxml_t curNode; + + if (strcmp ("artists", key) == 0) { + /* skip */ + for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); + curNode; curNode = curNode->next) { + PianoArtist_t *artist; + + if ((artist = calloc (1, sizeof (*artist))) == NULL) { + /* fail silently */ + break; + } + + memset (artist, 0, sizeof (*artist)); + + PianoXmlStructParser (ezxml_child (curNode, "struct"), + PianoXmlParseSearchArtistCb, artist); + + /* add result to linked list */ + if (searchResult->artists == NULL) { + searchResult->artists = artist; + } else { + PianoArtist_t *curArtist = searchResult->artists; + while (curArtist->next != NULL) { + curArtist = curArtist->next; + } + curArtist->next = artist; + } + } + } else if (strcmp ("songs", key) == 0) { + for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); + curNode; curNode = curNode->next) { + /* FIXME: copy & waste */ + PianoSong_t *tmpSong; + + if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { + /* fail silently */ + break; + } + + PianoXmlStructParser (ezxml_child (curNode, "struct"), + PianoXmlParsePlaylistCb, tmpSong); + /* begin linked list or append */ + if (searchResult->songs == NULL) { + searchResult->songs = tmpSong; + } else { + PianoSong_t *curSong = searchResult->songs; + while (curSong->next != NULL) { + curSong = curSong->next; + } + curSong->next = tmpSong; + } + } + } +} + +/* parse search result; searchResult is nulled before use + * @param xml document + * @param returns search result + * @return nothing yet + */ +PianoReturn_t PianoXmlParseSearch (char *xml, + PianoSearchResult_t *searchResult) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); + /* we need a "clean" search result (with null pointers) */ + memset (searchResult, 0, sizeof (*searchResult)); + PianoXmlStructParser (dataNode, PianoXmlParseSearchCb, searchResult); + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* FIXME: copy&waste (PianoXmlParseSearch) + */ +PianoReturn_t PianoXmlParseSeedSuggestions (char *xml, + PianoSearchResult_t *searchResult) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); + /* we need a "clean" search result (with null pointers) */ + memset (searchResult, 0, sizeof (*searchResult)); + /* reuse seach result parser; structure is nearly the same */ + PianoXmlParseSearchCb ("artists", dataNode, searchResult); + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* encode reserved xml chars + * TODO: remove and use ezxml_ampencode + * @param encode this + * @return encoded string or NULL + */ +char *PianoXmlEncodeString (const char *s) { + char *replacements[] = {"&&", "''", "\""", "<<", + ">>", NULL}; + char **r, *sOut, *sOutCurr, found; + + if ((sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut))) == NULL) { + return NULL; + } + + sOutCurr = sOut; + + while (*s != '\0') { + r = replacements; + found = 0; + while (*r != NULL) { + if (*s == *r[0]) { + found = 1; + strcat (sOutCurr, (*r) + 1); + sOutCurr += strlen ((*r) + 1); + break; + } + r++; + } + if (!found) { + *sOutCurr = *s; + sOutCurr++; + } + s++; + } + return sOut; +} + +PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, char *xml) { + ezxml_t xmlDoc, catNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + /* get all nodes */ + for (catNode = ezxml_child (xmlDoc, "category"); catNode; + catNode = catNode->next) { + PianoGenreCategory_t *tmpGenreCategory; + ezxml_t genreNode; + + if ((tmpGenreCategory = calloc (1, sizeof (*tmpGenreCategory))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + + tmpGenreCategory->name = strdup (ezxml_attr (catNode, "categoryName")); + + /* get genre subnodes */ + for (genreNode = ezxml_child (catNode, "genre"); genreNode; + genreNode = genreNode->next) { + PianoGenre_t *tmpGenre; + + if ((tmpGenre = calloc (1, sizeof (*tmpGenre))) == NULL) { + ezxml_free (xmlDoc); + return PIANO_RET_OUT_OF_MEMORY; + } + + /* get genre attributes */ + tmpGenre->name = strdup (ezxml_attr (genreNode, "name")); + tmpGenre->musicId = strdup (ezxml_attr (genreNode, "musicId")); + + /* append station */ + if (tmpGenreCategory->genres == NULL) { + tmpGenreCategory->genres = tmpGenre; + } else { + PianoGenre_t *curGenre = + tmpGenreCategory->genres; + while (curGenre->next != NULL) { + curGenre = curGenre->next; + } + curGenre->next = tmpGenre; + } + } + /* append category */ + if (ph->genreStations == NULL) { + ph->genreStations = tmpGenreCategory; + } else { + PianoGenreCategory_t *curCat = ph->genreStations; + while (curCat->next != NULL) { + curCat = curCat->next; + } + curCat->next = tmpGenreCategory; + } + } + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* dummy function, only checks for errors + * @param xml doc + * @return _OK or error + */ +PianoReturn_t PianoXmlParseTranformStation (char *xml) { + ezxml_t xmlDoc; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + ezxml_free (xmlDoc); + + return PIANO_RET_OK; +} + +/* parses "why did you play ...?" answer + * @param xml + * @param returns the answer + * @return _OK or error + */ +PianoReturn_t PianoXmlParseNarrative (char *xml, char **retNarrative) { + ezxml_t xmlDoc, dataNode; + PianoReturn_t ret; + + if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { + return ret; + } + + /* $textnode */ + dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); + *retNarrative = strdup (ezxml_txt (dataNode)); + + ezxml_free (xmlDoc); + + return ret; +} + diff --git a/src/libpiano/xml.h b/src/libpiano/xml.h new file mode 100644 index 0000000..dc778ca --- /dev/null +++ b/src/libpiano/xml.h @@ -0,0 +1,48 @@ +/* +Copyright (c) 2008-2010 + Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef _XML_H +#define _XML_H + +#include "piano.h" + +PianoReturn_t PianoXmlParseUserinfo (PianoHandle_t *ph, const char *xml); +PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, const char *xml); +PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, const char *xml, + PianoSong_t **); +PianoReturn_t PianoXmlParseSearch (const char *searchXml, + PianoSearchResult_t *searchResult); +PianoReturn_t PianoXmlParseSimple (const char *xml); +PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, + const char *xml); +PianoReturn_t PianoXmlParseAddSeed (PianoHandle_t *ph, const char *xml, + PianoStation_t *station); +PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, + const char *xmlContent); +PianoReturn_t PianoXmlParseTranformStation (const char *searchXml); +PianoReturn_t PianoXmlParseNarrative (const char *xml, char **retNarrative); +PianoReturn_t PianoXmlParseSeedSuggestions (char *, PianoSearchResult_t *); + +char *PianoXmlEncodeString (const char *s); + +#endif /* _XML_H */ diff --git a/src/libwaitress/config.h b/src/libwaitress/config.h new file mode 100644 index 0000000..e13fe40 --- /dev/null +++ b/src/libwaitress/config.h @@ -0,0 +1 @@ +#define PACKAGE "libwaitress" diff --git a/src/libwaitress/waitress.c b/src/libwaitress/waitress.c new file mode 100644 index 0000000..27f7b3d --- /dev/null +++ b/src/libwaitress/waitress.c @@ -0,0 +1,573 @@ +/* +Copyright (c) 2009-2010 + Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#define _POSIX_C_SOURCE 1 /* required by getaddrinfo() */ +#define _BSD_SOURCE /* snprintf() */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "waitress.h" + +typedef struct { + char *buf; + size_t pos; +} WaitressFetchBufCbBuffer_t; + +inline void WaitressInit (WaitressHandle_t *waith) { + memset (waith, 0, sizeof (*waith)); + waith->socktimeout = 30000; +} + +inline void WaitressFree (WaitressHandle_t *waith) { + memset (waith, 0, sizeof (*waith)); +} + +/* Proxy set up? + * @param Waitress handle + * @return true|false + */ +static inline char WaitressProxyEnabled (const WaitressHandle_t *waith) { + return *waith->proxyHost != '\0' && *waith->proxyPort != '\0'; +} + +/* Set http proxy + * @param waitress handle + * @param host + * @param port + */ +inline void WaitressSetProxy (WaitressHandle_t *waith, const char *host, + const char *port) { + strncpy (waith->proxyHost, host, sizeof (waith->proxyHost)-1); + strncpy (waith->proxyPort, port, sizeof (waith->proxyPort)-1); +} + +/* urlencode post-data + * @param encode this + * @return malloc'ed encoded string, don't forget to free it + */ +char *WaitressUrlEncode (const char *in) { + size_t inLen = strlen (in); + /* worst case: encode all characters */ + char *out = calloc (inLen * 3 + 1, sizeof (*in)); + const char *inPos = in; + char *outPos = out; + + while (inPos - in < inLen) { + if (!isalnum (*inPos) && *inPos != '_' && *inPos != '-' && *inPos != '.') { + *outPos++ = '%'; + snprintf (outPos, 3, "%02x", *inPos & 0xff); + outPos += 2; + } else { + /* copy character */ + *outPos++ = *inPos; + } + ++inPos; + } + + return out; +} + +/* Split http url into host, port and path + * @param url + * @param return buffer: host + * @param host buffer size + * @param return buffer: port, defaults to 80 + * @param port buffer size + * @param return buffer: path + * @param path buffer size + * @param 1 = ok, 0 = not a http url; if your buffers are too small horrible + * things will happen... But 1 is returned anyway. + */ +char WaitressSplitUrl (const char *url, char *retHost, size_t retHostSize, + char *retPort, size_t retPortSize, char *retPath, size_t retPathSize) { + size_t urlSize = strlen (url); + const char *urlPos = url, *lastPos; + + if (urlSize > sizeof ("http://")-1 && + memcmp (url, "http://", sizeof ("http://")-1) == 0) { + memset (retHost, 0, retHostSize); + memset (retPort, 0, retPortSize); + strncpy (retPort, "80", retPortSize-1); + memset (retPath, 0, retPathSize); + + urlPos += sizeof ("http://")-1; + lastPos = urlPos; + + /* find host */ + while (*urlPos != '\0' && *urlPos != ':' && *urlPos != '/' && + urlPos - lastPos < retHostSize-1) { + *retHost++ = *urlPos++; + } + lastPos = urlPos; + + /* port, if available */ + if (*urlPos == ':') { + /* skip : */ + ++urlPos; + ++lastPos; + while (*urlPos != '\0' && *urlPos != '/' && + urlPos - lastPos < retPortSize-1) { + *retPort++ = *urlPos++; + } + } + lastPos = urlPos; + + /* path */ + while (*urlPos != '\0' && *urlPos != '#' && + urlPos - lastPos < retPathSize-1) { + *retPath++ = *urlPos++; + } + } else { + return 0; + } + return 1; +} + +/* Parse url and set host, port, path + * @param Waitress handle + * @param url: protocol://host:port/path + */ +inline char WaitressSetUrl (WaitressHandle_t *waith, const char *url) { + return WaitressSplitUrl (url, waith->host, sizeof (waith->host), + waith->port, sizeof (waith->port), waith->path, sizeof (waith->path)); +} + +/* Set host, port, path + * @param Waitress handle + * @param host + * @param port (getaddrinfo () needs a string...) + * @param path, including leading / + */ +inline void WaitressSetHPP (WaitressHandle_t *waith, const char *host, + const char *port, const char *path) { + strncpy (waith->host, host, sizeof (waith->host)-1); + strncpy (waith->port, port, sizeof (waith->port)-1); + strncpy (waith->path, path, sizeof (waith->path)-1); +} + +/* Callback for WaitressFetchBuf, appends received data to NULL-terminated buffer + * @param received data + * @param data size + * @param buffer structure + */ +static WaitressCbReturn_t WaitressFetchBufCb (void *recvData, size_t recvDataSize, + void *extraData) { + char *recvBytes = recvData; + WaitressFetchBufCbBuffer_t *buffer = extraData; + + if (buffer->buf == NULL) { + if ((buffer->buf = malloc (sizeof (*buffer->buf) * + (recvDataSize + 1))) == NULL) { + return WAITRESS_CB_RET_ERR; + } + } else { + char *newbuf; + if ((newbuf = realloc (buffer->buf, + sizeof (*buffer->buf) * + (buffer->pos + recvDataSize + 1))) == NULL) { + free (buffer->buf); + return WAITRESS_CB_RET_ERR; + } + buffer->buf = newbuf; + } + memcpy (buffer->buf + buffer->pos, recvBytes, recvDataSize); + buffer->pos += recvDataSize; + *(buffer->buf+buffer->pos) = '\0'; + + return WAITRESS_CB_RET_OK; +} + +/* Fetch string. Beware! This overwrites your waith->data pointer + * @param waitress handle + * @param result buffer, malloced (don't forget to free it yourself) + */ +WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *waith, char **buf) { + WaitressFetchBufCbBuffer_t buffer; + WaitressReturn_t wRet; + + memset (&buffer, 0, sizeof (buffer)); + + waith->data = &buffer; + waith->callback = WaitressFetchBufCb; + + wRet = WaitressFetchCall (waith); + *buf = buffer.buf; + return wRet; +} + +/* poll wrapper that retries after signal interrupts, required for socksify + * wrapper + */ +static int WaitressPollLoop (struct pollfd *fds, nfds_t nfds, int timeout) { + int pollres = -1; + int pollerr = 0; + + do { + pollres = poll (fds, nfds, timeout); + pollerr = errno; + errno = 0; + } while (pollerr == EINTR || pollerr == EINPROGRESS || pollerr == EAGAIN); + + return pollres; +} + +/* write () wrapper with poll () timeout + * @param socket fd + * @param write buffer + * @param write count bytes + * @param reuse existing pollfd structure + * @param timeout (microseconds) + * @return WAITRESS_RET_OK, WAITRESS_RET_TIMEOUT or WAITRESS_RET_ERR + */ +static WaitressReturn_t WaitressPollWrite (int sockfd, const char *buf, size_t count, + struct pollfd *sockpoll, int timeout) { + int pollres = -1; + + sockpoll->events = POLLOUT; + pollres = WaitressPollLoop (sockpoll, 1, timeout); + if (pollres == 0) { + return WAITRESS_RET_TIMEOUT; + } else if (pollres == -1) { + return WAITRESS_RET_ERR; + } + if (write (sockfd, buf, count) == -1) { + return WAITRESS_RET_ERR; + } + return WAITRESS_RET_OK; +} + +/* read () wrapper with poll () timeout + * @param socket fd + * @param write to this buf, not NULL terminated + * @param buffer size + * @param reuse existing pollfd struct + * @param timeout (in microseconds) + * @param read () return value/written bytes + * @return WAITRESS_RET_OK, WAITRESS_RET_TIMEOUT, WAITRESS_RET_ERR + */ +static WaitressReturn_t WaitressPollRead (int sockfd, char *buf, size_t count, + struct pollfd *sockpoll, int timeout, ssize_t *retSize) { + int pollres = -1; + + sockpoll->events = POLLIN; + pollres = WaitressPollLoop (sockpoll, 1, timeout); + if (pollres == 0) { + return WAITRESS_RET_TIMEOUT; + } else if (pollres == -1) { + return WAITRESS_RET_ERR; + } + if ((*retSize = read (sockfd, buf, count)) == -1) { + return WAITRESS_RET_READ_ERR; + } + return WAITRESS_RET_OK; +} + +/* FIXME: compiler macros are ugly... */ +#define CLOSE_RET(ret) close (sockfd); return ret; +#define WRITE_RET(buf, count) \ + if ((wRet = WaitressPollWrite (sockfd, buf, count, \ + &sockpoll, waith->socktimeout)) != WAITRESS_RET_OK) { \ + CLOSE_RET (wRet); \ + } +#define READ_RET(buf, count, size) \ + if ((wRet = WaitressPollRead (sockfd, buf, count, \ + &sockpoll, waith->socktimeout, size)) != WAITRESS_RET_OK) { \ + CLOSE_RET (wRet); \ + } + +/* Receive data from host and call *callback () + * @param waitress handle + * @return WaitressReturn_t + */ +WaitressReturn_t WaitressFetchCall (WaitressHandle_t *waith) { + struct addrinfo hints, *res; + int sockfd; + char recvBuf[WAITRESS_RECV_BUFFER]; + char writeBuf[2*1024]; + ssize_t recvSize = 0; + WaitressReturn_t wRet = WAITRESS_RET_OK; + struct pollfd sockpoll; + int pollres; + /* header parser vars */ + char *nextLine = NULL, *thisLine = NULL; + enum {HDRM_HEAD, HDRM_LINES, HDRM_FINISHED} hdrParseMode = HDRM_HEAD; + char statusCode[3], val[256]; + unsigned int bufFilled = 0; + + /* initialize */ + waith->contentLength = 0; + waith->contentReceived = 0; + memset (&hints, 0, sizeof hints); + + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + /* Use proxy? */ + if (WaitressProxyEnabled (waith)) { + if (getaddrinfo (waith->proxyHost, waith->proxyPort, &hints, &res) != 0) { + return WAITRESS_RET_GETADDR_ERR; + } + } else { + if (getaddrinfo (waith->host, waith->port, &hints, &res) != 0) { + return WAITRESS_RET_GETADDR_ERR; + } + } + + if ((sockfd = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) { + freeaddrinfo (res); + return WAITRESS_RET_SOCK_ERR; + } + sockpoll.fd = sockfd; + + /* we need shorter timeouts for connect() */ + fcntl (sockfd, F_SETFL, O_NONBLOCK); + + /* increase socket receive buffer */ + const int sockopt = 256*1024; + setsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof (sockopt)); + + /* non-blocking connect will return immediately */ + connect (sockfd, res->ai_addr, res->ai_addrlen); + + sockpoll.events = POLLOUT; + pollres = WaitressPollLoop (&sockpoll, 1, waith->socktimeout); + freeaddrinfo (res); + if (pollres == 0) { + return WAITRESS_RET_TIMEOUT; + } else if (pollres == -1) { + return WAITRESS_RET_ERR; + } + /* check connect () return value */ + socklen_t pollresSize = sizeof (pollres); + getsockopt (sockfd, SOL_SOCKET, SO_ERROR, &pollres, &pollresSize); + if (pollres != 0) { + return WAITRESS_RET_CONNECT_REFUSED; + } + + /* send request */ + if (WaitressProxyEnabled (waith)) { + snprintf (writeBuf, sizeof (writeBuf), + "%s http://%s:%s%s HTTP/1.0\r\n", + (waith->method == WAITRESS_METHOD_GET ? "GET" : "POST"), + waith->host, waith->port, waith->path); + } else { + snprintf (writeBuf, sizeof (writeBuf), + "%s %s HTTP/1.0\r\n", + (waith->method == WAITRESS_METHOD_GET ? "GET" : "POST"), + waith->path); + } + WRITE_RET (writeBuf, strlen (writeBuf)); + + snprintf (writeBuf, sizeof (writeBuf), + "Host: %s\r\nUser-Agent: " PACKAGE "\r\n", waith->host); + WRITE_RET (writeBuf, strlen (writeBuf)); + + if (waith->method == WAITRESS_METHOD_POST && waith->postData != NULL) { + snprintf (writeBuf, sizeof (writeBuf), "Content-Length: %zu\r\n", + strlen (waith->postData)); + WRITE_RET (writeBuf, strlen (writeBuf)); + } + + if (waith->extraHeaders != NULL) { + WRITE_RET (waith->extraHeaders, strlen (waith->extraHeaders)); + } + + WRITE_RET ("\r\n", 2); + + if (waith->method == WAITRESS_METHOD_POST && waith->postData != NULL) { + WRITE_RET (waith->postData, strlen (waith->postData)); + } + + /* receive answer */ + nextLine = recvBuf; + while (hdrParseMode != HDRM_FINISHED) { + READ_RET (recvBuf+bufFilled, sizeof (recvBuf)-1 - bufFilled, &recvSize); + if (recvSize == 0) { + /* connection closed too early */ + CLOSE_RET (WAITRESS_RET_CONNECTION_CLOSED); + } + bufFilled += recvSize; + memset (recvBuf+bufFilled, 0, sizeof (recvBuf) - bufFilled); + thisLine = recvBuf; + + /* split */ + while ((nextLine = strchr (thisLine, '\n')) != NULL && + hdrParseMode != HDRM_FINISHED) { + /* make lines parseable by string routines */ + *nextLine = '\0'; + if (*(nextLine-1) == '\r') { + *(nextLine-1) = '\0'; + } + /* skip \0 */ + ++nextLine; + + 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 { + CLOSE_RET (WAITRESS_RET_STATUS_UNKNOWN); + } + hdrParseMode = HDRM_LINES; + } /* endif */ + break; + + /* Everything else, except status code */ + case HDRM_LINES: + /* empty line => content starts here */ + if (*thisLine == '\0') { + hdrParseMode = HDRM_FINISHED; + } else { + memset (val, 0, sizeof (val)); + if (sscanf (thisLine, "Content-Length: %255c", val) == 1) { + waith->contentLength = atol (val); + } + } + break; + + default: + break; + } /* end switch */ + thisLine = nextLine; + } /* end while strchr */ + memmove (recvBuf, thisLine, thisLine-recvBuf); + bufFilled -= (thisLine-recvBuf); + } /* end while hdrParseMode */ + + /* push remaining bytes */ + if (bufFilled > 0) { + waith->contentReceived += bufFilled; + if (waith->callback (thisLine, bufFilled, waith->data) == + WAITRESS_CB_RET_ERR) { + CLOSE_RET (WAITRESS_RET_CB_ABORT); + } + } + + /* receive content */ + do { + READ_RET (recvBuf, sizeof (recvBuf), &recvSize); + if (recvSize > 0) { + waith->contentReceived += recvSize; + if (waith->callback (recvBuf, recvSize, waith->data) == + WAITRESS_CB_RET_ERR) { + wRet = WAITRESS_RET_CB_ABORT; + break; + } + } + } while (recvSize > 0); + + close (sockfd); + + if (wRet == WAITRESS_RET_OK && waith->contentReceived < waith->contentLength) { + return WAITRESS_RET_PARTIAL_FILE; + } + return wRet; +} + +#undef CLOSE_RET +#undef WRITE_RET +#undef READ_RET + +const char *WaitressErrorToStr (WaitressReturn_t wRet) { + switch (wRet) { + case WAITRESS_RET_OK: + return "Everything's fine :)"; + break; + + case WAITRESS_RET_ERR: + return "Unknown."; + break; + + case WAITRESS_RET_STATUS_UNKNOWN: + return "Unknown HTTP status code."; + break; + + case WAITRESS_RET_NOTFOUND: + return "File not found."; + break; + + case WAITRESS_RET_FORBIDDEN: + return "Forbidden."; + break; + + case WAITRESS_RET_CONNECT_REFUSED: + return "Connection refused."; + break; + + case WAITRESS_RET_SOCK_ERR: + return "Socket error."; + break; + + case WAITRESS_RET_GETADDR_ERR: + return "getaddr failed."; + break; + + case WAITRESS_RET_CB_ABORT: + return "Callback aborted request."; + break; + + case WAITRESS_RET_HDR_OVERFLOW: + return "HTTP header overflow."; + break; + + case WAITRESS_RET_PARTIAL_FILE: + return "Partial file."; + break; + + case WAITRESS_RET_TIMEOUT: + return "Timeout."; + break; + + case WAITRESS_RET_READ_ERR: + return "Read error."; + break; + + case WAITRESS_RET_CONNECTION_CLOSED: + return "Connection closed by remote host."; + break; + + default: + return "No error message available."; + break; + } +} + diff --git a/src/libwaitress/waitress.h b/src/libwaitress/waitress.h new file mode 100644 index 0000000..04333fb --- /dev/null +++ b/src/libwaitress/waitress.h @@ -0,0 +1,78 @@ +/* +Copyright (c) 2009-2010 + Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef _WAITRESS_H +#define _WAITRESS_H + +#include + +#define WAITRESS_HOST_SIZE 100 +/* max: 65,535 */ +#define WAITRESS_PORT_SIZE 6 +#define WAITRESS_PATH_SIZE 1000 +#define WAITRESS_RECV_BUFFER 10*1024 + +typedef enum {WAITRESS_METHOD_GET = 0, WAITRESS_METHOD_POST} WaitressMethod_t; + +typedef enum {WAITRESS_CB_RET_ERR, WAITRESS_CB_RET_OK} WaitressCbReturn_t; + +typedef struct { + char host[WAITRESS_HOST_SIZE]; + char port[WAITRESS_PORT_SIZE]; + char path[WAITRESS_PATH_SIZE]; + WaitressMethod_t method; + const char *extraHeaders; + const char *postData; + size_t contentLength; + size_t contentReceived; + char proxyHost[WAITRESS_HOST_SIZE]; + char proxyPort[WAITRESS_PORT_SIZE]; + /* extra data handed over to callback function */ + void *data; + WaitressCbReturn_t (*callback) (void *, size_t, void *); + int socktimeout; +} WaitressHandle_t; + +typedef enum {WAITRESS_RET_ERR = 0, WAITRESS_RET_OK, WAITRESS_RET_STATUS_UNKNOWN, + WAITRESS_RET_NOTFOUND, WAITRESS_RET_FORBIDDEN, WAITRESS_RET_CONNECT_REFUSED, + WAITRESS_RET_SOCK_ERR, WAITRESS_RET_GETADDR_ERR, + WAITRESS_RET_CB_ABORT, WAITRESS_RET_HDR_OVERFLOW, + WAITRESS_RET_PARTIAL_FILE, WAITRESS_RET_TIMEOUT, WAITRESS_RET_READ_ERR, + WAITRESS_RET_CONNECTION_CLOSED +} WaitressReturn_t; + +void WaitressInit (WaitressHandle_t *); +void WaitressFree (WaitressHandle_t *); +void WaitressSetProxy (WaitressHandle_t *, const char *, const char *); +char *WaitressUrlEncode (const char *); +char WaitressSplitUrl (const char *, char *, size_t, char *, size_t, char *, + size_t); +char WaitressSetUrl (WaitressHandle_t *, const char *); +void WaitressSetHPP (WaitressHandle_t *, const char *, const char *, + const char *); +WaitressReturn_t WaitressFetchBuf (WaitressHandle_t *, char **); +WaitressReturn_t WaitressFetchCall (WaitressHandle_t *); +const char *WaitressErrorToStr (WaitressReturn_t); + +#endif /* _WAITRESS_H */ + diff --git a/src/pianobar.1 b/src/pianobar.1 deleted file mode 100644 index 6a6bd33..0000000 --- a/src/pianobar.1 +++ /dev/null @@ -1,227 +0,0 @@ -.TH pianobar 1 - -.SH NAME -pianobar \- console pandora.com music player - -.SH SYNOPSIS -.B pianobar - -.SH DESCRIPTION -.B pianobar -is a lightweight console music player for the personalized online radio -pandora.com. - -.SH FILES -.I $XDG_CONFIG_HOME/pianobar/config -or -.I ~/.config/pianobar/config -.RS -Per-user configuration file. See -.B CONFIGURATION. -.RE - -.I /etc/libao.conf -or -.I ~/.libao -.RS -Global/per-user audio output configuration. See libao documentation at -http://xiph.org/ao/doc/config.html -.RE - -.SH CONFIGURATION -The configuration file consists of simple -.B key = value -lines. Each terminated with a newline (\\n) character. Keys and values are both -case sensitive. act_*-keys control -.B pianobar's -key-bindings. - -.TP -.B act_help = ? -Show keybindings. - -.TP -.B act_songlove = + -Love currently played song. - -.TP -.B act_songban = - -Ban current track. It will not be played again and can only removed using the -pandora.com web interface. - -.TP -.B act_stationaddmusic = a -Add more music to current station. You will be asked for a search string. Just -follow the instructions. If you're clueless try '?' (without quotes). - -.TP -.B act_bookmark = b -Bookmark current song or artist. - -.TP -.B act_stationcreate = c -Create new station. You have to enter a search string and select the song or -artist of your choice. - -.TP -.B act_stationdelete = d -Delete current station. - -.TP -.B act_songexplain = e -Explain why this song is played. - -.TP -.B act_stationaddbygenre = g -Add genre station provided by pandora. - -.TP -.B act_history = h -Show history. - -.TP -.B act_songinfo = i -Print information about currently played song/station. - -.TP -.B act_addshared = j -Add shared station by id. id is a very long integer without "sh" at the -beginning. - -.TP -.B act_songmove = m -Move current song to another station - -.TP -.B act_songnext = n -Skip current song. - -.TP -.B act_songpause = p -Pause/Continue - -.TP -.B act_quit = q -Quit -.B pianobar. - -.TP -.B act_stationrename = r -Rename currently played station. - -.TP -.B act_stationchange = s -Select another station. - -.TP -.B act_songtired = t -Ban song for one month. - -.TP -.B act_upcoming = u -Show next songs in playlist. - -.TP -.B act_stationselectquickmix = x -Select quickmix stations. - -.TP -.B audio_format = {aacplus,mp3,mp3-hifi} -Select audio format. aacplus is default if both libraries (faad, mad) are -available. mp3-hifi is available for Pandora One customers only. - -.TP -.B autostart_station = stationid -Play this station when starting up. You can get the -.B stationid -by pressing -.B i -or the key you defined in -.B act_songinfo. - -.TP -.B ban_icon = ~/.config/pianobar/ctl - -.B n -is the keybinding for "next song". If you customized your keybindings you have to use these characters to control -.B pianobar. -.I This behaviour may change in the future! - -Another example: - - while true; do; - nc -l -p 12345 -s localhost localhost > ~/.config/pianobar/ctl; - sleep 1; - done - - echo -ne 'n\\x1a' | nc -q 0 127.0.0.1 12345 - -.SH EVENTCMD - -.B pianobar -can report certain "events" to an external application (see -.B CONFIGURATION -). This application is started with the event name as it's first argument. More -information (artist, title, album, stationName, error code, error description, -song length in milliseconds, rating, album art url) is supplied through stdin. - -Currently supported events are: artistbookmark, songban, songbookmark, -songexplain, songfinish, songlove, songmove, songshelf, songstart, -stationaddmusic, stationaddshared, stationcreate, stationdelete, -stationfetchplaylist, stationquickmixtoggle, stationrename - -An example script can be found in the contrib/ directory of -.B pianobar's -source distribution. - -.SH AUTHOR -Lars-Dominik Braun -- cgit v1.2.3