#include <nawa/connection/Connection.h>
Public Member Functions | |
virtual | ~Connection () |
nawa::Request const & | request () const noexcept |
nawa::Session & | session () noexcept |
nawa::Session const & | session () const noexcept |
nawa::Config & | config () 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) |
Response object to be passed back to NAWA and accessor to the request.
Definition at line 41 of file Connection.h.
|
virtual |
|
explicit |
Create a Connection object.
connectionInit | The ConnectionInitContainer object containing the necessary parameters. |
Definition at line 302 of file Connection.cpp.
|
noexcept |
The Request object representing the current request.
Definition at line 357 of file Connection.cpp.
|
noexcept |
The Session object for accessing the current session.
Definition at line 361 of file Connection.cpp.
|
noexcept |
The Session object for accessing the current session.
Definition at line 365 of file Connection.cpp.
|
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.
Definition at line 369 of file Connection.cpp.
|
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.
Definition at line 373 of file Connection.cpp.
|
noexcept |
Stream which allows you to write stuff to the HTTP body comfortably.
Definition at line 377 of file Connection.cpp.
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.
content | Complete content of the HTTP body. |
Definition at line 134 of file Connection.cpp.
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.
status | The HTTP status code to pass to the web server. |
Definition at line 345 of file Connection.cpp.
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, ...)
key | Key of the HTTP header (case-insensitive). |
value | Value of the HTTP header. |
Definition at line 218 of file Connection.cpp.
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.
key | Key of the HTTP header (case-insensitive). |
value | Value of the HTTP header. |
Definition at line 224 of file Connection.cpp.
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.
key | Key of the HTTP header(s) (case-insensitive). |
Definition at line 230 of file Connection.cpp.
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
key | Key of the cookie. Valid characters in the key (as regex): [A-Za-z0-9!#$%&'+-.^_`|~] |
cookie | Cookie 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.
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
key | Key of the cookie. Valid characters in the key (as regex): [A-Za-z0-9!#$%&'*+-.^_`|~] |
cookieContent | Cookie object containing the value and options of the cookie. |
Definition at line 324 of file Connection.cpp.
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.
key | Key of the cookie. |
Definition at line 328 of file Connection.cpp.
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.
policy | Cookie object containing default attributes for cookies. |
Definition at line 349 of file Connection.cpp.
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.
path | Path to the file, including the file name of course (better use absolute paths). |
contentType | The 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. |
forceDownload | Ask the browser to download the file by sending "content-disposition: attachment". |
downloadFilename | Preferred 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. |
checkIfModifiedSince | Check 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.
string Connection::getResponseBody | ( | ) |
unsigned int Connection::getStatus | ( | ) | const |
Get the HTTP response status.
Definition at line 353 of file Connection.cpp.
std::unordered_multimap< std::string, std::string > Connection::getHeaders | ( | bool | includeCookies = true | ) | const |
Assemble a map of all response headers.
includeCookies | Also include set-cookie headers for the cookies. |
Definition at line 236 of file Connection.cpp.
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.
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.
accessFilters | The filters to apply. |
Definition at line 381 of file Connection.cpp.