Skip to content

Response Documentation

Michelle Jiam edited this page Oct 13, 2022 · 2 revisions

Response class

  • Holds all the components of the response that's to be sent to the client.
  • ResponseHandler contains a Response::pointer member variable.
  • Inherits from HTTPMessage superclass.

Member types

Type Definition
pointer std::shared_ptr

Public methods

bool IsComplete() const

  • Returns: true if request has been executed and the response is fully formed and ready to be sent.

std::istream* GetBodyStream() const

  • Returns: pointer to an istream object containing the response payload body.

std::string const& GetReasonPhrase() const

  • Returns: string containing the reason phrase attached to the status code (e.g. "Not Found" for 404);

std::string const& GetRequestTarget() const

  • Returns: string containing the request target.

std::string const& GetResolvedPath() const

  • Returns: string containing the final target path after resolution with the config file.

std::string GetFieldsAsString() const

  • Returns: string containing all the header fields and values.

std::string& GetCompleteResponseString()

  • Returns: string containing the entire response (status line, headers, payload body).

void SetBodyStream(std::istream* stream)

  • Takes: pointer to istream object containing payload body.

void SetReasonPhrase(std::string const& phrase)

  • Takes: string containing reason phrase.

void SetRequestTarget(std::string const& path)

  • Takes: string containing request target path.

void SetResolvedPath(std::string const& path)

  • Takes: string containing final target path.

void SetComplete(bool value = true)

  • Takes: boolean, defaults to true.

ResponseHandler class

  • Handles the execution of the Request, and formation and sending of the Response.
  • Connection keeps a member variable of this type.
  • Can throw: SendFailureException (treated as fatal error).

Public methods

bool Ready()

  • Returns: true if Response is complete and ready to be sent. Calls on Response::IsDone.

bool IsDone() const

  • Returns: true if response has been completely transmitted and connection can be closed.

void Send(int fd)

  • Sends response string to client socket. If response is bigger than BUFFER_SIZE (defined in utils.hpp as 8192 bytes), sends remaining bytes in subsequent calls.
  • If 100 Continue response was sent, reassigns Response::pointer member variable for final response and doesn't set is_done to true.
  • Else if entire response has been sent, sets is_done to true.
  • Takes: file descriptor of the socket that's ready to be written to.

void HandleError(Request& request)

  • Handles HTTP errors. Checks if custom error page has been defined in the config file, otherwise serves static error page defined in error_responses.cpp.
  • Takes: reference to a Request.

void HandleExpect(Request& request)

  • Sends immediate 100: Continue response when Expect header is received (but no message body yet).
  • Takes: reference to a Request.

void HandleRegular(Request& request)

  • Handles redirected, CGI, and regular requests.
  • Takes: reference to a Request.

Response const& GetResponse()

  • Returns: a const-reference to the Response object.

ResponseGenerator class

  • Used by ResponseHandler to fill in the Response status line and headers during the Response formation process.

Public methods

void FormResponse(Response& response, Request* request)

  • Takes: a reference to a Response object to fill and a pointer to a Request for extracting information.

FileHandler class

  • Handles retrieval, deletion, and creation of files depending on request method.
  • ResponseHandler keeps a member variable of this type.
  • Can throw: 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error).

Public methods

std::istream* GetFile(std::string const& file_path)

  • Opens the file at file_path as a stream.
  • Takes: string referring to path of file to be retrieved.
  • Returns: pointer to a heap-allocated istream object referring to the file.

std::string GetExtension(std::string const& file_path) const

  • Returns the file extension without the '.'
  • Takes: string referring to path of file.
  • Returns: string containing file extension (e.g. "png" or "txt"). Or empty string if no extension found.

std::istream* ExecuteMethod(Response& response, Method method)

  • Executes request method.
  • Takes: a reference to a Response object and a Method enum class value (Get, GetError, GetGeneratedIndex, Post, Delete).
  • Returns: pointer to a heap-allocated istream object referring to the response payload body.