Some refactoring on the HTTP codebase
This commit is contained in:
@@ -147,8 +147,12 @@ public:
|
||||
//
|
||||
class HttpParser : public eng::nevernew {
|
||||
protected:
|
||||
// True if this is for parsing a request.
|
||||
// This is only used for generating error messages.
|
||||
// The request ID is not used by the parser, but
|
||||
// you can store a request ID in there for convenience.
|
||||
int64_t request_id_;
|
||||
|
||||
// True if this parser parsed a request. If false,
|
||||
// then it parsed a response.
|
||||
bool is_request_;
|
||||
|
||||
// The status code, a 3-digit number.
|
||||
@@ -260,28 +264,27 @@ protected:
|
||||
//
|
||||
bool parse_content(std::string_view &view, bool closed);
|
||||
|
||||
// Get or set the communication length.
|
||||
//
|
||||
int comm_length() const { return comm_length_; }
|
||||
void set_comm_length(int len) { comm_length_ = len; }
|
||||
|
||||
public:
|
||||
// Construct a blank parser.
|
||||
//
|
||||
HttpParser();
|
||||
|
||||
// Store the parsed fields into a lua table.
|
||||
//
|
||||
void store_parsed(LuaStack &LS, LuaSlot tab) const;
|
||||
void store(LuaStack &LS, LuaSlot tab) const;
|
||||
|
||||
// Emit all the parser fields as a debug string.
|
||||
//
|
||||
void parser_generate_debug_string(std::ostream &oss) const;
|
||||
|
||||
public:
|
||||
// The parser will not try to parse content longer than this.
|
||||
//
|
||||
const int64_t MAX_CONTENT_LENGTH = 1000000;
|
||||
|
||||
// Parse a request or a response.
|
||||
//
|
||||
// You can only parse once. If you need to parse again,
|
||||
// construct a new parser.
|
||||
//
|
||||
void parse_request(std::string_view view, bool closed);
|
||||
void parse_response(std::string_view view, bool closed);
|
||||
|
||||
// Store a status code and an error message, and clear the content.
|
||||
// This is generally used when the client detects an error,
|
||||
// such as a DNS lookup fail, a connection failed, an SSL negotiation
|
||||
@@ -289,101 +292,31 @@ public:
|
||||
//
|
||||
void fail(int status, std::string_view error);
|
||||
|
||||
// Return true if the communication was complete.
|
||||
//
|
||||
bool complete() const { return status_ != 0; }
|
||||
};
|
||||
|
||||
class HttpClientResponse : public HttpParser {
|
||||
private:
|
||||
// Most of the data is stored in the HttpParser.
|
||||
// That includes the status code, error, headers, and content.
|
||||
|
||||
// The request ID.
|
||||
int64_t request_id_;
|
||||
|
||||
// The length in bytes of the entire communication.
|
||||
// If the response is complete, but the comm_length_
|
||||
// is zero, it means we couldn't find the end of
|
||||
// the request.
|
||||
//
|
||||
int comm_length_;
|
||||
|
||||
public:
|
||||
// Construct a blank response.
|
||||
HttpClientResponse();
|
||||
|
||||
// Parse the HTTP response. The closed flag is to be set to true if the
|
||||
// remote has closed the connection.
|
||||
//
|
||||
// If the request is incomplete, generates a status code of zero. In that
|
||||
// case, loading more data from the server might improve the situation.
|
||||
//
|
||||
// If successful, indicates how much of the text was consumed by
|
||||
// setting comm_length_.
|
||||
//
|
||||
void parse(std::string_view text, bool closed);
|
||||
|
||||
// Get or Set the request ID.
|
||||
// This class does nothing with the request ID, it just stores it.
|
||||
// Get or set the request ID.
|
||||
//
|
||||
int64_t request_id() const { return request_id_; }
|
||||
void set_request_id(int64_t v) { request_id_ = v; }
|
||||
|
||||
// Convert the HTTP response to a lua table.
|
||||
//
|
||||
void store(LuaStack &LS, LuaSlot tab) const;
|
||||
|
||||
// Convert to a debug string.
|
||||
// Return true if the communication was complete.
|
||||
//
|
||||
eng::string DebugString() const;
|
||||
bool complete() const { return status_ != 0; }
|
||||
|
||||
// Synthesize an error response and store it in lua.
|
||||
// Return a debug string.
|
||||
//
|
||||
eng::string debug_string() const;
|
||||
|
||||
// Synthesize an error and store it in lua.
|
||||
//
|
||||
static void store_fail(LuaStack &LS, LuaSlot tab, int status, std::string_view error);
|
||||
};
|
||||
|
||||
class HttpServerRequest : public HttpParser {
|
||||
private:
|
||||
// The length in bytes of the entire communication.
|
||||
// If the request is complete, but the comm_length_
|
||||
// is zero, it means we couldn't find the end of
|
||||
// the request.
|
||||
//
|
||||
int comm_length_;
|
||||
|
||||
public:
|
||||
// Construct a blank request.
|
||||
//
|
||||
HttpServerRequest();
|
||||
|
||||
// Parse the HTTP request. The closed flag is to be set to true if the
|
||||
// remote has closed the connection.
|
||||
//
|
||||
// If the request is incomplete, generates a status code of zero. In that
|
||||
// case, loading more data from the server might improve the situation.
|
||||
//
|
||||
// If successful, indicates how much of the text was consumed by
|
||||
// setting comm_length.
|
||||
//
|
||||
void parse(std::string_view text, bool closed);
|
||||
|
||||
// Convert the HTTP request to a lua table.
|
||||
//
|
||||
void store(LuaStack &LS, LuaSlot tab) const;
|
||||
|
||||
// Convert to a debug string.
|
||||
//
|
||||
eng::string DebugString() const;
|
||||
};
|
||||
|
||||
class HttpClientRequestMap : public eng::map<int64_t, HttpClientRequest> {
|
||||
public:
|
||||
void serialize(StreamBuffer *sb) const;
|
||||
void deserialize(StreamBuffer *sb);
|
||||
};
|
||||
|
||||
using HttpClientResponseVec = eng::vector<HttpClientResponse>;
|
||||
using HttpParserVec = eng::vector<HttpParser>;
|
||||
|
||||
// This class associates an HTTP request to an actual communication
|
||||
// channel that is executing that request.
|
||||
|
||||
Reference in New Issue
Block a user