From a44a0fc3e85254ab0b541ca171565ab9aed35e96 Mon Sep 17 00:00:00 2001 From: antao Date: Mon, 4 Feb 2019 14:48:36 +0800 Subject: [PATCH] Add some comments --- lib/inc/drogon/Cookie.h | 1 + lib/inc/drogon/HttpResponse.h | 69 ++++++++++++++++++++------- lib/inc/drogon/MultiPart.h | 5 +- lib/src/HttpFileUploadRequest.h | 2 +- lib/src/HttpRequestImpl.h | 2 +- lib/src/HttpRequestParser.h | 2 +- lib/src/HttpResponseImpl.h | 6 +-- lib/src/HttpSimpleControllersRouter.h | 2 +- lib/src/MultiPart.cc | 10 ++-- 9 files changed, 68 insertions(+), 31 deletions(-) diff --git a/lib/inc/drogon/Cookie.h b/lib/inc/drogon/Cookie.h index 0cf60093..a7d24c2b 100755 --- a/lib/inc/drogon/Cookie.h +++ b/lib/inc/drogon/Cookie.h @@ -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; diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index 83cc33c3..2aeedcb6 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -28,70 +28,105 @@ typedef std::shared_ptr 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 &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 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() {} diff --git a/lib/inc/drogon/MultiPart.h b/lib/inc/drogon/MultiPart.h index 43d25756..089db090 100755 --- a/lib/inc/drogon/MultiPart.h +++ b/lib/inc/drogon/MultiPart.h @@ -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 _files; diff --git a/lib/src/HttpFileUploadRequest.h b/lib/src/HttpFileUploadRequest.h index c9cd3043..21e124e4 100644 --- a/lib/src/HttpFileUploadRequest.h +++ b/lib/src/HttpFileUploadRequest.h @@ -25,7 +25,7 @@ class HttpFileUploadRequest : public HttpRequestImpl public: const std::string &boundary() const { return _boundary; } const std::vector &files() const { return _files; } - HttpFileUploadRequest(const std::vector &files); + explicit HttpFileUploadRequest(const std::vector &files); private: std::string _boundary; diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 66e3f533..d1c2ad0f 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -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()), diff --git a/lib/src/HttpRequestParser.h b/lib/src/HttpRequestParser.h index 4c50a45a..bcf2a63b 100755 --- a/lib/src/HttpRequestParser.h +++ b/lib/src/HttpRequestParser.h @@ -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); diff --git a/lib/src/HttpResponseImpl.h b/lib/src/HttpResponseImpl.h index 1263d66a..7b60941e 100755 --- a/lib/src/HttpResponseImpl.h +++ b/lib/src/HttpResponseImpl.h @@ -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::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 renderToString() const; diff --git a/lib/src/HttpSimpleControllersRouter.h b/lib/src/HttpSimpleControllersRouter.h index 3956c9a7..2de366c8 100644 --- a/lib/src/HttpSimpleControllersRouter.h +++ b/lib/src/HttpSimpleControllersRouter.h @@ -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, diff --git a/lib/src/MultiPart.cc b/lib/src/MultiPart.cc index 8f0538a9..ac58bc60 100755 --- a/lib/src/MultiPart.cc +++ b/lib/src/MultiPart.cc @@ -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) {