class EE::Network::Http::Request

Overview

Define a HTTP request. More…

#include <http.hpp>

class Request {
public:
    // typedefs

    typedef std::function<bool(const Http&http, const Http::Request&request, const Http::Response&response, const Status&status, std::size_t totalBytes, std::size_t currentBytes)> ProgressCallback;
    typedef std::map<std::string, std::string> FieldTable;

    // enums

    enum Method;
    enum Status;

    // construction

    Request(
        const std::string& uri = "/",
        Method method = Get,
        const std::string& body = "",
        bool validateCertificate = true,
        bool validateHostname = true,
        bool followRedirect = true,
        bool compressedResponse = false
    );

    // methods

    static Method methodFromString(std::string methodString);
    static std::string methodToString(const Method& method);
    void setField(const std::string& field, const std::string& value);
    void setHeader(const std::string& field, const std::string& value);
    bool hasField(const std::string& field) const;
    const std::string& getField(const std::string& field) const;
    void setMethod(Method method);
    void setUri(const std::string& uri);
    void setHttpVersion(unsigned int major, unsigned int minor);
    void setBody(const std::string& body);
    const std::string& getUri() const;
    const Method& getMethod() const;
    const bool& getValidateCertificate() const;
    void setValidateCertificate(bool enable);
    const bool& getValidateHostname() const;
    void setValidateHostname(bool enable);
    const bool& getFollowRedirect() const;
    void setFollowRedirect(bool follow);
    const unsigned int& getMaxRedirects() const;
    void setMaxRedirects(unsigned int maxRedirects);
    void setProgressCallback(const ProgressCallback& progressCallback);
    const ProgressCallback& getProgressCallback() const;
    void cancel();
    const bool& isCancelled() const;
    const bool& isCompressedResponse() const;
    void setCompressedResponse(const bool& compressedResponse);
    void setContinue(const bool& resume);
    const bool& isContinue() const;
    bool isVerbose() const;
    void setVerbose(bool verbose);
};

Detailed Documentation

Define a HTTP request.

Typedefs

typedef std::function<bool(const Http&http, const Http::Request&request, const Http::Response&response, const Status&status, std::size_t totalBytes, std::size_t currentBytes)> ProgressCallback

Definition of the current progress callback

Parameters:

http

The http client

request

The http request

status

The status of the progress event

totalBytes

The total bytes of the document / files ( only available if Content-Length is returned, otherwise is 0 )

currentBytes

Current received total bytes

Returns:

True if continue the request, false will cancel the current request.

Construction

Request(
    const std::string& uri = "/",
    Method method = Get,
    const std::string& body = "",
    bool validateCertificate = true,
    bool validateHostname = true,
    bool followRedirect = true,
    bool compressedResponse = false
)

Default constructor This constructor creates a GET request, with the root URI (“/”) and an empty body.

Parameters:

uri

Target URI

method

Method to use for the request

body

Content of the request’s body

validateCertificate

Enables certificate validation for https request

validateHostname

Enables hostname validation for https request

followRedirect

Allow follor redirects to the request.

compressedResponse

Set if the requested response should be compressed ( if available )

Methods

static Method methodFromString(std::string methodString)

Returns:

Method from a method name string.

static std::string methodToString(const Method& method)

Returns:

The method string from a method

void setField(const std::string& field, const std::string& value)

Set the value of a field The field is created if it doesn’t exist. The name of the field is case insensitive. By default, a request doesn’t contain any field (but the mandatory fields are added later by the HTTP client when sending the request).

Parameters:

field

Name of the field to set

value

Value of the field

void setHeader(const std::string& field, const std::string& value)

See also:

setField

bool hasField(const std::string& field) const

Check if the request defines a field This function uses case-insensitive comparisons.

Parameters:

field

Name of the field to test

Returns:

True if the field exists, false otherwise

const std::string& getField(const std::string& field) const

Get the value of a field If the field field is not found in the response header, the empty string is returned. This function uses case-insensitive comparisons.

Parameters:

field

Name of the field to get

Returns:

Value of the field, or empty string if not found

void setMethod(Method method)

Set the request method See the Method enumeration for a complete list of all the availale methods. The method is Http::Request::Get by default.

Parameters:

method

Method to use for the request

void setUri(const std::string& uri)

Set the requested URI The URI is the resource (usually a web page or a file) that you want to get or post. The URI is “/” (the root page) by default.

Parameters:

uri

URI to request, relative to the host

void setHttpVersion(unsigned int major, unsigned int minor)

Set the HTTP version for the request The HTTP version is 1.0 by default.

Parameters:

major

Major HTTP version number

minor

Minor HTTP version number

void setBody(const std::string& body)

Set the body of the request The body of a request is optional and only makes sense for POST requests. It is ignored for all other methods. The body is empty by default.

Parameters:

body

Content of the body

const std::string& getUri() const

Returns:

The request Uri

const Method& getMethod() const

Returns:

The request Method

const bool& getValidateCertificate() const

Returns:

If SSL certificate validation is enabled

void setValidateCertificate(bool enable)

Enable/disable SSL certificate validation

const bool& getValidateHostname() const

Returns:

If SSL hostname validation is enabled

void setValidateHostname(bool enable)

Enable/disable SSL hostname validation

const bool& getFollowRedirect() const

Returns:

If requests follow redirects

void setFollowRedirect(bool follow)

Enables/Disables follow redirects

const unsigned int& getMaxRedirects() const

Returns:

The maximun number of redirects allowd if follow redirect is enabled.

void setMaxRedirects(unsigned int maxRedirects)

Set the maximun number of redirects allowed if follow redirect is enabled.

void setProgressCallback(const ProgressCallback& progressCallback)

Sets a progress callback

const ProgressCallback& getProgressCallback() const

Get the progress callback

void cancel()

Cancels the current request if being processed

const bool& isCancelled() const

Returns:

True if the current request was cancelled

const bool& isCompressedResponse() const

Returns:

If requests a compressed response

void setCompressedResponse(const bool& compressedResponse)

Set to request a compressed response from the server The returned response will be automatically decompressed by the client.

void setContinue(const bool& resume)

Resumes download if a file is already present

const bool& isContinue() const

Returns:

If must continue a download previously started.

bool isVerbose() const

Returns:

True if request is verbose logging

void setVerbose(bool verbose)

Set verbose logging