#include <stdlib.h>
#include "Url.h"
#include "Test.h"
BEGIN_NAMESPACE
Url::Url()
{
m_urlStr = "http://localhost:80/";
crackUrl();
}
Url::Url(const String& a_url)
{
m_urlStr = a_url;
crackUrl();
}
Url::~Url()
{
}
String Url::hostName() const
{
return m_hostName;
}
String Url::userName() const
{
return m_userName;
}
String Url::password() const
{
return m_password;
}
int Url::port() const
{
return m_port;
}
String Url::path() const
{
return m_path;
}
Protocol Url::protocol() const
{
return m_protocol;
}
Protocol protocolLookup(String a_str)
{
// TODO
return P_HTTP;
}
void Url::crackUrl()
{
std::string str = m_urlStr.toUtf8();
std::size_t hostStart = str.find("://");
hostStart = (hostStart == std::string::npos) ? 0 : (hostStart + 3);
std::size_t pathStart = str.find("/", hostStart);
std::string userPassAtHostPort = str.substr(hostStart, pathStart - hostStart);
std::size_t hostPortStart = userPassAtHostPort.find("@");
std::string userPass = (hostPortStart == std::string::npos) ? "" : userPassAtHostPort.substr(0, hostPortStart);
std::string hostPort = userPassAtHostPort.substr((hostPortStart == std::string::npos) ? 0 : hostPortStart + 1, -1);
std::size_t passStart = userPass.find(":");
std::size_t portStart = hostPort.find(":");
m_protocol = (hostStart) ? protocolLookup(str.substr(0, hostStart)) : P_HTTP;
m_userName = userPass.substr(0, passStart);
m_password = (passStart == std::string::npos) ? "" : userPass.substr(passStart + 1, -1);
m_hostName = hostPort.substr(0, portStart);
m_port = (portStart == std::string::npos) ? 80 : ::atoi(hostPort.substr(portStart + 1, -1).c_str());
m_path = (pathStart == std::string::npos) ? "/" : str.substr(pathStart, -1);
}
UNIT_TEST(UrlTests, 0)
{
Url defaultUrl;
CHECK(defaultUrl.protocol() == P_HTTP);
CHECK(defaultUrl.userName() == "");
CHECK(defaultUrl.password() == "");
CHECK(defaultUrl.hostName() == "localhost");
CHECK(defaultUrl.port() == 80);
CHECK(defaultUrl.path() == "/");
Url testUrl1("http://john:pass@host.com:8080/path/sub/file.html");
CHECK(testUrl1.protocol() == P_HTTP);
CHECK(testUrl1.userName() == "john");
CHECK(testUrl1.password() == "pass");
CHECK(testUrl1.hostName() == "host.com");
CHECK(testUrl1.port() == 8080);
CHECK(testUrl1.path() == "/path/sub/file.html");
Url testUrl2("http://john@host.com/path/sub/file.html");
CHECK(testUrl2.protocol() == P_HTTP);
CHECK(testUrl2.userName() == "john");
CHECK(testUrl2.password() == "");
CHECK(testUrl2.hostName() == "host.com");
CHECK(testUrl2.port() == 80);
CHECK(testUrl2.path() == "/path/sub/file.html");
Url testUrl3("http://host.com:8080/path/sub/file.html");
CHECK(testUrl3.protocol() == P_HTTP);
CHECK(testUrl3.userName() == "");
CHECK(testUrl3.password() == "");
CHECK(testUrl3.hostName() == "host.com");
CHECK(testUrl3.port() == 8080);
CHECK(testUrl3.path() == "/path/sub/file.html");
Url testUrl4("host.com/path/sub/file.html");
CHECK(testUrl4.protocol() == P_HTTP);
CHECK(testUrl4.userName() == "");
CHECK(testUrl4.password() == "");
CHECK(testUrl4.hostName() == "host.com");
CHECK(testUrl4.port() == 80);
CHECK(testUrl4.path() == "/path/sub/file.html");
Url testUrl5("http://host.com/path/sub/file.html");
CHECK(testUrl5.protocol() == P_HTTP);
CHECK(testUrl5.userName() == "");
CHECK(testUrl5.password() == "");
CHECK(testUrl5.hostName() == "host.com");
CHECK(testUrl5.port() == 80);
CHECK(testUrl5.path() == "/path/sub/file.html");
}
END_NAMESPACE