Add HTTPRequest and improvements to HTTPMessage parsing

This commit is contained in:
Vinnie Falco
2013-09-20 12:58:41 -07:00
parent 9534516b42
commit 373ca9cef0
9 changed files with 152 additions and 6 deletions

View File

@@ -117,6 +117,7 @@
<ClInclude Include="..\..\modules\beast_asio\http\HTTPMessage.h" />
<ClInclude Include="..\..\modules\beast_asio\http\HTTPParserImpl.h" />
<ClInclude Include="..\..\modules\beast_asio\http\HTTPParser.h" />
<ClInclude Include="..\..\modules\beast_asio\http\HTTPRequest.h" />
<ClInclude Include="..\..\modules\beast_asio\http\HTTPResponse.h" />
<ClInclude Include="..\..\modules\beast_asio\http\HTTPVersion.h" />
<ClInclude Include="..\..\modules\beast_asio\http\UniformResourceLocator.h" />
@@ -434,6 +435,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\http\HTTPRequest.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\http\HTTPResponse.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

View File

@@ -1095,6 +1095,9 @@
<ClInclude Include="..\..\beast\Uncopyable.h">
<Filter>beast</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_asio\http\HTTPRequest.h">
<Filter>beast_asio\http</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">
@@ -1622,6 +1625,9 @@
<ClCompile Include="..\..\beast\utility\impl\Journal.cpp">
<Filter>beast\utility\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_asio\http\HTTPRequest.cpp">
<Filter>beast_asio\http</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt">

View File

@@ -45,6 +45,7 @@ namespace beast
#include "http/HTTPField.cpp"
#include "http/HTTPHeaders.cpp"
#include "http/HTTPMessage.cpp"
#include "http/HTTPRequest.cpp"
#include "http/HTTPResponse.cpp"
#include "http/HTTPVersion.cpp"

View File

@@ -82,6 +82,7 @@ namespace beast
# include "http/HTTPField.h"
# include "http/HTTPHeaders.h"
# include "http/HTTPMessage.h"
# include "http/HTTPRequest.h"
# include "http/HTTPResponse.h"
# include "http/HTTPParser.h"
# include "http/UniformResourceLocator.h"

View File

@@ -44,7 +44,15 @@ std::size_t HTTPParser::process (void const* buf, std::size_t bytes)
if (m_impl->finished ())
{
if (m_type == typeResponse)
if (m_type == typeRequest)
{
m_request = new HTTPRequest (
m_impl->version (),
m_impl->fields (),
m_impl->body (),
m_impl->method ());
}
else if (m_type == typeResponse)
{
m_response = new HTTPResponse (
m_impl->version (),
@@ -54,7 +62,7 @@ std::size_t HTTPParser::process (void const* buf, std::size_t bytes)
}
else
{
// m_request = new HTTPRequest (
bassertfalse;
}
}
@@ -71,6 +79,23 @@ bool HTTPParser::finished () const
return m_impl->finished();
}
StringPairArray const& HTTPParser::fields () const
{
return m_impl->fields();
}
bool HTTPParser::headersComplete () const
{
return m_impl->headers_complete();
}
SharedPtr <HTTPRequest> const& HTTPParser::request ()
{
bassert (m_type == typeRequest);
return m_request;
}
SharedPtr <HTTPResponse> const& HTTPParser::response ()
{
bassert (m_type == typeResponse);

View File

@@ -57,18 +57,29 @@ public:
/** Returns `true` when parsing is successful and complete. */
bool finished () const;
/** Return the HTTPResponse object produce from the parsing.
/** Peek at the header fields as they are being built.
Only complete pairs will show up, never partial strings.
*/
StringPairArray const& fields () const;
/** Returns `true` if all the HTTP headers have been received. */
bool headersComplete () const;
/** Return the HTTPRequest object produced from the parsiing.
Only valid after finished returns `true`.
*/
SharedPtr <HTTPRequest> const& request ();
/** Return the HTTPResponse object produced from the parsing.
Only valid after finished returns `true`.
*/
SharedPtr <HTTPResponse> const& response ();
//SharedPtr <HTTPRequest> const& request ();
private:
Type m_type;
ScopedPointer <HTTPParserImpl> m_impl;
SharedPtr <HTTPRequest> m_request;
SharedPtr <HTTPResponse> m_response;
//SharedPtr <HTTPRequest> m_request;
};
#endif

View File

@@ -31,6 +31,7 @@ public:
explicit HTTPParserImpl (enum http_parser_type type)
: m_finished (false)
, m_was_value (false)
, m_headersComplete (false)
{
m_settings.on_message_begin = &HTTPParserImpl::on_message_begin;
m_settings.on_url = &HTTPParserImpl::on_url;
@@ -119,6 +120,11 @@ public:
return m_fields;
}
bool headers_complete () const
{
return m_headersComplete;
}
ContentBodyBuffer& body ()
{
return m_body;
@@ -174,6 +180,7 @@ private:
int onHeadersComplete ()
{
m_headersComplete = true;
int ec (0);
addFieldValue ();
return ec;
@@ -250,6 +257,7 @@ private:
bool m_was_value;
std::string m_field;
std::string m_value;
bool m_headersComplete;
ContentBodyBuffer m_body;
};

View File

@@ -0,0 +1,42 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================
HTTPRequest::HTTPRequest (
HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body,
unsigned short method_)
: HTTPMessage (version_, fields, body)
, m_method (method_)
{
}
unsigned short HTTPRequest::method () const
{
return m_method;
}
String HTTPRequest::toString () const
{
String s;
s << "Method: " << String::fromNumber (method ()) << newLine;
s << this->HTTPMessage::toString ();
return s;
}

View File

@@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
/*
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_HTTPREQUEST_H_INCLUDED
#define BEAST_ASIO_HTTPREQUEST_H_INCLUDED
class HTTPRequest : public HTTPMessage
{
public:
/** Construct a complete response from values.
Ownership of the fields and body parameters are
transferred from the caller.
*/
HTTPRequest (
HTTPVersion const& version_,
StringPairArray& fields,
ContentBodyBuffer& body,
unsigned short method_);
unsigned short method () const;
/** Convert the request into a string, excluding the body. */
String toString () const;
private:
unsigned short m_method;
};
#endif