2013-09-28 13:30:25 -04:00
|
|
|
|
|
|
|
// HTTPFormParser.h
|
|
|
|
|
|
|
|
// Declares the cHTTPFormParser class representing a parser for forms sent over HTTP
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
#include "MultipartParser.h"
|
|
|
|
|
2013-09-28 13:30:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd:
|
2016-02-20 05:50:52 -05:00
|
|
|
class cHTTPIncomingRequest;
|
2013-09-28 13:30:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cHTTPFormParser :
|
2013-10-04 07:07:57 -04:00
|
|
|
public std::map<AString, AString>,
|
|
|
|
public cMultipartParser::cCallbacks
|
2013-09-28 13:30:25 -04:00
|
|
|
{
|
|
|
|
public:
|
2013-10-13 14:29:57 -04:00
|
|
|
enum eKind
|
|
|
|
{
|
|
|
|
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"
|
|
|
|
} ;
|
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
class cCallbacks
|
|
|
|
{
|
|
|
|
public:
|
2014-04-02 10:39:42 -04:00
|
|
|
// Force a virtual destructor in descendants:
|
|
|
|
virtual ~cCallbacks() {}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Called when a new file part is encountered in the form data */
|
2013-10-04 07:07:57 -04:00
|
|
|
virtual void OnFileStart(cHTTPFormParser & a_Parser, const AString & a_FileName) = 0;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Called when more file data has come for the current file in the form data */
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnFileData(cHTTPFormParser & a_Parser, const char * a_Data, size_t a_Size) = 0;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Called when the current file part has ended in the form data */
|
2013-10-04 07:07:57 -04:00
|
|
|
virtual void OnFileEnd(cHTTPFormParser & a_Parser) = 0;
|
|
|
|
} ;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Creates a parser that is tied to a request and notifies of various events using a callback mechanism */
|
2016-02-20 05:50:52 -05:00
|
|
|
cHTTPFormParser(const cHTTPIncomingRequest & a_Request, cCallbacks & a_Callbacks);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Creates a parser with the specified content type that reads data from a string */
|
2014-04-02 09:36:25 -04:00
|
|
|
cHTTPFormParser(eKind a_Kind, const char * a_Data, size_t a_Size, cCallbacks & a_Callbacks);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Adds more data into the parser, as the request body is received */
|
2014-04-02 09:36:25 -04:00
|
|
|
void Parse(const char * a_Data, size_t a_Size);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-28 13:30:25 -04:00
|
|
|
/** Notifies that there's no more data incoming and the parser should finish its parsing.
|
2015-07-31 10:49:10 -04:00
|
|
|
Returns true if parsing successful. */
|
2013-09-28 13:30:25 -04:00
|
|
|
bool Finish(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Returns true if the headers suggest the request has form data parseable by this class */
|
2016-02-20 05:50:52 -05:00
|
|
|
static bool HasFormData(const cHTTPIncomingRequest & a_Request);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-09-28 13:30:25 -04:00
|
|
|
protected:
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The callbacks to call for incoming file data */
|
2013-10-04 07:07:57 -04:00
|
|
|
cCallbacks & m_Callbacks;
|
2013-09-28 13:30:25 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The kind of the parser (decided in the constructor, used in Parse() */
|
2013-09-28 13:30:25 -04:00
|
|
|
eKind m_Kind;
|
2013-10-04 07:07:57 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Buffer for the incoming data until it's parsed */
|
2013-09-28 13:30:25 -04:00
|
|
|
AString m_IncomingData;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** True if the information received so far is a valid form; set to false on first problem. Further parsing is skipped when false. */
|
2013-09-28 13:30:25 -04:00
|
|
|
bool m_IsValid;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The parser for the multipart data, if used */
|
2014-12-06 17:02:49 -05:00
|
|
|
std::unique_ptr<cMultipartParser> m_MultipartParser;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Name of the currently parsed part in multipart data */
|
2013-10-04 07:07:57 -04:00
|
|
|
AString m_CurrentPartName;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** True if the currently parsed part in multipart data is a file */
|
2013-10-04 07:07:57 -04:00
|
|
|
bool m_IsCurrentPartFile;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Filename of the current parsed part in multipart data (for file uploads) */
|
2013-10-04 07:07:57 -04:00
|
|
|
AString m_CurrentPartFileName;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Set to true after m_Callbacks.OnFileStart() has been called, reset to false on PartEnd */
|
2013-10-04 07:07:57 -04:00
|
|
|
bool m_FileHasBeenAnnounced;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Sets up the object for parsing a fpkMultipart request */
|
2016-02-20 05:50:52 -05:00
|
|
|
void BeginMultipart(const cHTTPIncomingRequest & a_Request);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Parses m_IncomingData as form-urlencoded data (fpkURL or fpkFormUrlEncoded kinds) */
|
2013-09-28 13:30:25 -04:00
|
|
|
void ParseFormUrlEncoded(void);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2013-10-04 07:07:57 -04:00
|
|
|
// cMultipartParser::cCallbacks overrides:
|
|
|
|
virtual void OnPartStart (void) override;
|
|
|
|
virtual void OnPartHeader(const AString & a_Key, const AString & a_Value) override;
|
2014-04-01 10:36:00 -04:00
|
|
|
virtual void OnPartData (const char * a_Data, size_t a_Size) override;
|
2013-10-04 07:07:57 -04:00
|
|
|
virtual void OnPartEnd (void) override;
|
2013-09-28 13:30:25 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|