void HTTPRequest::read(std::istream& istr) { static const int eof = std::char_traits<char>::eof(); std::string method; std::string uri; std::string version; method.reserve(16); uri.reserve(64); version.reserve(16); int ch = istr.get(); if (istr.bad()) throw NetException("Error reading HTTP request header"); if (ch == eof) throw NoMessageException(); while (Poco::Ascii::isSpace(ch)) ch = istr.get(); if (ch == eof) throw MessageException("No HTTP request header"); while (!Poco::Ascii::isSpace(ch) && ch != eof && method.length() < MAX_METHOD_LENGTH) { method += (char) ch; ch = istr.get(); } if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP request method invalid or too long"); while (Poco::Ascii::isSpace(ch)) ch = istr.get(); while (!Poco::Ascii::isSpace(ch) && ch != eof && uri.length() < MAX_URI_LENGTH) { uri += (char) ch; ch = istr.get(); } if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP request URI invalid or too long"); while (Poco::Ascii::isSpace(ch)) ch = istr.get(); while (!Poco::Ascii::isSpace(ch) && ch != eof && version.length() < MAX_VERSION_LENGTH) { version += (char) ch; ch = istr.get(); } if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP version string"); while (ch != '\n' && ch != eof) { ch = istr.get(); } HTTPMessage::read(istr); ch = istr.get(); while (ch != '\n' && ch != eof) { ch = istr.get(); } setMethod(method); setURI(uri); setVersion(version); }
void HTTPResponse::read(std::istream& istr) { static const int eof = std::char_traits<char>::eof(); std::string version; std::string status; std::string reason; int ch = istr.get(); if (ch == eof) throw NoMessageException(); while (Poco::Ascii::isSpace(ch)) ch = istr.get(); if (ch == eof) throw MessageException("No HTTP response header"); while (!Poco::Ascii::isSpace(ch) && ch != eof && version.length() < MAX_VERSION_LENGTH) { version += (char) ch; ch = istr.get(); } if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP version string"); while (Poco::Ascii::isSpace(ch)) ch = istr.get(); while (!Poco::Ascii::isSpace(ch) && ch != eof && status.length() < MAX_STATUS_LENGTH) { status += (char) ch; ch = istr.get(); } if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP status code"); while (Poco::Ascii::isSpace(ch) && ch != '\r' && ch != '\n' && ch != eof) ch = istr.get(); while (ch != '\r' && ch != '\n' && ch != eof && reason.length() < MAX_REASON_LENGTH) { reason += (char) ch; ch = istr.get(); } if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP reason string too long"); if (ch == '\r') ch = istr.get(); HTTPMessage::read(istr); ch = istr.get(); while (ch != '\n' && ch != eof) { ch = istr.get(); } setVersion(version); setStatus(status); setReason(reason); }