Some refactoring on the HTTP codebase
This commit is contained in:
@@ -644,6 +644,8 @@ void HttpClientRequestMap::deserialize(StreamBuffer *sb) {
|
||||
}
|
||||
|
||||
HttpParser::HttpParser() {
|
||||
request_id_ = 0;
|
||||
is_request_ = false;
|
||||
status_ = 0;
|
||||
mime_type_ = "";
|
||||
content_length_ = -1;
|
||||
@@ -959,7 +961,7 @@ bool HttpParser::parse_content(std::string_view &view, bool closed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void HttpParser::store_parsed(LuaStack &LS0, LuaSlot tab) const {
|
||||
void HttpParser::store(LuaStack &LS0, LuaSlot tab) const {
|
||||
LuaVar ptab;
|
||||
LuaStack LS(LS0.state(), ptab);
|
||||
|
||||
@@ -1002,7 +1004,11 @@ void HttpParser::store_parsed(LuaStack &LS0, LuaSlot tab) const {
|
||||
}
|
||||
}
|
||||
|
||||
void HttpParser::parser_generate_debug_string(std::ostream &oss) const {
|
||||
eng::string HttpParser::debug_string() const {
|
||||
eng::ostringstream oss;
|
||||
if (request_id_ != 0) {
|
||||
oss << " request_id: " << request_id_ << std::endl;
|
||||
}
|
||||
oss << " status_code: " << status_ << std::endl;
|
||||
oss << " error: " << error_ << std::endl;
|
||||
if (content_length_ >= 0) {
|
||||
@@ -1035,6 +1041,7 @@ void HttpParser::parser_generate_debug_string(std::ostream &oss) const {
|
||||
if (comm_length_ > 0) {
|
||||
oss << " comm_length: " << comm_length_ << std::endl;
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void HttpParser::clear_content_on_error() {
|
||||
@@ -1045,23 +1052,9 @@ void HttpParser::clear_content_on_error() {
|
||||
}
|
||||
}
|
||||
|
||||
HttpClientResponse::HttpClientResponse() {
|
||||
request_id_ = 0;
|
||||
is_request_ = false;
|
||||
}
|
||||
|
||||
eng::string HttpClientResponse::DebugString() const {
|
||||
eng::ostringstream oss;
|
||||
oss << "HttpClientResponse:" << std::endl;
|
||||
if (request_id_ != 0) {
|
||||
oss << " request_id: " << request_id_ << std::endl;
|
||||
}
|
||||
parser_generate_debug_string(oss);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void HttpClientResponse::parse(std::string_view view, bool closed) {
|
||||
void HttpParser::parse_response(std::string_view view, bool closed) {
|
||||
std::string_view original_view = view;
|
||||
is_request_ = false;
|
||||
|
||||
// Parse the status line.
|
||||
if (!parse_status_line(view, closed)) {
|
||||
@@ -1079,7 +1072,7 @@ void HttpClientResponse::parse(std::string_view view, bool closed) {
|
||||
}
|
||||
|
||||
// Calculate the response length.
|
||||
set_comm_length(original_view.size() - view.size());
|
||||
comm_length_ = original_view.size() - view.size();
|
||||
|
||||
// If it's not a redirect, ignore location.
|
||||
if ((status_ < 300) || (status_ > 399)) {
|
||||
@@ -1096,22 +1089,9 @@ void HttpClientResponse::parse(std::string_view view, bool closed) {
|
||||
clear_content_on_error();
|
||||
}
|
||||
|
||||
void HttpClientResponse::store(LuaStack &LS, LuaSlot tab) const {
|
||||
store_parsed(LS, tab);
|
||||
}
|
||||
|
||||
void HttpClientResponse::store_fail(LuaStack &LS, LuaSlot tab, int status_code, std::string_view error) {
|
||||
HttpClientResponse response;
|
||||
response.fail(status_code, error);
|
||||
response.store(LS, tab);
|
||||
}
|
||||
|
||||
HttpServerRequest::HttpServerRequest() {
|
||||
is_request_ = true;
|
||||
}
|
||||
|
||||
void HttpServerRequest::parse(std::string_view view, bool closed) {
|
||||
void HttpParser::parse_request(std::string_view view, bool closed) {
|
||||
std::string_view original_view = view;
|
||||
is_request_ = true;
|
||||
|
||||
// Parse the request line.
|
||||
if (!parse_request_line(view, closed)) {
|
||||
@@ -1131,7 +1111,7 @@ void HttpServerRequest::parse(std::string_view view, bool closed) {
|
||||
}
|
||||
|
||||
// Calculate the comm length.
|
||||
set_comm_length(original_view.size() - view.size());
|
||||
comm_length_ = original_view.size() - view.size();
|
||||
|
||||
// Always ignore location.
|
||||
location_.clear();
|
||||
@@ -1150,15 +1130,10 @@ void HttpServerRequest::parse(std::string_view view, bool closed) {
|
||||
clear_content_on_error();
|
||||
}
|
||||
|
||||
void HttpServerRequest::store(LuaStack &LS, LuaSlot tab) const {
|
||||
store_parsed(LS, tab);
|
||||
}
|
||||
|
||||
eng::string HttpServerRequest::DebugString() const {
|
||||
eng::ostringstream oss;
|
||||
oss << "HttpServerRequest:" << std::endl;
|
||||
parser_generate_debug_string(oss);
|
||||
return oss.str();
|
||||
void HttpParser::store_fail(LuaStack &LS, LuaSlot tab, int status_code, std::string_view error) {
|
||||
HttpParser parser;
|
||||
parser.fail(status_code, error);
|
||||
parser.store(LS, tab);
|
||||
}
|
||||
|
||||
LuaDefine(http_fixurl, "url", "validate URL and repair minor flaws in the URL syntax") {
|
||||
@@ -1264,9 +1239,9 @@ LuaDefine(http_clientresponse, "response",
|
||||
LuaArg text;
|
||||
LuaRet tab;
|
||||
LuaStack LS(L, text, tab);
|
||||
HttpClientResponse resp;
|
||||
resp.parse(LS.ckstring(text), true);
|
||||
resp.store(LS, tab);
|
||||
HttpParser parser;
|
||||
parser.parse_response(LS.ckstring(text), true);
|
||||
parser.store(LS, tab);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
@@ -1281,7 +1256,6 @@ LuaDefine(http_serverrequest, "request",
|
||||
"| params - a table of url-decoded URL parameters"
|
||||
"| content - the content, as a string (POST only)"
|
||||
"| mimetype - the mime type of the content (POST only)"
|
||||
"| location - for HTTP redirects, the target url."
|
||||
"|"
|
||||
"|If the mimetype is a text mimetype, then the content"
|
||||
"|is automatically converted to utf-8."
|
||||
@@ -1320,9 +1294,9 @@ LuaDefine(http_serverrequest, "request",
|
||||
LuaArg text;
|
||||
LuaRet tab;
|
||||
LuaStack LS(L, text, tab);
|
||||
HttpServerRequest req;
|
||||
req.parse(LS.ckstring(text), true);
|
||||
req.store(LS, tab);
|
||||
HttpParser parser;
|
||||
parser.parse_request(LS.ckstring(text), true);
|
||||
parser.store(LS, tab);
|
||||
return LS.result();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user