Class HttpResponse

Synopsis

#include <lib/inc/drogon/HttpResponse.h>

class HttpResponse

Description

No description yet.

Mentioned in

Methods

~HttpResponse
addCookieAdd a cookie.
addHeaderAdd a header.
asThis template enables explicit type conversion, see the above template.
bodyGet the response body.
clearReset the reponse object to its initial state.
contentTypeGet the response content type.
cookiesGet all cookies.
creationDateGet the creation timestamp of the response.
expiredTimeGet the expiration time of the response.
getBodyGet the response body.
getContentType
getCookieGet the cookie identified by the key parameter
getCookiesGet all cookies.
getCreationDate
getExpiredTime
getHeaderGet the header string identified by the key parameter.
getHeadersGet all headers of the response.
getJsonObject
getStatusCode
getVersionReturn the enum type version of the response.
headersGet all headers of the response.
ifCloseConnectionGet the status set by the setCloseConnetion() method.
jsonObjectGet the json object from the server response
newCustomHttpResponseCreate a custom HTTP response object. For using this template, users must specialize the toResponse template.
newFileResponseCreate a response that returns a file to the client.
newHttpJsonResponseCreate a response which returns a json object
newHttpResponseCreate a normal response with a status code of 200ok and a content type of text/html.
newHttpViewResponseCreate a response that returns a page rendered by a view named viewName.
newNotFoundResponseCreate a response which returns a 404 page.
newRedirectionResponseCreate a response that returns a redirection page, redirecting to another page located in the location parameter.
operator TThis template enables automatic type conversion. For using this template, user must specialize the fromResponse template. For example a shared_ptr<Json::Value> specialization version is available above, so we can use the following code to get a json object:
removeCookieRemove the cookie identified by the key parameter.
removeHeaderRemove the header identified by the key parameter.
setBodySet the response body(content).
setCloseConnectionSet if close the connection after the request is sent.
setContentTypeCodeSet the response content type, such as text/html, text/plaint, image/png and so on
setContentTypeCodeAndCustomStringSet the response content type and the content-type string, The string must contain the header name and CRLF
setExpiredTimeSet the expiration time of the response cache in memory
setPassThroughSet the reponse object to the pass-through mode or not. It's not by default when a new response object is created. In pass-through mode, no addtional headers (including server, date, content-type and content-length, etc.) are added to the response. This mode is useful for some applications such as a proxy.
setStatusCodeSet the status code of the response.
setVersionSet the http version, http1.0 or http1.1.
statusCodeGet the status code such as 200, 404.
versionReturn the enum type version of the response.

Source

Lines 66-368 in lib/inc/drogon/HttpResponse.h. Line 67 in lib/inc/drogon/HttpResponse.h.

class HttpResponse
{
  public:
    /**
     * @brief This template enables automatic type conversion. For using this
     * template, user must specialize the fromResponse template. For example a
     * shared_ptr<Json::Value> specialization version is available above, so
     * we can use the following code to get a json object:
     * @code
     *  std::shared_ptr<Json::Value> jsonPtr = *responsePtr;
     *  @endcode
     * With this template, user can use their favorite JSON library instead of
     * the default jsoncpp library or convert the response to an object of any
     * custom type.
     */
    template <typename T>
    operator T() const
    {
        return fromResponse<T>(*this);
    }

    /**
     * @brief This template enables explicit type conversion, see the above
     * template.
     */
    template <typename T>
    T as() const
    {
        return fromResponse<T>(*this);
    }

    /// Get the status code such as 200, 404
    virtual HttpStatusCode statusCode() const = 0;
    HttpStatusCode getStatusCode() const
    {
        return statusCode();
    }

    /// Set the status code of the response.
    virtual void setStatusCode(HttpStatusCode code) = 0;

    /// Get the creation timestamp of the response.
    virtual const trantor::Date &creationDate() const = 0;
    const trantor::Date &getCreationDate() const
    {
        return creationDate();
    }

    /// Set the http version, http1.0 or http1.1
    virtual void setVersion(const Version v) = 0;

    /// Set if close the connection after the request is sent.
    /**
     * @param on if the parameter is false, the connection keeps alive on the
     * condition that the client request has a 'keep-alive' head, otherwise it
     * is closed immediately after sending the last byte of the response. It's
     * false by default when the response is created.
     */
    virtual void setCloseConnection(bool on) = 0;

    /// Get the status set by the setCloseConnetion() method.
    virtual bool ifCloseConnection() const = 0;

    /// Set the response content type, such as text/html, text/plaint, image/png
    /// and so on. If the content type
    /// is a text type, the character set is utf8.
    virtual void setContentTypeCode(ContentType type) = 0;

    /// Set the response content type and the content-type string, The string
    /// must contain the header name and CRLF.
    /// For example, "content-type: text/plain\r\n"
    void setContentTypeCodeAndCustomString(ContentType type,
                                           const string_view &typeString)
    {
        setContentTypeCodeAndCustomString(type,
                                          typeString.data(),
                                          typeString.length());
    }
    template <int N>
    void setContentTypeCodeAndCustomString(ContentType type,
                                           const char (&typeString)[N])
    {
        assert(N > 0);
        setContentTypeCodeAndCustomString(type, typeString, N - 1);
    }

    /// Set the reponse content type and the character set.
    /// virtual void setContentTypeCodeAndCharacterSet(ContentType type, const
    /// std::string &charSet = "utf-8") = 0;

    /// Get the response content type.
    virtual ContentType contentType() const = 0;
    ContentType getContentType() const
    {
        return contentType();
    }

    /// Get the header string identified by the key parameter.
    /**
     * @note
     * If there is no the header, a empty string is retured.
     * The key is case insensitive
     */
    virtual const std::string &getHeader(const std::string &key) const = 0;
    virtual const std::string &getHeader(std::string &&key) const = 0;

    /// Remove the header identified by the key parameter.
    virtual void removeHeader(const std::string &key) = 0;

    /// Remove the header identified by the key parameter.
    virtual void removeHeader(std::string &&key) = 0;

    /// Get all headers of the response
    virtual const std::unordered_map<std::string, std::string> &headers()
        const = 0;

    /// Get all headers of the response
    const std::unordered_map<std::string, std::string> &getHeaders() const
    {
        return headers();
    }

    /// Add a header.
    virtual void addHeader(const std::string &key,
                           const std::string &value) = 0;

    /// Add a header.
    virtual void addHeader(const std::string &key, std::string &&value) = 0;

    /// Add a cookie
    virtual void addCookie(const std::string &key,
                           const std::string &value) = 0;

    /// Add a cookie
    virtual void addCookie(const Cookie &cookie) = 0;
    virtual void addCookie(Cookie &&cookie) = 0;

    /// Get the cookie identified by the key parameter.
    /// If there is no the cookie, the empty cookie is retured.
    virtual const Cookie &getCookie(const std::string &key) const = 0;

    /// Get all cookies.
    virtual const std::unordered_map<std::string, Cookie> &cookies() const = 0;

    /// Get all cookies.
    const std::unordered_map<std::string, Cookie> &getCookies() const
    {
        return cookies();
    }

    /// Remove the cookie identified by the key parameter.
    virtual void removeCookie(const std::string &key) = 0;

    /// Set the response body(content).
    /**
     * @note The body must match the content type
     */
    virtual void setBody(const std::string &body) = 0;

    /// Set the response body(content).
    virtual void setBody(std::string &&body) = 0;

    /// Set the response body(content).
    template <int N>
    void setBody(const char (&body)[N])
    {
        assert(strnlen(body, N) == N - 1);
        setBody(body, N - 1);
    }

    /// Get the response body.
    virtual const std::string &body() const = 0;

    /// Get the response body.
    const std::string &getBody() const
    {
        return body();
    }

    /// Get the response body.
    virtual std::string &body() = 0;

    /// Get the response body.
    std::string &getBody()
    {
        return body();
    }

    /// Return the enum type version of the response.
    /**
     * kHttp10 means Http version is 1.0
     * kHttp11 means Http verison is 1.1
     */
    virtual Version version() const = 0;

    /// Return the enum type version of the response.
    Version getVersion() const
    {
        return version();
    }

    /// Reset the reponse object to its initial state
    virtual void clear() = 0;

    /// Set the expiration time of the response cache in memory.
    /// in seconds, 0 means always cache, negative means not cache, default is
    /// -1.
    virtual void setExpiredTime(ssize_t expiredTime) = 0;

    /// Get the expiration time of the response.
    virtual ssize_t expiredTime() const = 0;
    ssize_t getExpiredTime() const
    {
        return expiredTime();
    }

    /// Get the json object from the server response.
    /// If the response is not in json format, then a empty shared_ptr is
    /// retured.
    virtual const std::shared_ptr<Json::Value> jsonObject() const = 0;
    const std::shared_ptr<Json::Value> getJsonObject() const
    {
        return jsonObject();
    }

    /**
     * @brief Set the reponse object to the pass-through mode or not. It's not
     * by default when a new response object is created.
     * In pass-through mode, no addtional headers (including server, date,
     * content-type and content-length, etc.) are added to the response. This
     * mode is useful for some applications such as a proxy.
     *
     * @param flag
     */
    virtual void setPassThrough(bool flag) = 0;

    /* The following methods are a series of factory methods that help users
     * create response objects. */

    /// Create a normal response with a status code of 200ok and a content type
    /// of text/html.
    static HttpResponsePtr newHttpResponse();
    /// Create a response which returns a 404 page.
    static HttpResponsePtr newNotFoundResponse();
    /// Create a response which returns a json object. Its content type is set
    /// to set/json.
    static HttpResponsePtr newHttpJsonResponse(const Json::Value &data);
    static HttpResponsePtr newHttpJsonResponse(Json::Value &&data);
    /// Create a response that returns a page rendered by a view named
    /// viewName.
    /**
     * @param viewName The name of the view
     * @param data is the data displayed on the page.
     * @note For more details, see the wiki pages, the "View" section.
     */
    static HttpResponsePtr newHttpViewResponse(
        const std::string &viewName,
        const HttpViewData &data = HttpViewData());

    /// Create a response that returns a redirection page, redirecting to
    /// another page located in the location parameter.
    /**
     * @param location The location to redirect
     * @param status The HTTP status code, k302Found by default. Users could set
     * it to one of the 301, 302, 303, 307, ...
     */
    static HttpResponsePtr newRedirectionResponse(
        const std::string &location,
        HttpStatusCode status = k302Found);

    /// Create a response that returns a file to the client.
    /**
     * @param fullPath is the full path to the file.
     * @param attachmentFileName if the parameter is not empty, the browser
     * does not open the file, but saves it as an attachment.
     * @param type if the parameter is CT_NONE, the content type is set by
     * drogon based on the file extension.
     */
    static HttpResponsePtr newFileResponse(
        const std::string &fullPath,
        const std::string &attachmentFileName = "",
        ContentType type = CT_NONE);

    /**
     * @brief Create a custom HTTP response object. For using this template,
     * users must specialize the toResponse template.
     */
    template <typename T>
    static HttpResponsePtr newCustomHttpResponse(T &&obj)
    {
        return toResponse(std::forward<T>(obj));
    }

    virtual ~HttpResponse()
    {
    }

  private:
    virtual void setBody(const char *body, size_t len) = 0;
    virtual void setContentTypeCodeAndCustomString(ContentType type,
                                                   const char *typeString,
                                                   size_t typeStringLength) = 0;
};





Add Discussion as Guest

Log in to DocsForge