Cuberite
A lightweight, fast and extensible game server for Minecraft
HTTPFormParser.h
Go to the documentation of this file.
1 
2 // HTTPFormParser.h
3 
4 // Declares the cHTTPFormParser class representing a parser for forms sent over HTTP
5 
6 
7 
8 
9 #pragma once
10 
11 #include "MultipartParser.h"
12 
13 
14 
15 
16 
17 // fwd:
19 
20 
21 
22 
23 
25  public std::map<AString, AString>,
27 {
28 public:
29  enum eKind
30  {
34  } ;
35 
36  class cCallbacks
37  {
38  public:
39  // Force a virtual destructor in descendants:
40  virtual ~cCallbacks() {}
41 
43  virtual void OnFileStart(cHTTPFormParser & a_Parser, const AString & a_FileName) = 0;
44 
46  virtual void OnFileData(cHTTPFormParser & a_Parser, const char * a_Data, size_t a_Size) = 0;
47 
49  virtual void OnFileEnd(cHTTPFormParser & a_Parser) = 0;
50  } ;
51 
52 
54  cHTTPFormParser(const cHTTPIncomingRequest & a_Request, cCallbacks & a_Callbacks);
55 
57  cHTTPFormParser(eKind a_Kind, const char * a_Data, size_t a_Size, cCallbacks & a_Callbacks);
58 
60  void Parse(const char * a_Data, size_t a_Size);
61 
64  bool Finish(void);
65 
67  static bool HasFormData(const cHTTPIncomingRequest & a_Request);
68 
69 protected:
70 
73 
76 
79 
81  bool m_IsValid;
82 
84  std::unique_ptr<cMultipartParser> m_MultipartParser;
85 
88 
91 
94 
97 
98 
100  void BeginMultipart(const cHTTPIncomingRequest & a_Request);
101 
103  void ParseFormUrlEncoded(void);
104 
105  // cMultipartParser::cCallbacks overrides:
106  virtual void OnPartStart (void) override;
107  virtual void OnPartHeader(const AString & a_Key, const AString & a_Value) override;
108  virtual void OnPartData (const char * a_Data, size_t a_Size) override;
109  virtual void OnPartEnd (void) override;
110 } ;
111 
112 
113 
114 
std::string AString
Definition: StringUtils.h:11
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.
Provides storage for an incoming HTTP request.
Definition: HTTPMessage.h:90