HTTPSClientSession.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // HTTPSClientSession.h
  3. //
  4. // Library: NetSSL_OpenSSL
  5. // Package: HTTPSClient
  6. // Module: HTTPSClientSession
  7. //
  8. // Definition of the HTTPSClientSession class.
  9. //
  10. // Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef NetSSL_HTTPSClientSession_INCLUDED
  16. #define NetSSL_HTTPSClientSession_INCLUDED
  17. #include "Poco/Net/NetSSL.h"
  18. #include "Poco/Net/Utility.h"
  19. #include "Poco/Net/HTTPClientSession.h"
  20. #include "Poco/Net/Context.h"
  21. #include "Poco/Net/Session.h"
  22. #include "Poco/Net/X509Certificate.h"
  23. namespace Poco {
  24. namespace Net {
  25. class SecureStreamSocket;
  26. class HTTPRequest;
  27. class HTTPResponse;
  28. class NetSSL_API HTTPSClientSession: public HTTPClientSession
  29. /// This class implements the client-side of
  30. /// a HTTPS session.
  31. ///
  32. /// To send a HTTPS request to a HTTPS server, first
  33. /// instantiate a HTTPSClientSession object and
  34. /// specify the server's host name and port number.
  35. ///
  36. /// Then create a HTTPRequest object, fill it accordingly,
  37. /// and pass it as argument to the sendRequest() method.
  38. ///
  39. /// sendRequest() will return an output stream that can
  40. /// be used to send the request body, if there is any.
  41. ///
  42. /// After you are done sending the request body, create
  43. /// a HTTPResponse object and pass it to receiveResponse().
  44. ///
  45. /// This will return an input stream that can be used to
  46. /// read the response body.
  47. ///
  48. /// See RFC 2616 <http://www.faqs.org/rfcs/rfc2616.html> for more
  49. /// information about the HTTP protocol.
  50. ///
  51. /// Note that sending requests that neither contain a content length
  52. /// field in the header nor are using chunked transfer encoding will
  53. /// result in a SSL protocol violation, as the framework shuts down
  54. /// the socket after sending the message body. No orderly SSL shutdown
  55. /// will be performed in this case.
  56. ///
  57. /// If session caching has been enabled for the Context object passed
  58. /// to the HTTPSClientSession, the HTTPSClientSession class will
  59. /// attempt to reuse a previously obtained Session object in
  60. /// case of a reconnect.
  61. {
  62. public:
  63. enum
  64. {
  65. HTTPS_PORT = 443
  66. };
  67. HTTPSClientSession();
  68. /// Creates an unconnected HTTPSClientSession.
  69. explicit HTTPSClientSession(const SecureStreamSocket& socket);
  70. /// Creates a HTTPSClientSession using the given socket.
  71. /// The socket must not be connected. The session
  72. /// takes ownership of the socket.
  73. HTTPSClientSession(const SecureStreamSocket& socket, Session::Ptr pSession);
  74. /// Creates a HTTPSClientSession using the given socket.
  75. /// The socket must not be connected. The session
  76. /// takes ownership of the socket.
  77. ///
  78. /// The given Session is reused, if possible (client session
  79. /// caching is enabled for the given Context, and the server
  80. /// agrees to reuse the session).
  81. HTTPSClientSession(const std::string& host, Poco::UInt16 port = HTTPS_PORT);
  82. /// Creates a HTTPSClientSession using the given host and port.
  83. explicit HTTPSClientSession(Context::Ptr pContext);
  84. /// Creates an unconnected HTTPSClientSession, using the
  85. /// give SSL context.
  86. HTTPSClientSession(Context::Ptr pContext, Session::Ptr pSession);
  87. /// Creates an unconnected HTTPSClientSession, using the
  88. /// give SSL context.
  89. ///
  90. /// The given Session is reused, if possible (client session
  91. /// caching is enabled for the given Context, and the server
  92. /// agrees to reuse the session).
  93. HTTPSClientSession(const std::string& host, Poco::UInt16 port, Context::Ptr pContext);
  94. /// Creates a HTTPSClientSession using the given host and port,
  95. /// using the given SSL context.
  96. HTTPSClientSession(const std::string& host, Poco::UInt16 port, Context::Ptr pContext, Session::Ptr pSession);
  97. /// Creates a HTTPSClientSession using the given host and port,
  98. /// using the given SSL context.
  99. ///
  100. /// The given Session is reused, if possible (client session
  101. /// caching is enabled for the given Context, and the server
  102. /// agrees to reuse the session).
  103. ~HTTPSClientSession();
  104. /// Destroys the HTTPSClientSession and closes
  105. /// the underlying socket.
  106. bool secure() const;
  107. /// Return true iff the session uses SSL or TLS,
  108. /// or false otherwise.
  109. X509Certificate serverCertificate();
  110. /// Returns the server's certificate.
  111. ///
  112. /// The certificate is available after the first request has been sent.
  113. Session::Ptr sslSession();
  114. /// Returns the SSL Session object for the current
  115. /// connection, if session caching has been enabled for
  116. /// the HTTPSClientSession's Context. A null pointer is
  117. /// returned otherwise.
  118. ///
  119. /// The Session object can be obtained after the first request has
  120. /// been sent.
  121. // HTTPSession
  122. void abort();
  123. protected:
  124. void connect(const SocketAddress& address);
  125. std::string proxyRequestPrefix() const;
  126. void proxyAuthenticate(HTTPRequest& request);
  127. int read(char* buffer, std::streamsize length);
  128. private:
  129. HTTPSClientSession(const HTTPSClientSession&);
  130. HTTPSClientSession& operator = (const HTTPSClientSession&);
  131. Context::Ptr _pContext;
  132. Session::Ptr _pSession;
  133. };
  134. } } // namespace Poco::Net
  135. #endif // Net_HTTPSClientSession_INCLUDED