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