diff --git a/src/main/native/cpp/support/HttpUtil.cpp b/src/main/native/cpp/support/HttpUtil.cpp index d096e31ec6..a7fac57884 100644 --- a/src/main/native/cpp/support/HttpUtil.cpp +++ b/src/main/native/cpp/support/HttpUtil.cpp @@ -17,24 +17,6 @@ namespace wpi { -llvm::StringRef ReadLine(raw_istream& is, llvm::SmallVectorImpl& buf, - int maxLen, bool* error) { - buf.clear(); - for (int i = 0; i < maxLen; ++i) { - char c; - is.read(c); - if (is.has_error()) { - *error = true; - return llvm::StringRef{buf.data(), buf.size()}; - } - if (c == '\r') continue; - buf.push_back(c); - if (c == '\n') break; - } - *error = false; - return llvm::StringRef{buf.data(), buf.size()}; -} - llvm::StringRef UnescapeURI(llvm::StringRef str, llvm::SmallVectorImpl& buf, bool* error) { buf.clear(); @@ -109,9 +91,8 @@ bool ParseHttpHeaders(raw_istream& is, llvm::SmallVectorImpl* contentType, bool inContentLength = false; llvm::SmallString<64> lineBuf; for (;;) { - bool error; - llvm::StringRef line = ReadLine(is, lineBuf, 1024, &error).rtrim(); - if (error) return false; + llvm::StringRef line = is.getline(lineBuf, 1024).rtrim(); + if (is.has_error()) return false; if (line.empty()) return true; // empty line signals end of headers // header fields start at the beginning of the line @@ -319,10 +300,9 @@ bool HttpConnection::Handshake(const HttpRequest& request, os.flush(); // read first line of response - bool error = false; llvm::SmallString<64> lineBuf; - llvm::StringRef line = ReadLine(is, lineBuf, 1024, &error).rtrim(); - if (error) { + llvm::StringRef line = is.getline(lineBuf, 1024).rtrim(); + if (is.has_error()) { *warnMsg = "disconnected before response"; return false; } diff --git a/src/main/native/cpp/support/raw_istream.cpp b/src/main/native/cpp/support/raw_istream.cpp index 2863a61d60..687d95b3ed 100644 --- a/src/main/native/cpp/support/raw_istream.cpp +++ b/src/main/native/cpp/support/raw_istream.cpp @@ -16,8 +16,25 @@ #include #endif +#include "llvm/SmallVector.h" +#include "llvm/StringRef.h" + using namespace wpi; +llvm::StringRef raw_istream::getline(llvm::SmallVectorImpl& buf, + int maxLen) { + buf.clear(); + for (int i = 0; i < maxLen; ++i) { + char c; + read(c); + if (has_error()) return llvm::StringRef{buf.data(), buf.size()}; + if (c == '\r') continue; + buf.push_back(c); + if (c == '\n') break; + } + return llvm::StringRef{buf.data(), buf.size()}; +} + void raw_mem_istream::close() {} std::size_t raw_mem_istream::in_avail() const { return m_left; } diff --git a/src/main/native/include/support/HttpUtil.h b/src/main/native/include/support/HttpUtil.h index 35349e5bc1..b380cb1f49 100644 --- a/src/main/native/include/support/HttpUtil.h +++ b/src/main/native/include/support/HttpUtil.h @@ -23,15 +23,6 @@ namespace wpi { -// Read a line from an input stream (up to a maximum length). -// The returned buffer will contain the trailing \n (unless the maximum length -// was reached). \r's are stripped from the buffer. -// @param buf Buffer for output -// @param error Set to true if an error occurred -// @return Line -llvm::StringRef ReadLine(wpi::raw_istream& is, llvm::SmallVectorImpl& buf, - int maxLen, bool* error); - // Unescape a %xx-encoded URI. // @param buf Buffer for output // @param error Set to true if an error occurred diff --git a/src/main/native/include/support/raw_istream.h b/src/main/native/include/support/raw_istream.h index d9a9b9cd98..49aeffde25 100644 --- a/src/main/native/include/support/raw_istream.h +++ b/src/main/native/include/support/raw_istream.h @@ -11,6 +11,12 @@ #include #include +namespace llvm { +template +class SmallVectorImpl; +class StringRef; +} + namespace wpi { class raw_istream { @@ -45,6 +51,14 @@ class raw_istream { return readlen; }; + // Read a line from an input stream (up to a maximum length). + // The returned buffer will contain the trailing \n (unless the maximum length + // was reached). \r's are stripped from the buffer. + // @param buf Buffer for output + // @param maxLen Maximum length + // @return Line + llvm::StringRef getline(llvm::SmallVectorImpl& buf, int maxLen); + virtual void close() = 0; virtual std::size_t in_avail() const = 0;