Remote Call Framework 3.2
HttpFrameFilter.hpp
Go to the documentation of this file.
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 3.2
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
20 
21 #ifndef INCLUDE_RCF_HTTPFRAMEFILTER_HPP
22 #define INCLUDE_RCF_HTTPFRAMEFILTER_HPP
23 
24 #include <map>
25 
26 #include <memory>
27 
28 #include <RCF/ByteBuffer.hpp>
29 #include <RCF/Filter.hpp>
30 #include <RCF/RecursionLimiter.hpp>
31 
32 namespace RCF {
33 
34  class Exception;
35  class MemOstream;
36  class MemIstream;
37  class ReallocBuffer;
38  class HttpMessage;
39  class HttpCookie;
40 
41  typedef std::shared_ptr<MemOstream> MemOstreamPtr;
42  typedef std::shared_ptr<ReallocBuffer> ReallocBufferPtr;
43 
44  void splitString(
45  const std::string & stringToSplit,
46  const char * splitAt,
47  std::vector<std::string> & lines);
48 
51  {
52  public:
53 
55  virtual void applyHeader(const std::vector<ByteBuffer>& messageBuffers, std::string& headerName, std::string& headerValue) = 0;
56 
58  virtual void verifyHeader(const HttpMessage& msg, ByteBuffer msgData) = 0;
59  };
60 
61  typedef std::shared_ptr<HttpMessageVerifier> HttpMessageVerifierPtr;
62 
63  class RCF_EXPORT HttpMessage
64  {
65  public:
66 
67  HttpMessage() :
68  mHeaderLen(0),
69  mContentLen(0),
70  mFrameLen(0)
71  {
72  }
73 
74  ~HttpMessage()
75  {
76  }
77 
78  bool parseHttpMessage(const char * pFrame, std::size_t bytesAvailable);
79 
80  void getHttpStatus(std::string& httpStatus, std::string& httpStatusMsg);
81  void getHeaderValue(const std::string& headerName, std::string& headerValue) const;
82 
83  typedef std::vector< std::pair< std::string, std::string > > HeaderList;
84  HeaderList mHeaderList;
85 
86  std::size_t mHeaderLen;
87  std::size_t mContentLen;
88  std::size_t mFrameLen;
89  std::string mHttpMessageHeader;
90  std::vector<std::string> mMessageLines;
91  std::string mRequestLine;
92  std::string mResponseLine;
93  };
94 
95  class HttpFrameFilter : public Filter
96  {
97  public:
98 
99  // Server-side constructor.
100  HttpFrameFilter(std::size_t maxMessageLength);
101 
102  // Client-side constructor.
103  HttpFrameFilter(
104  const std::string & serverAddr,
105  int serverPort,
106  const std::string & serverUrlPath);
107 
108  ~HttpFrameFilter();
109 
110  void init();
111  void resetState();
112 
113  void read(
114  const ByteBuffer & byteBuffer,
115  std::size_t bytesRequested);
116 
117  void write(const std::vector<ByteBuffer> &byteBuffers);
118 
119  void onReadCompleted(const ByteBuffer &byteBuffer);
120 
121  void onWriteCompleted(std::size_t bytesTransferred);
122 
123  int getFilterId() const;
124 
125  virtual std::size_t getFrameSize();
126 
127  const std::string & getHttpSessionId();
128  std::uint32_t getHttpSessionIndex();
129  const std::string & getConnectionHeader();
130 
131  void sendHttpTextResponse(const std::string & httpStatus, const std::string & responseText);
132  void sendHttpErrorResponse(MemOstreamPtr osPtr);
133  void onError(const Exception& e);
134 
135  // If these are set, then we are doing a HTTP response with chunked transfer encoding.
136  bool mChunkedResponseMode;
137  std::size_t mChunkedResponseCounter;
138 
139  bool tryParseHttpHeader();
140 
141  bool tryParseHttpChunkHeader();
142 
143  void getHttpFrameInfo(
144  std::string& requestLine,
145  std::vector< std::pair<std::string, std::string> >& headers);
146 
147  bool verifyReceivedMessage();
148 
149  private:
150 
151  void processSetCookieHeader(const std::string& headerValue);
152  void processLocationHeader();
153 
154 
155  void sendServerError(int error);
156 
157  void resizeReadBuffer(std::size_t newSize);
158 
159  const std::string & getServerHeader();
160 
161  std::string mServerAddr;
162  int mServerPort;
163  std::string mServerUrlPath;
164  std::string mServerHeaderValue;
165 
166  bool mClientSide;
167  std::string mHttpSessionId;
168  std::uint32_t mHttpSessionIndex;
169  std::string mConnectionHeader;
170  std::string mTransferEncoding;
171 
172  MemOstreamPtr mOsPtr;
173  std::vector<ByteBuffer> mWriteBuffers;
174  std::size_t mWritePos;
175 
176  ByteBuffer mOrigReadBuffer;
177  std::size_t mOrigBytesRequested;
178 
179  ReallocBufferPtr mReadBufferPtr;
180  std::size_t mBytesReceived;
181  std::size_t mReadPos;
182 
183  bool mProtocolChecked;
184  std::size_t mChunkHeaderLen;
185  std::size_t mChunkLen;
186  std::size_t mMaxReadPos;
187  std::size_t mMaxMessageLength;
188  bool mMaxMessageLengthSet;
189 
190  std::string mPrevHttpSessionId;
191  std::uint32_t mPrevHttpSessionIndex;
192 
193  HttpMessage mHttpMessage;
194 
195  std::map<std::string, HttpCookie> mCookies;
196 
197  RecursionState<ByteBuffer, std::size_t> mRecursionStateRead;
198  };
199 
200 } // namespace RCF
201 
202 #endif // ! INCLUDE_RCF_HTTPFRAMEFILTER_HPP
Base class for all RCF exceptions.
Definition: Exception.hpp:64
std::shared_ptr< HttpMessageVerifier > HttpMessageVerifierPtr
Reference counted wrapper for RCF::HttpMessageVerifier.
Definition: HttpFrameFilter.hpp:61
virtual void verifyHeader(const HttpMessage &msg, ByteBuffer msgData)=0
Check verification header on an incoming HTTP message, based on the message payload passed in msgData...
Definition: ByteBuffer.hpp:40
HTTP message verification mechanism, to allow applications to verify HTTP message payloads using cust...
Definition: HttpFrameFilter.hpp:50
Definition: AmiIoHandler.hpp:24
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...
virtual void applyHeader(const std::vector< ByteBuffer > &messageBuffers, std::string &headerName, std::string &headerValue)=0
Set verification header on an outgoing HTTP message, based on the message payload passed in messageBu...