16 m_Callbacks(a_Callbacks),
18 m_IsCurrentPartFile(false),
19 m_FileHasBeenAnnounced(false)
27 size_t idxQM = URL.find(
'?');
28 if (idxQM != AString::npos)
30 Parse(URL.c_str() + idxQM + 1, URL.size() - idxQM - 1);
36 if (strncmp(a_Request.
GetContentType().c_str(),
"application/x-www-form-urlencoded", 33) == 0)
41 if (strncmp(a_Request.
GetContentType().c_str(),
"multipart/form-data", 19) == 0)
57 m_Callbacks(a_Callbacks),
60 m_IsCurrentPartFile(false),
61 m_FileHasBeenAnnounced(false)
63 Parse(a_Data, a_Size);
127 (ContentType ==
"application/x-www-form-urlencoded") ||
128 (strncmp(ContentType.c_str(),
"multipart/form-data", 19) == 0) ||
131 (a_Request.
GetURL().find(
'?') != AString::npos)
155 for (AStringVector::iterator itr = Lines.begin(), end = Lines.end(); itr != end; ++itr)
158 switch (Components.size())
172 (*this)[name.second] =
"";
181 if (name.first && value.first)
183 (*this)[name.second] = value.second;
212 size_t len = a_Value.size();
213 size_t ParamsStart = AString::npos;
214 for (
size_t i = 0; i < len; ++i)
216 if (a_Value[i] >
' ')
218 if (strncmp(a_Value.c_str() + i,
"form-data", 9) != 0)
224 ParamsStart = a_Value.find(
';', i + 9);
228 if (ParamsStart == AString::npos)
236 cNameValueParser Parser(a_Value.data() + ParamsStart, a_Value.size() - ParamsStart);
270 itr->second.append(a_Data, a_Size);
std::pair< bool, AString > URLDecode(const AString &a_Text)
URL-Decodes the given string.
AString ReplaceAllCharOccurrences(const AString &a_String, char a_From, char a_To)
Replaces all occurrences of char a_From inside a_String with char a_To.
AStringVector StringSplit(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
std::vector< AString > AStringVector
bool Finish(void)
Notifies that there's no more data incoming and the parser should finish its parsing.
void ParseFormUrlEncoded(void)
Parses m_IncomingData as form-urlencoded data (fpkURL or fpkFormUrlEncoded kinds)
std::unique_ptr< cMultipartParser > m_MultipartParser
The parser for the multipart data, if used.
eKind m_Kind
The kind of the parser (decided in the constructor, used in Parse()
virtual void OnPartHeader(const AString &a_Key, const AString &a_Value) override
Called when a complete header line is received for a part.
virtual void OnPartStart(void) override
Called when a new part starts.
bool m_IsCurrentPartFile
True if the currently parsed part in multipart data is a file.
AString m_CurrentPartFileName
Filename of the current parsed part in multipart data (for file uploads)
AString m_CurrentPartName
Name of the currently parsed part in multipart data.
virtual void OnPartEnd(void) override
Called when the current part ends.
cHTTPFormParser(const cHTTPIncomingRequest &a_Request, cCallbacks &a_Callbacks)
Creates a parser that is tied to a request and notifies of various events using a callback mechanism.
void BeginMultipart(const cHTTPIncomingRequest &a_Request)
Sets up the object for parsing a fpkMultipart request.
bool m_FileHasBeenAnnounced
Set to true after m_Callbacks.OnFileStart() has been called, reset to false on PartEnd.
bool m_IsValid
True if the information received so far is a valid form; set to false on first problem.
static bool HasFormData(const cHTTPIncomingRequest &a_Request)
Returns true if the headers suggest the request has form data parseable by this class.
@ fpkURL
The form has been transmitted as parameters to a GET request.
@ fpkFormUrlEncoded
The form has been POSTed or PUT, with Content-Type of "application/x-www-form-urlencoded".
@ fpkMultipart
The form has been POSTed or PUT, with Content-Type of "multipart/form-data".
virtual void OnPartData(const char *a_Data, size_t a_Size) override
Called when body for a part is received.
cCallbacks & m_Callbacks
The callbacks to call for incoming file data.
AString m_IncomingData
Buffer for the incoming data until it's parsed.
void Parse(const char *a_Data, size_t a_Size)
Adds more data into the parser, as the request body is received.
virtual void OnFileEnd(cHTTPFormParser &a_Parser)=0
Called when the current file part has ended in the form data.
virtual void OnFileData(cHTTPFormParser &a_Parser, const char *a_Data, size_t a_Size)=0
Called when more file data has come for the current file in the form data.
virtual void OnFileStart(cHTTPFormParser &a_Parser, const AString &a_FileName)=0
Called when a new file part is encountered in the form data.
const AString & GetContentType(void) const
Provides storage for an incoming HTTP request.
const AString & GetMethod(void) const
Returns the method used in the request.
const AString & GetURL(void) const
Returns the URL used in the request.
bool Finish(void)
Notifies the parser that no more data will be coming.
bool IsValid(void) const
Returns true if the data parsed so far was valid.