summaryrefslogtreecommitdiffstats
path: root/src/HTTPServer/SslHTTPServerConnection.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-12-25 19:42:50 +0100
committerMattes D <github@xoft.cz>2016-03-01 16:19:55 +0100
commit2dbc54a14824329faf53851441c6585fd451f0a2 (patch)
treebd59ed0139bfa7aff6a337c45cc38446e8812387 /src/HTTPServer/SslHTTPServerConnection.cpp
parentMerge pull request #3057 from tonibm19/master (diff)
downloadcuberite-2dbc54a14824329faf53851441c6585fd451f0a2.tar
cuberite-2dbc54a14824329faf53851441c6585fd451f0a2.tar.gz
cuberite-2dbc54a14824329faf53851441c6585fd451f0a2.tar.bz2
cuberite-2dbc54a14824329faf53851441c6585fd451f0a2.tar.lz
cuberite-2dbc54a14824329faf53851441c6585fd451f0a2.tar.xz
cuberite-2dbc54a14824329faf53851441c6585fd451f0a2.tar.zst
cuberite-2dbc54a14824329faf53851441c6585fd451f0a2.zip
Diffstat (limited to 'src/HTTPServer/SslHTTPServerConnection.cpp')
-rw-r--r--src/HTTPServer/SslHTTPServerConnection.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/HTTPServer/SslHTTPServerConnection.cpp b/src/HTTPServer/SslHTTPServerConnection.cpp
new file mode 100644
index 000000000..547e6de3a
--- /dev/null
+++ b/src/HTTPServer/SslHTTPServerConnection.cpp
@@ -0,0 +1,115 @@
+
+// SslHTTPConnection.cpp
+
+// Implements the cSslHTTPServerConnection class representing a HTTP connection made over a SSL link
+
+#include "Globals.h"
+#include "SslHTTPServerConnection.h"
+#include "HTTPServer.h"
+
+
+
+
+
+cSslHTTPServerConnection::cSslHTTPServerConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cCryptoKeyPtr & a_PrivateKey) :
+ super(a_HTTPServer),
+ m_Ssl(64000),
+ m_Cert(a_Cert),
+ m_PrivateKey(a_PrivateKey)
+{
+ m_Ssl.Initialize(false);
+ m_Ssl.SetOwnCert(a_Cert, a_PrivateKey);
+}
+
+
+
+
+
+cSslHTTPServerConnection::~cSslHTTPServerConnection()
+{
+ m_Ssl.NotifyClose();
+}
+
+
+
+
+
+void cSslHTTPServerConnection::OnReceivedData(const char * a_Data, size_t a_Size)
+{
+ // Process the received data:
+ const char * Data = a_Data;
+ size_t Size = a_Size;
+ for (;;)
+ {
+ // Try to write as many bytes into Ssl's "incoming" buffer as possible:
+ size_t BytesWritten = 0;
+ if (Size > 0)
+ {
+ BytesWritten = m_Ssl.WriteIncoming(Data, Size);
+ Data += BytesWritten;
+ Size -= BytesWritten;
+ }
+
+ // Try to read as many bytes from SSL's decryption as possible:
+ char Buffer[32000];
+ int NumRead = m_Ssl.ReadPlain(Buffer, sizeof(Buffer));
+ if (NumRead > 0)
+ {
+ super::OnReceivedData(Buffer, static_cast<size_t>(NumRead));
+ // The link may have closed while processing the data, bail out:
+ return;
+ }
+ else if (NumRead == POLARSSL_ERR_NET_WANT_READ)
+ {
+ // SSL requires us to send data to peer first, do so by "sending" empty data:
+ SendData(nullptr, 0);
+ }
+
+ // If both failed, bail out:
+ if ((BytesWritten == 0) && (NumRead <= 0))
+ {
+ return;
+ }
+ }
+}
+
+
+
+
+
+void cSslHTTPServerConnection::SendData(const void * a_Data, size_t a_Size)
+{
+ const char * OutgoingData = reinterpret_cast<const char *>(a_Data);
+ size_t pos = 0;
+ for (;;)
+ {
+ // Write as many bytes from our buffer to SSL's encryption as possible:
+ int NumWritten = 0;
+ if (pos < a_Size)
+ {
+ NumWritten = m_Ssl.WritePlain(OutgoingData + pos, a_Size - pos);
+ if (NumWritten > 0)
+ {
+ pos += static_cast<size_t>(NumWritten);
+ }
+ }
+
+ // Read as many bytes from SSL's "outgoing" buffer as possible:
+ char Buffer[32000];
+ size_t NumBytes = m_Ssl.ReadOutgoing(Buffer, sizeof(Buffer));
+ if (NumBytes > 0)
+ {
+ m_Link->Send(Buffer, NumBytes);
+ }
+
+ // If both failed, bail out:
+ if ((NumWritten <= 0) && (NumBytes == 0))
+ {
+ return;
+ }
+ }
+}
+
+
+
+