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