diff --git a/src/url.c b/src/url.c new file mode 100644 index 00000000..4357a05b --- /dev/null +++ b/src/url.c @@ -0,0 +1,69 @@ +/* + * Tvheadend - URL Processing + * + * Copyright (C) 2013 Adam Sutton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "tvheadend.h" +#include "url.h" + +#include +#include +#include +#include + +int +urlparse ( const char *str, url_t *url ) +{ + static regex_t *exp = NULL; + regmatch_t m[12]; + char buf[16]; + + /* Create regexp */ + if (!exp) { + exp = calloc(1, sizeof(regex_t)); + if (regcomp(exp, URL_RE, REG_ICASE | REG_EXTENDED)) { + tvherror("iptv", "failed to compile regexp"); + exit(1); + } + } + + /* Execute */ + if (regexec(exp, str, 12, m, 0)) + return 1; + + /* Extract data */ +#define copy(x, i)\ + {\ + int len = m[i].rm_eo - m[i].rm_so;\ + if (len >= sizeof(x) - 1)\ + len = sizeof(x) - 1;\ + memcpy(x, str+m[i].rm_so, len);\ + x[len] = 0;\ + }(void)0 + copy(url->scheme, 1); + copy(url->user, 3); + copy(url->pass, 5); + copy(url->host, 6); + copy(url->path, 9); + copy(buf, 8); + url->port = atoi(buf); + + strncpy(url->raw, str, sizeof(url->raw)); + + + return 0; +} diff --git a/src/url.h b/src/url.h new file mode 100644 index 00000000..2c0d92e1 --- /dev/null +++ b/src/url.h @@ -0,0 +1,47 @@ +/* + * Tvheadend - URL Processing + * + * Copyright (C) 2013 Adam Sutton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __TVH_URL_H__ +#define __TVH_URL_H__ + +#include + +// TODO: might be better to find a lib to do this! + +/* 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]; +} 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