NAWA 0.9
Web Application Framework for C++
nawa::Connection Class Reference

#include <nawa/connection/Connection.h>

Public Member Functions

virtual ~Connection ()
 
nawa::Request const & request () const noexcept
 
nawa::Sessionsession () noexcept
 
nawa::Session const & session () const noexcept
 
nawa::Configconfig () noexcept
 
nawa::Config const & config () const noexcept
 
std::ostream & responseStream () noexcept
 
 Connection (ConnectionInitContainer const &connectionInit)
 
void setResponseBody (std::string content)
 
void setStatus (unsigned int status)
 
void setHeader (std::string key, std::string value)
 
void addHeader (std::string key, std::string value)
 
void unsetHeader (std::string key)
 
void setCookie (std::string const &key, Cookie cookie)
 
void setCookie (std::string const &key, std::string cookieContent)
 
void unsetCookie (const std::string &key)
 
void setCookiePolicy (Cookie policy)
 
void sendFile (std::string const &path, std::string const &contentType="", bool forceDownload=false, std::string const &downloadFilename="", bool checkIfModifiedSince=false)
 
std::string getResponseBody ()
 
unsigned int getStatus () const
 
std::unordered_multimap< std::string, std::string > getHeaders (bool includeCookies=true) const
 
void flushResponse ()
 
bool applyFilters (AccessFilterList const &accessFilters)
 

Detailed Description

Response object to be passed back to NAWA and accessor to the request.

Definition at line 41 of file Connection.h.

Constructor & Destructor Documentation

◆ ~Connection()

virtual nawa::Connection::~Connection ( )
virtual

◆ Connection()

Connection::Connection ( ConnectionInitContainer const &  connectionInit)
explicit

Create a Connection object.

Parameters
connectionInitThe ConnectionInitContainer object containing the necessary parameters.

Definition at line 302 of file Connection.cpp.

Member Function Documentation

◆ request()

Request const & Connection::request ( ) const
noexcept

The Request object representing the current request.

Returns
Reference to the Request object.

Definition at line 357 of file Connection.cpp.

◆ session() [1/2]

Session & Connection::session ( )
noexcept

The Session object for accessing the current session.

Returns
Reference to the Session object.

Definition at line 361 of file Connection.cpp.

◆ session() [2/2]

Session const & Connection::session ( ) const
noexcept

The Session object for accessing the current session.

Returns
Reference to the Session object.

Definition at line 365 of file Connection.cpp.

◆ config() [1/2]

Config & Connection::config ( )
noexcept

Access the NAWA configuration. This is a copy of the Config object that contains the values of the config file which was read at the startup of NAWA. You can use the Config::set method to change values at runtime, however, these changes only affect the current connection.

Returns
Reference to the Config object.

Definition at line 369 of file Connection.cpp.

◆ config() [2/2]

Config const & Connection::config ( ) const
noexcept

Access the NAWA configuration. This is a copy of the Config object that contains the values of the config file which was read at the startup of NAWA.

Returns
Reference to the Config object.

Definition at line 373 of file Connection.cpp.

◆ responseStream()

ostream & Connection::responseStream ( )
noexcept

Stream which allows you to write stuff to the HTTP body comfortably.

Returns
Reference to the response ostream.

Definition at line 377 of file Connection.cpp.

◆ setResponseBody()

void Connection::setResponseBody ( std::string  content)

Set the HTTP response body (everything that comes after the headers). This will overwrite everything that was set previously. You can use responseStream() instead to get a stream to write to.

Parameters
contentComplete content of the HTTP body.

Definition at line 134 of file Connection.cpp.

◆ setStatus()

void Connection::setStatus ( unsigned int  status)

Set the HTTP status code. It will be passed to the web server without checking for validity. For known status codes, the textual description will be appended.

Parameters
statusThe HTTP status code to pass to the web server.

Definition at line 345 of file Connection.cpp.

◆ setHeader()

void Connection::setHeader ( std::string  key,
std::string  value 
)

Set an HTTP header or overwrite all existing ones with the same key (keys are case-insensitive and will be converted to lowercase). Please note that the content-type header will be automatically set to "text/html; charset=utf-8", but can be overwritten, of course. There will be no checking or validation, please make sure that the content of the header is valid (value valid for key, no newlines, ...)

Parameters
keyKey of the HTTP header (case-insensitive).
valueValue of the HTTP header.

Definition at line 218 of file Connection.cpp.

◆ addHeader()

void Connection::addHeader ( std::string  key,
std::string  value 
)

Add an HTTP header with the given key (case-insensitive, see notes for setHeader). If headers with the given key have already been set, they will not be overwritten.

Parameters
keyKey of the HTTP header (case-insensitive).
valueValue of the HTTP header.

Definition at line 224 of file Connection.cpp.

◆ unsetHeader()

void Connection::unsetHeader ( std::string  key)

Unset all HTTP headers with the specified key if it exists (otherwise do nothing). Please note that you can only unset headers that were previously set in NAWA (including content-type), not those that are set by, for example, the web server.

Parameters
keyKey of the HTTP header(s) (case-insensitive).

Definition at line 230 of file Connection.cpp.

◆ setCookie() [1/2]

void Connection::setCookie ( std::string const &  key,
Cookie  cookie 
)

Set a new HTTP cookie or overwrite the cookie with the given key. Create a Cookie object first, setting at least the content of the cookie. This function may throw a nawa::Exception with error code 1 if the key or cookie content contain illicit characters. Works only before the response has been flushed. For more information about cookie setting and handling, see Environment

Parameters
keyKey of the cookie. Valid characters in the key (as regex): [A-Za-z0-9!#$%&'+-.^_`|~]
cookieCookie object containing the value and options of the cookie. Valid characters in the cookie content (as regex): [A-Za-z0-9!#$%&'()+-.\/:<=>?@[]^_`{|}~]

Definition at line 314 of file Connection.cpp.

◆ setCookie() [2/2]

void Connection::setCookie ( std::string const &  key,
std::string  cookieContent 
)

Set a new HTTP cookie or overwrite the cookie with the given key. This function will create a Cookie object with default attributes and the given content. It may throw a nawa::Exception with error code 1 if the key or cookie content contain illicit characters. Works only before the response has been flushed. For more information about cookie setting and handling, see Environment

Parameters
keyKey of the cookie. Valid characters in the key (as regex): [A-Za-z0-9!#$%&'*+-.^_`|~]
cookieContentCookie object containing the value and options of the cookie.

Definition at line 324 of file Connection.cpp.

◆ unsetCookie()

void Connection::unsetCookie ( const std::string &  key)

Unset an HTTP cookie that was previously set using setCookie(). Will just do nothing if no cookie with the given key exists. Won't remove a cookie from the user's browser, just undoes the setCookie operation.

Parameters
keyKey of the cookie.

Definition at line 328 of file Connection.cpp.

◆ setCookiePolicy()

void Connection::setCookiePolicy ( Cookie  policy)

This method can be used to set default attributes for cookies. All attributes which are not set or enabled in a cookie will be taken from the policy cookie, if set there. The content attribute will be ignored.

Parameters
policyCookie object containing default attributes for cookies.

Definition at line 349 of file Connection.cpp.

◆ sendFile()

void Connection::sendFile ( std::string const &  path,
std::string const &  contentType = "",
bool  forceDownload = false,
std::string const &  downloadFilename = "",
bool  checkIfModifiedSince = false 
)

Send a file from disk to the client. This will automatically set the content-type, content-length, and last-modified headers and replace the existing HTTP response body (if any) with the contents of the file. You are responsible to check request headers (such as accepts and if-modified-since). If the file cannot be read, a nawa::Exception with error code 1 will be thrown.

Parameters
pathPath to the file, including the file name of course (better use absolute paths).
contentTypeThe content-type string (such as image/png). If left empty, NAWA will try to guess the content type itself (this will only work for a few common file types), and use "application/octet-stream" if that fails. Content type guessing is done solely based on the file extension.
forceDownloadAsk the browser to download the file by sending "content-disposition: attachment".
downloadFilenamePreferred filename for saving the file on the disk of the client. If this parameter is set, and forceDownload is set to false, a "content-disposition: inline" header will be sent.
checkIfModifiedSinceCheck the if-modified-since header, if sent by the client, and compare it to the modification date of the file. Prepare a not-modified response and clear the body if the file has not been modified. Using this parameter only makes sense if the client requested exactly this file, of course.

Definition at line 139 of file Connection.cpp.

◆ getResponseBody()

string Connection::getResponseBody ( )

Get the response body.

Returns
The response body.

Definition at line 297 of file Connection.cpp.

◆ getStatus()

unsigned int Connection::getStatus ( ) const

Get the HTTP response status.

Returns
The HTTP response status.

Definition at line 353 of file Connection.cpp.

◆ getHeaders()

std::unordered_multimap< std::string, std::string > Connection::getHeaders ( bool  includeCookies = true) const

Assemble a map of all response headers.

Parameters
includeCookiesAlso include set-cookie headers for the cookies.
Returns
A map of all headers.

Definition at line 236 of file Connection.cpp.

◆ flushResponse()

void Connection::flushResponse ( )

Flush the Response object, i.e., send headers and body to the client and reset it. Please note that you cannot set cookies and headers anymore after flushing. Attempts to do so will be silently ignored.
NOTE: Flushing might not work as expected with Apache 2.4 and mod_proxy_fcgi

Definition at line 332 of file Connection.cpp.

◆ applyFilters()

bool Connection::applyFilters ( AccessFilterList const &  accessFilters)

Apply access filters (if enabled in the AccessFilterList object) and set the response accordingly, if they match. You shouldn't need to call this function manually, just add them to AppInit or (when using NAWA as a library) to the RequestHandler. Then, NAWA will filter your requests before they even reach your app or request handling function.

Parameters
accessFiltersThe filters to apply.
Returns
True if the request has been filtered and a response has already been set by this function (and the app should not be invoked on this request). False if the app should handle this request.

Definition at line 381 of file Connection.cpp.


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