diff --git a/configure b/configure index a90f3415..ff5b9c38 100755 --- a/configure +++ b/configure @@ -29,6 +29,7 @@ OPTIONS=( "zlib:auto" "libav:auto" "inotify:auto" + "uriparser:auto" "bundle:no" "dvbcsa:no" "epoll:yes" @@ -133,6 +134,17 @@ if enabled_or_auto zlib; then fi fi +# +# uriparser +# +if enabled_or_auto uriparser; then + if check_pkg liburiparser; then + enable uriparser + elif enabled uriparser; then + die "liburiparser development support not found (use --disable-uriparser)" + fi +fi + # # Bundling # diff --git a/debian/control b/debian/control index 29435c2a..b20b22d2 100644 --- a/debian/control +++ b/debian/control @@ -2,12 +2,12 @@ Source: tvheadend Section: video Priority: extra Maintainer: Andreas Öman -Build-Depends: debhelper (>= 7.0.50), pkg-config, libavahi-client-dev, libssl-dev, zlib1g-dev, wget, bzip2, libcurl4-gnutls-dev, git-core +Build-Depends: debhelper (>= 7.0.50), pkg-config, libavahi-client-dev, libssl-dev, zlib1g-dev, wget, bzip2, libcurl4-gnutls-dev, git-core, liburiparser-dev Standards-Version: 3.7.3 Package: tvheadend Architecture: any -Depends: ${shlibs:Depends}, libavahi-client3, zlib1g +Depends: ${shlibs:Depends}, libavahi-client3, zlib1g, liburiparser1 Recommends: xmltv-util Enhances: showtime Replaces: hts-tvheadend diff --git a/src/url.c b/src/url.c index 4357a05b..c0423429 100644 --- a/src/url.c +++ b/src/url.c @@ -25,8 +25,84 @@ #include #include +/* Use liburiparser if available */ +#if ENABLE_URIPARSER +#include + int urlparse ( const char *str, url_t *url ) +{ + UriParserStateA state; + UriPathSegmentA *path; + UriUriA uri; + char *s, buf[256]; + + /* Parse */ + state.uri = &uri; + if (uriParseUriA(&state, str) != URI_SUCCESS) { + uriFreeUriMembersA(&uri); + return -1; + } + + /* Store raw */ + strncpy(url->raw, str, sizeof(url->raw)); + + /* Copy */ +#define uri_copy(y, x)\ + if (x.first) {\ + size_t len = x.afterLast - x.first;\ + strncpy(y, x.first, len);\ + y[len] = 0;\ + } else {\ + *y = 0;\ + } + uri_copy(url->scheme, uri.scheme); + uri_copy(url->host, uri.hostText); + uri_copy(url->user, uri.userInfo); + uri_copy(url->query, uri.query); + uri_copy(url->frag, uri.fragment); + uri_copy(buf, uri.portText); + if (*buf) + url->port = atoi(buf); + else + url->port = 0; + *url->path = 0; + path = uri.pathHead; + while (path) { + strcat(url->path, "/"); + uri_copy(buf, path->text); + strcat(url->path, buf); + path = path->next; + } + // TODO: query/fragment + + /* Split user/pass */ + s = strstr(url->user, ":"); + if (s) { + strcpy(url->pass, s+1); + *s = 0; + } else { + *url->pass = 0; + } + + /* Cleanup */ + uriFreeUriMembersA(&uri); + return 0; +} + +/* Fallback to limited support */ +#else /* ENABLE_URIPARSER */ + +/* URL regexp - I probably found this online */ +// TODO: does not support ipv6 +#define UC "[a-z0-9_\\-\\.!£$%^&]" +#define PC UC +#define HC "[a-z0-9\\-\\.]" +#define URL_RE "^(\\w+)://(("UC"+)(:("PC"+))?@)?("HC"+)(:([0-9]+))?(/.*)?" + + +int +urlparse ( const char *str, url_t *url ) { static regex_t *exp = NULL; regmatch_t m[12]; @@ -61,9 +137,12 @@ urlparse ( const char *str, url_t *url ) copy(url->path, 9); copy(buf, 8); url->port = atoi(buf); + *url->query = 0; + *url->frag = 0; strncpy(url->raw, str, sizeof(url->raw)); - return 0; } + +#endif /* ENABLE_URIPARSER */ diff --git a/src/url.h b/src/url.h index 2c0d92e1..200c5905 100644 --- a/src/url.h +++ b/src/url.h @@ -22,26 +22,23 @@ #include -// TODO: might be better to find a lib to do this! +// TODO: limits are a bit arbitrary and it's a bit inflexible, but it +// does keep things simple, not having dynamically allocated strings /* URL structure */ typedef struct url { - char scheme[16]; - char user[128]; - char pass[128]; - char host[256]; - uint16_t port; - char path[256]; - char raw[1024]; + char scheme[32]; + char user[128]; + char pass[128]; + char host[256]; + short port; + char path[256]; + char query[1024]; + char frag[256]; + char raw[2048]; } url_t; -/* URL regexp - I probably found this online */ -#define UC "[a-z0-9_\\-\\.!£$%^&]" -#define PC UC -#define HC "[a-z0-9\\-\\.]" -#define URL_RE "^(\\w+)://(("UC"+)(:("PC"+))?@)?("HC"+)(:([0-9]+))?(/.*)?" - int urlparse ( const char *str, url_t *url ); #endif