libcex  1.0.0
Modern C++11 embedded webserver library
Public Types | Public Member Functions | List of all members
cex::Response Class Reference

Serves as interface to the response which will be sent to the client. Contains functions for modifying HTTP headers and specifying response contents. More...

#include <core.hpp>

Public Types

enum  State { stInit, stDone }
 State of the response. More...
 
enum  Flags { fNoFlags = 0x0000, fCompression = 0x000F, fCompressGZip = 0x0001, fCompressDeflate = 0x0002 }
 Flags describing features of the response. Currently this affects only compression. More...
 

Public Member Functions

 Response (evhtp_request *req)
 Constructs a new Response object. More...
 
void set (const char *name, const char *value)
 Sets a HTTP header to a given value. More...
 
void set (const char *name, int value)
 Sets a HTTP header to a given value. More...
 
int end (const char *string, int status)
 Sends a response to the client with the supplied HTTP code and payload text. More...
 
int end (const char *buffer, size_t bufLen, int status)
 Sends a response to the client with the supplied HTTP code and payload buffer. More...
 
int end (int status)
 Sends a response to the client with the supplied HTTP code and no body/payload. More...
 
int stream (int status, std::istream *stream)
 Streams a response to the client with the supplied HTTP code. More...
 
bool isState (State aState)
 Queries the state of the response. More...
 
bool isDone ()
 Checks if state of the response is stDone. More...
 
bool isPending ()
 Checks if state of the response is not stDone. More...
 
void setFlags (int newFlags)
 Sets the response object flags. More...
 
int getFlags ()
 Returns the currently set flags of the response object.
 

Detailed Description

Serves as interface to the response which will be sent to the client. Contains functions for modifying HTTP headers and specifying response contents.

Example:

app.use([](cex::Request* req, cex::Response* res, std::function<void()> next)
{
res->set("Content-Type", "application/json");
res->end("{\"id\": 10}", 200);
});

Member Enumeration Documentation

◆ Flags

Flags describing features of the response. Currently this affects only compression.

For compression to work, the library must be compiled with zlib support.

Enumerator
fCompressGZip 

Enable GZip compression of the response contents

fCompressDeflate 

Enable deflate compression of the response contents

◆ State

State of the response.

Enumerator
stInit 

Response is in pending state, and not yet completed.

stDone 

Response was completed, that means a response was sent to the client.

Constructor & Destructor Documentation

◆ Response()

cex::Response::Response ( evhtp_request *  req)

Constructs a new Response object.

Parameters
reqThe underlying libevhtp request object

Member Function Documentation

◆ end() [1/3]

int cex::Response::end ( const char *  string,
int  status 
)

Sends a response to the client with the supplied HTTP code and payload text.

Parameters
stringThe text which shall be sent to the client in the response body.
statusThe HTTP code which shall be sent to the client.

◆ end() [2/3]

int cex::Response::end ( const char *  buffer,
size_t  bufLen,
int  status 
)

Sends a response to the client with the supplied HTTP code and payload buffer.

Parameters
bufferThe data buffer which holds the data which shall be sent to the client in the response body.
bufLenThe number of bytes which of the buffer which shall be sent.
statusThe HTTP code which shall be sent to the client.

◆ end() [3/3]

int cex::Response::end ( int  status)

Sends a response to the client with the supplied HTTP code and no body/payload.

Parameters
statusThe HTTP code which shall be sent to the client.

◆ isDone()

bool cex::Response::isDone ( )
inline

Checks if state of the response is stDone.

Returns
true if the state of the object is stDone, otherwise false.

◆ isPending()

bool cex::Response::isPending ( )
inline

Checks if state of the response is not stDone.

Returns
true if the state of the object is not stDone, otherwise false.

◆ isState()

bool cex::Response::isState ( State  aState)
inline

Queries the state of the response.

Parameters
aStateThe state which shall be compared to the response object state
Returns
true if the state of the object matches the supplied state, otherwise false.

◆ set() [1/2]

void cex::Response::set ( const char *  name,
const char *  value 
)

Sets a HTTP header to a given value.

Parameters
nameName of the HTTP header
ValueThe value which shall be set

◆ set() [2/2]

void cex::Response::set ( const char *  name,
int  value 
)

Sets a HTTP header to a given value.

Parameters
nameName of the HTTP header
ValueThe value which shall be set

◆ setFlags()

void cex::Response::setFlags ( int  newFlags)
inline

Sets the response object flags.

Parameters
newFlagsThe flags which shall replace the currently set flags

◆ stream()

int cex::Response::stream ( int  status,
std::istream *  stream 
)

Streams a response to the client with the supplied HTTP code.

Parameters
statusThe HTTP code which shall be sent to the client.
streamA pointer to a std::istream instance which is used to read the response contents from.
Returns
cex::success (0) if the whole contents were successfully transferred or cex::fail (-1) if the stream could not be read.

This function is useful for transferring larger payloads (e.g. files) which shall not be fully loaded into memory.


The documentation for this class was generated from the following files: