Machinery for blocking http requests in place, except actual networking

This commit is contained in:
2022-05-03 00:36:11 -04:00
parent d28c588468
commit c41d0522df
9 changed files with 381 additions and 80 deletions

View File

@@ -24,6 +24,11 @@ using UrlParameters = eng::map<eng::string, eng::string>;
class HttpOutRequest : public eng::nevernew {
private:
// Request IDs.
int64_t request_id_;
int64_t place_id_;
int64_t thread_id_;
// If the request contains an error, the error
// message is stored here.
eng::string error_;
@@ -32,38 +37,36 @@ private:
// True is the default.
bool verify_certificate_;
// Method: GET, HEAD, POST, etc.
// Method: GET, HEAD, POST, etc. Must be all-caps.
eng::string method_;
// The hostname. This is used both for DNS lookup,
// and to create an HTTP Host header.
// The hostname. Not yet url-encoded.
eng::string host_;
// Port number.
int port_;
// You may specify either path or encoded_path.
// The path is not url-encoded, and must not include URL parameters.
// The path. Not yet url-encoded. Can not include URL parameters.
eng::string path_;
// If params is nonempty, then we will add URL parameters
// to the URL. The contents of the params field should not be
// urlencoded, the urlencoding is done automatically when the
// request is sent. If you specify encoded_path, then the
// params must be empty, because the encoded path already contains
// the params.
// URL parameters to append to the path. Not yet url-encoded.
UrlParameters params_;
private:
void fail(std::string_view error);
void send_internal(StreamBuffer *target, bool debug_string) const;
public:
// Construct an empty HTTP request.
// All of the fields have empty values.
HttpOutRequest();
// Get fields.
// Get the request IDs.
int64_t request_id() const { return request_id_; }
int64_t place_id() const { return place_id_; }
int64_t thread_id() const { return thread_id_; }
// Get request-related fields.
const eng::string &error() const { return error_; }
bool verify_certificate() const { return verify_certificate_; }
const eng::string &method() const { return method_; }
@@ -74,6 +77,11 @@ public:
// Get the network target, eg, "cert:host:port"
eng::string target() const;
// Set the request IDs.
void set_request_id(int64_t request_id) { request_id_ = request_id; }
void set_place_id(int64_t place_id) { place_id_ = place_id; }
void set_thread_id(int64_t thread_id) { thread_id_ = thread_id; }
// Populate an HTTP request a piece at a time.
// If you pass an invalid value, or if the field is
// already set, the routine will generate an error message
@@ -113,10 +121,20 @@ public:
// Put the request into the stream, assuming HTTP/1.1
void send(StreamBuffer *target) const { send_internal(target, false); }
// Serialize and deserialize.
void serialize(StreamBuffer *sb) const;
void deserialize(StreamBuffer *sb);
// Get the request as a debug string.
eng::string DebugString();
};
class HttpOutRequestMap : public eng::map<int64_t, HttpOutRequest> {
public:
void serialize(StreamBuffer *sb) const;
void deserialize(StreamBuffer *sb);
};
class HttpInResponse {
private:
// The HTTP response status code.
@@ -201,10 +219,13 @@ public:
void parse(const StreamBuffer *sb, bool closed);
// Convert the HTTP response to a lua table.
void store(LuaStack &LS, LuaSlot tab);
void store(LuaStack &LS, LuaSlot tab) const;
// Convert to a debug string.
eng::string DebugString() const;
// Synthesize an error response and store it in lua.
static void store_fail(LuaStack &LS, LuaSlot tab, int status_code, std::string_view error);
};
#endif // HTTP_HPP