From b715de1eb6f937b0a05d91842041a54455946061 Mon Sep 17 00:00:00 2001
From: Roland Reichwein <mail@reichwein.it>
Date: Sat, 30 May 2020 22:40:52 +0200
Subject: Bugfixes: write timeout and output size

---
 debian/changelog      |  7 +++++++
 http.cpp              |  1 +
 https.cpp             |  3 ++-
 plugins/fcgi/fcgi.cpp | 17 +++++------------
 webserver.conf        |  9 +++++++++
 5 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index eb9a600..1a7b163 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+webserver (1.9) UNRELEASED; urgency=medium
+
+  * Fix fcgi output size (content-length was 1 too big)
+  * Fix timeout for writing (output)
+
+ -- Roland Reichwein <rr@antcom.de>  Sat, 30 May 2020 22:39:49 +0200
+
 webserver (1.8) unstable; urgency=medium
 
   * Automatically reopen broken fcgi sockets
diff --git a/http.cpp b/http.cpp
index 6a488ed..1d3edee 100644
--- a/http.cpp
+++ b/http.cpp
@@ -63,6 +63,7 @@ class session : public std::enable_shared_from_this<session>
 
  void handle_request(::Server& server, request_type&& req)
  {
+  stream_.expires_after(std::chrono::seconds(300)); // timeout on write by server much longer than read timeout from client
   auto sp = std::make_shared<response_type>(generate_response(req, server));
 
   res_ = sp;
diff --git a/https.cpp b/https.cpp
index e56af9a..50ea31c 100644
--- a/https.cpp
+++ b/https.cpp
@@ -98,6 +98,7 @@ class session : public std::enable_shared_from_this<session>
 
  void handle_request(::Server& server, request_type&& req)
  {
+  beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(300)); // timeout on write by server much longer than read timeout from client
   auto sp = std::make_shared<response_type>(generate_response(req, server));
 
   res_ = sp;
@@ -225,7 +226,7 @@ public:
                 &session::on_read,
                 shared_from_this()));
 #else
-         http::async_read(stream_, buffer_, *parser_,
+        http::async_read(stream_, buffer_, *parser_,
             boost::asio::bind_executor(
                 strand_,
                 std::bind(
diff --git a/plugins/fcgi/fcgi.cpp b/plugins/fcgi/fcgi.cpp
index 22a4d40..0d845a6 100644
--- a/plugins/fcgi/fcgi.cpp
+++ b/plugins/fcgi/fcgi.cpp
@@ -453,7 +453,6 @@ std::string fcgi_plugin::fcgiQuery(FCGIContext& context)
  }
 
  std::istringstream is_out{output_data};
- std::string output;
  std::string line;
 
  // TODO: C++20 coroutine 
@@ -469,25 +468,19 @@ std::string fcgi_plugin::fcgiQuery(FCGIContext& context)
                                // read empty line
                                if (!isEmpty(line))
                                 throw std::runtime_error("Missing empty line between CGI header and body");
-                               if (in)
-                                in();
 
-                               // read remainder
-                               while (in) {
-                                line = in.get();
-                                output += line + '\n';
+                               if (in)
                                 in();
-                               }
-
-                               throw std::runtime_error("Input missing on processing CGI body");
  });
 
  do {
   std::getline(is_out, line);
   processLine(line);
- } while (!is_out.eof());
+ } while (!is_out.eof() && !isEmpty(line));
+
+ std::string r = output_data.substr(is_out.tellg());
 
- return output;
+ return r;
 }
 
 std::string fcgi_plugin::name()
diff --git a/webserver.conf b/webserver.conf
index 871a491..3a4d6f7 100644
--- a/webserver.conf
+++ b/webserver.conf
@@ -82,6 +82,15 @@
     <target>/home/ernie/code/webshop/html</target>
    </path>
 
+   <path requested="/downtube">
+    <plugin>static-files</plugin>
+    <target>/home/ernie/homepage/reichwein.it/downtube</target>
+   </path>
+   <path requested="/downtube/downtube.fcgi">
+    <plugin>fcgi</plugin>
+    <target>127.0.0.1:9004</target>
+   </path>
+
    <path requested="/redirect1">
     <plugin>redirect</plugin>
     <target>https://www.antcom.de/</target>
-- 
cgit v1.2.3