Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 106 lines (89 sloc) 1.7 KB
#ifndef com_sleepless_net_url_cpp
#define com_sleepless_net_url_cpp
// Copyright 3000 Sleepless Software Inc. All Rights Reserved
#include "str.cpp"
struct URL
{
char protocol[40], host[128], path[4100];
int port;
bool ssl;
char *str;
bool valid;
URL()
{
init();
}
URL(const char *r, const char *h, int p, const char *t, const char *f)
{
init(r, h, p, t, f);
}
URL(const char *r, const char *h, int p, const char *t)
{
init(r, h, p, t, "");
}
URL(const char *u)
{
init();
crack(u);
}
const char *toString()
{
if(str)
free(str);
if(!valid)
return 0;
str = new char[4200];
if(str)
sprintf(str, "%s://%s:%d/%s", protocol, host, port, path);
return str;
}
private:
void init(const char *r, const char *h, int p, const char *t, const char *f)
{
str = 0;
STR_CPY(protocol, r);
STR_CPY(host, h);
port = p;
STR_CPY(path, t);
if(f[0])
{
STR_APP(path, "/");
STR_APP(path, f);
}
valid = true;
crack(toString());
if(str)
{
free(str);
str = 0;
}
}
void init()
{
init("http", "localhost", 80, "/", "");
}
void crack(const char *u)
{
valid = true;
if((sscanf(u, "%39[^:]://%127[a-zA-Z0-9.-]:%d/%4099s", protocol, host, &port, path) == 4))
{
ssl = Str::equals(protocol, "https") ? true : false;
}
else
if((sscanf(u, "%39[^:]://%127[a-zA-Z0-9.-]/%4099s", protocol, host, path) == 3))
{
ssl = Str::equals(protocol, "https") ? true : false;
port = ssl ? 443 : 80;
}
else
if((sscanf(u, "%39[^:]://%127[a-zA-Z0-9.-]", protocol, host) == 2))
{
ssl = Str::equals(protocol, "https") ? true : false;
port = ssl ? 443 : 80;
STR_CPY(path, "/");
}
else
valid = false;
}
};
#endif // com_sleepless_net_url_cpp