17#include <drogon/exports.h>
22#include <drogon/Attribute.h>
23#include <drogon/UploadFile.h>
25#include <trantor/net/InetAddress.h>
26#include <trantor/net/Certificate.h>
27#include <trantor/utils/Date.h>
30#include <unordered_map>
33#include <trantor/net/TcpConnection.h>
38using HttpRequestPtr = std::shared_ptr<HttpRequest>;
47 LOG_ERROR <<
"You must specialize the fromRequest template for the type of "
60 LOG_ERROR <<
"You must specialize the toRequest template for the type of "
66HttpRequestPtr toRequest<const Json::Value &>(
const Json::Value &pJson);
68HttpRequestPtr
toRequest(Json::Value &&pJson);
71inline HttpRequestPtr toRequest<Json::Value &>(Json::Value &pJson)
73 return toRequest((
const Json::Value &)pJson);
105 template <
typename T>
114 const char *getMethodString()
const
122 HttpMethod getMethod()
const
143 virtual const std::string &
getHeader(std::string key)
const = 0;
152 virtual void addHeader(std::string field,
const std::string &value) = 0;
153 virtual void addHeader(std::string field, std::string &&value) = 0;
163 virtual void clearHeaders() = 0;
166 virtual const std::string &
getCookie(
const std::string &field)
const = 0;
169 virtual const SafeStringMap<std::string> &
headers()
const = 0;
178 virtual const SafeStringMap<std::string> &
cookies()
const = 0;
192 size_t getRealContentLength()
const
201 virtual const std::string &
query()
const = 0;
213 return std::string_view(bodyData(), bodyLength());
223 virtual const char *bodyData()
const = 0;
224 virtual size_t bodyLength()
const = 0;
233 virtual const std::string &
path()
const = 0;
253 return std::string_view(matchedPathPatternData(),
254 matchedPathPatternLength());
264 virtual const char *matchedPathPatternData()
const = 0;
265 virtual size_t matchedPathPatternLength()
const = 0;
271 const char *getVersionString()
const
290 virtual const SessionPtr &
session()
const = 0;
310 virtual const SafeStringMap<std::string> &
parameters()
const = 0;
319 virtual const std::string &
getParameter(
const std::string &key)
const = 0;
330 template <
typename T>
334 auto it = params.find(key);
335 if (it != params.end())
339 return std::optional<T>(
340 drogon::utils::fromString<T>(it->second));
342 catch (
const std::exception &e)
344 LOG_ERROR << e.what();
345 return std::optional<T>{};
350 return std::optional<T>{};
355 virtual const trantor::InetAddress &
peerAddr()
const = 0;
357 const trantor::InetAddress &getPeerAddr()
const
363 virtual const trantor::InetAddress &
localAddr()
const = 0;
365 const trantor::InetAddress &getLocalAddr()
const
373 const trantor::Date &getCreationDate()
const
379 virtual const trantor::CertificatePtr &peerCertificate()
const = 0;
381 const trantor::CertificatePtr &getPeerCertificate()
const
383 return peerCertificate();
391 virtual const std::shared_ptr<Json::Value> &
jsonObject()
const = 0;
412 ContentType getContentType()
const
438 const std::string &value) = 0;
445 const std::string &value) = 0;
452 const std::string &value) = 0;
472 virtual void addCookie(std::string key, std::string value) = 0;
511 const std::vector<UploadFile> &files);
517 template <
typename T>
546 if (
method() != HttpMethod::Options)
550 headers().find(
"access-control-request-method") !=
554 virtual bool isOnSecureConnection() const noexcept = 0;
555 virtual
void setContentTypeString(const
char *typeString,
556 size_t typeStringLength) = 0;
558 virtual
bool connected() const noexcept = 0;
560 virtual const
std::weak_ptr<trantor::TcpConnection> &getConnectionPtr()
575inline HttpRequestPtr
toRequest(Json::Value &&pJson)
583 return req.getJsonObject();
static std::string demangle(const char *mangled_name)
demangle the type name which is returned by typeid(T).name().
Definition DrClassMap.h:116
Abstract class for webapp developer to get or set the Http request;.
Definition HttpRequest.h:81
static HttpRequestPtr newCustomHttpRequest(T &&obj)
Create a custom HTTP request object. For using this template, users must specialize the toRequest tem...
Definition HttpRequest.h:518
virtual const trantor::Date & creationDate() const =0
Return the creation timestamp set by the framework.
virtual HttpMethod method() const =0
Return the enum type method of the request.
virtual const std::vector< std::string > & getRoutingParameters() const =0
virtual const std::string & getHeader(std::string key) const =0
Get the header string identified by the key parameter.
virtual Version version() const =0
Return the enum type version of the request.
const SafeStringMap< std::string > & getCookies() const
Get all cookies of the request.
Definition HttpRequest.h:181
virtual const char * methodString() const =0
Return the method string of the request, such as GET, POST, etc.
virtual const std::string & getParameter(const std::string &key) const =0
Get a parameter identified by the.
const std::string & getPath() const
Get the path of the request.
Definition HttpRequest.h:239
virtual void addHeader(std::string field, const std::string &value)=0
Set the header string identified by the field parameter.
virtual void setQueryParameter(const std::string &key, const std::string &value)=0
void setContentTypeString(const std::string_view &typeString)
For example, "content-type: text/plain\r\n" or "text/plain".
Definition HttpRequest.h:461
virtual ContentType contentType() const =0
Get the content type.
virtual void setBodyParameter(const std::string &key, const std::string &value)=0
static HttpRequestPtr newHttpFormPostRequest()
std::string_view matchedPathPattern() const
Get the matched path pattern after routing.
Definition HttpRequest.h:251
virtual const SessionPtr & session() const =0
Get the session to which the request belongs.
static HttpRequestPtr newHttpRequest()
Create a normal request with http method Get and version Http1.1.
virtual void setPath(const std::string &path)=0
virtual const SafeStringMap< std::string > & parameters() const =0
Get parameters of the request.
virtual void setCustomContentTypeString(const std::string &type)=0
std::optional< T > getOptionalParameter(const std::string &key)
Get the optional parameter identified by the key. if the parameter doesn't exist, or the original par...
Definition HttpRequest.h:331
Version getVersion() const
Return the enum type version of the request.
Definition HttpRequest.h:284
const std::shared_ptr< Json::Value > & getJsonObject() const
Get the Json object of the request.
Definition HttpRequest.h:394
virtual void setParameter(const std::string &key, const std::string &value)=0
Set the parameter of the request.
const AttributesPtr & getAttributes() const
Definition HttpRequest.h:304
virtual const std::string & path() const =0
Get the path of the request.
virtual void setRoutingParameters(std::vector< std::string > &¶ms)=0
This method usually is called by the framework.
virtual const trantor::InetAddress & peerAddr() const =0
Return the remote IP address and port.
virtual void setPathEncode(bool)=0
The default behavior is to encode the value of setPath using urlEncode. Setting the path encode to fa...
virtual void setBody(std::string &&body)=0
Set the content string of the request.
virtual void setBody(const std::string &body)=0
Set the content string of the request.
virtual bool isHead() const =0
Check if the method is or was HttpMethod::Head.
virtual size_t realContentLength() const =0
Return content length parsed from the Content-Length header If no Content-Length header,...
virtual void removeHeader(std::string key)=0
Remove the header identified by the key parameter.
const SafeStringMap< std::string > & getParameters() const
Get parameters of the request.
Definition HttpRequest.h:313
virtual const std::string & query() const =0
Get the query string of the request.
virtual void addCookie(std::string key, std::string value)=0
Add a cookie.
virtual const std::string & getOriginalPath() const =0
Get the original path of the request.(before url-decoding).
virtual const trantor::InetAddress & localAddr() const =0
Return the local IP address and port.
virtual const SafeStringMap< std::string > & cookies() const =0
Get all cookies of the request.
virtual const std::string & getCookie(const std::string &field) const =0
Get the cookie string identified by the field parameter.
const SessionPtr & getSession() const
Get the session to which the request belongs.
Definition HttpRequest.h:293
std::string_view getMatchedPathPattern() const
Get the matched path pattern after routing.
Definition HttpRequest.h:245
std::string_view getBody() const
Definition HttpRequest.h:218
virtual void setContentTypeCode(const ContentType type)=0
Set or get the content type.
virtual const std::shared_ptr< Json::Value > & jsonObject() const =0
Get the Json object of the request.
std::string_view body() const
Definition HttpRequest.h:211
bool isCorsRequest() const
Check if the request is a CORS request.
Definition HttpRequest.h:528
virtual const SafeStringMap< std::string > & headers() const =0
Get all headers of the request.
bool isCorsPreflightRequest() const
Check if the request is a CORS pre-flight request.
Definition HttpRequest.h:544
static HttpRequestPtr newHttpJsonRequest(const Json::Value &data)
virtual void setPassThrough(bool flag)=0
Set the request object to the pass-through mode or not. It's not by default when a new request object...
virtual const std::string & getJsonError() const =0
Get the error message of parsing the JSON body received from peer. This method usually is called afte...
virtual const AttributesPtr & attributes() const =0
const std::string & getQuery() const
Get the query string of the request.
Definition HttpRequest.h:204
virtual void setMethod(const HttpMethod method)=0
Set the Http method.
T as() const
This template enables explicit type conversion, see the above template.
Definition HttpRequest.h:106
const SafeStringMap< std::string > & getHeaders() const
Get all headers of the request.
Definition HttpRequest.h:172
virtual const char * versionString() const =0
static HttpRequestPtr newFileUploadRequest(const std::vector< UploadFile > &files)
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
T fromRequest(const HttpRequest &)
This template is used to convert a request object to a custom type object. Users must specialize the ...
Definition HttpRequest.h:45
HttpRequestPtr toRequest(T &&)
This template is used to create a request object from a custom type object by calling the newCustomHt...
Definition HttpRequest.h:58