Remote Call Framework 3.1
HttpFrameFilter.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2019, 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.1
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_HTTPFRAMEFILTER_HPP
20 #define INCLUDE_RCF_HTTPFRAMEFILTER_HPP
21 
22 #include <map>
23 
24 #include <memory>
25 
26 #include <RCF/ByteBuffer.hpp>
27 #include <RCF/Filter.hpp>
28 #include <RCF/RecursionLimiter.hpp>
29 
30 namespace RCF {
31 
32  class Exception;
33  class MemOstream;
34  class MemIstream;
35  class ReallocBuffer;
36 
37  typedef std::shared_ptr<MemOstream> MemOstreamPtr;
38  typedef std::shared_ptr<ReallocBuffer> ReallocBufferPtr;
39 
40  void splitString(
41  const std::string & stringToSplit,
42  const char * splitAt,
43  std::vector<std::string> & lines);
44 
45 
46  class HttpMessage
47  {
48  public:
49 
50  HttpMessage() :
51  mHeaderLen(0),
52  mContentLen(0),
53  mFrameLen(0)
54  {
55  }
56 
57  ~HttpMessage()
58  {
59  }
60 
61  bool parseHttpMessage(const char * pFrame, std::size_t bytesAvailable);
62 
63  void getHttpStatus(std::string& httpStatus, std::string& httpStatusMsg);
64  void getHeaderValue(const std::string& headerName, std::string& headerValue);
65 
66  typedef std::vector< std::pair< std::string, std::string > > HeaderList;
67  HeaderList mHeaderList;
68 
69  std::size_t mHeaderLen;
70  std::size_t mContentLen;
71  std::size_t mFrameLen;
72  std::string mHttpMessageHeader;
73  std::vector<std::string> mMessageLines;
74  std::string mRequestLine;
75  std::string mResponseLine;
76  };
77 
78  class HttpFrameFilter : public Filter
79  {
80  public:
81 
82  // Server-side constructor.
83  HttpFrameFilter(std::size_t maxMessageLength);
84 
85  // Client-side constructor.
86  HttpFrameFilter(
87  const std::string serverAddr,
88  int serverPort);
89 
90  ~HttpFrameFilter();
91 
92  void init();
93  void resetState();
94 
95  void read(
96  const ByteBuffer & byteBuffer,
97  std::size_t bytesRequested);
98 
99  void write(const std::vector<ByteBuffer> &byteBuffers);
100 
101  void onReadCompleted(const ByteBuffer &byteBuffer);
102 
103  void onWriteCompleted(std::size_t bytesTransferred);
104 
105  int getFilterId() const;
106 
107  virtual std::size_t getFrameSize();
108 
109  const std::string & getHttpSessionId();
110  std::uint32_t getHttpSessionIndex();
111  const std::string & getConnectionHeader();
112 
113  void sendHttpErrorResponse(MemOstreamPtr osPtr);
114  void onError(const Exception& e);
115 
116  // If these are set, then we are doing a HTTP response with chunked transfer encoding.
117  bool mChunkedResponseMode;
118  std::size_t mChunkedResponseCounter;
119 
120  bool tryParseHttpHeader();
121  bool tryParseHttpChunkHeader();
122 
123  void getHttpFrameInfo(
124  std::string& requestLine,
125  std::vector< std::pair<std::string, std::string> >& headers);
126 
127 
128  private:
129 
130  void sendServerError(int error);
131 
132  void resizeReadBuffer(std::size_t newSize);
133 
134  std::string mServerAddr;
135  int mServerPort;
136 
137  bool mClientSide;
138  std::string mHttpSessionId;
139  std::uint32_t mHttpSessionIndex;
140  std::string mConnectionHeader;
141  std::string mTransferEncoding;
142 
143  MemOstreamPtr mOsPtr;
144  std::vector<ByteBuffer> mWriteBuffers;
145  std::size_t mWritePos;
146 
147  ByteBuffer mOrigReadBuffer;
148  std::size_t mOrigBytesRequested;
149 
150  ReallocBufferPtr mReadBufferPtr;
151  std::size_t mBytesReceived;
152  std::size_t mReadPos;
153 
154  bool mProtocolChecked;
155  std::size_t mChunkHeaderLen;
156  std::size_t mChunkLen;
157  std::size_t mMaxReadPos;
158  std::size_t mMaxMessageLength;
159  bool mMaxMessageLengthSet;
160 
161  std::string mPrevHttpSessionId;
162  std::uint32_t mPrevHttpSessionIndex;
163 
164  HttpMessage mHttpMessage;
165 
166  std::map<std::string, HttpCookie> mCookies;
167 
168  RecursionState<ByteBuffer, std::size_t> mRecursionStateRead;
169  };
170 
171 } // namespace RCF
172 
173 #endif // ! INCLUDE_RCF_HTTPFRAMEFILTER_HPP
Definition: AmiIoHandler.hpp:24
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...