From 05213d6f1d8e946e717dee948bd14b6ef3876827 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 9 May 2020 18:12:28 +0200 Subject: Improve socket read handling --- plugins/fcgi/fcgi.cpp | 13 ++++++------- plugins/fcgi/socket.cpp | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) (limited to 'plugins') diff --git a/plugins/fcgi/fcgi.cpp b/plugins/fcgi/fcgi.cpp index bb0a8b4..d19916c 100644 --- a/plugins/fcgi/fcgi.cpp +++ b/plugins/fcgi/fcgi.cpp @@ -407,7 +407,7 @@ std::string fcgi_plugin::fcgiQuery(FCGIContext& context) FCGI_Record stdin_{FCGI_STDIN, id, body}; if (socket->write(stdin_.getBuffer()) != stdin_.getBuffer().size()) std::cerr << "Warning: Not all bytes written 6" << std::endl; - + if (body.size()) { FCGI_Record stdin_end{FCGI_STDIN, id, std::string{}}; if (socket->write(stdin_end.getBuffer()) != stdin_end.getBuffer().size()) @@ -424,15 +424,14 @@ std::string fcgi_plugin::fcgiQuery(FCGIContext& context) std::cerr << "Warning: Not all bytes written 8" << std::endl; #endif - bool ended{false}; std::vector inbuf; - std::vector inbuf_part(1024); + + bool ended{false}; while (!ended) { try { - size_t got {socket->read(inbuf_part)}; - inbuf.insert(inbuf.end(), inbuf_part.begin(), inbuf_part.begin() + got); + socket->read(inbuf); } catch (const fcgi_eof_error&) { - //std::cerr << "FCGI Warning: Early EOF" << std::endl; // seems to be ok here + std::cerr << "FCGI Warning: Early EOF" << std::endl; // seems to be ok here ended = true; //return HttpStatus("500", "FCGI connection: EOF on read", context.SetResponseHeader); } @@ -453,7 +452,7 @@ std::string fcgi_plugin::fcgiQuery(FCGIContext& context) } else throw std::runtime_error("Unhandled FCGI type: "s + std::to_string(r.getType())); } catch (const std::length_error& ex) { - // ignore if not enough data available yet + // ignore FCGI_Record construction if not enough data available for full record yet break; } } diff --git a/plugins/fcgi/socket.cpp b/plugins/fcgi/socket.cpp index 228964c..5402cfb 100644 --- a/plugins/fcgi/socket.cpp +++ b/plugins/fcgi/socket.cpp @@ -106,7 +106,17 @@ size_t TCPSocket::write(const std::vector& data) size_t TCPSocket::read(std::vector& data) { try { - return m_socket.read_some(boost::asio::buffer(data)); + size_t result{0}; + + while (m_socket.available()) { + std::vector inbuf_part(1024); + size_t got { m_socket.read_some(boost::asio::buffer(inbuf_part))}; + data.insert(data.end(), inbuf_part.begin(), inbuf_part.begin() + got); + result += got; + } + + return result; + } catch (const boost::system::system_error& ex) { if (ex.code() == boost::asio::error::eof) { throw fcgi_eof_error("EOF on read"); @@ -165,7 +175,16 @@ size_t FileSocket::write(const std::vector& data) size_t FileSocket::read(std::vector& data) { try { - return m_socket.read_some(boost::asio::buffer(data)); + size_t result{0}; + + while (m_socket.available()) { + std::vector inbuf_part(1024); + size_t got { m_socket.read_some(boost::asio::buffer(inbuf_part))}; + data.insert(data.end(), inbuf_part.begin(), inbuf_part.begin() + got); + result += got; + } + + return result; } catch (const boost::system::system_error& ex) { if (ex.code() == boost::asio::error::eof) { throw fcgi_eof_error("EOF on read"); -- cgit v1.2.3