mirror of
https://github.com/RGBCube/serenity
synced 2025-07-02 23:32:06 +00:00
LibHTTP: Unify and generalise response handling logic
This commit is contained in:
parent
3dd0f755d0
commit
155853afb2
8 changed files with 442 additions and 400 deletions
|
@ -31,39 +31,38 @@
|
|||
#include <LibCore/TCPSocket.h>
|
||||
#include <LibHTTP/HttpRequest.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <LibHTTP/Job.h>
|
||||
|
||||
namespace HTTP {
|
||||
|
||||
class HttpJob final : public Core::NetworkJob {
|
||||
class HttpJob final : public Job {
|
||||
C_OBJECT(HttpJob)
|
||||
public:
|
||||
explicit HttpJob(const HttpRequest&);
|
||||
virtual ~HttpJob() override;
|
||||
explicit HttpJob(const HttpRequest& request)
|
||||
: Job(request)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~HttpJob() override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void start() override;
|
||||
virtual void shutdown() override;
|
||||
|
||||
HttpResponse* response() { return static_cast<HttpResponse*>(Core::NetworkJob::response()); }
|
||||
const HttpResponse* response() const { return static_cast<const HttpResponse*>(Core::NetworkJob::response()); }
|
||||
protected:
|
||||
virtual void register_on_ready_to_read(Function<void()>) override;
|
||||
virtual void register_on_ready_to_write(Function<void()>) override;
|
||||
virtual bool can_read_line() override;
|
||||
virtual ByteBuffer read_line(size_t) override;
|
||||
virtual bool can_read() const override;
|
||||
virtual ByteBuffer receive(size_t) override;
|
||||
virtual bool eof() const override;
|
||||
virtual bool write(const ByteBuffer&) override;
|
||||
virtual bool is_established() const override { return true; }
|
||||
|
||||
private:
|
||||
void on_socket_connected();
|
||||
void finish_up();
|
||||
|
||||
enum class State {
|
||||
InStatus,
|
||||
InHeaders,
|
||||
InBody,
|
||||
Finished,
|
||||
};
|
||||
|
||||
HttpRequest m_request;
|
||||
RefPtr<Core::Socket> m_socket;
|
||||
State m_state { State::InStatus };
|
||||
int m_code { -1 };
|
||||
HashMap<String, String> m_headers;
|
||||
Vector<ByteBuffer> m_received_buffers;
|
||||
size_t m_received_size { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue