mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-30 16:05:51 +00:00
Add UniformResourceLocator and ParsedURL
This commit is contained in:
@@ -89,16 +89,15 @@
|
||||
<ClInclude Include="..\..\modules\beast_asio\basics\PeerRole.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\basics\SSLContext.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\beast_asio.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\client\HttpClient.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\parsehttp\http_parser.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogic.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogicPROXY.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogicSSL2.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogicSSL3.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetector.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HttpClient.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\InputParser.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\PrefilledReadStream.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\UniformResourceLocator.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\sockets\Socket.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\sockets\SocketBase.h" />
|
||||
<ClInclude Include="..\..\modules\beast_asio\sockets\SocketWrapper.h" />
|
||||
@@ -373,6 +372,12 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_asio\protocol\UniformResourceLocator.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_asio\sockets\Socket.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
|
||||
@@ -968,9 +968,6 @@
|
||||
<ClInclude Include="..\..\modules\beast_asio\tests\TestPeerType.h">
|
||||
<Filter>beast_asio\tests</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_asio\client\HttpClient.h">
|
||||
<Filter>beast_asio</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogic.h">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClInclude>
|
||||
@@ -986,15 +983,15 @@
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HandshakeDetector.h">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\HttpClient.h">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\InputParser.h">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\PrefilledReadStream.h">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\beast_asio\protocol\UniformResourceLocator.h">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">
|
||||
@@ -1483,6 +1480,9 @@
|
||||
<ClCompile Include="..\..\modules\beast_asio\protocol\HandshakeDetectLogicPROXY.cpp">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_asio\protocol\UniformResourceLocator.cpp">
|
||||
<Filter>beast_asio\protocol</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="..\..\TODO.txt" />
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace beast
|
||||
#include "protocol/HandshakeDetectLogicPROXY.cpp"
|
||||
|
||||
# include "parsehttp/http_parser.h"
|
||||
#include "protocol/UniformResourceLocator.cpp"
|
||||
|
||||
#include "tests/PeerTest.cpp"
|
||||
#include "tests/TestPeerBasics.cpp"
|
||||
|
||||
@@ -87,6 +87,7 @@ namespace beast
|
||||
#include "protocol/HandshakeDetectLogicSSL3.h"
|
||||
#include "protocol/HandshakeDetector.h"
|
||||
#include "protocol/PrefilledReadStream.h"
|
||||
#include "protocol/UniformResourceLocator.h"
|
||||
|
||||
#include "tests/TestPeerBasics.h"
|
||||
#include "tests/TestPeer.h"
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_ASIO_PROTOCOL_HTTPCLIENT_H_INCLUDED
|
||||
#define BEAST_ASIO_PROTOCOL_HTTPCLIENT_H_INCLUDED
|
||||
|
||||
#endif
|
||||
299
modules/beast_asio/protocol/UniformResourceLocator.cpp
Normal file
299
modules/beast_asio/protocol/UniformResourceLocator.cpp
Normal file
@@ -0,0 +1,299 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
UniformResourceLocator::UniformResourceLocator ()
|
||||
: m_port (0)
|
||||
{
|
||||
}
|
||||
|
||||
UniformResourceLocator::UniformResourceLocator (
|
||||
String scheme_,
|
||||
String host_,
|
||||
uint16 port_,
|
||||
String port_string_,
|
||||
String path_,
|
||||
String query_,
|
||||
String fragment_,
|
||||
String userinfo_)
|
||||
: m_scheme (scheme_)
|
||||
, m_host (host_)
|
||||
, m_port (port_)
|
||||
, m_port_string (port_string_)
|
||||
, m_path (path_)
|
||||
, m_query (query_)
|
||||
, m_fragment (fragment_)
|
||||
, m_userinfo (userinfo_)
|
||||
{
|
||||
}
|
||||
|
||||
UniformResourceLocator::UniformResourceLocator (UniformResourceLocator const& other)
|
||||
: m_scheme (other.m_scheme)
|
||||
, m_host (other.m_host)
|
||||
, m_port (other.m_port)
|
||||
, m_port_string (other.m_port_string)
|
||||
, m_path (other.m_path)
|
||||
, m_query (other.m_query)
|
||||
, m_fragment (other.m_fragment)
|
||||
, m_userinfo (other.m_userinfo)
|
||||
{
|
||||
}
|
||||
|
||||
UniformResourceLocator& UniformResourceLocator::operator= (UniformResourceLocator const& other)
|
||||
{
|
||||
m_scheme = other.m_scheme;
|
||||
m_host = other.m_host;
|
||||
m_port = other.m_port;
|
||||
m_port_string = other.m_port_string;
|
||||
m_path = other.m_path;
|
||||
m_query = other.m_query;
|
||||
m_fragment = other.m_fragment;
|
||||
m_userinfo = other.m_userinfo;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
String UniformResourceLocator::scheme () const
|
||||
{
|
||||
return m_scheme;
|
||||
}
|
||||
|
||||
String UniformResourceLocator::host () const
|
||||
{
|
||||
return m_host;
|
||||
}
|
||||
|
||||
String UniformResourceLocator::port_string () const
|
||||
{
|
||||
return m_port_string;
|
||||
}
|
||||
|
||||
uint16 UniformResourceLocator::port () const
|
||||
{
|
||||
return m_port;
|
||||
}
|
||||
|
||||
String UniformResourceLocator::path () const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
String UniformResourceLocator::query () const
|
||||
{
|
||||
return m_query;
|
||||
}
|
||||
|
||||
String UniformResourceLocator::fragment () const
|
||||
{
|
||||
return m_fragment;
|
||||
}
|
||||
|
||||
String UniformResourceLocator::userinfo () const
|
||||
{
|
||||
return m_userinfo;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
From
|
||||
http://en.wikipedia.org/wiki/URI_scheme
|
||||
|
||||
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
|
||||
|
||||
e.g.
|
||||
|
||||
foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose
|
||||
*/
|
||||
String UniformResourceLocator::full () const
|
||||
{
|
||||
String s;
|
||||
|
||||
s = scheme () + "://";
|
||||
|
||||
if (userinfo () != String::empty)
|
||||
s = userinfo () + "@";
|
||||
|
||||
s = s + host ();
|
||||
|
||||
if (port () != 0)
|
||||
s = s + ":" + String::fromNumber (port ());
|
||||
|
||||
s = s + path ();
|
||||
|
||||
if (query () != String::empty)
|
||||
s = "?" + query ();
|
||||
|
||||
if (fragment () != String::empty)
|
||||
s = "#" + fragment ();
|
||||
|
||||
return s;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
ParsedURL::ParsedURL ()
|
||||
: m_error (0)
|
||||
{
|
||||
}
|
||||
|
||||
ParsedURL::ParsedURL (String const& url)
|
||||
{
|
||||
std::string const ss (url.toStdString ());
|
||||
std::size_t const buflen (ss.size ());
|
||||
char const* const buf (ss.c_str ());
|
||||
|
||||
http_parser_url u;
|
||||
|
||||
m_error = http_parser_parse_url (buf, buflen, false, &u);
|
||||
|
||||
String scheme_;
|
||||
String host_;
|
||||
uint16 port_ (0);
|
||||
String port_string_;
|
||||
String path_;
|
||||
String query_;
|
||||
String fragment_;
|
||||
String userinfo_;
|
||||
|
||||
if (m_error == 0)
|
||||
{
|
||||
if ((u.field_set & (1<<UF_SCHEMA)) != 0)
|
||||
{
|
||||
scheme_ = String (
|
||||
buf + u.field_data [UF_SCHEMA].off,
|
||||
u.field_data [UF_SCHEMA].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_HOST)) != 0)
|
||||
{
|
||||
host_ = String (
|
||||
buf + u.field_data [UF_HOST].off,
|
||||
u.field_data [UF_HOST].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_PORT)) != 0)
|
||||
{
|
||||
port_ = u.port;
|
||||
port_string_ = String (
|
||||
buf + u.field_data [UF_PORT].off,
|
||||
u.field_data [UF_PORT].len);
|
||||
}
|
||||
else
|
||||
{
|
||||
port_ = 0;
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_PATH)) != 0)
|
||||
{
|
||||
path_ = String (
|
||||
buf + u.field_data [UF_PATH].off,
|
||||
u.field_data [UF_PATH].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_QUERY)) != 0)
|
||||
{
|
||||
query_ = String (
|
||||
buf + u.field_data [UF_QUERY].off,
|
||||
u.field_data [UF_QUERY].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_FRAGMENT)) != 0)
|
||||
{
|
||||
fragment_ = String (
|
||||
buf + u.field_data [UF_FRAGMENT].off,
|
||||
u.field_data [UF_FRAGMENT].len);
|
||||
}
|
||||
|
||||
if ((u.field_set & (1<<UF_USERINFO)) != 0)
|
||||
{
|
||||
userinfo_ = String (
|
||||
buf + u.field_data [UF_USERINFO].off,
|
||||
u.field_data [UF_USERINFO].len);
|
||||
}
|
||||
|
||||
m_url = UniformResourceLocator (
|
||||
scheme_,
|
||||
host_,
|
||||
port_,
|
||||
port_string_,
|
||||
path_,
|
||||
query_,
|
||||
fragment_,
|
||||
userinfo_);
|
||||
}
|
||||
}
|
||||
|
||||
ParsedURL::ParsedURL (int error, UniformResourceLocator const& url)
|
||||
: m_error (error)
|
||||
, m_url (url)
|
||||
{
|
||||
}
|
||||
|
||||
ParsedURL::ParsedURL (ParsedURL const& other)
|
||||
: m_error (other.m_error)
|
||||
, m_url (other.m_url)
|
||||
{
|
||||
}
|
||||
|
||||
ParsedURL& ParsedURL::operator= (ParsedURL const& other)
|
||||
{
|
||||
m_error = other.m_error;
|
||||
m_url = other.m_url;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int ParsedURL::error () const
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
UniformResourceLocator ParsedURL::url () const
|
||||
{
|
||||
return m_url;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class ParsedURLTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
void checkURL (String const& url)
|
||||
{
|
||||
ParsedURL result (url);
|
||||
expect (result.error () == 0);
|
||||
expect (result.url ().full () == url);
|
||||
}
|
||||
|
||||
void testURL ()
|
||||
{
|
||||
beginTestCase ("parse URL");
|
||||
|
||||
checkURL ("http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference.html");
|
||||
}
|
||||
|
||||
void runTest ()
|
||||
{
|
||||
testURL ();
|
||||
}
|
||||
|
||||
ParsedURLTests () : UnitTest ("ParsedURL", "beast", runManual)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
static ParsedURLTests parsedURLTests;
|
||||
123
modules/beast_asio/protocol/UniformResourceLocator.h
Normal file
123
modules/beast_asio/protocol/UniformResourceLocator.h
Normal file
@@ -0,0 +1,123 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_ASIO_PROTOCOL_UNIFORMRESOURCELOCATOR_H_INCLUDED
|
||||
#define BEAST_ASIO_PROTOCOL_UNIFORMRESOURCELOCATOR_H_INCLUDED
|
||||
|
||||
/** A URL.
|
||||
The accompanying robust parser is hardened against all forms of attack.
|
||||
*/
|
||||
class UniformResourceLocator
|
||||
{
|
||||
public:
|
||||
/** Construct an empty URL. */
|
||||
explicit UniformResourceLocator ();
|
||||
|
||||
/** Construct a URL from it's components. */
|
||||
UniformResourceLocator (
|
||||
String schema_,
|
||||
String host_,
|
||||
uint16 port_,
|
||||
String port_string_,
|
||||
String path_,
|
||||
String query_ = "",
|
||||
String fragment_ = "",
|
||||
String userinfo_ = "");
|
||||
|
||||
/** Copy construct a URL. */
|
||||
UniformResourceLocator (UniformResourceLocator const& other);
|
||||
|
||||
/** Copy assign a URL. */
|
||||
UniformResourceLocator& operator= (UniformResourceLocator const& other);
|
||||
|
||||
/** Returns the scheme of the URL.
|
||||
If no scheme was specified, the string will be empty.
|
||||
*/
|
||||
String scheme () const;
|
||||
|
||||
/** Returns the host of the URL.
|
||||
If no host was specified, the string will be empty.
|
||||
*/
|
||||
String host () const;
|
||||
|
||||
/** Returns the port number as an integer.
|
||||
If no port was specified, the value will be zero.
|
||||
*/
|
||||
uint16 port () const;
|
||||
|
||||
/** Returns the port number as a string.
|
||||
If no port was specified, the string will be empty.
|
||||
*/
|
||||
String port_string () const;
|
||||
|
||||
/** Returns the path of the URL.
|
||||
If no path was specified, the string will be empty.
|
||||
*/
|
||||
String path () const;
|
||||
|
||||
/** Returns the query parameters portion of the URL.
|
||||
If no query parameters were present, the string will be empty.
|
||||
*/
|
||||
String query () const;
|
||||
|
||||
/** Returns the URL fragment, if any. */
|
||||
String fragment () const;
|
||||
|
||||
/** Returns the user information, if any. */
|
||||
String userinfo () const;
|
||||
|
||||
/** Retrieve the full URL as a single string. */
|
||||
String full () const;
|
||||
|
||||
private:
|
||||
String m_scheme;
|
||||
String m_host;
|
||||
uint16 m_port;
|
||||
String m_port_string;
|
||||
String m_path;
|
||||
String m_query;
|
||||
String m_fragment;
|
||||
String m_userinfo;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Parses a String containing a URL.
|
||||
*/
|
||||
class ParsedURL
|
||||
{
|
||||
public:
|
||||
ParsedURL ();
|
||||
explicit ParsedURL (String const& url);
|
||||
ParsedURL (int error, UniformResourceLocator const& url);
|
||||
ParsedURL (ParsedURL const& other);
|
||||
ParsedURL& operator= (ParsedURL const& other);
|
||||
|
||||
/** Zero for success, else a non zero value indicating failure. */
|
||||
int error () const;
|
||||
|
||||
/** The parsed URL if there was no error. */
|
||||
UniformResourceLocator url () const;
|
||||
|
||||
private:
|
||||
int m_error;
|
||||
UniformResourceLocator m_url;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user