mirror of
https://gitlab.xiph.org/xiph/ezstream.git
synced 2024-09-15 04:08:07 -04:00
Move urlParse() to util.c
This commit is contained in:
parent
0f6de67977
commit
3ba4b06478
@ -74,7 +74,6 @@ typedef struct tag_ID3Tag {
|
||||
char genre;
|
||||
} ID3Tag;
|
||||
|
||||
int urlParse(const char *, char **, unsigned short *, char **);
|
||||
char * buildReencodeCommand(const char *, const char *, metadata_t);
|
||||
metadata_t getMetadata(const char *);
|
||||
FILE * openResource(stream_t, const char *, int *, metadata_t *,
|
||||
@ -118,57 +117,6 @@ sig_handler(int sig)
|
||||
}
|
||||
#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 *
|
||||
buildReencodeCommand(const char *extension, const char *fileName,
|
||||
metadata_t mdata)
|
||||
|
52
src/util.c
52
src/util.c
@ -29,6 +29,7 @@
|
||||
#ifdef HAVE_LANGINFO_H
|
||||
# include <langinfo.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#ifdef HAVE_LOCALE_H
|
||||
# include <locale.h>
|
||||
#endif
|
||||
@ -279,3 +280,54 @@ shellQuote(const char *in)
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -26,5 +26,6 @@ char * CHARtoUTF8(const char *, int);
|
||||
char * UTF8toCHAR(const char *, int);
|
||||
char * replaceString(const char *, const char *, const char *);
|
||||
char * shellQuote(const char *);
|
||||
int urlParse(const char *, char **, unsigned short *, char **);
|
||||
|
||||
#endif /* __UTIL_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user