1
0
mirror of https://gitlab.xiph.org/xiph/ezstream.git synced 2024-11-03 04:17:18 -05:00

Move urlParse() to util.c

This commit is contained in:
Moritz Grimm 2016-05-19 14:06:31 +02:00
parent 0f6de67977
commit 3ba4b06478
3 changed files with 53 additions and 52 deletions

View File

@ -74,7 +74,6 @@ typedef struct tag_ID3Tag {
char genre; char genre;
} ID3Tag; } ID3Tag;
int urlParse(const char *, char **, unsigned short *, char **);
char * buildReencodeCommand(const char *, const char *, metadata_t); char * buildReencodeCommand(const char *, const char *, metadata_t);
metadata_t getMetadata(const char *); metadata_t getMetadata(const char *);
FILE * openResource(stream_t, const char *, int *, metadata_t *, FILE * openResource(stream_t, const char *, int *, metadata_t *,
@ -118,57 +117,6 @@ sig_handler(int sig)
} }
#endif /* HAVE_SIGNALS */ #endif /* HAVE_SIGNALS */
int
urlParse(const char *url, char **hostname, unsigned short *port,
char **mountname)
{
const char *p1, *p2, *p3;
char tmpPort[6] = "";
size_t hostsiz, mountsiz;
const char *errstr;
if (strncmp(url, "http://", strlen("http://")) != 0) {
log_error("invalid <url>: not an HTTP address");
return (0);
}
p1 = url + strlen("http://");
p2 = strchr(p1, ':');
if (p2 == NULL) {
log_error("invalid <url>: missing port");
return (0);
}
hostsiz = (p2 - p1) + 1;
if (hostsiz <= 1) {
log_error("invalid <url>: missing host");
return (0);
}
*hostname = xmalloc(hostsiz);
strlcpy(*hostname, p1, hostsiz);
p2++;
p3 = strchr(p2, '/');
if (p3 == NULL || p3 - p2 >= (int)sizeof(tmpPort)) {
log_error("invalid <url>: mountpoint missing, or port number too long");
xfree(*hostname);
return (0);
}
strlcpy(tmpPort, p2, (p3 - p2) + 1UL);
*port = (unsigned short)strtonum(tmpPort, 1LL, (long long)USHRT_MAX, &errstr);
if (errstr) {
log_error("invalid <url>: port: %s is %s", tmpPort, errstr);
xfree(*hostname);
return (0);
}
mountsiz = strlen(p3) + 1;
*mountname = xmalloc(mountsiz);
strlcpy(*mountname, p3, mountsiz);
return (1);
}
char * char *
buildReencodeCommand(const char *extension, const char *fileName, buildReencodeCommand(const char *extension, const char *fileName,
metadata_t mdata) metadata_t mdata)

View File

@ -29,6 +29,7 @@
#ifdef HAVE_LANGINFO_H #ifdef HAVE_LANGINFO_H
# include <langinfo.h> # include <langinfo.h>
#endif #endif
#include <limits.h>
#ifdef HAVE_LOCALE_H #ifdef HAVE_LOCALE_H
# include <locale.h> # include <locale.h>
#endif #endif
@ -279,3 +280,54 @@ shellQuote(const char *in)
return (out); return (out);
} }
int
urlParse(const char *url, char **hostname, unsigned short *port,
char **mountname)
{
const char *p1, *p2, *p3;
char tmpPort[6] = "";
size_t hostsiz, mountsiz;
const char *errstr;
if (strncmp(url, "http://", strlen("http://")) != 0) {
log_error("invalid <url>: not an HTTP address");
return (0);
}
p1 = url + strlen("http://");
p2 = strchr(p1, ':');
if (p2 == NULL) {
log_error("invalid <url>: missing port");
return (0);
}
hostsiz = (p2 - p1) + 1;
if (hostsiz <= 1) {
log_error("invalid <url>: missing host");
return (0);
}
*hostname = xmalloc(hostsiz);
strlcpy(*hostname, p1, hostsiz);
p2++;
p3 = strchr(p2, '/');
if (p3 == NULL || p3 - p2 >= (int)sizeof(tmpPort)) {
log_error("invalid <url>: mountpoint missing, or port number too long");
xfree(*hostname);
return (0);
}
strlcpy(tmpPort, p2, (p3 - p2) + 1UL);
*port = (unsigned short)strtonum(tmpPort, 1LL, (long long)USHRT_MAX, &errstr);
if (errstr) {
log_error("invalid <url>: port: %s is %s", tmpPort, errstr);
xfree(*hostname);
return (0);
}
mountsiz = strlen(p3) + 1;
*mountname = xmalloc(mountsiz);
strlcpy(*mountname, p3, mountsiz);
return (1);
}

View File

@ -26,5 +26,6 @@ char * CHARtoUTF8(const char *, int);
char * UTF8toCHAR(const char *, int); char * UTF8toCHAR(const char *, int);
char * replaceString(const char *, const char *, const char *); char * replaceString(const char *, const char *, const char *);
char * shellQuote(const char *); char * shellQuote(const char *);
int urlParse(const char *, char **, unsigned short *, char **);
#endif /* __UTIL_H__ */ #endif /* __UTIL_H__ */