You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WebCC/webcc/http_response_parser.cc

49 lines
967 B
C++

#include "webcc/http_response_parser.h"
#include "webcc/logger.h"
#include "webcc/http_response.h"
namespace webcc {
HttpResponseParser::HttpResponseParser(HttpResponse* response)
: HttpParser(response), response_(response) {
}
bool HttpResponseParser::ParseStartLine(const std::string& line) {
response_->set_start_line(line + "\r\n");
std::size_t off = 0;
std::size_t pos = line.find(' ');
if (pos == std::string::npos) {
return false;
}
// HTTP version
off = pos + 1; // Skip space.
pos = line.find(' ', off);
if (pos == std::string::npos) {
return false;
}
// Status code
std::string status_str = line.substr(off, pos - off);
try {
response_->set_status(std::stoi(status_str));
} catch (const std::exception&) {
LOG_ERRO("Invalid HTTP status: %s", status_str.c_str());
return false;
}
if (response_->status() != HttpStatus::kOK) {
return false;
}
return true;
}
} // namespace webcc