libiqxmlrpc  0.12.12
http.h
1 // Libiqxmlrpc - an object-oriented XML-RPC solution.
2 // Copyright (C) 2011 Anton Dedov
3 
4 #ifndef _libiqxmlrpc_http_h_
5 #define _libiqxmlrpc_http_h_
6 
7 #include "except.h"
8 #include "inet_addr.h"
9 
10 #include <boost/function.hpp>
11 #include <boost/shared_ptr.hpp>
12 
13 #include <map>
14 #include <string>
15 
16 namespace iqxmlrpc {
17 
18 class Auth_Plugin_base;
19 
21 
25 namespace http {
26 
28 enum Verification_level { HTTP_CHECK_WEAK, HTTP_CHECK_STRICT };
29 
30 #ifdef _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable: 4251)
33 #endif
34 
37 class LIBIQXMLRPC_API Header {
38 public:
39  Header(Verification_level = HTTP_CHECK_WEAK);
40  virtual ~Header();
41 
42  unsigned content_length() const;
43  bool conn_keep_alive() const;
44  bool expect_continue() const;
45 
46  void set_content_length( size_t ln );
47  void set_conn_keep_alive( bool );
48  void set_option(const std::string& name, const std::string& value);
49 
51  std::string dump() const;
52 
53 protected:
54  bool option_exists(const std::string&) const;
55  void set_option_default(const std::string& name, const std::string& value);
56  void set_option_default(const std::string& name, unsigned value);
57  void set_option_checked(const std::string& name, const std::string& value);
58  void set_option(const std::string& name, size_t value);
59 
60  const std::string& get_head_line() const { return head_line_; }
61  std::string get_string(const std::string& name) const;
62  unsigned get_unsigned(const std::string& name) const;
63 
64  //
65  // Parser interface
66  //
67 
68  typedef boost::function<void (const std::string&)> Option_validator_fn;
69 
70  void register_validator(
71  const std::string&,
72  Option_validator_fn,
74 
75  void parse(const std::string&);
76 
77 private:
78  template <class T>
79  T get_option(const std::string& name) const;
80 
81  virtual std::string dump_head() const = 0;
82 
83 private:
84  struct Option_validator {
85  Verification_level level;
86  Option_validator_fn fn;
87  };
88 
89  typedef std::map<std::string, std::string> Options;
90  typedef std::multimap<std::string, Option_validator> Validators;
91 
92  std::string head_line_;
93  Options options_;
94  Validators validators_;
95  Verification_level ver_level_;
96 };
97 
98 #ifdef _MSC_VER
99 #pragma warning(pop)
100 #endif
101 
103 class LIBIQXMLRPC_API Request_header: public Header {
104  std::string uri_;
105 
106 public:
107  Request_header( Verification_level, const std::string& to_parse );
108  Request_header( const std::string& uri, const std::string& vhost, int port );
109 
110  const std::string& uri() const { return uri_; }
111  std::string host() const;
112  std::string agent() const;
113 
114  bool has_authinfo() const;
115  void get_authinfo(std::string& user, std::string& password) const;
116  void set_authinfo(const std::string& user, const std::string& password);
117 
118 private:
119  virtual std::string dump_head() const;
120 };
121 
123 class LIBIQXMLRPC_API Response_header: public Header {
124  int code_;
125  std::string phrase_;
126 
127 public:
128  Response_header( Verification_level, const std::string& to_parse );
129  Response_header( int = 200, const std::string& = "OK" );
130 
131  int code() const { return code_; }
132  const std::string& phrase() const { return phrase_; }
133  std::string server() const;
134 
135 private:
136  std::string current_date() const;
137  virtual std::string dump_head() const;
138 };
139 
140 #ifdef _MSC_VER
141 #pragma warning(push)
142 #pragma warning(disable: 4251)
143 #endif
144 
146 class LIBIQXMLRPC_API Packet {
147 protected:
148  boost::shared_ptr<http::Header> header_;
149  std::string content_;
150 
151 public:
152  Packet( http::Header* header, const std::string& content );
153  virtual ~Packet();
154 
157  void set_keep_alive( bool = true );
158 
159  const http::Header* header() const { return header_.get(); }
160  const std::string& content() const { return content_; }
161 
162  std::string dump() const
163  {
164  return header_->dump() + content_;
165  }
166 };
167 
168 #ifdef _MSC_VER
169 #pragma warning(pop)
170 #pragma warning(disable: 4251)
171 #endif
172 
176  std::string header_cache;
177  std::string content_cache;
178  Header* header;
179  Verification_level ver_level_;
180  bool constructed;
181  size_t pkt_max_sz;
182  size_t total_sz;
183  bool continue_sent_;
184 
185 public:
186  Packet_reader():
187  header(0),
188  constructed(false),
189  pkt_max_sz(0),
190  total_sz(0),
191  continue_sent_(false)
192  {
193  }
194 
195  ~Packet_reader()
196  {
197  if( !constructed )
198  delete header;
199  }
200 
201  void set_verification_level(Verification_level lev)
202  {
203  ver_level_ = lev;
204  }
205 
206  void set_max_size( size_t m )
207  {
208  pkt_max_sz = m;
209  }
210 
211  bool expect_continue() const;
212  Packet* read_request( const std::string& );
213  Packet* read_response( const std::string&, bool read_header_only );
214  void set_continue_sent();
215 
216 private:
217  void clear();
218  void check_sz( size_t );
219  bool read_header( const std::string& );
220 
221  template <class Header_type>
222  Packet* read_packet( const std::string&, bool = false );
223 };
224 
225 
227 class LIBIQXMLRPC_API Malformed_packet: public Exception {
228 public:
230  Exception( "Malformed HTTP packet received.") {}
231 
232  Malformed_packet(const std::string& problem_domain):
233  Exception( "Malformed HTTP packet received (" + problem_domain + ")." ) {}
234 };
235 
238 class LIBIQXMLRPC_API Error_response: public Packet, public Exception {
239 public:
240  Error_response( const std::string& phrase, int code ):
241  Packet( new Response_header(code, phrase), "" ),
242  Exception( "HTTP: " + phrase ) {}
243 
244  ~Error_response() throw() {};
245 
246  const Response_header* response_header() const
247  {
248  return dynamic_cast<const Response_header*>(header());
249  }
250 
251  // deprecated
252  std::string dump_error_response() const { return dump(); }
253 };
254 
255 } // namespace http
256 } // namespace iqxmlrpc
257 
258 #endif
259 // vim:ts=2:sw=2:et
HTTP request's header.
Definition: http.h:103
HTTP packet: Header + Content.
Definition: http.h:146
std::string dump() const
Return text representation of header including final CRLF.
Definition: http.cc:188
HTTP response's header.
Definition: http.h:123
Definition: http.h:175
Exception which is thrown on syntax error during HTTP packet parsing.
Definition: http.h:227
XML-RPC library.
Definition: auth_plugin.cc:6
Definition: http.h:238
Base class for iqxmlrpc exceptions.
Definition: except.h:23
Definition: http.h:37
Verification_level
The level of HTTP sanity checks.
Definition: http.h:28