Moved cHTTPRequest to a separate file, renamed to cHTTPRequestParser.
This commit is contained in:
parent
2dbc54a148
commit
b92346e3cc
@ -8,6 +8,7 @@ SET (SRCS
|
||||
EnvelopeParser.cpp
|
||||
HTTPFormParser.cpp
|
||||
HTTPMessage.cpp
|
||||
HTTPRequestParser.cpp
|
||||
HTTPServer.cpp
|
||||
HTTPServerConnection.cpp
|
||||
MultipartParser.cpp
|
||||
@ -20,6 +21,7 @@ SET (HDRS
|
||||
EnvelopeParser.h
|
||||
HTTPFormParser.h
|
||||
HTTPMessage.h
|
||||
HTTPRequestParser.h
|
||||
HTTPServer.h
|
||||
HTTPServerConnection.h
|
||||
MultipartParser.h
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
#include "Globals.h"
|
||||
#include "HTTPFormParser.h"
|
||||
#include "HTTPMessage.h"
|
||||
#include "HTTPRequestParser.h"
|
||||
#include "MultipartParser.h"
|
||||
#include "NameValueParser.h"
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
|
||||
|
||||
|
||||
cHTTPFormParser::cHTTPFormParser(cHTTPRequest & a_Request, cCallbacks & a_Callbacks) :
|
||||
cHTTPFormParser::cHTTPFormParser(cHTTPRequestParser & a_Request, cCallbacks & a_Callbacks) :
|
||||
m_Callbacks(a_Callbacks),
|
||||
m_IsValid(true),
|
||||
m_IsCurrentPartFile(false),
|
||||
@ -121,7 +121,7 @@ bool cHTTPFormParser::Finish(void)
|
||||
|
||||
|
||||
|
||||
bool cHTTPFormParser::HasFormData(const cHTTPRequest & a_Request)
|
||||
bool cHTTPFormParser::HasFormData(const cHTTPRequestParser & a_Request)
|
||||
{
|
||||
const AString & ContentType = a_Request.GetContentType();
|
||||
return (
|
||||
@ -138,7 +138,7 @@ bool cHTTPFormParser::HasFormData(const cHTTPRequest & a_Request)
|
||||
|
||||
|
||||
|
||||
void cHTTPFormParser::BeginMultipart(const cHTTPRequest & a_Request)
|
||||
void cHTTPFormParser::BeginMultipart(const cHTTPRequestParser & a_Request)
|
||||
{
|
||||
ASSERT(m_MultipartParser.get() == nullptr);
|
||||
m_MultipartParser.reset(new cMultipartParser(a_Request.GetContentType(), *this));
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
|
||||
// fwd:
|
||||
class cHTTPRequest;
|
||||
class cHTTPRequestParser;
|
||||
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ public:
|
||||
|
||||
|
||||
/** Creates a parser that is tied to a request and notifies of various events using a callback mechanism */
|
||||
cHTTPFormParser(cHTTPRequest & a_Request, cCallbacks & a_Callbacks);
|
||||
cHTTPFormParser(cHTTPRequestParser & a_Request, cCallbacks & a_Callbacks);
|
||||
|
||||
/** Creates a parser with the specified content type that reads data from a string */
|
||||
cHTTPFormParser(eKind a_Kind, const char * a_Data, size_t a_Size, cCallbacks & a_Callbacks);
|
||||
@ -64,7 +64,7 @@ public:
|
||||
bool Finish(void);
|
||||
|
||||
/** Returns true if the headers suggest the request has form data parseable by this class */
|
||||
static bool HasFormData(const cHTTPRequest & a_Request);
|
||||
static bool HasFormData(const cHTTPRequestParser & a_Request);
|
||||
|
||||
protected:
|
||||
|
||||
@ -97,7 +97,7 @@ protected:
|
||||
|
||||
|
||||
/** Sets up the object for parsing a fpkMultipart request */
|
||||
void BeginMultipart(const cHTTPRequest & a_Request);
|
||||
void BeginMultipart(const cHTTPRequestParser & a_Request);
|
||||
|
||||
/** Parses m_IncomingData as form-urlencoded data (fpkURL or fpkFormUrlEncoded kinds) */
|
||||
void ParseFormUrlEncoded(void);
|
||||
|
@ -35,8 +35,8 @@ cHTTPMessage::cHTTPMessage(eKind a_Kind) :
|
||||
|
||||
void cHTTPMessage::AddHeader(const AString & a_Key, const AString & a_Value)
|
||||
{
|
||||
AString Key = StrToLower(a_Key);
|
||||
cNameValueMap::iterator itr = m_Headers.find(Key);
|
||||
auto Key = StrToLower(a_Key);
|
||||
auto itr = m_Headers.find(Key);
|
||||
if (itr == m_Headers.end())
|
||||
{
|
||||
m_Headers[Key] = a_Value;
|
||||
@ -66,194 +66,6 @@ void cHTTPMessage::AddHeader(const AString & a_Key, const AString & a_Value)
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// cHTTPRequest:
|
||||
|
||||
cHTTPRequest::cHTTPRequest(void) :
|
||||
super(mkRequest),
|
||||
m_EnvelopeParser(*this),
|
||||
m_IsValid(true),
|
||||
m_UserData(nullptr),
|
||||
m_HasAuth(false),
|
||||
m_AllowKeepAlive(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t cHTTPRequest::ParseHeaders(const char * a_Data, size_t a_Size)
|
||||
{
|
||||
if (!m_IsValid)
|
||||
{
|
||||
return AString::npos;
|
||||
}
|
||||
|
||||
if (m_Method.empty())
|
||||
{
|
||||
// The first line hasn't been processed yet
|
||||
size_t res = ParseRequestLine(a_Data, a_Size);
|
||||
ASSERT((res == AString::npos) || (res <= a_Size));
|
||||
if ((res == AString::npos) || (res == a_Size))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
size_t res2 = m_EnvelopeParser.Parse(a_Data + res, a_Size - res);
|
||||
ASSERT((res2 == AString::npos) || (res2 <= a_Size - res));
|
||||
if (res2 == AString::npos)
|
||||
{
|
||||
m_IsValid = false;
|
||||
return res2;
|
||||
}
|
||||
return res2 + res;
|
||||
}
|
||||
|
||||
if (m_EnvelopeParser.IsInHeaders())
|
||||
{
|
||||
size_t res = m_EnvelopeParser.Parse(a_Data, a_Size);
|
||||
ASSERT((res == AString::npos) || (res <= a_Size));
|
||||
if (res == AString::npos)
|
||||
{
|
||||
m_IsValid = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
AString cHTTPRequest::GetBareURL(void) const
|
||||
{
|
||||
size_t idxQM = m_URL.find('?');
|
||||
if (idxQM != AString::npos)
|
||||
{
|
||||
return m_URL.substr(0, idxQM);
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_URL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t cHTTPRequest::ParseRequestLine(const char * a_Data, size_t a_Size)
|
||||
{
|
||||
auto inBufferSoFar = m_IncomingHeaderData.size();
|
||||
m_IncomingHeaderData.append(a_Data, a_Size);
|
||||
auto IdxEnd = m_IncomingHeaderData.size();
|
||||
|
||||
// Ignore the initial CRLFs (HTTP spec's "should")
|
||||
size_t LineStart = 0;
|
||||
while (
|
||||
(LineStart < IdxEnd) &&
|
||||
(
|
||||
(m_IncomingHeaderData[LineStart] == '\r') ||
|
||||
(m_IncomingHeaderData[LineStart] == '\n')
|
||||
)
|
||||
)
|
||||
{
|
||||
LineStart++;
|
||||
}
|
||||
if (LineStart >= IdxEnd)
|
||||
{
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
|
||||
int NumSpaces = 0;
|
||||
size_t MethodEnd = 0;
|
||||
size_t URLEnd = 0;
|
||||
for (size_t i = LineStart; i < IdxEnd; i++)
|
||||
{
|
||||
switch (m_IncomingHeaderData[i])
|
||||
{
|
||||
case ' ':
|
||||
{
|
||||
switch (NumSpaces)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
MethodEnd = i;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
URLEnd = i;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// Too many spaces in the request
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
}
|
||||
NumSpaces += 1;
|
||||
break;
|
||||
}
|
||||
case '\n':
|
||||
{
|
||||
if ((i == 0) || (m_IncomingHeaderData[i - 1] != '\r') || (NumSpaces != 2) || (i < URLEnd + 7))
|
||||
{
|
||||
// LF too early, without a CR, without two preceeding spaces or too soon after the second space
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
// Check that there's HTTP / version at the end
|
||||
if (strncmp(m_IncomingHeaderData.c_str() + URLEnd + 1, "HTTP/1.", 7) != 0)
|
||||
{
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
m_Method = m_IncomingHeaderData.substr(LineStart, MethodEnd - LineStart);
|
||||
m_URL = m_IncomingHeaderData.substr(MethodEnd + 1, URLEnd - MethodEnd - 1);
|
||||
return i + 1 - inBufferSoFar;
|
||||
}
|
||||
} // switch (m_IncomingHeaderData[i])
|
||||
} // for i - m_IncomingHeaderData[]
|
||||
|
||||
// CRLF hasn't been encountered yet, consider all data consumed
|
||||
return a_Size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cHTTPRequest::OnHeaderLine(const AString & a_Key, const AString & a_Value)
|
||||
{
|
||||
if (
|
||||
(NoCaseCompare(a_Key, "Authorization") == 0) &&
|
||||
(strncmp(a_Value.c_str(), "Basic ", 6) == 0)
|
||||
)
|
||||
{
|
||||
AString UserPass = Base64Decode(a_Value.substr(6));
|
||||
size_t idxCol = UserPass.find(':');
|
||||
if (idxCol != AString::npos)
|
||||
{
|
||||
m_AuthUsername = UserPass.substr(0, idxCol);
|
||||
m_AuthPassword = UserPass.substr(idxCol + 1);
|
||||
m_HasAuth = true;
|
||||
}
|
||||
}
|
||||
if ((a_Key == "Connection") && (NoCaseCompare(a_Value, "keep-alive") == 0))
|
||||
{
|
||||
m_AllowKeepAlive = true;
|
||||
}
|
||||
AddHeader(a_Key, a_Value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// cHTTPResponse:
|
||||
|
||||
@ -271,7 +83,7 @@ void cHTTPResponse::AppendToData(AString & a_DataStream) const
|
||||
a_DataStream.append("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: ");
|
||||
a_DataStream.append(m_ContentType);
|
||||
a_DataStream.append("\r\n");
|
||||
for (cNameValueMap::const_iterator itr = m_Headers.begin(), end = m_Headers.end(); itr != end; ++itr)
|
||||
for (auto itr = m_Headers.cbegin(), end = m_Headers.cend(); itr != end; ++itr)
|
||||
{
|
||||
if ((itr->first == "Content-Type") || (itr->first == "Content-Length"))
|
||||
{
|
||||
|
@ -49,7 +49,7 @@ protected:
|
||||
|
||||
eKind m_Kind;
|
||||
|
||||
cNameValueMap m_Headers;
|
||||
AStringMap m_Headers;
|
||||
|
||||
/** Type of the content; parsed by AddHeader(), set directly by SetContentLength() */
|
||||
AString m_ContentType;
|
||||
@ -64,99 +64,6 @@ protected:
|
||||
|
||||
|
||||
|
||||
class cHTTPRequest :
|
||||
public cHTTPMessage,
|
||||
protected cEnvelopeParser::cCallbacks
|
||||
{
|
||||
typedef cHTTPMessage super;
|
||||
|
||||
public:
|
||||
cHTTPRequest(void);
|
||||
|
||||
/** Parses the request line and then headers from the received data.
|
||||
Returns the number of bytes consumed or AString::npos number for error
|
||||
*/
|
||||
size_t ParseHeaders(const char * a_Data, size_t a_Size);
|
||||
|
||||
/** Returns true if the request did contain a Content-Length header */
|
||||
bool HasReceivedContentLength(void) const { return (m_ContentLength != AString::npos); }
|
||||
|
||||
/** Returns the method used in the request */
|
||||
const AString & GetMethod(void) const { return m_Method; }
|
||||
|
||||
/** Returns the URL used in the request */
|
||||
const AString & GetURL(void) const { return m_URL; }
|
||||
|
||||
/** Returns the URL used in the request, without any parameters */
|
||||
AString GetBareURL(void) const;
|
||||
|
||||
/** Sets the UserData pointer that is stored within this request.
|
||||
The request doesn't touch this data (doesn't delete it)! */
|
||||
void SetUserData(void * a_UserData) { m_UserData = a_UserData; }
|
||||
|
||||
/** Retrieves the UserData pointer that has been stored within this request. */
|
||||
void * GetUserData(void) const { return m_UserData; }
|
||||
|
||||
/** Returns true if more data is expected for the request headers */
|
||||
bool IsInHeaders(void) const { return m_EnvelopeParser.IsInHeaders(); }
|
||||
|
||||
/** Returns true if the request did present auth data that was understood by the parser */
|
||||
bool HasAuth(void) const { return m_HasAuth; }
|
||||
|
||||
/** Returns the username that the request presented. Only valid if HasAuth() is true */
|
||||
const AString & GetAuthUsername(void) const { return m_AuthUsername; }
|
||||
|
||||
/** Returns the password that the request presented. Only valid if HasAuth() is true */
|
||||
const AString & GetAuthPassword(void) const { return m_AuthPassword; }
|
||||
|
||||
bool DoesAllowKeepAlive(void) const { return m_AllowKeepAlive; }
|
||||
|
||||
protected:
|
||||
/** Parser for the envelope data */
|
||||
cEnvelopeParser m_EnvelopeParser;
|
||||
|
||||
/** True if the data received so far is parsed successfully. When false, all further parsing is skipped */
|
||||
bool m_IsValid;
|
||||
|
||||
/** Bufferred incoming data, while parsing for the request line */
|
||||
AString m_IncomingHeaderData;
|
||||
|
||||
/** Method of the request (GET / PUT / POST / ...) */
|
||||
AString m_Method;
|
||||
|
||||
/** Full URL of the request */
|
||||
AString m_URL;
|
||||
|
||||
/** Data that the HTTPServer callbacks are allowed to store. */
|
||||
void * m_UserData;
|
||||
|
||||
/** Set to true if the request contains auth data that was understood by the parser */
|
||||
bool m_HasAuth;
|
||||
|
||||
/** The username used for auth */
|
||||
AString m_AuthUsername;
|
||||
|
||||
/** The password used for auth */
|
||||
AString m_AuthPassword;
|
||||
|
||||
/** Set to true if the request indicated that it supports keepalives.
|
||||
If false, the server will close the connection once the request is finished */
|
||||
bool m_AllowKeepAlive;
|
||||
|
||||
|
||||
/** Parses the incoming data for the first line (RequestLine)
|
||||
Returns the number of bytes consumed, or AString::npos for an error
|
||||
*/
|
||||
size_t ParseRequestLine(const char * a_Data, size_t a_Size);
|
||||
|
||||
// cEnvelopeParser::cCallbacks overrides:
|
||||
virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class cHTTPResponse :
|
||||
public cHTTPMessage
|
||||
{
|
||||
|
192
src/HTTPServer/HTTPRequestParser.cpp
Normal file
192
src/HTTPServer/HTTPRequestParser.cpp
Normal file
@ -0,0 +1,192 @@
|
||||
|
||||
// HTTPRequestParser.cpp
|
||||
|
||||
// Implements the cHTTPRequestParser class representing the parser for incoming HTTP requests
|
||||
|
||||
#include "Globals.h"
|
||||
#include "HTTPRequestParser.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
cHTTPRequestParser::cHTTPRequestParser(void) :
|
||||
super(mkRequest),
|
||||
m_EnvelopeParser(*this),
|
||||
m_IsValid(true),
|
||||
m_UserData(nullptr),
|
||||
m_HasAuth(false),
|
||||
m_AllowKeepAlive(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t cHTTPRequestParser::ParseHeaders(const char * a_Data, size_t a_Size)
|
||||
{
|
||||
if (!m_IsValid)
|
||||
{
|
||||
return AString::npos;
|
||||
}
|
||||
|
||||
if (m_Method.empty())
|
||||
{
|
||||
// The first line hasn't been processed yet
|
||||
size_t res = ParseRequestLine(a_Data, a_Size);
|
||||
if ((res == AString::npos) || (res == a_Size))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
size_t res2 = m_EnvelopeParser.Parse(a_Data + res, a_Size - res);
|
||||
if (res2 == AString::npos)
|
||||
{
|
||||
m_IsValid = false;
|
||||
return res2;
|
||||
}
|
||||
return res2 + res;
|
||||
}
|
||||
|
||||
if (m_EnvelopeParser.IsInHeaders())
|
||||
{
|
||||
size_t res = m_EnvelopeParser.Parse(a_Data, a_Size);
|
||||
if (res == AString::npos)
|
||||
{
|
||||
m_IsValid = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
AString cHTTPRequestParser::GetBareURL(void) const
|
||||
{
|
||||
size_t idxQM = m_URL.find('?');
|
||||
if (idxQM != AString::npos)
|
||||
{
|
||||
return m_URL.substr(0, idxQM);
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_URL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t cHTTPRequestParser::ParseRequestLine(const char * a_Data, size_t a_Size)
|
||||
{
|
||||
m_IncomingHeaderData.append(a_Data, a_Size);
|
||||
size_t IdxEnd = m_IncomingHeaderData.size();
|
||||
|
||||
// Ignore the initial CRLFs (HTTP spec's "should")
|
||||
size_t LineStart = 0;
|
||||
while (
|
||||
(LineStart < IdxEnd) &&
|
||||
(
|
||||
(m_IncomingHeaderData[LineStart] == '\r') ||
|
||||
(m_IncomingHeaderData[LineStart] == '\n')
|
||||
)
|
||||
)
|
||||
{
|
||||
LineStart++;
|
||||
}
|
||||
if (LineStart >= IdxEnd)
|
||||
{
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
|
||||
int NumSpaces = 0;
|
||||
size_t MethodEnd = 0;
|
||||
size_t URLEnd = 0;
|
||||
for (size_t i = LineStart; i < IdxEnd; i++)
|
||||
{
|
||||
switch (m_IncomingHeaderData[i])
|
||||
{
|
||||
case ' ':
|
||||
{
|
||||
switch (NumSpaces)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
MethodEnd = i;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
URLEnd = i;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// Too many spaces in the request
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
}
|
||||
NumSpaces += 1;
|
||||
break;
|
||||
}
|
||||
case '\n':
|
||||
{
|
||||
if ((i == 0) || (m_IncomingHeaderData[i - 1] != '\r') || (NumSpaces != 2) || (i < URLEnd + 7))
|
||||
{
|
||||
// LF too early, without a CR, without two preceeding spaces or too soon after the second space
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
// Check that there's HTTP / version at the end
|
||||
if (strncmp(m_IncomingHeaderData.c_str() + URLEnd + 1, "HTTP/1.", 7) != 0)
|
||||
{
|
||||
m_IsValid = false;
|
||||
return AString::npos;
|
||||
}
|
||||
m_Method = m_IncomingHeaderData.substr(LineStart, MethodEnd - LineStart);
|
||||
m_URL = m_IncomingHeaderData.substr(MethodEnd + 1, URLEnd - MethodEnd - 1);
|
||||
return i + 1;
|
||||
}
|
||||
} // switch (m_IncomingHeaderData[i])
|
||||
} // for i - m_IncomingHeaderData[]
|
||||
|
||||
// CRLF hasn't been encountered yet, consider all data consumed
|
||||
return a_Size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cHTTPRequestParser::OnHeaderLine(const AString & a_Key, const AString & a_Value)
|
||||
{
|
||||
if (
|
||||
(NoCaseCompare(a_Key, "Authorization") == 0) &&
|
||||
(strncmp(a_Value.c_str(), "Basic ", 6) == 0)
|
||||
)
|
||||
{
|
||||
AString UserPass = Base64Decode(a_Value.substr(6));
|
||||
size_t idxCol = UserPass.find(':');
|
||||
if (idxCol != AString::npos)
|
||||
{
|
||||
m_AuthUsername = UserPass.substr(0, idxCol);
|
||||
m_AuthPassword = UserPass.substr(idxCol + 1);
|
||||
m_HasAuth = true;
|
||||
}
|
||||
}
|
||||
if ((a_Key == "Connection") && (NoCaseCompare(a_Value, "keep-alive") == 0))
|
||||
{
|
||||
m_AllowKeepAlive = true;
|
||||
}
|
||||
AddHeader(a_Key, a_Value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
109
src/HTTPServer/HTTPRequestParser.h
Normal file
109
src/HTTPServer/HTTPRequestParser.h
Normal file
@ -0,0 +1,109 @@
|
||||
|
||||
// HTTPRequestParser.h
|
||||
|
||||
// Declares the cHTTPRequestParser class representing the parser for incoming HTTP requests
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "HTTPMessage.h"
|
||||
#include "EnvelopeParser.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class cHTTPRequestParser :
|
||||
public cHTTPMessage,
|
||||
protected cEnvelopeParser::cCallbacks
|
||||
{
|
||||
typedef cHTTPMessage super;
|
||||
|
||||
public:
|
||||
cHTTPRequestParser(void);
|
||||
|
||||
/** Parses the request line and then headers from the received data.
|
||||
Returns the number of bytes consumed or AString::npos number for error
|
||||
*/
|
||||
size_t ParseHeaders(const char * a_Data, size_t a_Size);
|
||||
|
||||
/** Returns true if the request did contain a Content-Length header */
|
||||
bool HasReceivedContentLength(void) const { return (m_ContentLength != AString::npos); }
|
||||
|
||||
/** Returns the method used in the request */
|
||||
const AString & GetMethod(void) const { return m_Method; }
|
||||
|
||||
/** Returns the URL used in the request */
|
||||
const AString & GetURL(void) const { return m_URL; }
|
||||
|
||||
/** Returns the URL used in the request, without any parameters */
|
||||
AString GetBareURL(void) const;
|
||||
|
||||
/** Sets the UserData pointer that is stored within this request.
|
||||
The request doesn't touch this data (doesn't delete it)! */
|
||||
void SetUserData(void * a_UserData) { m_UserData = a_UserData; }
|
||||
|
||||
/** Retrieves the UserData pointer that has been stored within this request. */
|
||||
void * GetUserData(void) const { return m_UserData; }
|
||||
|
||||
/** Returns true if more data is expected for the request headers */
|
||||
bool IsInHeaders(void) const { return m_EnvelopeParser.IsInHeaders(); }
|
||||
|
||||
/** Returns true if the request did present auth data that was understood by the parser */
|
||||
bool HasAuth(void) const { return m_HasAuth; }
|
||||
|
||||
/** Returns the username that the request presented. Only valid if HasAuth() is true */
|
||||
const AString & GetAuthUsername(void) const { return m_AuthUsername; }
|
||||
|
||||
/** Returns the password that the request presented. Only valid if HasAuth() is true */
|
||||
const AString & GetAuthPassword(void) const { return m_AuthPassword; }
|
||||
|
||||
bool DoesAllowKeepAlive(void) const { return m_AllowKeepAlive; }
|
||||
|
||||
protected:
|
||||
/** Parser for the envelope data */
|
||||
cEnvelopeParser m_EnvelopeParser;
|
||||
|
||||
/** True if the data received so far is parsed successfully. When false, all further parsing is skipped */
|
||||
bool m_IsValid;
|
||||
|
||||
/** Bufferred incoming data, while parsing for the request line */
|
||||
AString m_IncomingHeaderData;
|
||||
|
||||
/** Method of the request (GET / PUT / POST / ...) */
|
||||
AString m_Method;
|
||||
|
||||
/** Full URL of the request */
|
||||
AString m_URL;
|
||||
|
||||
/** Data that the HTTPServer callbacks are allowed to store. */
|
||||
void * m_UserData;
|
||||
|
||||
/** Set to true if the request contains auth data that was understood by the parser */
|
||||
bool m_HasAuth;
|
||||
|
||||
/** The username used for auth */
|
||||
AString m_AuthUsername;
|
||||
|
||||
/** The password used for auth */
|
||||
AString m_AuthPassword;
|
||||
|
||||
/** Set to true if the request indicated that it supports keepalives.
|
||||
If false, the server will close the connection once the request is finished */
|
||||
bool m_AllowKeepAlive;
|
||||
|
||||
|
||||
/** Parses the incoming data for the first line (RequestLine)
|
||||
Returns the number of bytes consumed, or AString::npos for an error
|
||||
*/
|
||||
size_t ParseRequestLine(const char * a_Data, size_t a_Size);
|
||||
|
||||
// cEnvelopeParser::cCallbacks overrides:
|
||||
virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
#include "Globals.h"
|
||||
#include "HTTPServer.h"
|
||||
#include "HTTPMessage.h"
|
||||
#include "HTTPRequestParser.h"
|
||||
#include "HTTPServerConnection.h"
|
||||
#include "HTTPFormParser.h"
|
||||
#include "SslHTTPServerConnection.h"
|
||||
@ -28,7 +28,7 @@ class cDebugCallbacks :
|
||||
public cHTTPServer::cCallbacks,
|
||||
protected cHTTPFormParser::cCallbacks
|
||||
{
|
||||
virtual void OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request) override
|
||||
virtual void OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request) override
|
||||
{
|
||||
UNUSED(a_Connection);
|
||||
|
||||
@ -39,7 +39,7 @@ class cDebugCallbacks :
|
||||
}
|
||||
|
||||
|
||||
virtual void OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size) override
|
||||
virtual void OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request, const char * a_Data, size_t a_Size) override
|
||||
{
|
||||
UNUSED(a_Connection);
|
||||
|
||||
@ -51,7 +51,7 @@ class cDebugCallbacks :
|
||||
}
|
||||
|
||||
|
||||
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request) override
|
||||
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request) override
|
||||
{
|
||||
cHTTPFormParser * FormParser = reinterpret_cast<cHTTPFormParser *>(a_Request.GetUserData());
|
||||
if (FormParser != nullptr)
|
||||
@ -280,7 +280,7 @@ cTCPLink::cCallbacksPtr cHTTPServer::OnIncomingConnection(const AString & a_Remo
|
||||
|
||||
|
||||
|
||||
void cHTTPServer::NewRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cHTTPServer::NewRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
m_Callbacks->OnRequestBegun(a_Connection, a_Request);
|
||||
}
|
||||
@ -289,7 +289,7 @@ void cHTTPServer::NewRequest(cHTTPServerConnection & a_Connection, cHTTPRequest
|
||||
|
||||
|
||||
|
||||
void cHTTPServer::RequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size)
|
||||
void cHTTPServer::RequestBody(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request, const char * a_Data, size_t a_Size)
|
||||
{
|
||||
m_Callbacks->OnRequestBody(a_Connection, a_Request, a_Data, a_Size);
|
||||
}
|
||||
@ -298,7 +298,7 @@ void cHTTPServer::RequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest
|
||||
|
||||
|
||||
|
||||
void cHTTPServer::RequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cHTTPServer::RequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
m_Callbacks->OnRequestFinished(a_Connection, a_Request);
|
||||
a_Connection.AwaitNextRequest();
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
// fwd:
|
||||
class cHTTPMessage;
|
||||
class cHTTPRequest;
|
||||
class cHTTPRequestParser;
|
||||
class cHTTPResponse;
|
||||
class cHTTPServerConnection;
|
||||
|
||||
@ -40,14 +40,14 @@ public:
|
||||
|
||||
/** Called when a new request arrives over a connection and all its headers have been parsed.
|
||||
The request body needn't have arrived yet. */
|
||||
virtual void OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request) = 0;
|
||||
virtual void OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request) = 0;
|
||||
|
||||
/** Called when another part of request body has arrived.
|
||||
May be called multiple times for a single request. */
|
||||
virtual void OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size) = 0;
|
||||
virtual void OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request, const char * a_Data, size_t a_Size) = 0;
|
||||
|
||||
/** Called when the request body has been fully received in previous calls to OnRequestBody() */
|
||||
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request) = 0;
|
||||
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request) = 0;
|
||||
} ;
|
||||
|
||||
cHTTPServer(void);
|
||||
@ -85,14 +85,14 @@ protected:
|
||||
cTCPLink::cCallbacksPtr OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort);
|
||||
|
||||
/** Called by cHTTPServerConnection when it finishes parsing the request header */
|
||||
void NewRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request);
|
||||
void NewRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request);
|
||||
|
||||
/** Called by cHTTPConenction when it receives more data for the request body.
|
||||
May be called multiple times for a single request. */
|
||||
void RequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size);
|
||||
void RequestBody(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request, const char * a_Data, size_t a_Size);
|
||||
|
||||
/** Called by cHTTPServerConnection when it detects that the request has finished (all of its body has been received) */
|
||||
void RequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request);
|
||||
void RequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request);
|
||||
} ;
|
||||
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
#include "Globals.h"
|
||||
#include "HTTPServerConnection.h"
|
||||
#include "HTTPMessage.h"
|
||||
#include "HTTPRequestParser.h"
|
||||
#include "HTTPServer.h"
|
||||
|
||||
|
||||
@ -166,7 +166,7 @@ void cHTTPServerConnection::OnReceivedData(const char * a_Data, size_t a_Size)
|
||||
{
|
||||
if (m_CurrentRequest == nullptr)
|
||||
{
|
||||
m_CurrentRequest = new cHTTPRequest;
|
||||
m_CurrentRequest = new cHTTPRequestParser;
|
||||
}
|
||||
|
||||
size_t BytesConsumed = m_CurrentRequest->ParseHeaders(a_Data, a_Size);
|
||||
|
@ -18,7 +18,7 @@
|
||||
// fwd:
|
||||
class cHTTPServer;
|
||||
class cHTTPResponse;
|
||||
class cHTTPRequest;
|
||||
class cHTTPRequestParser;
|
||||
|
||||
|
||||
|
||||
@ -81,7 +81,7 @@ protected:
|
||||
|
||||
/** The request being currently received
|
||||
Valid only between having parsed the headers and finishing receiving the body. */
|
||||
cHTTPRequest * m_CurrentRequest;
|
||||
cHTTPRequestParser * m_CurrentRequest;
|
||||
|
||||
/** Number of bytes that remain to read for the complete body of the message to be received.
|
||||
Valid only in wcsRecvBody */
|
||||
|
@ -12,7 +12,7 @@
|
||||
#include "Server.h"
|
||||
#include "Root.h"
|
||||
|
||||
#include "HTTPServer/HTTPMessage.h"
|
||||
#include "HTTPServer/HTTPRequestParser.h"
|
||||
#include "HTTPServer/HTTPServerConnection.h"
|
||||
|
||||
|
||||
@ -212,7 +212,7 @@ bool cWebAdmin::LoadLoginTemplate(void)
|
||||
|
||||
|
||||
|
||||
void cWebAdmin::HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cWebAdmin::HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
if (!a_Request.HasAuth())
|
||||
{
|
||||
@ -349,7 +349,7 @@ void cWebAdmin::HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTT
|
||||
|
||||
|
||||
|
||||
void cWebAdmin::HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cWebAdmin::HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
UNUSED(a_Request);
|
||||
|
||||
@ -364,7 +364,7 @@ void cWebAdmin::HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPReq
|
||||
|
||||
|
||||
|
||||
void cWebAdmin::HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cWebAdmin::HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
AString FileURL = a_Request.GetURL();
|
||||
std::replace(FileURL.begin(), FileURL.end(), '\\', '/');
|
||||
@ -621,7 +621,7 @@ AString cWebAdmin::GetBaseURL(const AStringVector & a_URLSplit)
|
||||
|
||||
|
||||
|
||||
void cWebAdmin::OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cWebAdmin::OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
UNUSED(a_Connection);
|
||||
const AString & URL = a_Request.GetURL();
|
||||
@ -645,7 +645,7 @@ void cWebAdmin::OnRequestBegun(cHTTPServerConnection & a_Connection, cHTTPReques
|
||||
|
||||
|
||||
|
||||
void cWebAdmin::OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size)
|
||||
void cWebAdmin::OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request, const char * a_Data, size_t a_Size)
|
||||
{
|
||||
UNUSED(a_Connection);
|
||||
cRequestData * Data = reinterpret_cast<cRequestData *>(a_Request.GetUserData());
|
||||
@ -660,7 +660,7 @@ void cWebAdmin::OnRequestBody(cHTTPServerConnection & a_Connection, cHTTPRequest
|
||||
|
||||
|
||||
|
||||
void cWebAdmin::OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request)
|
||||
void cWebAdmin::OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request)
|
||||
{
|
||||
const AString & URL = a_Request.GetURL();
|
||||
if (
|
||||
|
@ -190,7 +190,7 @@ protected:
|
||||
cHTTPFormParser m_Form;
|
||||
|
||||
|
||||
cWebadminRequestData(cHTTPRequest & a_Request) :
|
||||
cWebadminRequestData(cHTTPRequestParser & a_Request) :
|
||||
m_Form(a_Request, *this)
|
||||
{
|
||||
}
|
||||
@ -236,18 +236,18 @@ protected:
|
||||
cHTTPServer m_HTTPServer;
|
||||
|
||||
/** Handles requests coming to the "/webadmin" or "/~webadmin" URLs */
|
||||
void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request);
|
||||
void HandleWebadminRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request);
|
||||
|
||||
/** Handles requests for the root page */
|
||||
void HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request);
|
||||
void HandleRootRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request);
|
||||
|
||||
/** Handles requests for a file */
|
||||
void HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request);
|
||||
void HandleFileRequest(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request);
|
||||
|
||||
// cHTTPServer::cCallbacks overrides:
|
||||
virtual void OnRequestBegun (cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request) override;
|
||||
virtual void OnRequestBody (cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request, const char * a_Data, size_t a_Size) override;
|
||||
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequest & a_Request) override;
|
||||
virtual void OnRequestBegun (cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request) override;
|
||||
virtual void OnRequestBody (cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request, const char * a_Data, size_t a_Size) override;
|
||||
virtual void OnRequestFinished(cHTTPServerConnection & a_Connection, cHTTPRequestParser & a_Request) override;
|
||||
} ; // tolua_export
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user