libutil++  1.9.3
 All Classes Functions Variables
HTTPConnection.h
1 /*
2 ** libutil++
3 ** $Id: HTTPConnection.h 1783 2016-11-27 19:17:46Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__server__HTTPConnection_H__
10 #define __libutilxx__sella__server__HTTPConnection_H__
11 
12 #include "../../common.h"
13 #include "../net/SocketException.h"
14 #include "../net/TCPSocket.h"
15 #include "../net/SecureTCPSocket.h"
16 #include "../util/Time.h"
17 #include "Code.h"
18 #include "HTTPRequest.h"
19 #include "ServerException.h"
20 
21 #include <set>
22 #include <memory>
23 
24 namespace sella {
25  namespace server {
26  class HTTPConnection;
27  class HTTP;
28  }
29 }
30 
33  public:
34  typedef std::shared_ptr<HTTPConnection> shared;
35 
36  static const timeval ReceiveTimeoutDefault; /* { 5, 0 }; */
37  static const std::string MIMEFallback; /* text/plain; charset=us-ascii */
38  static const std::string ContentTypeDefault; /* text/html; charset=iso-8859-1 */
39 
40  enum Compression : uint8_t { None, Gzip, Deflate };
41 
42  public:
43  HTTPConnection(const HTTP &http, sella::net::TCPSocket::shared &socket, timeval tv = ReceiveTimeoutDefault) throw ();
44  virtual ~HTTPConnection() throw ();
45 
46  static const HTTPConnection::shared shared_ptr(const HTTP &http, sella::net::TCPSocket::shared &socket, timeval tv = ReceiveTimeoutDefault) throw ();
47 
48  const HTTPRequest& getRequest(void) throw (ServerException);
49  struct timespec getRuntime(void) const throw ();
50  bool isUsable(void) const throw ();
51  bool isSecure(void) const throw ();
52 
53  void response(const std::string &body, const std::string &contentType = ContentTypeDefault, bool close = true) throw (ServerException);
54  void response(const std::vector<char> &content, const std::string &contentType, bool close = true) throw (ServerException);
55  void error(int code, const std::string &detail = std::string(), const std::string &contentType = ContentTypeDefault) throw (ServerException);
56  void error(int code, const std::vector<char> &content, const std::string &contentType = ContentTypeDefault) throw (ServerException);
57 
58  void methodOptions(void) throw (ServerException);
59  void methodGet(void) throw (ServerException);
60  void methodHead(void) throw (ServerException);
61  void methodPost(void) throw (ServerException);
62  void methodPut(void) throw (ServerException);
63  void methodDelete(void) throw (ServerException);
64  void methodTrace(void) throw (ServerException);
65  void methodConnect(void) throw (ServerException);
66  void methodPatch(void) throw (ServerException);
67 
68  sella::net::IPAddressPort getLocalIPAddressPort(void) const throw (ServerException);
69  sella::net::IPAddressPort getRemoteIPAddressPort(void) const throw (ServerException);
70 
71  const char* getSSLLibraryVersion(void) const throw ();
72  const char* getActiveProtocol(void) const throw ();
73  const char* getActiveCipher(void) const throw ();
74 
75  std::string getContentType(HTTPRequest::AcceptType type, const std::string &fallback = std::string()) const throw ();
76 
77  void clear(void) throw ();
78 
79  protected:
80  const HTTP &http;
81  sella::net::TCPSocket::shared socket;
82  struct timespec start;
83  HTTPRequest request;
84 
85  void response(int code, const std::string &body, const std::string &contentType = ContentTypeDefault, bool close = true) throw (ServerException);
86  void response(int code, const std::vector<char> &content, const std::string &contentType, bool close = true) throw (ServerException);
87 
88  void sendStatusLine(int code, const std::string &detail = std::string()) throw (sella::net::SocketException);
89  void sendHeaders(size_t size, const std::string &contentType = ContentTypeDefault, bool close = true, Compression compression = None) throw (sella::net::SocketException);
90  void sendBody(const std::string &body) throw (sella::net::SocketException);
91  void sendContent(const std::vector<char> &content) throw (sella::net::SocketException);
92 
93  void read(std::string &buffer) throw (ServerException);
94  void chunker(void) throw (ServerException);
95 
96  Compression getCompressionType(const std::string &body, const std::string &contentType) const throw ();
97  Compression getCompressionType(const std::vector<char> &content, const std::string &contentType) const throw ();
98 
99  const std::string buildHeaders(size_t contentSize, const std::string &contentType = ContentTypeDefault, bool close = true, bool useAllow = false, Compression compression = None, const std::string &transferEncoding = std::string()) throw ();
100 
101  void getFileContent(const std::string &file, std::vector<char> &content, std::string &contentType) throw (ServerException);
102  size_t getFileSize(std::ifstream &ifile) throw (ServerException);
103  std::string getErrorBody(int code, const std::string &detail = std::string(), const std::string &callback = std::string()) throw ();
104 };
105 
106 #endif
107 
108 /*
109 ** vim: noet ts=3 sw=3
110 */