Add some comments
This commit is contained in:
parent
e7dbb3bbc5
commit
a44a0fc3e8
|
@ -45,6 +45,7 @@ class Cookie
|
|||
const std::string &path() const { return _path; }
|
||||
const std::string &key() const { return _key; }
|
||||
const std::string &value() const { return _value; }
|
||||
operator bool() const { return (!_key.empty()) && (!_value.empty()); }
|
||||
|
||||
private:
|
||||
trantor::Date _expiresDate;
|
||||
|
|
|
@ -28,70 +28,105 @@ typedef std::shared_ptr<HttpResponse> HttpResponsePtr;
|
|||
class HttpResponse
|
||||
{
|
||||
public:
|
||||
explicit HttpResponse()
|
||||
HttpResponse()
|
||||
{
|
||||
}
|
||||
|
||||
/// Get the status code such as 200, 404
|
||||
virtual HttpStatusCode statusCode() = 0;
|
||||
|
||||
virtual const trantor::Date &creationDate() const = 0;
|
||||
|
||||
/// Set the status code of the response.
|
||||
virtual void setStatusCode(HttpStatusCode code) = 0;
|
||||
|
||||
/// Set the status code and the status message of the response. Usually not used.
|
||||
virtual void setStatusCode(HttpStatusCode code, const std::string &status_message) = 0;
|
||||
|
||||
/// Get the creation timestamp of the response.
|
||||
virtual const trantor::Date &creationDate() const = 0;
|
||||
|
||||
/// Set the http version, http1.0 or http1.1
|
||||
virtual void setVersion(const Version v) = 0;
|
||||
|
||||
/// If @param on is false, the connection will keep alive on the condition that the client request has a
|
||||
// 'keep-alive' head, otherwise it will be closed. It's false by default when the reponse is created.
|
||||
virtual void setCloseConnection(bool on) = 0;
|
||||
|
||||
/// Get the status set by the setCloseConnetion() method.
|
||||
virtual bool closeConnection() const = 0;
|
||||
|
||||
/// Set the reponse content type, such as text/html, text/plaint, image/png and so on. If the content type
|
||||
/// is a text type, the character set is utf8.
|
||||
virtual void setContentTypeCode(ContentType type) = 0;
|
||||
|
||||
/// Set the reponse content type and the character set.
|
||||
virtual void setContentTypeCodeAndCharacterSet(ContentType type, const std::string &charSet = "utf-8") = 0;
|
||||
|
||||
/// Get the response content type.
|
||||
virtual ContentType getContentTypeCode() = 0;
|
||||
|
||||
/// Get the header string identified by the @param key. If there is no the header, the @param defaultVal will be retured.
|
||||
/// The @param key is case insensitive
|
||||
virtual const std::string &getHeader(const std::string &key, const std::string &defaultVal = std::string()) const = 0;
|
||||
|
||||
virtual const std::string &getHeader(std::string &&key, const std::string &defaultVal = std::string()) const = 0;
|
||||
|
||||
/// Add a header.
|
||||
virtual void addHeader(const std::string &key, const std::string &value) = 0;
|
||||
|
||||
virtual void addHeader(const std::string &key, std::string &&value) = 0;
|
||||
|
||||
virtual void addHeader(const char *start, const char *colon, const char *end) = 0;
|
||||
|
||||
/// Add a cookie
|
||||
virtual void addCookie(const std::string &key, const std::string &value) = 0;
|
||||
|
||||
virtual void addCookie(const Cookie &cookie) = 0;
|
||||
|
||||
/// Get the cookie identified by the @param key. If there is no the cookie, the @param defaultCookie will be retured.
|
||||
virtual const Cookie &getCookie(const std::string &key, const Cookie &defaultCookie = Cookie()) const = 0;
|
||||
|
||||
/// Get all cookies.
|
||||
virtual const std::unordered_map<std::string, Cookie> &cookies() const = 0;
|
||||
|
||||
/// Remove the cookie identified by the @param key.
|
||||
virtual void removeCookie(const std::string &key) = 0;
|
||||
|
||||
/// Set the response body(content). The @param body must match the content type
|
||||
virtual void setBody(const std::string &body) = 0;
|
||||
|
||||
virtual void setBody(std::string &&body) = 0;
|
||||
|
||||
virtual void redirect(const std::string &url) = 0;
|
||||
|
||||
virtual void clear() = 0;
|
||||
|
||||
virtual void setExpiredTime(ssize_t expiredTime) = 0;
|
||||
virtual ssize_t expiredTime() const = 0;
|
||||
|
||||
/// Get the response body.
|
||||
virtual const std::string &getBody() const = 0;
|
||||
virtual std::string &getBody() = 0;
|
||||
|
||||
/// Reset the reponse object to its initial state
|
||||
virtual void clear() = 0;
|
||||
|
||||
/// Set the expiration time of the response cache in memory.
|
||||
/// in seconds, 0 means always cache, negative means not cache, default is -1.
|
||||
virtual void setExpiredTime(ssize_t expiredTime) = 0;
|
||||
|
||||
/// Get the expiration time of the response.
|
||||
virtual ssize_t expiredTime() const = 0;
|
||||
|
||||
/// Get the json object from the server response. If the response is not in json format,
|
||||
/// then a empty shared_ptr will be retured.
|
||||
virtual const std::shared_ptr<Json::Value> getJsonObject() const = 0;
|
||||
|
||||
/// The following methods are a series of factory methods that help users create response objects.
|
||||
|
||||
/// Create a normal response with a status code of 200ok and a content type of text/html.
|
||||
static HttpResponsePtr newHttpResponse();
|
||||
/// Create a response which returns a 404 page.
|
||||
static HttpResponsePtr newNotFoundResponse();
|
||||
/// Create a response which returns a json object. Its content type is set to set/json.
|
||||
static HttpResponsePtr newHttpJsonResponse(const Json::Value &data);
|
||||
/// Create a response that returns a page rendered by a view named @param viewName.
|
||||
/// @param data is the data displayed on the page.
|
||||
/// For more details, see the wiki pages, the "View" section.
|
||||
static HttpResponsePtr newHttpViewResponse(const std::string &viewName, const HttpViewData &data = HttpViewData());
|
||||
/// Create a response that returns a 302 Found page, redirecting to another page located in the @param path.
|
||||
static HttpResponsePtr newLocationRedirectResponse(const std::string &path);
|
||||
/// Create a response that returns a file to the client.
|
||||
/**
|
||||
* @param fullPath is the full path to the file.
|
||||
* If @param attachmentFileName is not empty, the browser does not open the file, but saves it as an attachment.
|
||||
* If the @param type is CT_NONE, the content type will be set by drogon based on the file extension.
|
||||
*/
|
||||
static HttpResponsePtr newFileResponse(const std::string &fullPath, const std::string &attachmentFileName = "", ContentType type = CT_NONE);
|
||||
|
||||
virtual ~HttpResponse() {}
|
||||
|
|
|
@ -71,6 +71,7 @@ class HttpFile
|
|||
std::string _fileContent;
|
||||
};
|
||||
|
||||
/// A parser class which help the user to get the files and the parameters in the multipart format request.
|
||||
class MultiPartParser
|
||||
{
|
||||
public:
|
||||
|
@ -85,8 +86,8 @@ class MultiPartParser
|
|||
/// Parse the http request stream to get files and parameters.
|
||||
int parse(const HttpRequestPtr &req);
|
||||
|
||||
/// Parse the http response stream to get files and parameters.
|
||||
int parse(const HttpResponsePtr &req);
|
||||
/// Parse the http response stream to get files and parameters.
|
||||
/// int parse(const HttpResponsePtr &req);
|
||||
|
||||
protected:
|
||||
std::vector<HttpFile> _files;
|
||||
|
|
|
@ -25,7 +25,7 @@ class HttpFileUploadRequest : public HttpRequestImpl
|
|||
public:
|
||||
const std::string &boundary() const { return _boundary; }
|
||||
const std::vector<UploadFile> &files() const { return _files; }
|
||||
HttpFileUploadRequest(const std::vector<UploadFile> &files);
|
||||
explicit HttpFileUploadRequest(const std::vector<UploadFile> &files);
|
||||
|
||||
private:
|
||||
std::string _boundary;
|
||||
|
|
|
@ -42,7 +42,7 @@ class HttpRequestImpl : public HttpRequest
|
|||
public:
|
||||
friend class HttpRequestParser;
|
||||
|
||||
HttpRequestImpl(trantor::EventLoop *loop)
|
||||
explicit HttpRequestImpl(trantor::EventLoop *loop)
|
||||
: _method(Invalid),
|
||||
_version(kUnknown),
|
||||
_date(trantor::Date::now()),
|
||||
|
|
|
@ -36,7 +36,7 @@ class HttpRequestParser
|
|||
kGotAll,
|
||||
};
|
||||
|
||||
HttpRequestParser(const trantor::TcpConnectionPtr &connPtr);
|
||||
explicit HttpRequestParser(const trantor::TcpConnectionPtr &connPtr);
|
||||
|
||||
// return false if any error
|
||||
bool parseRequest(MsgBuffer *buf);
|
||||
|
|
|
@ -136,7 +136,7 @@ class HttpResponseImpl : public HttpResponse
|
|||
_headers[std::move(field)] = std::move(value);
|
||||
}
|
||||
|
||||
virtual void addHeader(const char *start, const char *colon, const char *end) override
|
||||
void addHeader(const char *start, const char *colon, const char *end)
|
||||
{
|
||||
_fullHeaderString.reset();
|
||||
std::string field(start, colon);
|
||||
|
@ -260,9 +260,9 @@ class HttpResponseImpl : public HttpResponse
|
|||
_bodyPtr = std::make_shared<std::string>(std::move(body));
|
||||
}
|
||||
|
||||
virtual void redirect(const std::string &url) override
|
||||
void redirect(const std::string &url)
|
||||
{
|
||||
_headers["Location"] = url;
|
||||
_headers["location"] = url;
|
||||
}
|
||||
std::shared_ptr<std::string> renderToString() const;
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ class HttpControllersRouter;
|
|||
class HttpSimpleControllersRouter : public trantor::NonCopyable
|
||||
{
|
||||
public:
|
||||
HttpSimpleControllersRouter(HttpControllersRouter &httpCtrlRouter)
|
||||
explicit HttpSimpleControllersRouter(HttpControllersRouter &httpCtrlRouter)
|
||||
: _httpCtrlsRouter(httpCtrlRouter) {}
|
||||
void registerHttpSimpleController(const std::string &pathName,
|
||||
const std::string &ctrlName,
|
||||
|
|
|
@ -67,11 +67,11 @@ int MultiPartParser::parse(const HttpRequestPtr &req)
|
|||
return parse(content, boundary);
|
||||
}
|
||||
|
||||
int MultiPartParser::parse(const HttpResponsePtr &resp)
|
||||
{
|
||||
/// TODO:
|
||||
return 0;
|
||||
}
|
||||
// int MultiPartParser::parse(const HttpResponsePtr &resp)
|
||||
// {
|
||||
// /// TODO:
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
int MultiPartParser::parseEntity(const char *begin, const char *end)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue