Cuberite
A lightweight, fast and extensible game server for Minecraft
HTTPMessageParser.h
Go to the documentation of this file.
1 
2 // HTTPMessageParser.h
3 
4 // Declares the cHTTPMessageParser class that parses HTTP messages (request or response) being pushed into the parser,
5 // and reports the individual parts via callbacks
6 
7 
8 
9 
10 
11 #pragma once
12 
13 #include "EnvelopeParser.h"
14 #include "TransferEncodingParser.h"
15 
16 
17 
18 
19 
23 {
24 public:
25  class cCallbacks
26  {
27  public:
28  // Force a virtual destructor in descendants:
29  virtual ~cCallbacks() {}
30 
32  virtual void OnError(const AString & a_ErrorDescription) = 0;
33 
36  virtual void OnFirstLine(const AString & a_FirstLine) = 0;
37 
39  virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) = 0;
40 
42  virtual void OnHeadersFinished(void) = 0;
43 
45  virtual void OnBodyData(const void * a_Data, size_t a_Size) = 0;
46 
48  virtual void OnBodyFinished(void) = 0;
49  };
50 
52  cHTTPMessageParser(cCallbacks & a_Callbacks);
53 
56  size_t Parse(const char * a_Data, size_t a_Size);
57 
60  void Finish(void);
61 
63  bool IsFinished(void) const { return m_IsFinished; }
64 
66  void Reset(void);
67 
68 
69 protected:
70 
73 
76 
79 
82 
85 
88 
91 
95 
100 
101 
105  size_t ParseFirstLine(void);
106 
110  size_t ParseBody(const char * a_Data, size_t a_Size);
111 
113  void HeadersFinished(void);
114 
115  // cEnvelopeParser::cCallbacks overrides:
116  virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override;
117 
118  // cTransferEncodingParser::cCallbacks overrides:
119  virtual void OnError(const AString & a_ErrorDescription) override;
120  virtual void OnBodyData(const void * a_Data, size_t a_Size) override;
121  virtual void OnBodyFinished(void) override;
122 };
123 
124 
125 
126 
void HeadersFinished(void)
Called internally when the headers-parsing has just finished.
size_t m_ContentLength
The content length, parsed from the headers, if available.
cCallbacks & m_Callbacks
The callbacks used for reporting.
AString m_TransferEncoding
The transfer encoding to be used by the parser.
virtual void OnBodyData(const void *a_Data, size_t a_Size)=0
Called for each chunk of the incoming body data.
bool m_IsFinished
True if the response has been fully parsed.
AString m_Buffer
Buffer for the incoming data until the status line is parsed.
void Reset(void)
Resets the parser to the initial state, so that a new request can be parsed.
bool m_HasHadError
Set to true if an error has been encountered by the parser.
cHTTPMessageParser(cCallbacks &a_Callbacks)
Creates a new parser instance that will use the specified callbacks for reporting.
size_t ParseBody(const char *a_Data, size_t a_Size)
Parses the message body.
void Finish(void)
Called when the server indicates no more data will be sent (HTTP 1.0 socket closed).
size_t ParseFirstLine(void)
Parses the first line out of m_Buffer.
bool IsFinished(void) const
Returns true if the entire response has been already parsed.
cEnvelopeParser m_EnvelopeParser
Parser for the envelope data (headers)
virtual void OnFirstLine(const AString &a_FirstLine)=0
Called when the first line of the request or response is fully parsed.
std::string AString
Definition: StringUtils.h:13
virtual void OnBodyFinished(void)=0
Called when the entire body has been reported by OnBodyData().
cTransferEncodingParserPtr m_TransferEncodingParser
The specific parser for the transfer encoding used by this response.
AString m_FirstLine
The complete first line of the response.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value)=0
Called when a single header line is parsed.
virtual void OnError(const AString &a_ErrorDescription)=0
Called when an error has occured while parsing.
std::shared_ptr< cTransferEncodingParser > cTransferEncodingParserPtr
virtual void OnHeadersFinished(void)=0
Called when all the headers have been parsed.
size_t Parse(const char *a_Data, size_t a_Size)
Parses the incoming data and calls the appropriate callbacks.