2019-03-26 14:35:33 +00:00
|
|
|
#include "wsfs/provider/impl/url.h"
|
2019-02-23 14:57:57 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
struct wsfsp_impl_url_protocol
|
2019-02-23 14:57:57 +00:00
|
|
|
{
|
|
|
|
char const * name;
|
|
|
|
size_t name_length;
|
|
|
|
int default_port;
|
2019-02-23 15:19:00 +00:00
|
|
|
bool use_tls;
|
2019-02-23 14:57:57 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
static bool wsfsp_impl_url_readprotocol(
|
|
|
|
struct wsfsp_impl_url * url,
|
2019-02-23 14:57:57 +00:00
|
|
|
char const * * data)
|
|
|
|
{
|
2019-03-26 14:35:33 +00:00
|
|
|
static struct wsfsp_impl_url_protocol const known_protocols[] =
|
2019-02-23 14:57:57 +00:00
|
|
|
{
|
2019-02-23 15:19:00 +00:00
|
|
|
{"ws://", 5, 80, false},
|
|
|
|
{"wss://", 6, 443, true}
|
2019-02-23 14:57:57 +00:00
|
|
|
};
|
|
|
|
static size_t const count = (sizeof(known_protocols) / sizeof(known_protocols[0]));
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for(size_t i = 0; (!found) && (i < count); i++)
|
|
|
|
{
|
2019-03-26 14:35:33 +00:00
|
|
|
struct wsfsp_impl_url_protocol const * protocol = &known_protocols[i];
|
2019-02-23 14:57:57 +00:00
|
|
|
if (0 == strncmp(*data, protocol->name, protocol->name_length))
|
|
|
|
{
|
|
|
|
url->port = protocol->default_port;
|
2019-02-23 15:19:00 +00:00
|
|
|
url->use_tls = protocol->use_tls;
|
2019-02-23 14:57:57 +00:00
|
|
|
*data = *data + protocol->name_length;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
static bool wsfsp_impl_url_readhost(
|
|
|
|
struct wsfsp_impl_url * url,
|
2019-02-23 14:57:57 +00:00
|
|
|
char const * * data)
|
|
|
|
{
|
|
|
|
char * end = strpbrk(*data, ":/");
|
|
|
|
bool const result = (NULL != end);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
size_t length = end - *data;
|
|
|
|
url->host = strndup(*data, length);
|
|
|
|
*data = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
static bool wsfsp_impl_url_readport(
|
|
|
|
struct wsfsp_impl_url * url,
|
2019-02-23 14:57:57 +00:00
|
|
|
char const * * data)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
if (':' == **data)
|
|
|
|
{
|
|
|
|
*data = *data + 1;
|
|
|
|
char * end = strchr(*data, '/');
|
|
|
|
result = (NULL != end);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
url->port = atoi(*data);
|
|
|
|
*data = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = ('/' == **data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
static bool wsfsp_impl_url_readpath(
|
|
|
|
struct wsfsp_impl_url * url,
|
2019-02-23 14:57:57 +00:00
|
|
|
char const * * data)
|
|
|
|
{
|
|
|
|
bool const result = ('/' == **data);
|
|
|
|
url->path = strdup(*data);
|
|
|
|
*data = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
bool wsfsp_impl_url_init(
|
|
|
|
struct wsfsp_impl_url * url,
|
2019-02-23 14:57:57 +00:00
|
|
|
char const * value)
|
|
|
|
{
|
2019-03-26 14:35:33 +00:00
|
|
|
memset(url, 0, sizeof(struct wsfsp_impl_url));
|
2019-02-23 14:57:57 +00:00
|
|
|
char const * data = value;
|
|
|
|
|
|
|
|
bool const result =
|
2019-03-26 14:35:33 +00:00
|
|
|
wsfsp_impl_url_readprotocol(url, &data) &&
|
|
|
|
wsfsp_impl_url_readhost(url, &data) &&
|
|
|
|
wsfsp_impl_url_readport(url, &data) &&
|
|
|
|
wsfsp_impl_url_readpath(url, &data)
|
2019-02-23 14:57:57 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
2019-03-26 14:35:33 +00:00
|
|
|
wsfsp_impl_url_cleanup(url);
|
2019-02-23 14:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
void wsfsp_impl_url_cleanup(
|
|
|
|
struct wsfsp_impl_url * url)
|
2019-02-23 14:57:57 +00:00
|
|
|
{
|
|
|
|
free(url->host);
|
|
|
|
free(url->path);
|
2019-03-26 14:35:33 +00:00
|
|
|
memset(url, 0, sizeof(struct wsfsp_impl_url));
|
2019-02-23 14:57:57 +00:00
|
|
|
}
|
|
|
|
|