Browse Source

WinTLS: Fix busy loop on EOF

Fixes gh#277
Tatsuhiro Tsujikawa 11 years ago
parent
commit
e7e80e5ec6
2 changed files with 22 additions and 1 deletions
  1. 19 1
      src/WinTLSSession.cc
  2. 3 0
      src/WinTLSSession.h

+ 19 - 1
src/WinTLSSession.cc

@@ -35,6 +35,7 @@
 
 #include "WinTLSSession.h"
 
+#include <cassert>
 #include <sstream>
 
 #include "LogFactory.h"
@@ -133,7 +134,8 @@ WinTLSSession::WinTLSSession(WinTLSContext* ctx)
     cred_(ctx->getCredHandle()),
     writeBuffered_(0),
     state_(st_constructed),
-    status_(SEC_E_OK)
+    status_(SEC_E_OK),
+    eof_(false)
 {
   memset(&handle_, 0, sizeof(handle_));
 }
@@ -442,6 +444,20 @@ ssize_t WinTLSSession::readData(void* data, size_t len)
     return len;
   }
 
+  if(eof_) {
+    if(decBuf_.size()) {
+      A2_LOG_DEBUG("WinTLS: Sending out decrypted buffer after EOF");
+      auto nread = decBuf_.size();
+      assert(nread < len);
+      memcpy(data, decBuf_.data(), nread);
+      decBuf_.clear();
+      return nread;
+    }
+    A2_LOG_DEBUG("WinTLS: EOF was already seen");
+
+    return 0;
+  }
+
   if (state_ == st_handshake_write || state_ == st_handshake_write_last ||
       state_ == st_handshake_read) {
     // Renegotiating
@@ -469,6 +485,8 @@ ssize_t WinTLSSession::readData(void* data, size_t len)
       break;
     }
     if (read == 0) {
+      A2_LOG_DEBUG("WinTLS: EOF sensed");
+      eof_ = true;
       break;
     }
     if (read < 0) {

+ 3 - 0
src/WinTLSSession.h

@@ -207,6 +207,9 @@ private:
 
   SECURITY_STATUS status_;
   std::unique_ptr<SecPkgContext_StreamSizes> streamSizes_;
+
+  // true on EOF
+  bool eof_;
 };
 
 } // namespace aria2