HTTPSession.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // HTTPSession.h
  3. //
  4. // Library: Net
  5. // Package: HTTP
  6. // Module: HTTPSession
  7. //
  8. // Definition of the HTTPSession class.
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Net_HTTPSession_INCLUDED
  16. #define Net_HTTPSession_INCLUDED
  17. #include "Poco/Net/Net.h"
  18. #include "Poco/Net/StreamSocket.h"
  19. #include "Poco/Timespan.h"
  20. #include "Poco/Exception.h"
  21. #include "Poco/Any.h"
  22. #include "Poco/Buffer.h"
  23. #include <ios>
  24. namespace Poco {
  25. namespace Net {
  26. class Net_API HTTPSession
  27. /// HTTPSession implements basic HTTP session management
  28. /// for both HTTP clients and HTTP servers.
  29. ///
  30. /// HTTPSession implements buffering for HTTP connections, as well
  31. /// as specific support for the various HTTP stream classes.
  32. ///
  33. /// This class can not be instantiated. HTTPClientSession or
  34. /// HTTPServerSession must be used instead.
  35. {
  36. public:
  37. void setKeepAlive(bool keepAlive);
  38. /// Sets the keep-alive flag for this session.
  39. ///
  40. /// If the keep-alive flag is enabled, persistent
  41. /// HTTP/1.1 connections are supported.
  42. bool getKeepAlive() const;
  43. /// Returns the value of the keep-alive flag for
  44. /// this session.
  45. void setTimeout(const Poco::Timespan& timeout);
  46. /// Sets the timeout for the HTTP session.
  47. void setTimeout(const Poco::Timespan& connectionTimeout, const Poco::Timespan& sendTimeout, const Poco::Timespan& receiveTimeout);
  48. /// Sets different timeouts for the HTTP session.
  49. Poco::Timespan getTimeout() const;
  50. /// Returns the timeout for the HTTP session.
  51. bool connected() const;
  52. /// Returns true if the underlying socket is connected.
  53. virtual void abort();
  54. /// Aborts a session in progress by shutting down
  55. /// and closing the underlying socket.
  56. const Poco::Exception* networkException() const;
  57. /// If sending or receiving data over the underlying
  58. /// socket connection resulted in an exception, a
  59. /// pointer to this exception is returned.
  60. ///
  61. /// Otherwise, NULL is returned.
  62. void attachSessionData(const Poco::Any& data);
  63. /// Allows to attach an application-specific data
  64. /// item to the session.
  65. ///
  66. /// On the server side, this can be used to manage
  67. /// data that must be maintained over the entire
  68. /// lifetime of a persistent connection (that is,
  69. /// multiple requests sent over the same connection).
  70. const Poco::Any& sessionData() const;
  71. /// Returns the data attached with attachSessionData(),
  72. /// or an empty Poco::Any if no user data has been
  73. /// attached.
  74. enum
  75. {
  76. HTTP_PORT = 80
  77. };
  78. StreamSocket detachSocket();
  79. /// Detaches the socket from the session.
  80. ///
  81. /// The socket is returned, and a new, uninitialized socket is
  82. /// attached to the session.
  83. StreamSocket& socket();
  84. /// Returns a reference to the underlying socket.
  85. void drainBuffer(Poco::Buffer<char>& buffer);
  86. /// Copies all bytes remaining in the internal buffer to the
  87. /// given Poco::Buffer, resizing it as necessary.
  88. ///
  89. /// This is usually used together with detachSocket() to
  90. /// obtain any data already read from the socket, but not
  91. /// yet processed.
  92. protected:
  93. HTTPSession();
  94. /// Creates a HTTP session using an
  95. /// unconnected stream socket.
  96. HTTPSession(const StreamSocket& socket);
  97. /// Creates a HTTP session using the
  98. /// given socket. The session takes ownership
  99. /// of the socket and closes it when it's no
  100. /// longer used.
  101. HTTPSession(const StreamSocket& socket, bool keepAlive);
  102. /// Creates a HTTP session using the
  103. /// given socket. The session takes ownership
  104. /// of the socket and closes it when it's no
  105. /// longer used.
  106. virtual ~HTTPSession();
  107. /// Destroys the HTTPSession and closes the
  108. /// underlying socket.
  109. int get();
  110. /// Returns the next byte in the buffer.
  111. /// Reads more data from the socket if there are
  112. /// no bytes left in the buffer.
  113. int peek();
  114. /// Peeks at the next character in the buffer.
  115. /// Reads more data from the socket if there are
  116. /// no bytes left in the buffer.
  117. virtual int read(char* buffer, std::streamsize length);
  118. /// Reads up to length bytes.
  119. ///
  120. /// If there is data in the buffer, this data
  121. /// is returned. Otherwise, data is read from
  122. /// the socket to avoid unnecessary buffering.
  123. virtual int write(const char* buffer, std::streamsize length);
  124. /// Writes data to the socket.
  125. int receive(char* buffer, int length);
  126. /// Reads up to length bytes.
  127. int buffered() const;
  128. /// Returns the number of bytes in the buffer.
  129. void refill();
  130. /// Refills the internal buffer.
  131. virtual void connect(const SocketAddress& address);
  132. /// Connects the underlying socket to the given address
  133. /// and sets the socket's receive timeout.
  134. void attachSocket(const StreamSocket& socket);
  135. /// Attaches a socket to the session, replacing the
  136. /// previously attached socket.
  137. void close();
  138. /// Closes the underlying socket.
  139. void setException(const Poco::Exception& exc);
  140. /// Stores a clone of the exception.
  141. void clearException();
  142. /// Clears the stored exception.
  143. private:
  144. enum
  145. {
  146. HTTP_DEFAULT_TIMEOUT = 60000000,
  147. HTTP_DEFAULT_CONNECTION_TIMEOUT = 30000000
  148. };
  149. HTTPSession(const HTTPSession&);
  150. HTTPSession& operator = (const HTTPSession&);
  151. StreamSocket _socket;
  152. char* _pBuffer;
  153. char* _pCurrent;
  154. char* _pEnd;
  155. bool _keepAlive;
  156. Poco::Timespan _connectionTimeout;
  157. Poco::Timespan _receiveTimeout;
  158. Poco::Timespan _sendTimeout;
  159. Poco::Exception* _pException;
  160. Poco::Any _data;
  161. friend class HTTPStreamBuf;
  162. friend class HTTPHeaderStreamBuf;
  163. friend class HTTPFixedLengthStreamBuf;
  164. friend class HTTPChunkedStreamBuf;
  165. };
  166. //
  167. // inlines
  168. //
  169. inline bool HTTPSession::getKeepAlive() const
  170. {
  171. return _keepAlive;
  172. }
  173. inline Poco::Timespan HTTPSession::getTimeout() const
  174. {
  175. return _receiveTimeout;
  176. }
  177. inline StreamSocket& HTTPSession::socket()
  178. {
  179. return _socket;
  180. }
  181. inline const Poco::Exception* HTTPSession::networkException() const
  182. {
  183. return _pException;
  184. }
  185. inline int HTTPSession::buffered() const
  186. {
  187. return static_cast<int>(_pEnd - _pCurrent);
  188. }
  189. inline const Poco::Any& HTTPSession::sessionData() const
  190. {
  191. return _data;
  192. }
  193. } } // namespace Poco::Net
  194. #endif // Net_HTTPSession_INCLUDED