diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..921ad96d --- /dev/null +++ b/.clang-format @@ -0,0 +1,20 @@ +--- +BasedOnStyle: Google +BreakBeforeBraces: Allman +AllowAllParametersOfDeclarationOnNextLine: false +IndentWidth: '4' +Language: Cpp +UseTab: Never +ColumnLimit: 128 +SortIncludes: false +AllowShortFunctionsOnASingleLine: None +AllowShortBlocksOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +BinPackArguments: false +BinPackParameters: false +ExperimentalAutoDetectBinPacking: false +BreakConstructorInitializersBeforeComma: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +AccessModifierOffset: -2 +... diff --git a/format.sh b/format.sh new file mode 100755 index 00000000..ed38f5c2 --- /dev/null +++ b/format.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +find lib orm_lib -name *.h -o -name *.cc|xargs clang-format -i -style=file diff --git a/lib/inc/drogon/CacheMap.h b/lib/inc/drogon/CacheMap.h old mode 100755 new mode 100644 index 52ab3655..87031489 --- a/lib/inc/drogon/CacheMap.h +++ b/lib/inc/drogon/CacheMap.h @@ -14,32 +14,33 @@ #pragma once -#include -#include +#include +#include +#include #include #include -#include -#include #include +#include +#include #include #include -#include -#include +#include #define WHEELS_NUM 4 #define BUCKET_NUM_PER_WHEEL 200 #define TICK_INTERVAL 1.0 -//Four wheels with 200 buckets per wheel means the cache map can work with -//a timeout up to 200^4 seconds,about 50 years; +// Four wheels with 200 buckets per wheel means the cache map can work with +// a timeout up to 200^4 seconds,about 50 years; namespace drogon { - class CallbackEntry { public: - CallbackEntry(std::function cb) : cb_(std::move(cb)) {} + CallbackEntry(std::function cb) : cb_(std::move(cb)) + { + } ~CallbackEntry() { cb_(); @@ -69,25 +70,21 @@ class CacheMap * number of wheels * @param bucketsNumPerWheel * buckets number per wheel - * The max delay of the CacheMap is about tickInterval*(bucketsNumPerWheel^wheelsNum) seconds. + * The max delay of the CacheMap is about + * tickInterval*(bucketsNumPerWheel^wheelsNum) seconds. */ CacheMap(trantor::EventLoop *loop, float tickInterval = TICK_INTERVAL, size_t wheelsNum = WHEELS_NUM, size_t bucketsNumPerWheel = BUCKET_NUM_PER_WHEEL) - : _loop(loop), - _tickInterval(tickInterval), - _wheelsNum(wheelsNum), - _bucketsNumPerWheel(bucketsNumPerWheel) + : _loop(loop), _tickInterval(tickInterval), _wheelsNum(wheelsNum), _bucketsNumPerWheel(bucketsNumPerWheel) { _wheels.resize(_wheelsNum); for (size_t i = 0; i < _wheelsNum; i++) { _wheels[i].resize(_bucketsNumPerWheel); } - if (_tickInterval > 0 && - _wheelsNum > 0 && - _bucketsNumPerWheel > 0) + if (_tickInterval > 0 && _wheelsNum > 0 && _bucketsNumPerWheel > 0) { _timerId = _loop->runEvery(_tickInterval, [=]() { _ticksCounter++; @@ -100,7 +97,7 @@ class CacheMap CallbackBucket tmp; { std::lock_guard lock(bucketMutex_); - //use tmp val to make this critical area as short as possible. + // use tmp val to make this critical area as short as possible. _wheels[i].front().swap(tmp); _wheels[i].pop_front(); _wheels[i].push_back(CallbackBucket()); @@ -167,7 +164,10 @@ class CacheMap } } - void insert(const T1 &key, const T2 &value, size_t timeout = 0, std::function timeoutCallback = std::function()) + void insert(const T1 &key, + const T2 &value, + size_t timeout = 0, + std::function timeoutCallback = std::function()) { if (timeout > 0) { @@ -231,7 +231,7 @@ class CacheMap /// Atomically find and get the value of a keyword /** * Return true when the value is found, and the value - * is assigned to the @param value + * is assigned to the @param value */ bool findAndFetch(const T1 &key, T2 &value) { @@ -255,7 +255,7 @@ class CacheMap /// Erases the value of the keyword. void erase(const T1 &key) { - //in this case,we don't evoke the timeout callback; + // in this case,we don't evoke the timeout callback; std::lock_guard lock(mtx_); _map.erase(key); } @@ -280,7 +280,7 @@ class CacheMap void insertEntry(size_t delay, CallbackEntryPtr entryPtr) { - //protected by bucketMutex; + // protected by bucketMutex; if (delay <= 0) return; delay = delay / _tickInterval + 1; @@ -304,7 +304,7 @@ class CacheMap } else { - //delay is too long to put entry at valid position in wheels; + // delay is too long to put entry at valid position in wheels; _wheels[i][_bucketsNumPerWheel - 1].insert(entryPtr); } delay = (delay + (t % _bucketsNumPerWheel) - 1) / _bucketsNumPerWheel; @@ -337,9 +337,8 @@ class CacheMap { auto &value = _map[key]; auto entryPtr = value._weakEntryPtr.lock(); - //entryPtr is used to avoid race conditions - if (value.timeout > 0 && - !entryPtr) + // entryPtr is used to avoid race conditions + if (value.timeout > 0 && !entryPtr) { if (value._timeoutCallback) { @@ -361,4 +360,4 @@ class CacheMap } }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/Cookie.h b/lib/inc/drogon/Cookie.h old mode 100755 new mode 100644 index afc774b0..f4c9c794 --- a/lib/inc/drogon/Cookie.h +++ b/lib/inc/drogon/Cookie.h @@ -13,51 +13,113 @@ */ #pragma once -#include #include +#include namespace drogon { - class Cookie { public: - Cookie(const std::string &key, const std::string &value) - : _key(key), - _value(value) + Cookie(const std::string &key, const std::string &value) : _key(key), _value(value) { } Cookie() = default; - ~Cookie() {} - void setExpiresDate(const trantor::Date &date) { _expiresDate = date; } - void setHttpOnly(bool only) { _httpOnly = only; } - void setSecure(bool secure) { _secure = secure; } - void setDomain(const std::string &domain) { _domain = domain; } - void setPath(const std::string &path) { _path = path; } - void setKey(const std::string &key) { _key = key; } - void setValue(const std::string &value) { _value = value; } + ~Cookie() + { + } + void setExpiresDate(const trantor::Date &date) + { + _expiresDate = date; + } + void setHttpOnly(bool only) + { + _httpOnly = only; + } + void setSecure(bool secure) + { + _secure = secure; + } + void setDomain(const std::string &domain) + { + _domain = domain; + } + void setPath(const std::string &path) + { + _path = path; + } + void setKey(const std::string &key) + { + _key = key; + } + void setValue(const std::string &value) + { + _value = value; + } std::string cookieString() const; - std::string getCookieString() const { return cookieString(); } - - const trantor::Date &expiresDate() const { return _expiresDate; } - const trantor::Date &getExpiresDate() const { return _expiresDate; } + std::string getCookieString() const + { + return cookieString(); + } - const std::string &domain() const { return _domain; } - const std::string &getDomain() const { return _domain; } + const trantor::Date &expiresDate() const + { + return _expiresDate; + } + const trantor::Date &getExpiresDate() const + { + return _expiresDate; + } - const std::string &path() const { return _path; } - const std::string &getPath() const { return _path; } + const std::string &domain() const + { + return _domain; + } + const std::string &getDomain() const + { + return _domain; + } - const std::string &key() const { return _key; } - const std::string &getKey() const { return _key; } + const std::string &path() const + { + return _path; + } + const std::string &getPath() const + { + return _path; + } - const std::string &value() const { return _value; } - const std::string &getValue() const { return _value; } + const std::string &key() const + { + return _key; + } + const std::string &getKey() const + { + return _key; + } - operator bool() const { return (!_key.empty()) && (!_value.empty()); } - bool isHttpOnly() const { return _httpOnly; } - bool isSecure() const { return _secure; } + const std::string &value() const + { + return _value; + } + const std::string &getValue() const + { + return _value; + } + + operator bool() const + { + return (!_key.empty()) && (!_value.empty()); + } + bool isHttpOnly() const + { + return _httpOnly; + } + bool isSecure() const + { + return _secure; + } private: trantor::Date _expiresDate; @@ -69,4 +131,4 @@ class Cookie std::string _value; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/DrClassMap.h b/lib/inc/drogon/DrClassMap.h old mode 100755 new mode 100644 index bc07b1a2..0a215a4d --- a/lib/inc/drogon/DrClassMap.h +++ b/lib/inc/drogon/DrClassMap.h @@ -14,25 +14,24 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace drogon { - class DrObjectBase; typedef std::function DrAllocFunc; class DrClassMap { -public: + public: static void registerClass(const std::string &className, const DrAllocFunc &func); static DrObjectBase *newObject(const std::string &className); static const std::shared_ptr &getSingleInstance(const std::string &className); @@ -48,8 +47,8 @@ public: { std::size_t len = 0; int status = 0; - std::unique_ptr ptr( - __cxxabiv1::__cxa_demangle(mangled_name, nullptr, &len, &status), &std::free); + std::unique_ptr ptr(__cxxabiv1::__cxa_demangle(mangled_name, nullptr, &len, &status), + &std::free); if (status == 0) { return std::string(ptr.get()); @@ -58,8 +57,8 @@ public: return ""; } -protected: + protected: static std::unordered_map &getMap(); }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/DrObject.h b/lib/inc/drogon/DrObject.h old mode 100755 new mode 100644 index 69be472c..6d9644d8 --- a/lib/inc/drogon/DrObject.h +++ b/lib/inc/drogon/DrObject.h @@ -33,7 +33,9 @@ class DrObjectBase { return (className() == class_name); } - virtual ~DrObjectBase() {} + virtual ~DrObjectBase() + { + } }; /** @@ -59,8 +61,10 @@ class DrObject : public virtual DrObjectBase } protected: - //protect constructor to make this class only inheritable - DrObject() {} + // protect constructor to make this class only inheritable + DrObject() + { + } private: class DrAllocator @@ -78,9 +82,7 @@ class DrObject : public virtual DrObjectBase template typename std::enable_if::value, void>::type registerClass() { - DrClassMap::registerClass(className(), []() -> DrObjectBase * { - return new T; - }); + DrClassMap::registerClass(className(), []() -> DrObjectBase * { return new T; }); } template typename std::enable_if::value, void>::type registerClass() @@ -88,10 +90,10 @@ class DrObject : public virtual DrObjectBase } }; - //use static val to register allocator function for class T; + // use static val to register allocator function for class T; static DrAllocator _alloc; }; template typename DrObject::DrAllocator DrObject::_alloc; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/DrTemplate.h b/lib/inc/drogon/DrTemplate.h old mode 100755 new mode 100644 index f7f2d816..ee244fbc --- a/lib/inc/drogon/DrTemplate.h +++ b/lib/inc/drogon/DrTemplate.h @@ -18,12 +18,13 @@ #include namespace drogon { - template class DrTemplate : public DrObject, public DrTemplateBase { protected: - DrTemplate() {} + DrTemplate() + { + } }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/DrTemplateBase.h b/lib/inc/drogon/DrTemplateBase.h old mode 100755 new mode 100644 index 215e5b14..5b06233d --- a/lib/inc/drogon/DrTemplateBase.h +++ b/lib/inc/drogon/DrTemplateBase.h @@ -16,17 +16,17 @@ #include #include -#include #include +#include namespace drogon { - typedef HttpViewData DrTemplateData; /// The templating engine class /** - * This class can generate a text string from the template file and template data. + * This class can generate a text string from the template file and template + * data. * For more details on the template file, see the wiki site (the 'View' section) */ class DrTemplateBase : public virtual DrObjectBase @@ -35,15 +35,17 @@ class DrTemplateBase : public virtual DrObjectBase /// Create an object of the implementation class /** * The @param templateName represents the name of the template file. - * A template file is a description file with a special format. Its extension is - * usually .csp. The user should preprocess the template file - * with the drogon_ctl tool to create c++ source files. + * A template file is a description file with a special format. Its extension + * is + * usually .csp. The user should preprocess the template file + * with the drogon_ctl tool to create c++ source files. */ static std::shared_ptr newTemplate(std::string templateName); /// Generate the text string /** - * The @param data represents data rendered in the string in a format according to the template file. + * The @param data represents data rendered in the string in a format + * according to the template file. */ virtual std::string genText(const DrTemplateData &data = DrTemplateData()) = 0; @@ -51,4 +53,4 @@ class DrTemplateBase : public virtual DrObjectBase DrTemplateBase(){}; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h old mode 100755 new mode 100644 index 54e267a7..43b6272c --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -2,7 +2,7 @@ * * HttpAppFramework.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -18,39 +18,40 @@ #if USE_ORM #include #endif -#include -#include -#include -#include +#include +#include #include +#include +#include +#include #include #include -#include -#include #include -#include -#include -#include +#include #include +#include #include -#include -#include +#include +#include +#include +#include #include #include -#include -#include +#include +#include #include -#include +#include namespace drogon { -//the drogon banner -const char banner[] = " _ \n" - " __| |_ __ ___ __ _ ___ _ __ \n" - " / _` | '__/ _ \\ / _` |/ _ \\| '_ \\ \n" - "| (_| | | | (_) | (_| | (_) | | | |\n" - " \\__,_|_| \\___/ \\__, |\\___/|_| |_|\n" - " |___/ \n"; +// the drogon banner +const char banner[] = + " _ \n" + " __| |_ __ ___ __ _ ___ _ __ \n" + " / _` | '__/ _ \\ / _` |/ _ \\| '_ \\ \n" + "| (_| | | | (_) | (_| | (_) | | | |\n" + " \\__,_|_| \\___/ \\__, |\\___/|_| |_|\n" + " |___/ \n"; inline std::string getVersion() { return VERSION; @@ -68,20 +69,21 @@ typedef std::function AdviceChainCallback; class HttpAppFramework : public trantor::NonCopyable { -public: + public: virtual ~HttpAppFramework(); - ///Get the instance of HttpAppFramework + /// Get the instance of HttpAppFramework /** * HttpAppFramework works at singleton mode, so any calling of this * method gets the same instance; - * Calling drogon::HttpAppFramework::instance() - * can be replaced by a simple interface -- drogon::app() + * Calling drogon::HttpAppFramework::instance() + * can be replaced by a simple interface -- drogon::app() */ static HttpAppFramework &instance(); - ///Run the event loop + /// Run the event loop /** - * Calling this method starts the IO event loops and the main loop of the application; + * Calling this method starts the IO event loops and the main loop of the + * application; * This method MUST be called in the main thread. * This method blocks the main thread until the main event loop exits. */ @@ -90,19 +92,19 @@ public: /// Return true if the framework is running virtual bool isRunning() = 0; - ///Quit the event loop + /// Quit the event loop /** * Calling this method results in stopping all network IO in the - * framework and interrupting the blocking of the run() method. Usually, + * framework and interrupting the blocking of the run() method. Usually, * after calling this method, the application exits. - * + * * NOTE: * This method can be called in any thread and anywhere. * This method should not be called before calling run(). */ virtual void quit() = 0; - ///Get the main event loop of the framework; + /// Get the main event loop of the framework; /** * NOTE: * The event loop is not the network IO loop, but the main event loop @@ -112,18 +114,18 @@ public: */ virtual trantor::EventLoop *getLoop() = 0; - ///Set custom 404 page + /// Set custom 404 page /** - * After calling this method, the @param resp object is returned + * After calling this method, the @param resp object is returned * by the HttpResponse::newNotFoundResponse() method. */ virtual void setCustom404Page(const HttpResponsePtr &resp) = 0; - ///Get the plugin object registered in the framework + /// Get the plugin object registered in the framework /** * NOTE: * This method is usually called after the framework runs. - * Calling this method in the initAndStart() method of plugins is also valid. + * Calling this method in the initAndStart() method of plugins is also valid. */ template T *getPlugin() @@ -133,39 +135,45 @@ public: return dynamic_cast(getPlugin(T::classTypeName())); } - ///Get the plugin object registered in the framework + /// Get the plugin object registered in the framework /** * @param name: is the class name of the plugin. - * + * * NOTE: * This method is usually called after the framework runs. - * Calling this method in the initAndStart() method of plugins is also valid. + * Calling this method in the initAndStart() method of plugins is also valid. */ virtual PluginBase *getPlugin(const std::string &name) = 0; - ///The following is a series of methods of AOP + /// The following is a series of methods of AOP - ///The @param advice is called immediately after the main event loop runs. + /// The @param advice is called immediately after the main event loop runs. virtual void registerBeginningAdvice(const std::function &advice) = 0; - ///The @param advice is called immediately when a new connection is established. + /// The @param advice is called immediately when a new connection is + /// established. /** - * The first parameter of the @param advice is the remote address of the new connection, the second one + * The first parameter of the @param advice is the remote address of the new + * connection, the second one * is the local address of it. * If the @param advice returns a false value, drogon closes the connection. * Users can use this advice to implement some security policies. */ - virtual void registerNewConnectionAdvice(const std::function &advice) = 0; + virtual void registerNewConnectionAdvice( + const std::function &advice) = 0; - ///The @param advice is called immediately after the request is created and before it matches any handler paths. + /// The @param advice is called immediately after the request is created and + /// before it matches any handler paths. /** - * The parameters of the @param advice are same as those of the doFilter method of the Filter class. - * The following diagram shows the location of the AOP join points during http request processing. - * - * - * +-----------+ +------------+ - * | Request | | Response | - * +-----------+ +------------+ + * The parameters of the @param advice are same as those of the doFilter + * method of the Filter class. + * The following diagram shows the location of the AOP join points during http + * request processing. + * + * + * +-----------+ +------------+ + * | Request | | Response | + * +-----------+ +------------+ * | ^ * v | * Pre-routing join point o----------->[Advice callback]----------->+ @@ -193,98 +201,120 @@ public: * | | * v | * Post-handling join point o---------------------------------------->+ - * + * */ - virtual void registerPreRoutingAdvice(const std::function &advice) = 0; + virtual void registerPreRoutingAdvice( + const std::function &advice) = 0; - ///The @param advice is called at the same time as the above advice. It can be thought of as an observer who cannot respond to http requests. + /// The @param advice is called at the same time as the above advice. It can + /// be thought of as an observer who cannot respond + /// to http requests. /** - * This advice has less overhead than the above one. - * If one does not intend to intercept the http request, please use this interface. + * This advice has less overhead than the above one. + * If one does not intend to intercept the http request, please use this + * interface. */ virtual void registerPreRoutingAdvice(const std::function &advice) = 0; - ///The @param advice is called immediately after the request matchs a handler path - ///and before any 'doFilter' method of filters applies. + /// The @param advice is called immediately after the request matchs a handler + /// path + /// and before any 'doFilter' method of filters applies. /** - * The parameters of the @param advice are same as those of the doFilter method of the Filter class. + * The parameters of the @param advice are same as those of the doFilter + * method of the Filter class. */ - virtual void registerPostRoutingAdvice(const std::function &advice) = 0; + virtual void registerPostRoutingAdvice( + const std::function &advice) = 0; - ///The @param advice is called at the same time as the above advice. It can be thought of as an observer who cannot respond to http requests. + /// The @param advice is called at the same time as the above advice. It can + /// be thought of as an observer who cannot respond + /// to http requests. /** - * This advice has less overhead than the above one. - * If one does not intend to intercept the http request, please use this interface. + * This advice has less overhead than the above one. + * If one does not intend to intercept the http request, please use this + * interface. */ virtual void registerPostRoutingAdvice(const std::function &advice) = 0; - ///The @param advice is called immediately after the request is approved by all filters and before it is handled. + /// The @param advice is called immediately after the request is approved by + /// all filters and before it is handled. /** - * The parameters of the @param advice are same as those of the doFilter method of the Filter class. + * The parameters of the @param advice are same as those of the doFilter + * method of the Filter class. */ - virtual void registerPreHandlingAdvice(const std::function &advice) = 0; + virtual void registerPreHandlingAdvice( + const std::function &advice) = 0; - ///The @param advice is called at the same time as the above advice. It can be thought of as an observer who cannot respond to http requests. + /// The @param advice is called at the same time as the above advice. It can + /// be thought of as an observer who cannot respond + /// to http requests. /** - * This advice has less overhead than the above one. - * If one does not intend to intercept the http request, please use this interface. + * This advice has less overhead than the above one. + * If one does not intend to intercept the http request, please use this + * interface. */ virtual void registerPreHandlingAdvice(const std::function &advice) = 0; - ///The @param advice is called immediately after the request is handled and a response object is created by handlers. - virtual void registerPostHandlingAdvice(const std::function &advice) = 0; + /// The @param advice is called immediately after the request is handled and a + /// response object is created by handlers. + virtual void registerPostHandlingAdvice( + const std::function &advice) = 0; - ///End of AOP methods + /// End of AOP methods - ///Load the configuration file with json format. + /// Load the configuration file with json format. virtual void loadConfigFile(const std::string &fileName) = 0; - ///Register a HttpSimpleController object into the framework. + /// Register a HttpSimpleController object into the framework. /** - * @param pathName: When the path of a http request is equal to the @param pathName, the asyncHandleHttpRequest() method + * @param pathName: When the path of a http request is equal to the @param + * pathName, the asyncHandleHttpRequest() method * of the controller is called. - * @param ctrlName is the name of the controller. It includes the namespace to which the controller belongs. - * @param filtersAndMethods is a vector containing Http methods or filter name constraints. - * + * @param ctrlName is the name of the controller. It includes the namespace to + * which the controller belongs. + * @param filtersAndMethods is a vector containing Http methods or filter name + * constraints. + * * FOR EXAMPLE: * app.registerHttpSimpleController("/userinfo","UserInfoCtrl",{Get,"LoginFilter"}); - * + * * NOTE: - * Users can perform the same operation through the configuration file or a macro in the header file. + * Users can perform the same operation through the configuration file or a + * macro in the header file. */ virtual void registerHttpSimpleController(const std::string &pathName, const std::string &ctrlName, const std::vector &filtersAndMethods = std::vector()) = 0; - ///Register a handler into the framework. + /// Register a handler into the framework. /** - * @param pathPattern: When the path of a http request matches the @param pathPattern, the handler indicated by - * the @param function is called. - * @param function indicates any type of callable object with a valid processing interface. - * @param filtersAndMethods is the same as the third parameter in the above method. - * + * @param pathPattern: When the path of a http request matches the @param + * pathPattern, the handler indicated by + * the @param function is called. + * @param function indicates any type of callable object with a valid + * processing interface. + * @param filtersAndMethods is the same as the third parameter in the above + * method. + * * FOR EXAMPLE: * app.registerHandler("/hello?username={1}", * [](const HttpRequestPtr& req, - * const std::function & callback, + * const std::function + * & callback, * const std::string &name) * { * Json::Value json; * json["result"]="ok"; * json["message"]=std::string("hello,")+name; - * auto resp=HttpResponse::newHttpJsonResponse(json); + * auto + * resp=HttpResponse::newHttpJsonResponse(json); * callback(resp); * }, * {Get,"LoginFilter"}); - * + * * NOTE: - * As you can see in the above example, this method supports parameters mapping. + * As you can see in the above example, this method supports parameters + * mapping. */ template void registerHandler(const std::string &pathPattern, @@ -295,8 +325,7 @@ public: LOG_TRACE << "pathPattern:" << pathPattern; internal::HttpBinderBasePtr binder; - binder = std::make_shared< - internal::HttpBinder>(std::forward(function)); + binder = std::make_shared>(std::forward(function)); std::vector validMethods; std::vector filters; @@ -325,21 +354,25 @@ public: } /// Register a WebSocketController into the framework. - /// The parameters of this method are the same as those in the registerHttpSimpleController() method. + /// The parameters of this method are the same as those in the + /// registerHttpSimpleController() method. virtual void registerWebSocketController(const std::string &pathName, const std::string &crtlName, - const std::vector &filters = - std::vector()) = 0; + const std::vector &filters = std::vector()) = 0; /// Register controller objects created and initialized by the user /** - * Drogon can only automatically create controllers using the default constructor. - * Sometimes users want to be able to create controllers using constructors with - * parameters. Controllers created by user in this way should be registered to the framework - * via this method. - * The macro or configuration file is still valid for the path routing configuration + * Drogon can only automatically create controllers using the default + * constructor. + * Sometimes users want to be able to create controllers using constructors + * with + * parameters. Controllers created by user in this way should be registered to + * the framework + * via this method. + * The macro or configuration file is still valid for the path routing + * configuration * of the controller created by users. - * + * * NOTE: * The declaration of the controller class must be as follows: * class ApiTest : public drogon::HttpController @@ -348,7 +381,8 @@ public: * ApiTest(const std::string &str); * ... * }; - * The second template parameter must be explicitly set to false to disable automatic creation. + * The second template parameter must be explicitly set to false to disable + * automatic creation. * And then user can create and register it somewhere as follows: * auto ctrlPtr=std::make_shared("hello world"); * drogon::app().registerController(ctrlPtr); @@ -361,7 +395,10 @@ public: internal::IsSubClass::value || internal::IsSubClass::value, "Error! Only controller objects can be registered here"); - static_assert(!T::isAutoCreation, "Controllers created and initialized automatically by drogon cannot be registered here"); + static_assert(!T::isAutoCreation, + "Controllers created and initialized " + "automatically by drogon cannot be " + "registered here"); DrClassMap::setSingleInstance(ctrlPtr); T::initPathRouting(); } @@ -373,67 +410,77 @@ public: template void registerFilter(const std::shared_ptr &filterPtr) { - static_assert(internal::IsSubClass::value, - "Error! Only fitler objects can be registered here"); - static_assert(!T::isAutoCreation, "Filters created and initialized automatically by drogon cannot be registered here"); + static_assert(internal::IsSubClass::value, "Error! Only fitler objects can be registered here"); + static_assert(!T::isAutoCreation, + "Filters created and initialized " + "automatically by drogon cannot be " + "registered here"); DrClassMap::setSingleInstance(filterPtr); } /// Forward the http request /** - * The @param hostString is the address where the request is forwarded. The following strings are + * The @param hostString is the address where the request is forwarded. The + * following strings are * valid for the @param hostString: - * + * * https://www.baidu.com * http://www.baidu.com * https://127.0.0.1:8080/ * http://127.0.0.1 - * http://[::1]:8080/ - * + * http://[::1]:8080/ + * * NOTE: - * If the @param hostString is empty, the request is handled by the same application, so in this condition - * one should modify the path of the @param req before forwarding to avoid infinite loop processing. - * - * This method can be used to implement reverse proxy or redirection on the server side. + * If the @param hostString is empty, the request is handled by the same + * application, so in this condition + * one should modify the path of the @param req before forwarding to avoid + * infinite loop processing. + * + * This method can be used to implement reverse proxy or redirection on the + * server side. */ - virtual void forward(const HttpRequestPtr &req, std::function &&callback, const std::string &hostString = "") = 0; + virtual void forward(const HttpRequestPtr &req, + std::function &&callback, + const std::string &hostString = "") = 0; - ///Get information about the handlers registered to drogon + /// Get information about the handlers registered to drogon /** - * The first item of std::tuple in the return value represents the path pattern of the handler; - * The last item in std::tuple is the description of the handler. + * The first item of std::tuple in the return value represents the path + * pattern of the handler; + * The last item in std::tuple is the description of the handler. */ virtual std::vector> getHandlersInfo() const = 0; - ///Get the custom configuration defined by users in the configuration file. + /// Get the custom configuration defined by users in the configuration file. virtual const Json::Value &getCustomConfig() const = 0; - ///Set the number of threads for IO event loops + /// Set the number of threads for IO event loops /** - * The default value is 1, if @param threadNum is 0, the number is equal to the number of CPU cores. - * + * The default value is 1, if @param threadNum is 0, the number is equal to + * the number of CPU cores. + * * NOTE: * This number is usually less than or equal to the number of CPU cores. * This number can be configured in the configuration file. */ virtual void setThreadNum(size_t threadNum) = 0; - ///Get the number of threads for IO event loops + /// Get the number of threads for IO event loops virtual size_t getThreadNum() const = 0; - ///Set the global cert file and private key file for https - ///These options can be configured in the configuration file. - virtual void setSSLFiles(const std::string &certPath, - const std::string &keyPath) = 0; + /// Set the global cert file and private key file for https + /// These options can be configured in the configuration file. + virtual void setSSLFiles(const std::string &certPath, const std::string &keyPath) = 0; - ///Add a listener for http or https service + /// Add a listener for http or https service /** * @param ip is the ip that the listener listens on. * @param port is the port that the listener listens on. * If @param useSSL is true, the listener is used for the https service. - * @param certFile and @param keyFile specify the cert file and the private key file for this listener. If + * @param certFile and @param keyFile specify the cert file and the private + * key file for this listener. If * they are empty, the global configuration set by the above method is used. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ @@ -443,119 +490,120 @@ public: const std::string &certFile = "", const std::string &keyFile = "") = 0; - ///Enable sessions supporting. + /// Enable sessions supporting. /** * Disabled by default. * If there isn't any request from a client for @param timeout(>0) seconds, * the session of the client is destroyed. * If the @param timeout is equal to 0, sessions will remain permanently - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void enableSession(const size_t timeout = 0) = 0; - ///A wrapper of the above method. + /// A wrapper of the above method. /** * Users can set the timeout value as follows: * app().enableSession(0.2h); - * app().enableSession(12min); + * app().enableSession(12min); */ inline void enableSession(const std::chrono::duration &timeout) { enableSession((size_t)timeout.count()); } - ///Disable sessions supporting. - /** + /// Disable sessions supporting. + /** * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void disableSession() = 0; - ///Set the root path of HTTP document, defaut path is ./ - /** + /// Set the root path of HTTP document, defaut path is ./ + /** * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setDocumentRoot(const std::string &rootPath) = 0; - ///Get the document root directory. + /// Get the document root directory. virtual const std::string &getDocumentRoot() const = 0; - ///Set the path to store uploaded files. - /** - * If the @param uploadPath isn't prefixed with /, ./ or ../, it is relative path of document_root path, + /// Set the path to store uploaded files. + /** + * If the @param uploadPath isn't prefixed with /, ./ or ../, it is relative + * path of document_root path, * The default value is 'uploads'. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setUploadPath(const std::string &uploadPath) = 0; - ///Get the path to store uploaded files. + /// Get the path to store uploaded files. virtual const std::string &getUploadPath() const = 0; - ///Set types of files that can be downloaded. - /** + /// Set types of files that can be downloaded. + /** * FOR EXAMPLE: * app.setFileTypes({"html","txt","png","jpg"}); - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setFileTypes(const std::vector &types) = 0; - ///Enable supporting for dynamic views loading. + /// Enable supporting for dynamic views loading. /** * Disabled by default. * The @param libPaths is a vactor that contains paths to view files. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void enableDynamicViewsLoading(const std::vector &libPaths) = 0; - ///Set the maximum number of all connections. - /** + /// Set the maximum number of all connections. + /** * The default value is 100000. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setMaxConnectionNum(size_t maxConnections) = 0; - ///Set the maximum number of connections per remote IP. - /** + /// Set the maximum number of connections per remote IP. + /** * The default value is 0 which means no limit. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setMaxConnectionNumPerIP(size_t maxConnectionsPerIP) = 0; - ///Make the application run as a daemon. - /** + /// Make the application run as a daemon. + /** * Disabled by default. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void enableRunAsDaemon() = 0; - ///Make the application restart after crashing. - /** + /// Make the application restart after crashing. + /** * Disabled by default. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void enableRelaunchOnError() = 0; - ///Set the output path of logs. + /// Set the output path of logs. /** * @param logSize indicates the maximum size of the log file. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ @@ -563,30 +611,35 @@ public: const std::string &logfileBaseName = "", size_t logSize = 100000000) = 0; - ///Set the log level - /** - * The @param level is one of TRACE, DEBUG, INFO, WARN. The Default value is DEBUG. - * + /// Set the log level + /** + * The @param level is one of TRACE, DEBUG, INFO, WARN. The Default value is + * DEBUG. + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setLogLevel(trantor::Logger::LogLevel level) = 0; - ///If @param sendFile is true, sendfile() system-call is used to send static files to clients; + /// If @param sendFile is true, sendfile() system-call is used to send static + /// files to clients; /** * The default value is true. - * + * * NOTE: * This operation can be performed by an option in the configuration file. - * Even though sendfile() is enabled, only files larger than 200k are sent this way, - * because the advantages of sendfile() can only be reflected in sending large files. + * Even though sendfile() is enabled, only files larger than 200k are sent + * this way, + * because the advantages of sendfile() can only be reflected in sending large + * files. */ virtual void enableSendfile(bool sendFile) = 0; - ///If @param useGzip is true, use gzip to compress the response body's content; + /// If @param useGzip is true, use gzip to compress the response body's + /// content; /** * The default value is true. - * + * * NOTE: * This operation can be performed by an option in the configuration file. * After gzip is enabled, gzip is used under the following conditions: @@ -595,99 +648,111 @@ public: */ virtual void enableGzip(bool useGzip) = 0; - ///Return true if gzip is enabled. + /// Return true if gzip is enabled. virtual bool isGzipEnabled() const = 0; - ///Set the time in which the static file response is cached in memory. + /// Set the time in which the static file response is cached in memory. /** - * @param cacheTime: in seconds. 0 means always cached, negative means no cache - * + * @param cacheTime: in seconds. 0 means always cached, negative means no + * cache + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setStaticFilesCacheTime(int cacheTime) = 0; - ///Get the time set by the above method. + /// Get the time set by the above method. virtual int staticFilesCacheTime() const = 0; - ///Set the lifetime of the connection without read or write + /// Set the lifetime of the connection without read or write /** - * @param timeout: in seconds. 60 by default. Setting the timeout to 0 means that drogon does not close idle connections. - * + * @param timeout: in seconds. 60 by default. Setting the timeout to 0 means + * that drogon does not close idle connections. + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setIdleConnectionTimeout(size_t timeout) = 0; - ///A wrapper of the above method. + /// A wrapper of the above method. /** * Users can set the timeout value as follows: * app().setIdleConnectionTimeout(0.5h); - * app().setIdleConnectionTimeout(30min); + * app().setIdleConnectionTimeout(30min); */ inline void setIdleConnectionTimeout(const std::chrono::duration &timeout) { setIdleConnectionTimeout((size_t)timeout.count()); } - ///Set the 'server' header field in each response sent by drogon. + /// Set the 'server' header field in each response sent by drogon. /** - * @param server: empty string by default with which the 'server' header field is + * @param server: empty string by default with which the 'server' header field + * is * set to "Server: drogon/version string\r\n" - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setServerHeaderField(const std::string &server) = 0; - ///Set the maximum number of requests that can be served through one keep-alive connection. + /// Set the maximum number of requests that can be served through one + /// keep-alive connection. /** - * After the maximum number of requests are made, the connection is closed. The default value is + * After the maximum number of requests are made, the connection is closed. + * The default value is * 0 which means no limit. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setKeepaliveRequestsNumber(const size_t number) = 0; - ///Set the maximum number of unhandled requests that can be cached in pipelining buffer. + /// Set the maximum number of unhandled requests that can be cached in + /// pipelining buffer. /** * The default value of 0 means no limit. - * After the maximum number of requests cached in pipelining buffer are made, the connection is closed. - * + * After the maximum number of requests cached in pipelining buffer are made, + * the connection is closed. + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setPipeliningRequestsNumber(const size_t number) = 0; - ///Set the gzip_static option. + /// Set the gzip_static option. /** - * If it is set to true, when the client requests a static file, drogon first finds the compressed - * file with the extension ".gz" in the same path and send the compressed file to the client. + * If it is set to true, when the client requests a static file, drogon first + * finds the compressed + * file with the extension ".gz" in the same path and send the compressed file + * to the client. * The default value is true. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setGzipStatic(bool useGzipStatic) = 0; - ///Set the max body size of the requests received by drogon. The default value is 1M. + /// Set the max body size of the requests received by drogon. The default + /// value is 1M. /** * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setClientMaxBodySize(size_t maxSize) = 0; - ///Set the max size of messages sent by WebSocket client. The default value is 128K. + /// Set the max size of messages sent by WebSocket client. The default value + /// is 128K. /** * NOTE: * This operation can be performed by an option in the configuration file. */ virtual void setClientMaxWebSocketMessageSize(size_t maxSize) = 0; - //Set the HTML file of the home page, the default value is "index.html" + // Set the HTML file of the home page, the default value is "index.html" /** - * If there isn't any handler registered to the path "/", the home page file in the "document_root" + * If there isn't any handler registered to the path "/", the home page file + * in the "document_root" * is send to clients as a response to the request for "/". * NOTE: * This operation can be performed by an option in the configuration file. @@ -695,31 +760,32 @@ public: virtual void setHomePage(const std::string &homePageFile) = 0; #if USE_ORM - ///Get a database client by @param name + /// Get a database client by @param name /** * NOTE: * This method must be called after the framework has been run. */ virtual orm::DbClientPtr getDbClient(const std::string &name = "default") = 0; - ///Get a 'fast' database client by @param name + /// Get a 'fast' database client by @param name /** * NOTE: * This method must be called after the framework has been run. */ virtual orm::DbClientPtr getFastDbClient(const std::string &name = "default") = 0; - ///Create a database client + /// Create a database client /** * @param dbType: The database type is one of "postgresql","mysql","sqlite3". * @param host: IP or host name. * @param port: The port on which the database server is listening. * @databaseName, @param userName, @param password: ... - * @connectionNum: The number of connections to the database server. It's valid only if @param isFast is false. + * @connectionNum: The number of connections to the database server. It's + * valid only if @param isFast is false. * @filename: The file name of sqlite3 database file. * @name: The client name. * @isFast: Indicates if the client is a fast database client. - * + * * NOTE: * This operation can be performed by an option in the configuration file. */ @@ -735,7 +801,7 @@ public: const bool isFast = false) = 0; #endif -private: + private: virtual void registerHttpController(const std::string &pathPattern, const internal::HttpBinderBasePtr &binder, const std::vector &validMethods = std::vector(), @@ -748,4 +814,4 @@ inline HttpAppFramework &app() return HttpAppFramework::instance(); } -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpBinder.h b/lib/inc/drogon/HttpBinder.h old mode 100755 new mode 100644 index 7e5f9bd2..7862be05 --- a/lib/inc/drogon/HttpBinder.h +++ b/lib/inc/drogon/HttpBinder.h @@ -2,7 +2,7 @@ * * HttpBinder.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,15 +14,15 @@ #pragma once +#include +#include #include #include #include -#include -#include #include -#include -#include #include +#include +#include /// The classes in the file are internal tool classes. Do not include this /// file directly and use any of these classes directly. @@ -31,8 +31,8 @@ namespace drogon { namespace internal { - -//we only accept value type or const lreference type or right reference type as the handle method parameters type +// we only accept value type or const lreference type or right reference type as +// the handle method parameters type template struct BinderArgTypeTraits { @@ -68,17 +68,22 @@ class HttpBinderBase { public: virtual void handleHttpRequest(std::list &pathParameter, - const HttpRequestPtr &req, std::function &&callback) = 0; + const HttpRequestPtr &req, + std::function &&callback) = 0; virtual size_t paramCount() = 0; virtual const std::string &handlerName() const = 0; - virtual ~HttpBinderBase() {} + virtual ~HttpBinderBase() + { + } }; template T &getControllerObj() { - //Initialization of function-local statics is guaranteed to occur only once even when - //called from multiple threads, and may be more efficient than the equivalent code using std::call_once. + // Initialization of function-local statics is guaranteed to occur only once + // even when + // called from multiple threads, and may be more efficient than the equivalent + // code using std::call_once. static T obj; return obj; } @@ -90,7 +95,8 @@ class HttpBinder : public HttpBinderBase public: typedef FUNCTION FunctionType; virtual void handleHttpRequest(std::list &pathParameter, - const HttpRequestPtr &req, std::function &&callback) override + const HttpRequestPtr &req, + std::function &&callback) override { run(pathParameter, req, std::move(callback)); } @@ -112,7 +118,7 @@ class HttpBinder : public HttpBinderBase return _handlerName; } -private: + private: FUNCTION _func; typedef FunctionTraits traits; @@ -121,17 +127,20 @@ private: static const size_t argument_count = traits::arity; std::string _handlerName; - template + template typename std::enable_if<(sizeof...(Values) < Boundary), void>::type run( std::list &pathParameter, - const HttpRequestPtr &req, std::function &&callback, + const HttpRequestPtr &req, + std::function &&callback, Values &&... values) { - //call this function recursively until parameter's count equals to the count of target function parameters + // call this function recursively until parameter's count equals to the + // count of target function parameters static_assert(BinderArgTypeTraits>::isValid, - "your handler argument type must be value type or const left reference type or right reference type"); - typedef typename std::remove_cv>::type>::type ValueType; + "your handler argument type must be value type or const left reference " + "type or right reference type"); + typedef + typename std::remove_cv>::type>::type ValueType; ValueType value = ValueType(); if (!pathParameter.empty()) { @@ -146,12 +155,11 @@ private: run(pathParameter, req, std::move(callback), std::forward(values)..., std::move(value)); } - template < - typename... Values, - std::size_t Boundary = argument_count> + template typename std::enable_if<(sizeof...(Values) == Boundary), void>::type run( std::list &pathParameter, - const HttpRequestPtr &req, std::function &&callback, + const HttpRequestPtr &req, + std::function &&callback, Values &&... values) { callFunction(req, std::move(callback), std::move(values)...); @@ -159,9 +167,8 @@ private: template - typename std::enable_if::type callFunction( - const HttpRequestPtr &req, std::function &&callback, - Values &&... values) + typename std::enable_if::type + callFunction(const HttpRequestPtr &req, std::function &&callback, Values &&... values) { static auto &obj = getControllerObj(); (obj.*_func)(req, std::move(callback), std::move(values)...); @@ -169,22 +176,20 @@ private: template - typename std::enable_if::type callFunction( - const HttpRequestPtr &req, std::function &&callback, - Values &&... values) + typename std::enable_if::type + callFunction(const HttpRequestPtr &req, std::function &&callback, Values &&... values) { static auto objPtr = DrClassMap::getSingleInstance(); (*objPtr.*_func)(req, std::move(callback), std::move(values)...); }; - template - typename std::enable_if::type callFunction( - const HttpRequestPtr &req, std::function &&callback, - Values &&... values) + template + typename std::enable_if::type callFunction(const HttpRequestPtr &req, + std::function &&callback, + Values &&... values) { _func(req, std::move(callback), std::move(values)...); }; }; -} // namespace internal -} // namespace drogon +} // namespace internal +} // namespace drogon diff --git a/lib/inc/drogon/HttpClient.h b/lib/inc/drogon/HttpClient.h index 659ea150..943418e7 100644 --- a/lib/inc/drogon/HttpClient.h +++ b/lib/inc/drogon/HttpClient.h @@ -1,7 +1,7 @@ /** * * HttpClient.h - * + * * An Tao * * Copyright 2018, An Tao. All rights reserved. @@ -17,13 +17,12 @@ #include #include #include -#include -#include #include #include +#include +#include namespace drogon { - class HttpClient; typedef std::shared_ptr HttpClientPtr; @@ -31,16 +30,19 @@ typedef std::function HttpReqCallback; /// Asynchronous http client /** - * HttpClient implementation object uses the HttpAppFramework's event loop by default, + * HttpClient implementation object uses the HttpAppFramework's event loop by + * default, * so you should call app().run() to make the client work. - * Each HttpClient object establishes a persistent connection with the server. - * If the connection is broken, the client attempts to reconnect + * Each HttpClient object establishes a persistent connection with the server. + * If the connection is broken, the client attempts to reconnect * when calling the sendRequest method. - * - * Using the static mathod newHttpClient(...) to get shared_ptr of the object implementing - * the class, the shared_ptr is retained in the framework until all response callbacks + * + * Using the static mathod newHttpClient(...) to get shared_ptr of the object + * implementing + * the class, the shared_ptr is retained in the framework until all response + * callbacks * are invoked without fear of accidental deconstruction. - * + * * TODO:SSL server verification */ class HttpClient : public trantor::NonCopyable @@ -48,28 +50,30 @@ class HttpClient : public trantor::NonCopyable public: /// Send a request asynchronously to the server /** - * The response from the http server is obtained + * The response from the http server is obtained * in the callback function. */ virtual void sendRequest(const HttpRequestPtr &req, const HttpReqCallback &callback) = 0; virtual void sendRequest(const HttpRequestPtr &req, HttpReqCallback &&callback) = 0; - /// Set the pipelining depth, which is the number of requests that are not responding. + /// Set the pipelining depth, which is the number of requests that are not + /// responding. /** - * If this method is not called, the default depth value is 0 which means the pipelining is disabled. + * If this method is not called, the default depth value is 0 which means the + * pipelining is disabled. * For details about pipelining, see rfc2616-8.1.2.2 */ virtual void setPipeliningDepth(size_t depth) = 0; /// Use ip and port to connect to server - /** - * If useSSL is set to true, the client + /** + * If useSSL is set to true, the client * connects to the server using https. - * - * If the loop parameter is set to nullptr, the client + * + * If the loop parameter is set to nullptr, the client * uses the HttpAppFramework's event loop, otherwise it * runs in the loop identified by the parameter. - * + * * Note: The @param ip support for both ipv4 and ipv6 address */ static HttpClientPtr newHttpClient(const std::string &ip, @@ -81,33 +85,34 @@ class HttpClient : public trantor::NonCopyable virtual trantor::EventLoop *getLoop() = 0; /// Use hostString to connect to server - /** + /** * Examples for hostString: * https://www.baidu.com * http://www.baidu.com * https://127.0.0.1:8080/ * http://127.0.0.1 * http://[::1]:8080/ //IPv6 address must be enclosed in [], rfc2732 - * - * The @param hostString must be prefixed by 'http://' or 'https://' - * - * If the loop parameter is set to nullptr, the client + * + * The @param hostString must be prefixed by 'http://' or 'https://' + * + * If the loop parameter is set to nullptr, the client * uses the HttpAppFramework's event loop, otherwise it * runs in the loop identified by the parameter. - * + * * NOTE: - * Don't add path and parameters in hostString, the request path - * and parameters should be set in HttpRequestPtr when calling + * Don't add path and parameters in hostString, the request path + * and parameters should be set in HttpRequestPtr when calling * the sendRequest() method. - * + * */ - static HttpClientPtr newHttpClient(const std::string &hostString, - trantor::EventLoop *loop = nullptr); + static HttpClientPtr newHttpClient(const std::string &hostString, trantor::EventLoop *loop = nullptr); - virtual ~HttpClient() {} + virtual ~HttpClient() + { + } protected: HttpClient() = default; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpController.h b/lib/inc/drogon/HttpController.h old mode 100755 new mode 100644 index b2448d4e..428b4228 --- a/lib/inc/drogon/HttpController.h +++ b/lib/inc/drogon/HttpController.h @@ -16,22 +16,20 @@ #include #include -#include -#include -#include #include +#include +#include +#include -/// For more details on the class, see the wiki site (the 'HttpController' section) +/// For more details on the class, see the wiki site (the 'HttpController' +/// section) #define METHOD_LIST_BEGIN \ static void initPathRouting() \ { +#define METHOD_ADD(method, pattern, filters...) registerMethod(&method, pattern, {filters}, true, #method) -#define METHOD_ADD(method, pattern, filters...) \ - registerMethod(&method, pattern, {filters}, true, #method) - -#define ADD_METHOD_TO(method, path_pattern, filters...) \ - registerMethod(&method, path_pattern, {filters}, false, #method) +#define ADD_METHOD_TO(method, path_pattern, filters...) registerMethod(&method, path_pattern, {filters}, false, #method) #define METHOD_LIST_END \ return; \ @@ -39,7 +37,6 @@ namespace drogon { - class HttpControllerBase { }; @@ -51,48 +48,39 @@ class HttpController : public DrObject, public HttpControllerBase static const bool isAutoCreation = AutoCreation; protected: - template - static void registerMethod(FUNCTION &&function, - const std::string &pattern, - const std::vector &filtersAndMethods = std::vector(), - bool classNameInPath = true, - const std::string &handlerName = "") - { - if (classNameInPath) - { - std::string path = "/"; - path.append(HttpController::classTypeName()); - LOG_TRACE << "classname:" << HttpController::classTypeName(); + template + static void registerMethod(FUNCTION &&function, + const std::string &pattern, + const std::vector &filtersAndMethods = std::vector(), + bool classNameInPath = true, + const std::string &handlerName = "") + { + if (classNameInPath) + { + std::string path = "/"; + path.append(HttpController::classTypeName()); + LOG_TRACE << "classname:" << HttpController::classTypeName(); - //transform(path.begin(), path.end(), path.begin(), tolower); - std::string::size_type pos; - while ((pos = path.find("::")) != std::string::npos) - { - path.replace(pos, 2, "/"); - } - if (pattern.empty() || pattern[0] == '/') - app().registerHandler(path + pattern, - std::forward(function), - filtersAndMethods, - handlerName); - else - app().registerHandler(path + "/" + pattern, - std::forward(function), - filtersAndMethods, - handlerName); - } - else - { - std::string path = pattern; - if (path.empty() || path[0] != '/') - { - path = "/" + path; - } - app().registerHandler(path, - std::forward(function), - filtersAndMethods, - handlerName); - } + // transform(path.begin(), path.end(), path.begin(), tolower); + std::string::size_type pos; + while ((pos = path.find("::")) != std::string::npos) + { + path.replace(pos, 2, "/"); + } + if (pattern.empty() || pattern[0] == '/') + app().registerHandler(path + pattern, std::forward(function), filtersAndMethods, handlerName); + else + app().registerHandler(path + "/" + pattern, std::forward(function), filtersAndMethods, handlerName); + } + else + { + std::string path = pattern; + if (path.empty() || path[0] != '/') + { + path = "/" + path; + } + app().registerHandler(path, std::forward(function), filtersAndMethods, handlerName); + } } private: @@ -105,7 +93,7 @@ class HttpController : public DrObject, public HttpControllerBase T::initPathRouting(); } }; - //use static value to register controller method in framework before main(); + // use static value to register controller method in framework before main(); static methodRegister _register; virtual void *touch() { @@ -114,4 +102,4 @@ class HttpController : public DrObject, public HttpControllerBase }; template typename HttpController::methodRegister HttpController::_register; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpFilter.h b/lib/inc/drogon/HttpFilter.h old mode 100755 new mode 100644 index f48ca0e5..d41688e9 --- a/lib/inc/drogon/HttpFilter.h +++ b/lib/inc/drogon/HttpFilter.h @@ -2,7 +2,7 @@ * * HttpFilter.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,8 +15,8 @@ #pragma once #include -#include #include +#include #include /// For more details on the class, see the wiki site (the 'Filter' section) @@ -27,26 +27,31 @@ typedef std::function FilterCallback; typedef std::function FilterChainCallback; class HttpFilterBase : public virtual DrObjectBase { -public: + public: /// This virtual function should be overrided in subclasses. /** - * This method is an asynchronous interface, user should return the result via 'FilterCallback' + * This method is an asynchronous interface, user should return the result via + * 'FilterCallback' * or 'FilterChainCallback'. - * If @param fcb is called, the response object is send to the client by the callback, - * and doFilter methods of next filters and the handler registed on the path are not called anymore. - * If @param fccb is called, the next filter's doFilter method or the handler + * If @param fcb is called, the response object is send to the client by the + * callback, + * and doFilter methods of next filters and the handler registed on the path + * are not called anymore. + * If @param fccb is called, the next filter's doFilter method or the handler * registered on the path is called. */ - virtual void doFilter(const HttpRequestPtr &req, - FilterCallback &&fcb, - FilterChainCallback &&fccb) = 0; - virtual ~HttpFilterBase() {} + virtual void doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) = 0; + virtual ~HttpFilterBase() + { + } }; template class HttpFilter : public DrObject, public HttpFilterBase { -public: + public: static const bool isAutoCreation = AutoCreation; - virtual ~HttpFilter() {} + virtual ~HttpFilter() + { + } }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h old mode 100755 new mode 100644 index 5f8c2d62..c1278d16 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -17,23 +17,22 @@ #include #include #include +#include +#include +#include #include #include -#include #include -#include -#include namespace drogon { - class HttpRequest; typedef std::shared_ptr HttpRequestPtr; /// Abstract class for webapp developer to get or set the Http request; class HttpRequest { -public: + public: enum Version { kUnknown = 0, @@ -42,11 +41,17 @@ public: }; /// Return the method string of the request, such as GET, POST, etc. virtual const char *methodString() const = 0; - const char *getMethodString() const { return methodString(); } + const char *getMethodString() const + { + return methodString(); + } /// Return the enum type method of the request. virtual HttpMethod method() const = 0; - HttpMethod getMethod() const { return method(); } + HttpMethod getMethod() const + { + return method(); + } /// Get the header string identified by the @param field virtual const std::string &getHeader(const std::string &field, const std::string &defaultVal = std::string()) const = 0; @@ -60,31 +65,52 @@ public: /// Get all headers of the request virtual const std::unordered_map &headers() const = 0; - const std::unordered_map &getHeaders() const { return headers(); } + const std::unordered_map &getHeaders() const + { + return headers(); + } /// Get all cookies of the request virtual const std::unordered_map &cookies() const = 0; - const std::unordered_map &getCookies() const { return cookies(); } + const std::unordered_map &getCookies() const + { + return cookies(); + } /// Get the query string of the request. /** - * If the http method is GET, the query string is the substring after the '?' in the URL string. - * If the http method is POST, the query string is the content(body) string of the HTTP request. + * If the http method is GET, the query string is the substring after the '?' + * in the URL string. + * If the http method is POST, the query string is the content(body) string of + * the HTTP request. */ virtual const std::string &query() const = 0; - const std::string &getQuery() const { return query(); } + const std::string &getQuery() const + { + return query(); + } - /// Get the content string of the request, which is the body part of the request. + /// Get the content string of the request, which is the body part of the + /// request. virtual const std::string &body() const = 0; - const std::string &getBody() const { return body(); } + const std::string &getBody() const + { + return body(); + } /// Get the path of the request. virtual const std::string &path() const = 0; - const std::string &getPath() const { return path(); } + const std::string &getPath() const + { + return path(); + } /// Get the matched path pattern after routing virtual const string_view &matchedPathPattern() const = 0; - const string_view &getMatchedPathPattern() const { return matchedPathPattern(); } + const string_view &getMatchedPathPattern() const + { + return matchedPathPattern(); + } /// Return the enum type version of the request. /** @@ -92,42 +118,66 @@ public: * kHttp11 means Http verison is 1.1 */ virtual Version version() const = 0; - Version getVersion() const { return version(); } + Version getVersion() const + { + return version(); + } /// Get the session to which the request belongs. virtual SessionPtr session() const = 0; - SessionPtr getSession() const { return session(); } + SessionPtr getSession() const + { + return session(); + } /// Get parameters of the request. virtual const std::unordered_map ¶meters() const = 0; - const std::unordered_map &getParameters() const { return parameters(); } + const std::unordered_map &getParameters() const + { + return parameters(); + } /// Get a parameter identified by the @param key virtual const std::string &getParameter(const std::string &key, const std::string &defaultVal = std::string()) const = 0; /// Return the remote IP address and port virtual const trantor::InetAddress &peerAddr() const = 0; - const trantor::InetAddress &getPeerAddr() const { return peerAddr(); } + const trantor::InetAddress &getPeerAddr() const + { + return peerAddr(); + } /// Return the local IP address and port virtual const trantor::InetAddress &localAddr() const = 0; - const trantor::InetAddress &getLocalAddr() const { return localAddr(); } + const trantor::InetAddress &getLocalAddr() const + { + return localAddr(); + } /// Return the creation timestamp set by the framework. virtual const trantor::Date &creationDate() const = 0; - const trantor::Date &getCreationDate() const { return creationDate(); } + const trantor::Date &getCreationDate() const + { + return creationDate(); + } /// Get the Json object of the request /** - * The content type of the request must be 'application/json', otherwise + * The content type of the request must be 'application/json', otherwise * the method returns an empty object. */ virtual const std::shared_ptr jsonObject() const = 0; - const std::shared_ptr getJsonObject() const { return jsonObject(); } + const std::shared_ptr getJsonObject() const + { + return jsonObject(); + } /// Get the content type virtual ContentType contentType() const = 0; - ContentType getContentType() const { return contentType(); } + ContentType getContentType() const + { + return contentType(); + } /// Set the Http method virtual void setMethod(const HttpMethod method) = 0; @@ -141,7 +191,8 @@ public: /// Set or get the content type virtual void setContentTypeCode(const ContentType type) = 0; - /// The following methods are a series of factory methods that help users create request objects. + /// The following methods are a series of factory methods that help users + /// create request objects. /// Create a normal request with http method Get and version Http1.1. static HttpRequestPtr newHttpRequest(); @@ -149,7 +200,8 @@ public: /// Create a http request with: /// Method: Get /// Version: Http1.1 - /// Content type: application/json, the @param data is serialized into the content of the request. + /// Content type: application/json, the @param data is serialized into the + /// content of the request. static HttpRequestPtr newHttpJsonRequest(const Json::Value &data); /// Create a http request with: @@ -162,10 +214,13 @@ public: /// Method: Post /// Version: Http1.1 /// Content type: multipart/form-data - /// The @param files represents pload files which are transferred to the server via the multipart/form-data format + /// The @param files represents pload files which are transferred to the + /// server via the multipart/form-data format static HttpRequestPtr newFileUploadRequest(const std::vector &files); - virtual ~HttpRequest() {} + virtual ~HttpRequest() + { + } }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h old mode 100755 new mode 100644 index f7f6358d..60f80d9f --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -1,7 +1,7 @@ /** * HttpResponse.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,12 +13,12 @@ #pragma once -#include #include #include +#include #include -#include #include +#include namespace drogon { @@ -27,42 +27,55 @@ class HttpResponse; typedef std::shared_ptr HttpResponsePtr; class HttpResponse { -public: + public: HttpResponse() { } /// Get the status code such as 200, 404 virtual HttpStatusCode statusCode() const = 0; - HttpStatusCode getStatusCode() const { return statusCode(); } + HttpStatusCode getStatusCode() const + { + return statusCode(); + } /// Set the status code of the response. virtual void setStatusCode(HttpStatusCode code) = 0; /// Get the creation timestamp of the response. virtual const trantor::Date &creationDate() const = 0; - const trantor::Date &getCreationDate() const { return creationDate(); } + const trantor::Date &getCreationDate() const + { + return creationDate(); + } /// Set the http version, http1.0 or http1.1 virtual void setVersion(const Version v) = 0; - /// If @param on is false, the connection keeps alive on the condition that the client request has a - // 'keep-alive' head, otherwise it is closed immediately after sending the last byte of the response. + /// If @param on is false, the connection keeps alive on the condition that + /// the client request has a + // 'keep-alive' head, otherwise it is closed immediately after sending the + // last byte of the response. // It's false by default when the response is created. virtual void setCloseConnection(bool on) = 0; /// Get the status set by the setCloseConnetion() method. virtual bool ifCloseConnection() const = 0; - /// Set the reponse content type, such as text/html, text/plaint, image/png and so on. If the content type + /// 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; + /// virtual void setContentTypeCodeAndCharacterSet(ContentType type, const + /// std::string &charSet = "utf-8") = 0; /// Get the response content type. virtual ContentType contentType() const = 0; - ContentType getContentType() const { return contentType(); } + ContentType getContentType() const + { + return contentType(); + } /// Get the header string identified by the @param key. /// If there is no the header, the @param defaultVal is retured. @@ -76,7 +89,10 @@ public: /// Get all headers of the response virtual const std::unordered_map &headers() const = 0; - const std::unordered_map &getHeaders() const { return headers(); } + const std::unordered_map &getHeaders() const + { + return headers(); + } /// Add a header. virtual void addHeader(const std::string &key, const std::string &value) = 0; @@ -92,20 +108,30 @@ public: /// Get all cookies. virtual const std::unordered_map &cookies() const = 0; - const std::unordered_map &getCookies() const { return cookies(); } + const std::unordered_map &getCookies() const + { + return cookies(); + } /// 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 + /// 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; /// Get the response body. virtual const std::string &body() const = 0; - const std::string &getBody() const { return body(); } + const std::string &getBody() const + { + return body(); + } virtual std::string &body() = 0; - std::string &getBody() { return body(); } + std::string &getBody() + { + return body(); + } /// Reset the reponse object to its initial state virtual void clear() = 0; @@ -116,36 +142,53 @@ public: /// Get the expiration time of the response. virtual ssize_t expiredTime() const = 0; - ssize_t getExpiredTime() const { return expiredTime(); } + ssize_t getExpiredTime() const + { + return expiredTime(); + } /// Get the json object from the server response. /// If the response is not in json format, then a empty shared_ptr is retured. virtual const std::shared_ptr jsonObject() const = 0; - const std::shared_ptr getJsonObject() const { return jsonObject(); } + const std::shared_ptr getJsonObject() const + { + return jsonObject(); + } - /// The following methods are a series of factory methods that help users create response objects. + /// 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. + /// 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. + /// 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. + /// 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 location. + /// Create a response that returns a 302 Found page, redirecting to another + /// page located in the @param location. static HttpResponsePtr newRedirectionResponse(const std::string &location); /// 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 is set by drogon based on the file extension. + * 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 is set by drogon based on + * the file extension. */ - static HttpResponsePtr newFileResponse(const std::string &fullPath, const std::string &attachmentFileName = "", ContentType type = CT_NONE); + static HttpResponsePtr newFileResponse(const std::string &fullPath, + const std::string &attachmentFileName = "", + ContentType type = CT_NONE); - virtual ~HttpResponse() {} + virtual ~HttpResponse() + { + } }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpSimpleController.h b/lib/inc/drogon/HttpSimpleController.h old mode 100755 new mode 100644 index 01cc8f15..1731afd6 --- a/lib/inc/drogon/HttpSimpleController.h +++ b/lib/inc/drogon/HttpSimpleController.h @@ -2,7 +2,7 @@ * * HttpSimpleController.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -16,26 +16,25 @@ #include #include -#include -#include -#include #include +#include +#include +#include #define PATH_LIST_BEGIN \ static void initPathRouting() \ { - #define PATH_ADD(path, filters...) __registerSelf(path, {filters}); -#define PATH_LIST_END \ - } +#define PATH_LIST_END } namespace drogon { - class HttpSimpleControllerBase : public virtual DrObjectBase { public: virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, std::function &&callback) = 0; - virtual ~HttpSimpleControllerBase() {} + virtual ~HttpSimpleControllerBase() + { + } }; template @@ -43,14 +42,19 @@ class HttpSimpleController : public DrObject, public HttpSimpleControllerBase { public: static const bool isAutoCreation = AutoCreation; - virtual ~HttpSimpleController() {} + virtual ~HttpSimpleController() + { + } protected: - HttpSimpleController() {} + HttpSimpleController() + { + } static void __registerSelf(const std::string &path, const std::vector &filtersAndMethods) { LOG_TRACE << "register simple controller(" << HttpSimpleController::classTypeName() << ") on path:" << path; - HttpAppFramework::instance().registerHttpSimpleController(path, HttpSimpleController::classTypeName(), filtersAndMethods); + HttpAppFramework::instance().registerHttpSimpleController( + path, HttpSimpleController::classTypeName(), filtersAndMethods); } private: @@ -75,4 +79,4 @@ class HttpSimpleController : public DrObject, public HttpSimpleControllerBase template typename HttpSimpleController::pathRegister HttpSimpleController::_register; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpTypes.h b/lib/inc/drogon/HttpTypes.h index 88a71747..0e101270 100644 --- a/lib/inc/drogon/HttpTypes.h +++ b/lib/inc/drogon/HttpTypes.h @@ -1,7 +1,7 @@ /** * HttpTypes.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,16 +12,15 @@ */ #pragma once -#include #include +#include #include namespace drogon { - enum HttpStatusCode { - //rfc2616-6.1.1 + // rfc2616-6.1.1 kUnknown = 0, k100Continue = 100, k101SwitchingProtocols = 101, @@ -119,4 +118,4 @@ enum class ReqResult Timeout }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpViewBase.h b/lib/inc/drogon/HttpViewBase.h old mode 100755 new mode 100644 index 7a4be478..c1b30462 --- a/lib/inc/drogon/HttpViewBase.h +++ b/lib/inc/drogon/HttpViewBase.h @@ -2,7 +2,7 @@ * * HttpViewBase.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,14 +15,13 @@ #pragma once #include -#include #include +#include #include #include namespace drogon { - class HttpViewBase : virtual public DrObjectBase { public: @@ -35,4 +34,4 @@ class HttpViewBase : virtual public DrObjectBase virtual HttpResponsePtr genHttpResponse(const HttpViewData &) = 0; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/HttpViewData.h b/lib/inc/drogon/HttpViewData.h old mode 100755 new mode 100644 index 5d326144..66cd5a22 --- a/lib/inc/drogon/HttpViewData.h +++ b/lib/inc/drogon/HttpViewData.h @@ -14,24 +14,24 @@ #pragma once -#include #include +#include #include -#include -#include #include -#include #include +#include +#include +#include namespace drogon { - /// This class represents the data set displayed in views. class HttpViewData { public: - /// The function template is used to get an item in the data set by the @param key. + /// The function template is used to get an item in the data set by the @param + /// key. template const T &get(const std::string &key, T &&nullVal = T()) const { @@ -60,7 +60,8 @@ class HttpViewData _viewData[key] = obj; } - /// Insert an item identified by the @param key into the data set; The item is converted to a string. + /// Insert an item identified by the @param key into the data set; The item is + /// converted to a string. template void insertAsString(const std::string &key, T &&val) { @@ -70,8 +71,7 @@ class HttpViewData } /// Insert a formated string identified by the @param key. - void insertFormattedString(const std::string &key, - const char *format, ...) + void insertFormattedString(const std::string &key, const char *format, ...) { std::string strBuffer; strBuffer.resize(128); @@ -133,4 +133,4 @@ class HttpViewData mutable ViewDataMap _viewData; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/IntranetIpFilter.h b/lib/inc/drogon/IntranetIpFilter.h index bec38f5d..6c639df6 100644 --- a/lib/inc/drogon/IntranetIpFilter.h +++ b/lib/inc/drogon/IntranetIpFilter.h @@ -21,9 +21,9 @@ namespace drogon class IntranetIpFilter : public HttpFilter { public: - IntranetIpFilter() {} - virtual void doFilter(const HttpRequestPtr &req, - FilterCallback &&fcb, - FilterChainCallback &&fccb) override; + IntranetIpFilter() + { + } + virtual void doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) override; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/LocalHostFilter.h b/lib/inc/drogon/LocalHostFilter.h index 8d7f7107..fd46b48c 100644 --- a/lib/inc/drogon/LocalHostFilter.h +++ b/lib/inc/drogon/LocalHostFilter.h @@ -2,7 +2,7 @@ * * LocalHostFilter.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -21,9 +21,9 @@ namespace drogon class LocalHostFilter : public HttpFilter { public: - LocalHostFilter() {} - virtual void doFilter(const HttpRequestPtr &req, - FilterCallback &&fcb, - FilterChainCallback &&fccb) override; + LocalHostFilter() + { + } + virtual void doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) override; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/MultiPart.h b/lib/inc/drogon/MultiPart.h old mode 100755 new mode 100644 index 5bfe3736..b4b847a6 --- a/lib/inc/drogon/MultiPart.h +++ b/lib/inc/drogon/MultiPart.h @@ -16,51 +16,69 @@ #include #include -#include #include +#include #include namespace drogon { - class HttpFile { public: /// Return the file name; - const std::string &getFileName() const { return _fileName; }; + const std::string &getFileName() const + { + return _fileName; + }; /// Set the file name - void setFileName(const std::string &filename) { _fileName = filename; }; + void setFileName(const std::string &filename) + { + _fileName = filename; + }; /// Set the contents of the file, usually called by the FileUpload parser. - void setFile(const std::string &file) { _fileContent = file; }; - void setFile(std::string &&file) { _fileContent = std::move(file); } + void setFile(const std::string &file) + { + _fileContent = file; + }; + void setFile(std::string &&file) + { + _fileContent = std::move(file); + } /// Save the file to the file system. /** - * The folder saving the file is app().getUploadPath(). - * The full path is app().getUploadPath()+"/"+this->getFileName() + * The folder saving the file is app().getUploadPath(). + * The full path is app().getUploadPath()+"/"+this->getFileName() */ int save() const; /// Save the file to @param path /** - * If the @param path is prefixed with "/", "./" or "../", or the @param path is - * "." or "..", the full path is @param path+"/"+this->getFileName(), otherwise - * the file is saved as app().getUploadPath()+"/"+@param path+"/"+this->getFileName() + * If the @param path is prefixed with "/", "./" or "../", or the @param path + * is + * "." or "..", the full path is @param path+"/"+this->getFileName(), + * otherwise + * the file is saved as app().getUploadPath()+"/"+@param + * path+"/"+this->getFileName() */ int save(const std::string &path) const; /// Save the file to file system with a new name /** - * If the @param filename isn't prefixed with "/", "./" or "../", the full path is + * If the @param filename isn't prefixed with "/", "./" or "../", the full + * path is * app().getUploadPath()+"/"+@param filename, otherwise * the file is saved as @param filename */ int saveAs(const std::string &filename) const; /// Return the file length. - int64_t fileLength() const { return _fileContent.length(); }; + int64_t fileLength() const + { + return _fileContent.length(); + }; /// Return the md5 string of the file const std::string getMd5() const; @@ -71,7 +89,8 @@ class HttpFile std::string _fileContent; }; -/// A parser class which help the user to get the files and the parameters in the multipart format request. +/// A parser class which help the user to get the files and the parameters in +/// the multipart format request. class MultiPartParser { public: @@ -80,13 +99,14 @@ class MultiPartParser /// Get files, This method should be called after calling the parse() method. const std::vector &getFiles(); - /// Get parameters, This method should be called after calling the parse () method. + /// Get parameters, This method should be called after calling the parse () + /// method. const std::map &getParameters() const; /// 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. + /// Parse the http response stream to get files and parameters. /// int parse(const HttpResponsePtr &req); protected: @@ -96,6 +116,6 @@ class MultiPartParser int parseEntity(const char *begin, const char *end); }; -typedef MultiPartParser FileUpload; /// In order to be compatible with old interfaces +typedef MultiPartParser FileUpload; /// In order to be compatible with old interfaces -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/NotFound.h b/lib/inc/drogon/NotFound.h old mode 100755 new mode 100644 index be7aa0be..b9b4bbbb --- a/lib/inc/drogon/NotFound.h +++ b/lib/inc/drogon/NotFound.h @@ -1,10 +1,10 @@ -//this file is generated by program automatically,don't modify it! +// this file is generated by program automatically,don't modify it! /** * * NotFound.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -19,7 +19,7 @@ class NotFound : public drogon::DrTemplate { public: - NotFound(){}; - virtual ~NotFound(){}; - virtual std::string genText(const drogon::HttpViewData &) override; + NotFound(){}; + virtual ~NotFound(){}; + virtual std::string genText(const drogon::HttpViewData &) override; }; diff --git a/lib/inc/drogon/Session.h b/lib/inc/drogon/Session.h old mode 100755 new mode 100644 index 1ea10dff..4cb4df80 --- a/lib/inc/drogon/Session.h +++ b/lib/inc/drogon/Session.h @@ -2,7 +2,7 @@ * * Session.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,15 +14,14 @@ #pragma once -#include +#include #include +#include #include #include -#include namespace drogon { - class Session { public: @@ -80,4 +79,4 @@ class Session typedef std::shared_ptr SessionPtr; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/UploadFile.h b/lib/inc/drogon/UploadFile.h index 5b765903..b6b0eefa 100644 --- a/lib/inc/drogon/UploadFile.h +++ b/lib/inc/drogon/UploadFile.h @@ -20,16 +20,17 @@ namespace drogon class UploadFile { public: - /// This class represents an upload file which will be transferred to the server via the multipart/form-data format + /// This class represents an upload file which will be transferred to the + /// server via the multipart/form-data format /** * @param filePath: The file location on local host, including file name. - * @param fileName: The file name provided to the server. If it is empty by default, the file name in the @param filePath + * @param fileName: The file name provided to the server. If it is empty by + * default, the file name in the @param filePath * is provided to the server. * @param itemName: The item name on the browser form. */ explicit UploadFile(const std::string &filePath, const std::string &fileName = "", const std::string &itemName = "file") - : _path(filePath), - _itemName(itemName) + : _path(filePath), _itemName(itemName) { if (!fileName.empty()) { @@ -48,13 +49,22 @@ class UploadFile } } } - const std::string &path() const { return _path; } - const std::string &fileName() const { return _fileName; } - const std::string &itemName() const { return _itemName; } + const std::string &path() const + { + return _path; + } + const std::string &fileName() const + { + return _fileName; + } + const std::string &itemName() const + { + return _itemName; + } private: std::string _path; std::string _fileName; std::string _itemName; }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/inc/drogon/WebSocketClient.h b/lib/inc/drogon/WebSocketClient.h index 1ae65219..f40715dd 100644 --- a/lib/inc/drogon/WebSocketClient.h +++ b/lib/inc/drogon/WebSocketClient.h @@ -17,14 +17,13 @@ #include #include #include -#include -#include #include #include +#include +#include namespace drogon { - class WebSocketClient; typedef std::shared_ptr WebSocketClientPtr; typedef std::function WebSocketRequestCallback; @@ -36,10 +35,14 @@ class WebSocketClient /// Get the WebSocket connection that is typically used to send messages. virtual WebSocketConnectionPtr getConnection() = 0; - /// Set messages handler. When a message is recieved from the server, the @param callback is called. - virtual void setMessageHandler(const std::function &callback) = 0; + /// Set messages handler. When a message is recieved from the server, the + /// @param callback is called. + virtual void setMessageHandler( + const std::function + &callback) = 0; - /// Set the connection handler. When the connection is established or closed, the @param callback is called with a bool + /// Set the connection handler. When the connection is established or closed, + /// the @param callback is called with a bool /// parameter. virtual void setConnectionClosedHandler(const std::function &callback) = 0; @@ -50,14 +53,14 @@ class WebSocketClient virtual trantor::EventLoop *getLoop() = 0; /// Use ip and port to connect to server - /** - * If useSSL is set to true, the client + /** + * If useSSL is set to true, the client * connects to the server using SSL. - * - * If the loop parameter is set to nullptr, the client + * + * If the loop parameter is set to nullptr, the client * uses the HttpAppFramework's event loop, otherwise it * runs in the loop identified by the parameter. - * + * * Note: The @param ip support for both ipv4 and ipv6 address */ static WebSocketClientPtr newWebSocketClient(const std::string &ip, @@ -66,30 +69,31 @@ class WebSocketClient trantor::EventLoop *loop = nullptr); /// Use hostString to connect to server - /** + /** * Examples for hostString: * wss://www.google.com * ws://www.google.com * wss://127.0.0.1:8080/ * ws://127.0.0.1 - * - * The @param hostString must be prefixed by 'ws://' or 'wss://' + * + * The @param hostString must be prefixed by 'ws://' or 'wss://' * and doesn't support for ipv6 address if the host is in ip format - * - * If the @param loop is set to nullptr, the client + * + * If the @param loop is set to nullptr, the client * uses the HttpAppFramework's main event loop, otherwise it * runs in the loop identified by the parameter. - * + * * NOTE: - * Don't add path and parameters in hostString, the request path - * and parameters should be set in HttpRequestPtr when calling + * Don't add path and parameters in hostString, the request path + * and parameters should be set in HttpRequestPtr when calling * the connectToServer() method. - * + * */ - static WebSocketClientPtr newWebSocketClient(const std::string &hostString, - trantor::EventLoop *loop = nullptr); + static WebSocketClientPtr newWebSocketClient(const std::string &hostString, trantor::EventLoop *loop = nullptr); - virtual ~WebSocketClient() {} + virtual ~WebSocketClient() + { + } }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/inc/drogon/WebSocketConnection.h b/lib/inc/drogon/WebSocketConnection.h index ddf68d41..488785d2 100644 --- a/lib/inc/drogon/WebSocketConnection.h +++ b/lib/inc/drogon/WebSocketConnection.h @@ -2,7 +2,7 @@ * * WebSocketConnection.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,13 +15,12 @@ #pragma once #include -#include -#include -#include #include +#include +#include +#include namespace drogon { - enum class WebSocketMessageType { Text, @@ -46,8 +45,8 @@ class WebSocketConnection virtual bool connected() const = 0; virtual bool disconnected() const = 0; - virtual void shutdown() = 0; //close write - virtual void forceClose() = 0; //close + virtual void shutdown() = 0; // close write + virtual void forceClose() = 0; // close virtual void setContext(const any &context) = 0; virtual const any &getContext() const = 0; @@ -56,9 +55,10 @@ class WebSocketConnection /// Set the heartbeat(ping) message sent to the server. /** * NOTE: - * Both the server and the client in Drogon automatically send the pong message after receiving the ping message. + * Both the server and the client in Drogon automatically send the pong + * message after receiving the ping message. */ virtual void setPingMessage(const std::string &message, const std::chrono::duration &interval) = 0; }; typedef std::shared_ptr WebSocketConnectionPtr; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/WebSocketController.h b/lib/inc/drogon/WebSocketController.h index cf4ffad1..a4568dc4 100644 --- a/lib/inc/drogon/WebSocketController.h +++ b/lib/inc/drogon/WebSocketController.h @@ -2,7 +2,7 @@ * * WebSocketController.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -17,19 +17,18 @@ #include #include #include -#include -#include -#include -#include #include #include +#include +#include +#include +#include #define WS_PATH_LIST_BEGIN \ static std::vector>> __paths() \ { \ std::vector>> vet; -#define WS_PATH_ADD(path, filters...) \ - vet.push_back({path, {filters}}) +#define WS_PATH_ADD(path, filters...) vet.push_back({path, {filters}}) #define WS_PATH_LIST_END \ return vet; \ @@ -37,23 +36,21 @@ namespace drogon { - class WebSocketControllerBase : public virtual DrObjectBase { public: - //This function is called when a new message is received - virtual void handleNewMessage(const WebSocketConnectionPtr &, - std::string &&, - const WebSocketMessageType &) = 0; + // This function is called when a new message is received + virtual void handleNewMessage(const WebSocketConnectionPtr &, std::string &&, const WebSocketMessageType &) = 0; - //This function is called after a new connection of WebSocket is established. - virtual void handleNewConnection(const HttpRequestPtr &, - const WebSocketConnectionPtr &) = 0; + // This function is called after a new connection of WebSocket is established. + virtual void handleNewConnection(const HttpRequestPtr &, const WebSocketConnectionPtr &) = 0; - //This function is called after a WebSocket connection is closed + // This function is called after a WebSocket connection is closed virtual void handleConnectionClosed(const WebSocketConnectionPtr &) = 0; - virtual ~WebSocketControllerBase() {} + virtual ~WebSocketControllerBase() + { + } }; typedef std::shared_ptr WebSocketControllerBasePtr; @@ -63,21 +60,25 @@ class WebSocketController : public DrObject, public WebSocketControllerBase { public: static const bool isAutoCreation = AutoCreation; - virtual ~WebSocketController() {} + virtual ~WebSocketController() + { + } static void initPathRouting() { auto vPaths = T::__paths(); for (auto const &path : vPaths) { - LOG_TRACE << "register websocket controller (" << WebSocketController::classTypeName() << ") on path:" << path.first; - HttpAppFramework::instance().registerWebSocketController(path.first, - WebSocketController::classTypeName(), - path.second); + LOG_TRACE << "register websocket controller (" << WebSocketController::classTypeName() + << ") on path:" << path.first; + HttpAppFramework::instance().registerWebSocketController( + path.first, WebSocketController::classTypeName(), path.second); } } protected: - WebSocketController() {} + WebSocketController() + { + } private: class pathRegister @@ -101,4 +102,4 @@ class WebSocketController : public DrObject, public WebSocketControllerBase template typename WebSocketController::pathRegister WebSocketController::_register; -} // namespace drogon +} // namespace drogon diff --git a/lib/inc/drogon/drogon.h b/lib/inc/drogon/drogon.h index fc55cc57..343ac305 100644 --- a/lib/inc/drogon/drogon.h +++ b/lib/inc/drogon/drogon.h @@ -2,7 +2,7 @@ * * drogon.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -16,14 +16,14 @@ #include -#include #include -#include #include +#include +#include +#include #include +#include #include #include -#include -#include #include \ No newline at end of file diff --git a/lib/inc/drogon/plugins/Plugin.h b/lib/inc/drogon/plugins/Plugin.h index 77501dbf..258de470 100644 --- a/lib/inc/drogon/plugins/Plugin.h +++ b/lib/inc/drogon/plugins/Plugin.h @@ -1,7 +1,7 @@ /** * Plugin.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,14 +14,13 @@ #pragma once #include -#include -#include #include #include +#include +#include namespace drogon { - enum class PluginState { None, @@ -34,7 +33,10 @@ class PluginBase : public virtual DrObjectBase, public trantor::NonCopyable public: /// This method is usually called by drogon. /// It always returns PlugiinState::Initialized if the user calls it. - PluginState stat() const { return _stat; } + PluginState stat() const + { + return _stat; + } /// This method must be called by drogon. void initialize() @@ -53,7 +55,7 @@ class PluginBase : public virtual DrObjectBase, public trantor::NonCopyable } else if (_stat == PluginState::Initialized) { - //Do nothing; + // Do nothing; } else { @@ -70,17 +72,30 @@ class PluginBase : public virtual DrObjectBase, public trantor::NonCopyable /// It must be implemented by the user. virtual void shutdown() = 0; - virtual ~PluginBase() {} + virtual ~PluginBase() + { + } protected: - PluginBase() {} + PluginBase() + { + } private: PluginState _stat = PluginState::None; friend class PluginsManager; - void setConfig(const Json::Value &config) { _config = config; } - void addDependency(PluginBase *dp) { _dependencies.push_back(dp); } - void setInitializedCallback(const std::function &cb) { _initializedCallback = cb; } + void setConfig(const Json::Value &config) + { + _config = config; + } + void addDependency(PluginBase *dp) + { + _dependencies.push_back(dp); + } + void setInitializedCallback(const std::function &cb) + { + _initializedCallback = cb; + } Json::Value _config; std::vector _dependencies; std::function _initializedCallback; @@ -91,8 +106,14 @@ struct IsPlugin { typedef typename std::remove_cv::type>::type TYPE; - static int test(void *p) { return 0; } - static char test(PluginBase *p) { return 0; } + static int test(void *p) + { + return 0; + } + static char test(PluginBase *p) + { + return 0; + } static constexpr bool value = (sizeof(test((TYPE *)nullptr)) == sizeof(int)); }; @@ -100,10 +121,14 @@ template class Plugin : public PluginBase, public DrObject { public: - virtual ~Plugin() {} + virtual ~Plugin() + { + } protected: - Plugin() {} + Plugin() + { + } }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/inc/drogon/utils/ClassTraits.h b/lib/inc/drogon/utils/ClassTraits.h index e499e86b..5a1dcf22 100644 --- a/lib/inc/drogon/utils/ClassTraits.h +++ b/lib/inc/drogon/utils/ClassTraits.h @@ -18,17 +18,22 @@ namespace drogon { namespace internal { - /// This template is used to check whether S is a subclass of B. template struct IsSubClass { typedef typename std::remove_cv::type>::type SubType; typedef typename std::remove_cv::type>::type BaseType; - static char test(void *) { return 0; } - static int test(BaseType *) { return 0; } + static char test(void *) + { + return 0; + } + static int test(BaseType *) + { + return 0; + } static const bool value = (sizeof(test((SubType *)nullptr)) == sizeof(int)); }; -} // namespace internal -} // namespace drogon \ No newline at end of file +} // namespace internal +} // namespace drogon \ No newline at end of file diff --git a/lib/inc/drogon/utils/FunctionTraits.h b/lib/inc/drogon/utils/FunctionTraits.h old mode 100755 new mode 100644 index f5d1c9c5..bfcd53b9 --- a/lib/inc/drogon/utils/FunctionTraits.h +++ b/lib/inc/drogon/utils/FunctionTraits.h @@ -16,10 +16,10 @@ #include #include +#include +#include #include #include -#include -#include namespace drogon { @@ -30,14 +30,12 @@ typedef std::shared_ptr HttpResponsePtr; namespace internal { - template struct FunctionTraits; -//functor,lambda,std::function... +// functor,lambda,std::function... template -struct FunctionTraits : public FunctionTraits< - decltype(&std::remove_reference::type::operator())> +struct FunctionTraits : public FunctionTraits::type::operator())> { static const bool isClassFunction = false; static const bool isDrObjectClass = false; @@ -48,63 +46,61 @@ struct FunctionTraits : public FunctionTraits< } }; -//class instance method of const object -template +// class instance method of const object +template struct FunctionTraits : FunctionTraits { static const bool isClassFunction = true; static const bool isDrObjectClass = IsSubClass>::value; typedef ClassType class_type; - static const std::string name() { return std::string("Class Function"); } + static const std::string name() + { + return std::string("Class Function"); + } }; -//class instance method of non-const object -template < - typename ClassType, - typename ReturnType, - typename... Arguments> +// class instance method of non-const object +template struct FunctionTraits : FunctionTraits { static const bool isClassFunction = true; static const bool isDrObjectClass = IsSubClass>::value; typedef ClassType class_type; - static const std::string name() { return std::string("Class Function"); } + static const std::string name() + { + return std::string("Class Function"); + } }; -//normal function for HTTP handling -template < - typename ReturnType, - typename... Arguments> +// normal function for HTTP handling +template struct FunctionTraits< - ReturnType (*)(const HttpRequestPtr &req, std::function &&callback, Arguments...)> : FunctionTraits + ReturnType (*)(const HttpRequestPtr &req, std::function &&callback, Arguments...)> + : FunctionTraits { static const bool isHTTPFunction = true; typedef void class_type; }; -//normal function -template < - typename ReturnType, - typename... Arguments> -struct FunctionTraits< - ReturnType (*)(Arguments...)> +// normal function +template +struct FunctionTraits { typedef ReturnType result_type; template - using argument = typename std::tuple_element< - Index, - std::tuple>::type; + using argument = typename std::tuple_element>::type; static const std::size_t arity = sizeof...(Arguments); typedef void class_type; static const bool isHTTPFunction = false; static const bool isClassFunction = false; static const bool isDrObjectClass = false; - static const std::string name() { return std::string("Normal or Static Function"); } + static const std::string name() + { + return std::string("Normal or Static Function"); + } }; -} // namespace internal -} // namespace drogon +} // namespace internal +} // namespace drogon diff --git a/lib/inc/drogon/utils/Utilities.h b/lib/inc/drogon/utils/Utilities.h old mode 100755 new mode 100644 index d7b84ff4..ed1d1779 --- a/lib/inc/drogon/utils/Utilities.h +++ b/lib/inc/drogon/utils/Utilities.h @@ -15,16 +15,15 @@ #pragma once #include -#include -#include -#include #include +#include +#include +#include namespace drogon { namespace utils { - /// Determine if the string is an integer bool isInteger(const std::string &str); @@ -92,5 +91,5 @@ std::string formattedString(const char *format, ...); */ int createPath(const std::string &path); -} // namespace utils -} // namespace drogon +} // namespace utils +} // namespace drogon diff --git a/lib/src/AOPAdvice.cc b/lib/src/AOPAdvice.cc index 416f8133..fe39b896 100644 --- a/lib/src/AOPAdvice.cc +++ b/lib/src/AOPAdvice.cc @@ -3,21 +3,19 @@ namespace drogon { - -void doAdvicesChain(const std::vector> &advices, - size_t index, - const HttpRequestImplPtr &req, - const std::shared_ptr> &callbackPtr, - std::function &&missCallback) +void doAdvicesChain( + const std::vector> &advices, + size_t index, + const HttpRequestImplPtr &req, + const std::shared_ptr> &callbackPtr, + std::function &&missCallback) { if (index < advices.size()) { auto &advice = advices[index]; advice(req, [callbackPtr](const HttpResponsePtr &resp) { (*callbackPtr)(resp); }, - [index, req, callbackPtr, &advices, missCallback = std::move(missCallback)]() mutable { + [ index, req, callbackPtr, &advices, missCallback = std::move(missCallback) ]() mutable { doAdvicesChain(advices, index + 1, req, callbackPtr, std::move(missCallback)); }); } @@ -27,20 +25,19 @@ void doAdvicesChain(const std::vector> &advices, - size_t index, - const HttpRequestImplPtr &req, - const std::shared_ptr> &callbackPtr, - std::function &&missCallback) +void doAdvicesChain( + const std::deque> &advices, + size_t index, + const HttpRequestImplPtr &req, + const std::shared_ptr> &callbackPtr, + std::function &&missCallback) { if (index < advices.size()) { auto &advice = advices[index]; advice(req, [callbackPtr](const HttpResponsePtr &resp) { (*callbackPtr)(resp); }, - [index, req, callbackPtr, &advices, missCallback = std::move(missCallback)]() mutable { + [ index, req, callbackPtr, &advices, missCallback = std::move(missCallback) ]() mutable { doAdvicesChain(advices, index + 1, req, callbackPtr, std::move(missCallback)); }); } @@ -50,4 +47,4 @@ void doAdvicesChain(const std::deque> &advices, - size_t index, - const HttpRequestImplPtr &req, - const std::shared_ptr> &callbackPtr, - std::function &&missCallback); -void doAdvicesChain(const std::deque> &advices, - size_t index, - const HttpRequestImplPtr &req, - const std::shared_ptr> &callbackPtr, - std::function &&missCallback); -} // namespace drogon \ No newline at end of file +void doAdvicesChain( + const std::vector> &advices, + size_t index, + const HttpRequestImplPtr &req, + const std::shared_ptr> &callbackPtr, + std::function &&missCallback); +void doAdvicesChain( + const std::deque> &advices, + size_t index, + const HttpRequestImplPtr &req, + const std::shared_ptr> &callbackPtr, + std::function &&missCallback); +} // namespace drogon \ No newline at end of file diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index 631ba65a..292e578a 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -2,7 +2,7 @@ * * ConfigLoader.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,13 +14,13 @@ #include "ConfigLoader.h" -#include #include -#include #include -#include -#include +#include #include +#include +#include +#include using namespace drogon; static bool bytesSize(std::string &sizeStr, size_t &size) @@ -35,43 +35,43 @@ static bool bytesSize(std::string &sizeStr, size_t &size) size = 1; switch (sizeStr[sizeStr.length() - 1]) { - case 'k': - case 'K': - size = 1024; - sizeStr.resize(sizeStr.length() - 1); - break; - case 'M': - case 'm': - size = (1024 * 1024); - sizeStr.resize(sizeStr.length() - 1); - break; - case 'g': - case 'G': - size = (1024 * 1024 * 1024); - sizeStr.resize(sizeStr.length() - 1); - break; + case 'k': + case 'K': + size = 1024; + sizeStr.resize(sizeStr.length() - 1); + break; + case 'M': + case 'm': + size = (1024 * 1024); + sizeStr.resize(sizeStr.length() - 1); + break; + case 'g': + case 'G': + size = (1024 * 1024 * 1024); + sizeStr.resize(sizeStr.length() - 1); + break; #if ((ULONG_MAX) != (UINT_MAX)) - //64bit system - case 't': - case 'T': - size = (1024L * 1024L * 1024L * 1024L); - sizeStr.resize(sizeStr.length() - 1); - break; + // 64bit system + case 't': + case 'T': + size = (1024L * 1024L * 1024L * 1024L); + sizeStr.resize(sizeStr.length() - 1); + break; #endif - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '7': - case '8': - case '9': - break; - default: - std::cerr << "Invalid value of client_max_body_size: " << sizeStr << std::endl; - return false; - break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '7': + case '8': + case '9': + break; + default: + std::cerr << "Invalid value of client_max_body_size: " << sizeStr << std::endl; + return false; + break; } std::istringstream iss(sizeStr); size_t tmpSize; @@ -175,7 +175,7 @@ static void loadControllers(const Json::Value &controllers) { constraints.push_back(Post); } - else if (strMethod == "head") //The branch nerver work + else if (strMethod == "head") // The branch nerver work { constraints.push_back(Head); } @@ -203,34 +203,34 @@ static void loadApp(const Json::Value &app) { if (!app) return; - //threads number + // threads number auto threadsNum = app.get("threads_num", 1).asUInt64(); if (threadsNum == 0) { - //set the number to the number of processors. + // set the number to the number of processors. threadsNum = std::thread::hardware_concurrency(); LOG_DEBUG << "The number of processors is " << threadsNum; } if (threadsNum < 1) threadsNum = 1; drogon::app().setThreadNum(threadsNum); - //session + // session auto enableSession = app.get("enable_session", false).asBool(); auto timeout = app.get("session_timeout", 0).asUInt64(); if (enableSession) drogon::app().enableSession(timeout); else drogon::app().disableSession(); - //document root + // document root auto documentRoot = app.get("document_root", "").asString(); if (documentRoot != "") { drogon::app().setDocumentRoot(documentRoot); } - //upload path + // upload path auto uploadPath = app.get("upload_path", "uploads").asString(); drogon::app().setUploadPath(uploadPath); - //file types + // file types auto fileTypes = app["file_types"]; if (fileTypes.isArray() && !fileTypes.empty()) { @@ -242,20 +242,20 @@ static void loadApp(const Json::Value &app) } drogon::app().setFileTypes(types); } - //max connections + // max connections auto maxConns = app.get("max_connections", 0).asUInt64(); if (maxConns > 0) { drogon::app().setMaxConnectionNum(maxConns); } - //max connections per IP + // max connections per IP auto maxConnsPerIP = app.get("max_connections_per_ip", 0).asUInt64(); if (maxConnsPerIP > 0) { drogon::app().setMaxConnectionNumPerIP(maxConnsPerIP); } - //dynamic views + // dynamic views auto enableDynamicViews = app.get("load_dynamic_views", false).asBool(); if (enableDynamicViews) { @@ -271,15 +271,15 @@ static void loadApp(const Json::Value &app) drogon::app().enableDynamicViewsLoading(paths); } } - //log + // log loadLogSetting(app["log"]); - //run as daemon + // run as daemon auto runAsDaemon = app.get("run_as_daemon", false).asBool(); if (runAsDaemon) { drogon::app().enableRunAsDaemon(); } - //relaunch + // relaunch auto relaunch = app.get("relaunch_on_error", false).asBool(); if (relaunch) { @@ -292,7 +292,7 @@ static void loadApp(const Json::Value &app) auto staticFilesCacheTime = app.get("static_files_cache_time", 5).asInt(); drogon::app().setStaticFilesCacheTime(staticFilesCacheTime); loadControllers(app["simple_controllers_map"]); - //Kick off idle connections + // Kick off idle connections auto kickOffTimeout = app.get("idle_connection_timeout", 60).asUInt64(); drogon::app().setIdleConnectionTimeout(kickOffTimeout); auto server = app.get("server_header_field", "").asString(); @@ -350,7 +350,9 @@ static void loadDbClients(const Json::Value &dbClients) drogon::app().createDbClient(type, host, (u_short)port, dbname, user, password, connNum, filename, name, isFast); } #else - std::cout << "No database is supported by drogon, please install the database development library first." << std::endl; + std::cout << "No database is supported by drogon, please install the " + "database development library first." + << std::endl; exit(1); #endif } @@ -380,7 +382,7 @@ static void loadSSL(const Json::Value &sslFiles) } void ConfigLoader::load() { - //std::cout<<_configJsonRoot< #include #include +#include namespace drogon { @@ -25,11 +25,14 @@ class ConfigLoader : public trantor::NonCopyable public: explicit ConfigLoader(const std::string &configFile); ~ConfigLoader(); - const Json::Value &jsonValue() const { return _configJsonRoot; } + const Json::Value &jsonValue() const + { + return _configJsonRoot; + } void load(); private: std::string _configFile; Json::Value _configJsonRoot; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/Cookie.cc b/lib/src/Cookie.cc old mode 100755 new mode 100644 index 8b52aa84..2600b99a --- a/lib/src/Cookie.cc +++ b/lib/src/Cookie.cc @@ -2,7 +2,7 @@ * * Cookie.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -39,7 +39,7 @@ std::string Cookie::cookieString() const { ret.append("HttpOnly; "); } - ret.resize(ret.length() - 2); //delete last semicolon + ret.resize(ret.length() - 2); // delete last semicolon ret.append("\r\n"); return ret; } diff --git a/lib/src/DrClassMap.cc b/lib/src/DrClassMap.cc old mode 100755 new mode 100644 index d65d3236..c293dd4d --- a/lib/src/DrClassMap.cc +++ b/lib/src/DrClassMap.cc @@ -2,7 +2,7 @@ * * DrClassMap.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -22,7 +22,6 @@ namespace drogon { namespace internal { - static std::unordered_map> &getObjsMap() { static std::unordered_map> singleInstanceMap; @@ -35,8 +34,8 @@ static std::mutex &getMapMutex() return mtx; } -} // namespace internal -} // namespace drogon +} // namespace internal +} // namespace drogon void DrClassMap::registerClass(const std::string &className, const DrAllocFunc &func) { diff --git a/lib/src/DrTemplateBase.cc b/lib/src/DrTemplateBase.cc old mode 100755 new mode 100644 index 32396905..3274fdc5 --- a/lib/src/DrTemplateBase.cc +++ b/lib/src/DrTemplateBase.cc @@ -2,7 +2,7 @@ * * DrTemplateBase.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,10 +12,10 @@ * */ -#include #include -#include +#include #include +#include using namespace drogon; diff --git a/lib/src/FiltersFunction.cc b/lib/src/FiltersFunction.cc index 385de879..f3344902 100644 --- a/lib/src/FiltersFunction.cc +++ b/lib/src/FiltersFunction.cc @@ -2,7 +2,7 @@ * * FiltersFunction.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -22,7 +22,6 @@ namespace drogon { namespace FiltersFunction { - static void doFilterChains(const std::vector> &filters, size_t index, const HttpRequestImplPtr &req, @@ -34,15 +33,16 @@ static void doFilterChains(const std::vector> &f if (index < filters.size()) { auto &filter = filters[index]; - filter->doFilter(req, - [needSetJsessionid, callbackPtr, sessionIdPtr](const HttpResponsePtr &res) { - if (needSetJsessionid && res->statusCode() != k404NotFound) - res->addCookie("JSESSIONID", *sessionIdPtr); - (*callbackPtr)(res); - }, - [=, &filters, missCallback = std::move(missCallback)]() mutable { - doFilterChains(filters, index + 1, req, callbackPtr, needSetJsessionid, sessionIdPtr, std::move(missCallback)); - }); + filter->doFilter( + req, + [needSetJsessionid, callbackPtr, sessionIdPtr](const HttpResponsePtr &res) { + if (needSetJsessionid && res->statusCode() != k404NotFound) + res->addCookie("JSESSIONID", *sessionIdPtr); + (*callbackPtr)(res); + }, + [ =, &filters, missCallback = std::move(missCallback) ]() mutable { + doFilterChains(filters, index + 1, req, callbackPtr, needSetJsessionid, sessionIdPtr, std::move(missCallback)); + }); } else { @@ -74,9 +74,8 @@ void doFilters(const std::vector> &filters, const std::shared_ptr &sessionIdPtr, std::function &&missCallback) { - doFilterChains(filters, 0, req, callbackPtr, needSetJsessionid, sessionIdPtr, std::move(missCallback)); } -} // namespace FiltersFunction -} // namespace drogon \ No newline at end of file +} // namespace FiltersFunction +} // namespace drogon \ No newline at end of file diff --git a/lib/src/FiltersFunction.h b/lib/src/FiltersFunction.h index 0863a304..d85bcc49 100644 --- a/lib/src/FiltersFunction.h +++ b/lib/src/FiltersFunction.h @@ -2,7 +2,7 @@ * * FiltersFunction.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -16,15 +16,14 @@ #include "HttpRequestImpl.h" #include +#include #include #include -#include namespace drogon { namespace FiltersFunction { - std::vector> createFilters(const std::vector &filterNames); void doFilters(const std::vector> &filters, const HttpRequestImplPtr &req, @@ -33,5 +32,5 @@ void doFilters(const std::vector> &filters, const std::shared_ptr &sessionIdPtr, std::function &&missCallback); -} // namespace FiltersFunction -} // namespace drogon \ No newline at end of file +} // namespace FiltersFunction +} // namespace drogon \ No newline at end of file diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc old mode 100755 new mode 100644 index 4124efbe..4e328e6c --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -2,7 +2,7 @@ * * HttpAppFrameworkImpl.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,51 +13,49 @@ */ #include "HttpAppFrameworkImpl.h" +#include "AOPAdvice.h" #include "ConfigLoader.h" #include "HttpServer.h" -#include "AOPAdvice.h" #if USE_ORM #include "../../orm_lib/src/DbClientLockFree.h" #endif -#include -#include +#include +#include #include #include #include -#include +#include #include -#include +#include +#include +#include +#include #include +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include +#include #include -#include #include -#include -#include +#include +#include +#include using namespace drogon; using namespace std::placeholders; -///Make sure that the main event loop is initialized in the main thread. +/// Make sure that the main event loop is initialized in the main thread. drogon::InitBeforeMainFunction drogon::HttpAppFrameworkImpl::_initFirst([]() { - HttpAppFrameworkImpl::instance().getLoop()->runInLoop([]() { - LOG_TRACE << "Initialize the main event loop in the main thread"; - }); + HttpAppFrameworkImpl::instance().getLoop()->runInLoop( + []() { LOG_TRACE << "Initialize the main event loop in the main thread"; }); }); namespace drogon { - class DrogonFileLocker : public trantor::NonCopyable { -public: + public: DrogonFileLocker() { _fd = open("/tmp/drogon.lock", O_TRUNC | O_CREAT, 0755); @@ -68,11 +66,11 @@ public: close(_fd); } -private: + private: int _fd = 0; }; -} // namespace drogon +} // namespace drogon static void godaemon(void) { printf("Initializing daemon mode\n"); @@ -82,7 +80,7 @@ static void godaemon(void) pid_t pid; pid = fork(); if (pid > 0) - exit(0); // parent + exit(0); // parent if (pid < 0) { perror("fork"); @@ -113,10 +111,9 @@ void HttpAppFrameworkImpl::enableDynamicViewsLoading(const std::vector= 2 && libpath[0] == '.' && libpath[1] == '/') || - (libpath.length() >= 3 && libpath[0] == '.' && libpath[1] == '.' && libpath[2] == '/') || - libpath == "." || libpath == "..") + if (libpath[0] == '/' || (libpath.length() >= 2 && libpath[0] == '.' && libpath[1] == '/') || + (libpath.length() >= 3 && libpath[0] == '.' && libpath[1] == '.' && libpath[2] == '/') || libpath == "." || + libpath == "..") { _libFilePaths.push_back(libpath); } @@ -182,9 +179,7 @@ void HttpAppFrameworkImpl::loadConfigFile(const std::string &fileName) loader.load(); _jsonConfig = loader.jsonValue(); } -void HttpAppFrameworkImpl::setLogPath(const std::string &logPath, - const std::string &logfileBaseName, - size_t logfileSize) +void HttpAppFrameworkImpl::setLogPath(const std::string &logPath, const std::string &logfileBaseName, size_t logfileSize) { if (logPath == "") return; @@ -206,8 +201,7 @@ void HttpAppFrameworkImpl::setLogLevel(trantor::Logger::LogLevel level) { trantor::Logger::setLogLevel(level); } -void HttpAppFrameworkImpl::setSSLFiles(const std::string &certPath, - const std::string &keyPath) +void HttpAppFrameworkImpl::setSSLFiles(const std::string &certPath, const std::string &keyPath) { _sslCertPath = certPath; _sslKeyPath = keyPath; @@ -238,14 +232,14 @@ void HttpAppFrameworkImpl::run() if (_runAsDaemon) { - //go daemon! + // go daemon! godaemon(); #ifdef __linux__ getLoop()->resetTimerQueue(); #endif getLoop()->resetAfterFork(); } - //set relaunching + // set relaunching if (_relaunchOnError) { while (true) @@ -259,7 +253,7 @@ void HttpAppFrameworkImpl::run() } else if (child_pid == 0) { - //child + // child break; } waitpid(child_pid, &child_status, 0); @@ -269,7 +263,7 @@ void HttpAppFrameworkImpl::run() getLoop()->resetAfterFork(); } - //set logger + // set logger if (!_logPath.empty()) { if (access(_logPath.c_str(), R_OK | W_OK) >= 0) @@ -281,7 +275,8 @@ void HttpAppFrameworkImpl::run() } asyncFileLogger.setFileName(baseName, ".log", _logPath); asyncFileLogger.startLogging(); - trantor::Logger::setOutputFunction([&](const char *msg, const uint64_t len) { asyncFileLogger.output(msg, len); }, [&]() { asyncFileLogger.flush(); }); + trantor::Logger::setOutputFunction([&](const char *msg, const uint64_t len) { asyncFileLogger.output(msg, len); }, + [&]() { asyncFileLogger.flush(); }); asyncFileLogger.setFileSizeLimit(_logfileSize); } else @@ -294,7 +289,7 @@ void HttpAppFrameworkImpl::run() { LOG_INFO << "Start child process"; } - //now start runing!! + // now start runing!! _running = true; @@ -323,20 +318,14 @@ void HttpAppFrameworkImpl::run() { DrogonFileLocker lock; // Check whether the port is in use. - TcpServer server(getLoop(), - InetAddress(ip, std::get<1>(listener), isIpv6), - "drogonPortTest", - true, - false); - serverPtr = std::make_shared(loopThreadPtr->getLoop(), - InetAddress(ip, std::get<1>(listener), isIpv6), - "drogon"); + TcpServer server(getLoop(), InetAddress(ip, std::get<1>(listener), isIpv6), "drogonPortTest", true, false); + serverPtr = std::make_shared( + loopThreadPtr->getLoop(), InetAddress(ip, std::get<1>(listener), isIpv6), "drogon"); } else { - serverPtr = std::make_shared(loopThreadPtr->getLoop(), - InetAddress(ip, std::get<1>(listener), isIpv6), - "drogon"); + serverPtr = std::make_shared( + loopThreadPtr->getLoop(), InetAddress(ip, std::get<1>(listener), isIpv6), "drogon"); } if (std::get<2>(listener)) @@ -372,9 +361,8 @@ void HttpAppFrameworkImpl::run() loopThreads.push_back(loopThreadPtr); auto ip = std::get<0>(listener); bool isIpv6 = ip.find(":") == std::string::npos ? false : true; - auto serverPtr = std::make_shared(loopThreadPtr->getLoop(), - InetAddress(ip, std::get<1>(listener), isIpv6), - "drogon"); + auto serverPtr = + std::make_shared(loopThreadPtr->getLoop(), InetAddress(ip, std::get<1>(listener), isIpv6), "drogon"); if (std::get<2>(listener)) { #ifdef USE_OPENSSL @@ -408,7 +396,8 @@ void HttpAppFrameworkImpl::run() #endif #if USE_ORM - // A fast database client instance should be created in the main event loop, so put the main loop into ioLoops. + // A fast database client instance should be created in the main event loop, + // so put the main loop into ioLoops. ioLoops.push_back(getLoop()); createDbClients(ioLoops); ioLoops.pop_back(); @@ -437,23 +426,25 @@ void HttpAppFrameworkImpl::run() tmpTimeout = tmpTimeout / 100; } } - _sessionMapPtr = std::unique_ptr>(new CacheMap(getLoop(), 1.0, wheelNum, bucketNum)); + _sessionMapPtr = std::unique_ptr>( + new CacheMap(getLoop(), 1.0, wheelNum, bucketNum)); } else if (_sessionTimeout == 0) { - _sessionMapPtr = std::unique_ptr>(new CacheMap(getLoop(), 0, 0, 0)); + _sessionMapPtr = + std::unique_ptr>(new CacheMap(getLoop(), 0, 0, 0)); } } - _responseCachingMap = std::unique_ptr>(new CacheMap(getLoop(), 1.0, 4, 50)); //Max timeout up to about 70 days; + _responseCachingMap = std::unique_ptr>( + new CacheMap(getLoop(), 1.0, 4, 50)); // Max timeout up to about 70 days; - //Initialize plugins + // Initialize plugins const auto &pluginConfig = _jsonConfig["plugins"]; if (!pluginConfig.isNull()) { - _pluginsManager.initializeAllPlugins(pluginConfig, - [](PluginBase *plugin) { - //TODO: new plugin - }); + _pluginsManager.initializeAllPlugins(pluginConfig, [](PluginBase *plugin) { + // TODO: new plugin + }); } // Let listener event loops run when everything is ready. @@ -481,7 +472,8 @@ void HttpAppFrameworkImpl::createDbClients(const std::vector(new drogon::orm::DbClientLockFree(dbInfo._connectionInfo, loop, dbInfo._dbType)); + _dbFastClientsMap[dbInfo._name][loop] = std::shared_ptr( + new drogon::orm::DbClientLockFree(dbInfo._connectionInfo, loop, dbInfo._dbType)); } } } @@ -490,19 +482,22 @@ void HttpAppFrameworkImpl::createDbClients(const std::vectorsecond++ > _maxConnectionNumPerIP) { - conn->getLoop()->queueInLoop([conn]() { - conn->forceClose(); - }); + conn->getLoop()->queueInLoop([conn]() { conn->forceClose(); }); return; } } @@ -557,7 +550,8 @@ void HttpAppFrameworkImpl::onConnection(const TcpConnectionPtr &conn) if (conn->getContext().empty()) #endif { - //If the connection is connected to the SSL port and then disconnected before the SSL handshake. + // If the connection is connected to the SSL port and then disconnected + // before the SSL handshake. return; } _connectionNum--; @@ -580,8 +574,7 @@ void HttpAppFrameworkImpl::onConnection(const TcpConnectionPtr &conn) void HttpAppFrameworkImpl::setUploadPath(const std::string &uploadPath) { assert(!uploadPath.empty()); - if (uploadPath[0] == '/' || - (uploadPath.length() >= 2 && uploadPath[0] == '.' && uploadPath[1] == '/') || + if (uploadPath[0] == '/' || (uploadPath.length() >= 2 && uploadPath[0] == '.' && uploadPath[1] == '/') || (uploadPath.length() >= 3 && uploadPath[0] == '.' && uploadPath[1] == '.' && uploadPath[2] == '/') || uploadPath == "." || uploadPath == "..") { @@ -612,7 +605,8 @@ std::vector> HttpAppFrameworkIm return ret; } -void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::function &&callback) +void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, + std::function &&callback) { LOG_TRACE << "new request:" << req->peerAddr().toIpPort() << "->" << req->localAddr().toIpPort(); LOG_TRACE << "Headers " << req->methodString() << " " << req->path(); @@ -656,17 +650,17 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu transform(filetype.begin(), filetype.end(), filetype.begin(), tolower); if (_fileTypeSet.find(filetype) != _fileTypeSet.end()) { - //LOG_INFO << "file query!" << path; + // LOG_INFO << "file query!" << path; std::string filePath = _rootPath + path; if (filePath.find("/../") != std::string::npos) { - //Downloading files from the parent folder is forbidden. + // Downloading files from the parent folder is forbidden. auto resp = HttpResponse::newHttpResponse(); resp->setStatusCode(k403Forbidden); callback(resp); return; } - //find cached response + // find cached response HttpResponsePtr cachedResp; { std::lock_guard guard(_staticFilesCacheMutex); @@ -680,15 +674,16 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu } } - //check last modified time,rfc2616-14.25 - //If-Modified-Since: Mon, 15 Oct 2018 06:26:33 GMT + // check last modified time,rfc2616-14.25 + // If-Modified-Since: Mon, 15 Oct 2018 06:26:33 GMT std::string timeStr; if (_enableLastModify) { if (cachedResp) { - if (std::dynamic_pointer_cast(cachedResp)->getHeaderBy("last-modified") == req->getHeaderBy("if-modified-since")) + if (std::dynamic_pointer_cast(cachedResp)->getHeaderBy("last-modified") == + req->getHeaderBy("if-modified-since")) { std::shared_ptr resp = std::make_shared(); resp->setStatusCode(k304NotModified); @@ -733,8 +728,9 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu { if (needSetJsessionid) { - //make a copy - auto newCachedResp = std::make_shared(*std::dynamic_pointer_cast(cachedResp)); + // make a copy + auto newCachedResp = + std::make_shared(*std::dynamic_pointer_cast(cachedResp)); newCachedResp->addCookie("JSESSIONID", sessionId); newCachedResp->setExpiredTime(-1); callback(newCachedResp); @@ -746,7 +742,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu HttpResponsePtr resp; if (_gzipStaticFlag && req->getHeaderBy("accept-encoding").find("gzip") != std::string::npos) { - //Find compressed file first. + // Find compressed file first. auto gzipFileName = filePath + ".gz"; std::ifstream infile(gzipFileName, std::ifstream::binary); if (infile) @@ -764,7 +760,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu resp->addHeader("Last-Modified", timeStr); resp->addHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT"); } - //cache the response for 5 seconds by default + // cache the response for 5 seconds by default if (_staticFilesCacheTime >= 0) { resp->setExpiredTime(_staticFilesCacheTime); @@ -783,7 +779,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu auto newCachedResp = resp; if (resp->expiredTime() >= 0) { - //make a copy + // make a copy newCachedResp = std::make_shared(*std::dynamic_pointer_cast(resp)); newCachedResp->setExpiredTime(-1); } @@ -797,7 +793,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu } } - //Route to controller + // Route to controller if (!_preRoutingObservers.empty()) { for (auto &observer : _preRoutingObservers) @@ -816,17 +812,19 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestImplPtr &req, std::fu doAdvicesChain(_preRoutingAdvices, 0, req, - std::make_shared>([callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) { - if (!needSetJsessionid || resp->statusCode() == k404NotFound) - (*callbackPtr)(resp); - else - { - resp->addCookie("JSESSIONID", *sessionIdPtr); - (*callbackPtr)(resp); - } - }), + std::make_shared>( + [callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) { + if (!needSetJsessionid || resp->statusCode() == k404NotFound) + (*callbackPtr)(resp); + else + { + resp->addCookie("JSESSIONID", *sessionIdPtr); + (*callbackPtr)(resp); + } + }), [this, callbackPtr, req, needSetJsessionid, sessionIdPtr]() { - _httpSimpleCtrlsRouter.route(req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); + _httpSimpleCtrlsRouter.route( + req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); }); } } @@ -854,8 +852,7 @@ orm::DbClientPtr HttpAppFrameworkImpl::getDbClient(const std::string &name) } orm::DbClientPtr HttpAppFrameworkImpl::getFastDbClient(const std::string &name) { - assert(_dbFastClientsMap[name].find(trantor::EventLoop::getEventLoopOfCurrentThread()) != - _dbFastClientsMap[name].end()); + assert(_dbFastClientsMap[name].find(trantor::EventLoop::getEventLoopOfCurrentThread()) != _dbFastClientsMap[name].end()); return _dbFastClientsMap[name][trantor::EventLoop::getEventLoopOfCurrentThread()]; } void HttpAppFrameworkImpl::createDbClient(const std::string &dbType, @@ -870,7 +867,8 @@ void HttpAppFrameworkImpl::createDbClient(const std::string &dbType, const bool isFast) { assert(!_running); - auto connStr = utils::formattedString("host=%s port=%u dbname=%s user=%s", host.c_str(), port, databaseName.c_str(), userName.c_str()); + auto connStr = + utils::formattedString("host=%s port=%u dbname=%s user=%s", host.c_str(), port, databaseName.c_str(), userName.c_str()); if (!password.empty()) { connStr += " password="; @@ -890,7 +888,9 @@ void HttpAppFrameworkImpl::createDbClient(const std::string &dbType, info._dbType = orm::ClientType::PostgreSQL; _dbInfos.push_back(info); #else - std::cout << "The PostgreSQL is not supported by drogon, please install the development library first." << std::endl; + std::cout << "The PostgreSQL is not supported by drogon, please install " + "the development library first." + << std::endl; exit(1); #endif } @@ -900,7 +900,9 @@ void HttpAppFrameworkImpl::createDbClient(const std::string &dbType, info._dbType = orm::ClientType::Mysql; _dbInfos.push_back(info); #else - std::cout << "The Mysql is not supported by drogon, please install the development library first." << std::endl; + std::cout << "The Mysql is not supported by drogon, please install the " + "development library first." + << std::endl; exit(1); #endif } @@ -912,14 +914,18 @@ void HttpAppFrameworkImpl::createDbClient(const std::string &dbType, info._dbType = orm::ClientType::Sqlite3; _dbInfos.push_back(info); #else - std::cout << "The Sqlite3 is not supported by drogon, please install the development library first." << std::endl; + std::cout << "The Sqlite3 is not supported by drogon, please install the " + "development library first." + << std::endl; exit(1); #endif } } #endif -void HttpAppFrameworkImpl::forward(const HttpRequestImplPtr &req, std::function &&callback, const std::string &hostString) +void HttpAppFrameworkImpl::forward(const HttpRequestImplPtr &req, + std::function &&callback, + const std::string &hostString) { if (hostString.empty()) { @@ -940,7 +946,9 @@ void HttpAppFrameworkImpl::forward(const HttpRequestImplPtr &req, std::function< } else { - clientPtr = std::make_shared(trantor::EventLoop::getEventLoopOfCurrentThread() ? trantor::EventLoop::getEventLoopOfCurrentThread() : getLoop(), + clientPtr = std::make_shared(trantor::EventLoop::getEventLoopOfCurrentThread() + ? trantor::EventLoop::getEventLoopOfCurrentThread() + : getLoop(), hostString); clientsMap[hostString] = clientPtr; } diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index b406ba82..f57c3a3a 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -2,7 +2,7 @@ * * HttpAppFrameworkImpl.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,15 +14,15 @@ #pragma once +#include "HttpClientImpl.h" +#include "HttpControllersRouter.h" #include "HttpRequestImpl.h" #include "HttpResponseImpl.h" -#include "HttpClientImpl.h" -#include "SharedLibManager.h" -#include "HttpControllersRouter.h" #include "HttpSimpleControllersRouter.h" +#include "PluginsManager.h" +#include "SharedLibManager.h" #include "WebSocketConnectionImpl.h" #include "WebsocketControllersRouter.h" -#include "PluginsManager.h" #include #include @@ -30,11 +30,11 @@ #include #include -#include -#include #include #include #include +#include +#include namespace drogon { @@ -47,10 +47,19 @@ struct InitBeforeMainFunction }; class HttpAppFrameworkImpl : public HttpAppFramework { -public: + public: HttpAppFrameworkImpl() - : _httpCtrlsRouter(_postRoutingAdvices, _postRoutingObservers, _preHandlingAdvices, _preHandlingObservers, _postHandlingAdvices), - _httpSimpleCtrlsRouter(_httpCtrlsRouter, _postRoutingAdvices, _postRoutingObservers, _preHandlingAdvices, _preHandlingObservers, _postHandlingAdvices), + : _httpCtrlsRouter(_postRoutingAdvices, + _postRoutingObservers, + _preHandlingAdvices, + _preHandlingObservers, + _postHandlingAdvices), + _httpSimpleCtrlsRouter(_httpCtrlsRouter, + _postRoutingAdvices, + _postRoutingObservers, + _preHandlingAdvices, + _preHandlingObservers, + _postHandlingAdvices), _uploadPath(_rootPath + "uploads"), _connectionNum(0) { @@ -72,18 +81,18 @@ public: const std::string &certFile = "", const std::string &keyFile = "") override; virtual void setThreadNum(size_t threadNum) override; - virtual size_t getThreadNum() const override { return _threadNum; } - virtual void setSSLFiles(const std::string &certPath, - const std::string &keyPath) override; + virtual size_t getThreadNum() const override + { + return _threadNum; + } + virtual void setSSLFiles(const std::string &certPath, const std::string &keyPath) override; virtual void run() override; virtual void registerWebSocketController(const std::string &pathName, const std::string &crtlName, - const std::vector &filters = - std::vector()) override; + const std::vector &filters = std::vector()) override; virtual void registerHttpSimpleController(const std::string &pathName, const std::string &crtlName, - const std::vector &filtersAndMethods = - std::vector()) override; + const std::vector &filtersAndMethods = std::vector()) override; virtual void setCustom404Page(const HttpResponsePtr &resp) override { @@ -96,38 +105,40 @@ public: return _custom404; } - virtual void forward(const HttpRequestPtr &req, std::function &&callback, const std::string &hostString = "") override + virtual void forward(const HttpRequestPtr &req, + std::function &&callback, + const std::string &hostString = "") override { forward(std::dynamic_pointer_cast(req), std::move(callback), hostString); } - - void forward(const HttpRequestImplPtr &req, std::function &&callback, const std::string &hostString); + + void forward(const HttpRequestImplPtr &req, + std::function &&callback, + const std::string &hostString); virtual void registerBeginningAdvice(const std::function &advice) override { getLoop()->runInLoop(advice); } - virtual void registerNewConnectionAdvice(const std::function &advice) override + virtual void registerNewConnectionAdvice( + const std::function &advice) override { _newConnectionAdvices.emplace_back(advice); } - virtual void registerPreRoutingAdvice(const std::function &advice) override + virtual void registerPreRoutingAdvice( + const std::function &advice) override { _preRoutingAdvices.emplace_back(advice); } - virtual void registerPostRoutingAdvice(const std::function &advice) override + virtual void registerPostRoutingAdvice( + const std::function &advice) override { _postRoutingAdvices.emplace_front(advice); } - virtual void registerPreHandlingAdvice(const std::function &advice) override + virtual void registerPreHandlingAdvice( + const std::function &advice) override { _preHandlingAdvices.emplace_back(advice); } @@ -144,7 +155,8 @@ public: { _preHandlingObservers.emplace_back(advice); } - virtual void registerPostHandlingAdvice(const std::function &advice) override + virtual void registerPostHandlingAdvice( + const std::function &advice) override { _postHandlingAdvices.emplace_front(advice); } @@ -154,46 +166,118 @@ public: _useSession = true; _sessionTimeout = timeout; } - virtual void disableSession() override { _useSession = false; } - virtual const std::string &getDocumentRoot() const override { return _rootPath; } - virtual void setDocumentRoot(const std::string &rootPath) override { _rootPath = rootPath; } - virtual const std::string &getUploadPath() const override { return _uploadPath; } + virtual void disableSession() override + { + _useSession = false; + } + virtual const std::string &getDocumentRoot() const override + { + return _rootPath; + } + virtual void setDocumentRoot(const std::string &rootPath) override + { + _rootPath = rootPath; + } + virtual const std::string &getUploadPath() const override + { + return _uploadPath; + } virtual void setUploadPath(const std::string &uploadPath) override; virtual void setFileTypes(const std::vector &types) override; virtual void enableDynamicViewsLoading(const std::vector &libPaths) override; virtual void setMaxConnectionNum(size_t maxConnections) override; virtual void setMaxConnectionNumPerIP(size_t maxConnectionsPerIP) override; virtual void loadConfigFile(const std::string &fileName) override; - virtual void enableRunAsDaemon() override { _runAsDaemon = true; } - virtual void enableRelaunchOnError() override { _relaunchOnError = true; } + virtual void enableRunAsDaemon() override + { + _runAsDaemon = true; + } + virtual void enableRelaunchOnError() override + { + _relaunchOnError = true; + } virtual void setLogPath(const std::string &logPath, const std::string &logfileBaseName = "", size_t logfileSize = 100000000) override; virtual void setLogLevel(trantor::Logger::LogLevel level) override; - virtual void enableSendfile(bool sendFile) override { _useSendfile = sendFile; } - virtual void enableGzip(bool useGzip) override { _useGzip = useGzip; } - virtual bool isGzipEnabled() const override { return _useGzip; } - virtual void setStaticFilesCacheTime(int cacheTime) override { _staticFilesCacheTime = cacheTime; } - virtual int staticFilesCacheTime() const override { return _staticFilesCacheTime; } - virtual void setIdleConnectionTimeout(size_t timeout) override { _idleConnectionTimeout = timeout; } - virtual void setKeepaliveRequestsNumber(const size_t number) override { _keepaliveRequestsNumber = number; } - virtual void setPipeliningRequestsNumber(const size_t number) override { _pipeliningRequestsNumber = number; } - virtual void setGzipStatic(bool useGzipStatic) override { _gzipStaticFlag = useGzipStatic; } - bool getGzipStatic() const { return _gzipStaticFlag; } - virtual void setClientMaxBodySize(size_t maxSize) override { _clientMaxBodySize = maxSize; } - virtual void setClientMaxWebSocketMessageSize(size_t maxSize) override { _clientMaxWebSocketMessageSize = maxSize; } - virtual void setHomePage(const std::string &homePageFile) override { _homePageFile = homePageFile; } - const std::string &getHomePage() const { return _homePageFile; } - size_t getClientMaxBodySize() const { return _clientMaxBodySize; } - size_t getClientMaxWebSocketMessageSize() const { return _clientMaxWebSocketMessageSize; } + virtual void enableSendfile(bool sendFile) override + { + _useSendfile = sendFile; + } + virtual void enableGzip(bool useGzip) override + { + _useGzip = useGzip; + } + virtual bool isGzipEnabled() const override + { + return _useGzip; + } + virtual void setStaticFilesCacheTime(int cacheTime) override + { + _staticFilesCacheTime = cacheTime; + } + virtual int staticFilesCacheTime() const override + { + return _staticFilesCacheTime; + } + virtual void setIdleConnectionTimeout(size_t timeout) override + { + _idleConnectionTimeout = timeout; + } + virtual void setKeepaliveRequestsNumber(const size_t number) override + { + _keepaliveRequestsNumber = number; + } + virtual void setPipeliningRequestsNumber(const size_t number) override + { + _pipeliningRequestsNumber = number; + } + virtual void setGzipStatic(bool useGzipStatic) override + { + _gzipStaticFlag = useGzipStatic; + } + bool getGzipStatic() const + { + return _gzipStaticFlag; + } + virtual void setClientMaxBodySize(size_t maxSize) override + { + _clientMaxBodySize = maxSize; + } + virtual void setClientMaxWebSocketMessageSize(size_t maxSize) override + { + _clientMaxWebSocketMessageSize = maxSize; + } + virtual void setHomePage(const std::string &homePageFile) override + { + _homePageFile = homePageFile; + } + const std::string &getHomePage() const + { + return _homePageFile; + } + size_t getClientMaxBodySize() const + { + return _clientMaxBodySize; + } + size_t getClientMaxWebSocketMessageSize() const + { + return _clientMaxWebSocketMessageSize; + } virtual std::vector> getHandlersInfo() const override; - size_t keepaliveRequestsNumber() const { return _keepaliveRequestsNumber; } - size_t pipeliningRequestsNumber() const { return _pipeliningRequestsNumber; } + size_t keepaliveRequestsNumber() const + { + return _keepaliveRequestsNumber; + } + size_t pipeliningRequestsNumber() const + { + return _pipeliningRequestsNumber; + } virtual ~HttpAppFrameworkImpl() noexcept { - //Destroy the following objects before _loop destruction + // Destroy the following objects before _loop destruction _sharedLibManagerPtr.reset(); _sessionMapPtr.reset(); } @@ -243,9 +327,12 @@ public: static HttpAppFrameworkImpl _instance; return _instance; } - bool useSendfile() { return _useSendfile; } + bool useSendfile() + { + return _useSendfile; + } -private: + private: virtual void registerHttpController(const std::string &pathPattern, const internal::HttpBinderBasePtr &binder, const std::vector &validMethods = std::vector(), @@ -261,8 +348,9 @@ private: const std::vector &validMethods, const std::vector &filters); - //We use a uuid string as session id; - //set _sessionTimeout=0 to make location session valid forever based on cookies; + // We use a uuid string as session id; + // set _sessionTimeout=0 to make location session valid forever based on + // cookies; size_t _sessionTimeout = 0; size_t _idleConnectionTimeout = 60; bool _useSession = false; @@ -279,9 +367,25 @@ private: WebsocketControllersRouter _websockCtrlsRouter; bool _enableLastModify = true; - std::set _fileTypeSet = {"html", "js", "css", "xml", "xsl", "txt", "svg", "ttf", - "otf", "woff2", "woff", "eot", "png", "jpg", "jpeg", - "gif", "bmp", "ico", "icns"}; + std::set _fileTypeSet = {"html", + "js", + "css", + "xml", + "xsl", + "txt", + "svg", + "ttf", + "otf", + "woff2", + "woff", + "eot", + "png", + "jpg", + "jpeg", + "gif", + "bmp", + "ico", + "icns"}; std::string _rootPath = "./"; std::string _uploadPath; std::atomic_bool _running; @@ -316,7 +420,7 @@ private: int _staticFilesCacheTime = 5; std::unordered_map> _staticFilesCache; std::mutex _staticFilesCacheMutex; - //Json::Value _customConfig; + // Json::Value _customConfig; Json::Value _jsonConfig; PluginsManager _pluginsManager; HttpResponsePtr _custom404; @@ -336,25 +440,14 @@ private: #endif static InitBeforeMainFunction _initFirst; std::vector> _newConnectionAdvices; - std::vector> - _preRoutingAdvices; - std::deque> - _postRoutingAdvices; - std::vector> - _preHandlingAdvices; - std::deque> - _postHandlingAdvices; + std::vector> _preRoutingAdvices; + std::deque> _postRoutingAdvices; + std::vector> _preHandlingAdvices; + std::deque> _postHandlingAdvices; std::vector> _preRoutingObservers; std::deque> _postRoutingObservers; std::vector> _preHandlingObservers; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpClientImpl.cc b/lib/src/HttpClientImpl.cc index d36ba2de..bb406c63 100644 --- a/lib/src/HttpClientImpl.cc +++ b/lib/src/HttpClientImpl.cc @@ -2,7 +2,7 @@ * * HttpClientImpl.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,27 +13,21 @@ */ #include "HttpClientImpl.h" +#include "HttpAppFrameworkImpl.h" #include "HttpRequestImpl.h" #include "HttpResponseParser.h" -#include "HttpAppFrameworkImpl.h" -#include #include +#include using namespace drogon; using namespace std::placeholders; -HttpClientImpl::HttpClientImpl(trantor::EventLoop *loop, - const trantor::InetAddress &addr, - bool useSSL) - : _loop(loop), - _server(addr), - _useSSL(useSSL) +HttpClientImpl::HttpClientImpl(trantor::EventLoop *loop, const trantor::InetAddress &addr, bool useSSL) + : _loop(loop), _server(addr), _useSSL(useSSL) { } -HttpClientImpl::HttpClientImpl(trantor::EventLoop *loop, - const std::string &hostString) - : _loop(loop) +HttpClientImpl::HttpClientImpl(trantor::EventLoop *loop, const std::string &hostString) : _loop(loop) { auto lowerHost = hostString; std::transform(lowerHost.begin(), lowerHost.end(), lowerHost.begin(), tolower); @@ -54,7 +48,7 @@ HttpClientImpl::HttpClientImpl(trantor::EventLoop *loop, auto pos = lowerHost.find("]"); if (lowerHost[0] == '[' && pos != std::string::npos) { - //ipv6 + // ipv6 _domain = lowerHost.substr(1, pos - 1); if (lowerHost[pos + 1] == ':') { @@ -129,21 +123,16 @@ HttpClientImpl::~HttpClientImpl() void HttpClientImpl::sendRequest(const drogon::HttpRequestPtr &req, const drogon::HttpReqCallback &callback) { auto thisPtr = shared_from_this(); - _loop->runInLoop([thisPtr, req, callback]() { - thisPtr->sendRequestInLoop(req, callback); - }); + _loop->runInLoop([thisPtr, req, callback]() { thisPtr->sendRequestInLoop(req, callback); }); } void HttpClientImpl::sendRequest(const drogon::HttpRequestPtr &req, drogon::HttpReqCallback &&callback) { auto thisPtr = shared_from_this(); - _loop->runInLoop([thisPtr, req, callback = std::move(callback)]() { - thisPtr->sendRequestInLoop(req, callback); - }); + _loop->runInLoop([ thisPtr, req, callback = std::move(callback) ]() { thisPtr->sendRequestInLoop(req, callback); }); } -void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, - const drogon::HttpReqCallback &callback) +void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, const drogon::HttpReqCallback &callback) { _loop->assertInLoopThread(); req->addHeader("Connection", "Keep-Alive"); @@ -170,16 +159,13 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, } } - if (_server.ipNetEndian() == 0 && !hasIpv6Address && - !_domain.empty() && - _server.portNetEndian() != 0) + if (_server.ipNetEndian() == 0 && !hasIpv6Address && !_domain.empty() && _server.portNetEndian() != 0) { - //dns - //TODO: timeout should be set by user + // dns + // TODO: timeout should be set by user if (InetAddress::resolve(_domain, &_server) == false) { - callback(ReqResult::BadServerAddress, - HttpResponse::newHttpResponse()); + callback(ReqResult::BadServerAddress, HttpResponse::newHttpResponse()); return; } LOG_TRACE << "dns:domain=" << _domain << ";ip=" << _server.toIp(); @@ -199,9 +185,8 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, auto thisPtr = shared_from_this(); std::weak_ptr weakPtr = thisPtr; assert(_requestsBuffer.empty()); - _requestsBuffer.push({req, [thisPtr, callback](ReqResult result, const HttpResponsePtr &response) { - callback(result, response); - }}); + _requestsBuffer.push( + {req, [thisPtr, callback](ReqResult result, const HttpResponsePtr &response) { callback(result, response); }}); _tcpClient->setConnectionCallback([weakPtr](const trantor::TcpConnectionPtr &connPtr) { auto thisPtr = weakPtr.lock(); if (!thisPtr) @@ -209,7 +194,7 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, if (connPtr->connected()) { connPtr->setContext(HttpResponseParser(connPtr)); - //send request; + // send request; LOG_TRACE << "Connection established!"; while (thisPtr->_pipeliningCallbacks.size() <= thisPtr->_pipeliningDepth && !thisPtr->_requestsBuffer.empty()) @@ -229,7 +214,7 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, auto thisPtr = weakPtr.lock(); if (!thisPtr) return; - //can't connect to server + // can't connect to server thisPtr->onError(ReqResult::BadServerAddress); }); _tcpClient->setMessageCallback([weakPtr](const trantor::TcpConnectionPtr &connPtr, trantor::MsgBuffer *msg) { @@ -243,14 +228,13 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, } else { - callback(ReqResult::BadServerAddress, - HttpResponse::newHttpResponse()); + callback(ReqResult::BadServerAddress, HttpResponse::newHttpResponse()); return; } } else { - //send request; + // send request; auto connPtr = _tcpClient->connection(); auto thisPtr = shared_from_this(); if (connPtr && connPtr->connected()) @@ -258,9 +242,8 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, if (_pipeliningCallbacks.size() <= _pipeliningDepth && _requestsBuffer.empty()) { sendReq(connPtr, req); - _pipeliningCallbacks.push([thisPtr, callback](ReqResult result, const HttpResponsePtr &response) { - callback(result, response); - }); + _pipeliningCallbacks.push( + [thisPtr, callback](ReqResult result, const HttpResponsePtr &response) { callback(result, response); }); } else { @@ -271,16 +254,14 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, } else { - _requestsBuffer.push({req, [thisPtr, callback](ReqResult result, const HttpResponsePtr &response) { - callback(result, response); - }}); + _requestsBuffer.push( + {req, [thisPtr, callback](ReqResult result, const HttpResponsePtr &response) { callback(result, response); }}); } } } void HttpClientImpl::sendReq(const trantor::TcpConnectionPtr &connPtr, const HttpRequestPtr &req) { - trantor::MsgBuffer buffer; auto implPtr = std::dynamic_pointer_cast(req); assert(implPtr); @@ -293,7 +274,7 @@ void HttpClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr, tra { HttpResponseParser *responseParser = any_cast(connPtr->getMutableContext()); - //LOG_TRACE << "###:" << msg->readableBytes(); + // LOG_TRACE << "###:" << msg->readableBytes(); while (msg->readableBytes() > 0) { if (!responseParser->parseResponse(msg)) @@ -348,7 +329,8 @@ void HttpClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr, tra HttpClientPtr HttpClient::newHttpClient(const std::string &ip, uint16_t port, bool useSSL, trantor::EventLoop *loop) { bool isIpv6 = ip.find(":") == std::string::npos ? false : true; - return std::make_shared(loop == nullptr ? app().getLoop() : loop, trantor::InetAddress(ip, port, isIpv6), useSSL); + return std::make_shared( + loop == nullptr ? app().getLoop() : loop, trantor::InetAddress(ip, port, isIpv6), useSSL); } HttpClientPtr HttpClient::newHttpClient(const std::string &hostString, trantor::EventLoop *loop) diff --git a/lib/src/HttpClientImpl.h b/lib/src/HttpClientImpl.h index 7fe05ce2..df674d55 100644 --- a/lib/src/HttpClientImpl.h +++ b/lib/src/HttpClientImpl.h @@ -2,7 +2,7 @@ * * HttpClientImpl.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,10 +15,10 @@ #pragma once #include -#include -#include #include #include +#include +#include namespace drogon { @@ -29,7 +29,10 @@ class HttpClientImpl : public HttpClient, public std::enable_shared_from_this HttpClientImplPtr; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpControllersRouter.cc b/lib/src/HttpControllersRouter.cc index 18d9c386..3e73d6a9 100644 --- a/lib/src/HttpControllersRouter.cc +++ b/lib/src/HttpControllersRouter.cc @@ -2,7 +2,7 @@ * * HttpControllersRouter.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,23 +12,22 @@ * */ -#include "FiltersFunction.h" #include "HttpControllersRouter.h" +#include "FiltersFunction.h" +#include "HttpAppFrameworkImpl.h" #include "HttpRequestImpl.h" #include "HttpResponseImpl.h" -#include "HttpAppFrameworkImpl.h" using namespace drogon; namespace drogon { - -static void doWhenNoHandlerFound(const HttpRequestImplPtr &req, - std::function &&callback) +static void doWhenNoHandlerFound(const HttpRequestImplPtr &req, std::function &&callback) { if (req->path() == "/" && !HttpAppFrameworkImpl::instance().getHomePage().empty()) { - // auto resp = drogon::HttpResponse::newRedirectionResponse("/" + HttpAppFrameworkImpl::instance().getHomePage()); + // auto resp = drogon::HttpResponse::newRedirectionResponse("/" + + // HttpAppFrameworkImpl::instance().getHomePage()); // callback(resp); // Redirect on the server side req->setPath("/" + HttpAppFrameworkImpl::instance().getHomePage()); @@ -39,7 +38,7 @@ static void doWhenNoHandlerFound(const HttpRequestImplPtr &req, callback(resp); } -} // namespace drogon +} // namespace drogon void HttpControllersRouter::init(const std::vector &ioLoops) { std::string regString; @@ -62,7 +61,7 @@ void HttpControllersRouter::init(const std::vector &ioLoop } } if (regString.length() > 0) - regString.resize(regString.length() - 1); //remove the last '|' + regString.resize(regString.length() - 1); // remove the last '|' LOG_TRACE << "regex string:" << regString; _ctrlRegex = std::regex(regString, std::regex_constants::icase); } @@ -76,10 +75,11 @@ std::vector> HttpControllersRou { if (item._binders[i]) { - auto description = item._binders[i]->_handlerName.empty() ? std::string("Handler: ") + item._binders[i]->_binderPtr->handlerName() : std::string("HttpController: ") + item._binders[i]->_handlerName; - auto info = std::tuple(item._pathPattern, - (HttpMethod)i, - std::move(description)); + auto description = item._binders[i]->_handlerName.empty() + ? std::string("Handler: ") + item._binders[i]->_binderPtr->handlerName() + : std::string("HttpController: ") + item._binders[i]->_handlerName; + auto info = + std::tuple(item._pathPattern, (HttpMethod)i, std::move(description)); ret.emplace_back(std::move(info)); } } @@ -92,7 +92,7 @@ void HttpControllersRouter::addHttpPath(const std::string &path, const std::vector &filters, const std::string &handlerName) { - //Path is like /api/v1/service/method/{1}/{2}/xxx... + // Path is like /api/v1/service/method/{1}/{2}/xxx... std::vector places; std::string tmpPath = path; std::string paras = ""; @@ -112,8 +112,7 @@ void HttpControllersRouter::addHttpPath(const std::string &path, size_t place = (size_t)std::stoi(results[1].str()); if (place > binder->paramCount() || place == 0) { - LOG_ERROR << "parameter placeholder(value=" << place << ") out of range (1 to " - << binder->paramCount() << ")"; + LOG_ERROR << "parameter placeholder(value=" << place << ") out of range (1 to " << binder->paramCount() << ")"; exit(0); } places.push_back(place); @@ -131,8 +130,8 @@ void HttpControllersRouter::addHttpPath(const std::string &path, size_t place = (size_t)std::stoi(results[2].str()); if (place > binder->paramCount() || place == 0) { - LOG_ERROR << "parameter placeholder(value=" << place << ") out of range (1 to " - << binder->paramCount() << ")"; + LOG_ERROR << "parameter placeholder(value=" << place << ") out of range (1 to " << binder->paramCount() + << ")"; exit(0); } parametersPlaces[results[1].str()] = place; @@ -201,7 +200,7 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req, bool needSetJsessionid, std::string &&sessionId) { - //Find http controller + // Find http controller if (_ctrlRegex.mark_count() > 0) { std::smatch result; @@ -209,7 +208,8 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req, { for (size_t i = 1; i < result.size(); i++) { - //TODO: Is there any better way to find the sub-match index without using loop? + // TODO: Is there any better way to find the sub-match index without + // using loop? if (!result[i].matched) continue; if (i <= _ctrlVector.size()) @@ -221,7 +221,7 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req, auto &binder = routerItem._binders[req->method()]; if (!binder) { - //Invalid Http Method + // Invalid Http Method auto res = drogon::HttpResponse::newHttpResponse(); if (req->method() != Options) { @@ -247,50 +247,69 @@ void HttpControllersRouter::route(const HttpRequestImplPtr &req, { auto &filters = binder->_filters; auto sessionIdPtr = std::make_shared(std::move(sessionId)); - auto callbackPtr = std::make_shared>(std::move(callback)); - FiltersFunction::doFilters(filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder, &routerItem]() { - doPreHandlingAdvices(binder, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); - }); + auto callbackPtr = + std::make_shared>(std::move(callback)); + FiltersFunction::doFilters( + filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder, &routerItem]() { + doPreHandlingAdvices(binder, + routerItem, + req, + std::move(*callbackPtr), + needSetJsessionid, + std::move(*sessionIdPtr)); + }); } else { - doPreHandlingAdvices(binder, routerItem, req, std::move(callback), needSetJsessionid, std::move(sessionId)); + doPreHandlingAdvices( + binder, routerItem, req, std::move(callback), needSetJsessionid, std::move(sessionId)); } } else { auto callbackPtr = std::make_shared>(std::move(callback)); - doAdvicesChain(_postRoutingAdvices, - 0, - req, - callbackPtr, - [&binder, sessionId = std::move(sessionId), callbackPtr, req, needSetJsessionid, this, &routerItem]() mutable { - if (!binder->_filters.empty()) - { - auto &filters = binder->_filters; - auto sessionIdPtr = std::make_shared(std::move(sessionId)); - FiltersFunction::doFilters(filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder, &routerItem]() { - doPreHandlingAdvices(binder, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); - }); - } - else - { - doPreHandlingAdvices(binder, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(sessionId)); - } - }); + doAdvicesChain(_postRoutingAdvices, 0, req, callbackPtr, [ + &binder, + sessionId = std::move(sessionId), + callbackPtr, + req, + needSetJsessionid, + this, + &routerItem + ]() mutable { + if (!binder->_filters.empty()) + { + auto &filters = binder->_filters; + auto sessionIdPtr = std::make_shared(std::move(sessionId)); + FiltersFunction::doFilters( + filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder, &routerItem]() { + doPreHandlingAdvices(binder, + routerItem, + req, + std::move(*callbackPtr), + needSetJsessionid, + std::move(*sessionIdPtr)); + }); + } + else + { + doPreHandlingAdvices( + binder, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(sessionId)); + } + }); } } } } else { - //No handler found + // No handler found doWhenNoHandlerFound(req, std::move(callback)); } } else { - //No handler found + // No handler found doWhenNoHandlerFound(req, std::move(callback)); } } @@ -303,18 +322,19 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP std::string &&sessionId) { HttpResponsePtr &responsePtr = ctrlBinderPtr->_responsePtrMap[req->getLoop()]; - if (responsePtr && (responsePtr->expiredTime() == 0 || (trantor::Date::now() < responsePtr->creationDate().after(responsePtr->expiredTime())))) + if (responsePtr && (responsePtr->expiredTime() == 0 || + (trantor::Date::now() < responsePtr->creationDate().after(responsePtr->expiredTime())))) { - //use cached response! + // use cached response! LOG_TRACE << "Use cached response"; if (!needSetJsessionid || responsePtr->statusCode() == k404NotFound) invokeCallback(callback, req, responsePtr); else { - //make a copy response; + // make a copy response; auto newResp = std::make_shared(*std::dynamic_pointer_cast(responsePtr)); - newResp->setExpiredTime(-1); //make it temporary + newResp->setExpiredTime(-1); // make it temporary newResp->addCookie("JSESSIONID", sessionId); invokeCallback(callback, req, newResp); } @@ -339,8 +359,7 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP auto qureyPara = req->getParameters(); for (auto const ¶meter : qureyPara) { - if (ctrlBinderPtr->_queryParametersPlaces.find(parameter.first) != - ctrlBinderPtr->_queryParametersPlaces.end()) + if (ctrlBinderPtr->_queryParametersPlaces.find(parameter.first) != ctrlBinderPtr->_queryParametersPlaces.end()) { auto place = ctrlBinderPtr->_queryParametersPlaces.find(parameter.first)->second; if (place > params.size()) @@ -350,42 +369,41 @@ void HttpControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlBinderP } } std::list paraList; - for (auto &p : params) ///Use reference + for (auto &p : params) /// Use reference { LOG_TRACE << p; paraList.push_back(std::move(p)); } - ctrlBinderPtr->_binderPtr->handleHttpRequest(paraList, req, [=, callback = std::move(callback), sessionId = std::move(sessionId)](const HttpResponsePtr &resp) { - LOG_TRACE << "http resp:needSetJsessionid=" << needSetJsessionid << ";JSESSIONID=" << sessionId; - auto newResp = resp; - if (resp->expiredTime() >= 0) - { - //cache the response; - std::dynamic_pointer_cast(resp)->makeHeaderString(); - auto loop = req->getLoop(); - if (loop->isInLoopThread()) - { - ctrlBinderPtr->_responsePtrMap[loop] = resp; - } - else - { - req->getLoop()->queueInLoop([loop, resp, ctrlBinderPtr]() { - ctrlBinderPtr->_responsePtrMap[loop] = resp; - }); - } - } - if (needSetJsessionid && resp->statusCode() != k404NotFound) - { + ctrlBinderPtr->_binderPtr->handleHttpRequest( + paraList, req, [ =, callback = std::move(callback), sessionId = std::move(sessionId) ](const HttpResponsePtr &resp) { + LOG_TRACE << "http resp:needSetJsessionid=" << needSetJsessionid << ";JSESSIONID=" << sessionId; + auto newResp = resp; if (resp->expiredTime() >= 0) { - //make a copy - newResp = std::make_shared(*std::dynamic_pointer_cast(resp)); - newResp->setExpiredTime(-1); //make it temporary + // cache the response; + std::dynamic_pointer_cast(resp)->makeHeaderString(); + auto loop = req->getLoop(); + if (loop->isInLoopThread()) + { + ctrlBinderPtr->_responsePtrMap[loop] = resp; + } + else + { + req->getLoop()->queueInLoop([loop, resp, ctrlBinderPtr]() { ctrlBinderPtr->_responsePtrMap[loop] = resp; }); + } } - newResp->addCookie("JSESSIONID", sessionId); - } - invokeCallback(callback, req, newResp); - }); + if (needSetJsessionid && resp->statusCode() != k404NotFound) + { + if (resp->expiredTime() >= 0) + { + // make a copy + newResp = std::make_shared(*std::dynamic_pointer_cast(resp)); + newResp->setExpiredTime(-1); // make it temporary + } + newResp->addCookie("JSESSIONID", sessionId); + } + invokeCallback(callback, req, newResp); + }); return; } @@ -437,20 +455,23 @@ void HttpControllersRouter::doPreHandlingAdvices(const CtrlBinderPtr &ctrlBinder { auto callbackPtr = std::make_shared>(std::move(callback)); auto sessionIdPtr = std::make_shared(std::move(sessionId)); - doAdvicesChain(_preHandlingAdvices, - 0, - req, - std::make_shared>([callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) { - if (!needSetJsessionid || resp->statusCode() == k404NotFound) - (*callbackPtr)(resp); - else - { - resp->addCookie("JSESSIONID", *sessionIdPtr); - (*callbackPtr)(resp); - } - }), - [this, ctrlBinderPtr, &routerItem, req, callbackPtr, needSetJsessionid, sessionIdPtr]() { - doControllerHandler(ctrlBinderPtr, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); - }); + doAdvicesChain( + _preHandlingAdvices, + 0, + req, + std::make_shared>( + [callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) { + if (!needSetJsessionid || resp->statusCode() == k404NotFound) + (*callbackPtr)(resp); + else + { + resp->addCookie("JSESSIONID", *sessionIdPtr); + (*callbackPtr)(resp); + } + }), + [this, ctrlBinderPtr, &routerItem, req, callbackPtr, needSetJsessionid, sessionIdPtr]() { + doControllerHandler( + ctrlBinderPtr, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); + }); } } \ No newline at end of file diff --git a/lib/src/HttpControllersRouter.h b/lib/src/HttpControllersRouter.h index 10e2178b..129ae818 100644 --- a/lib/src/HttpControllersRouter.h +++ b/lib/src/HttpControllersRouter.h @@ -2,7 +2,7 @@ * * HttpControllersRouter.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,39 +13,32 @@ */ #pragma once +#include "AOPAdvice.h" #include "HttpRequestImpl.h" #include "HttpResponseImpl.h" -#include "AOPAdvice.h" -#include +#include #include #include -#include +#include +#include #include #include -#include -#include -#include +#include +#include namespace drogon { class HttpControllersRouter : public trantor::NonCopyable { -public: - HttpControllersRouter(const std::deque> - &postRoutingAdvices, - const std::deque> - &postRoutingObservers, - const std::vector> - &preHandlingAdvices, - const std::vector> - &preHandlingObservers, - const std::deque> - &postHandlingAdvices) + public: + HttpControllersRouter( + const std::deque> + &postRoutingAdvices, + const std::deque> &postRoutingObservers, + const std::vector> + &preHandlingAdvices, + const std::vector> &preHandlingObservers, + const std::deque> &postHandlingAdvices) : _postRoutingAdvices(postRoutingAdvices), _preHandlingAdvices(preHandlingAdvices), _postRoutingObservers(postRoutingObservers), @@ -65,7 +58,7 @@ public: std::string &&sessionId); std::vector> getHandlersInfo() const; -private: + private: struct CtrlBinder { internal::HttpBinderBasePtr _binderPtr; @@ -83,25 +76,19 @@ private: std::string _pathParameterPattern; std::string _pathPattern; std::regex _regex; - CtrlBinderPtr _binders[Invalid] = {nullptr}; //The enum value of Invalid is the http methods number + CtrlBinderPtr _binders[Invalid] = {nullptr}; // The enum value of Invalid is the http methods number }; std::vector _ctrlVector; std::mutex _ctrlMutex; std::regex _ctrlRegex; - const std::deque> &_postRoutingAdvices; - const std::vector> &_preHandlingAdvices; - const std::deque> - &_postRoutingObservers; - const std::vector> - &_preHandlingObservers; - const std::deque> - &_postHandlingAdvices; + const std::deque> + &_postRoutingAdvices; + const std::vector> + &_preHandlingAdvices; + const std::deque> &_postRoutingObservers; + const std::vector> &_preHandlingObservers; + const std::deque> &_postHandlingAdvices; void doPreHandlingAdvices(const CtrlBinderPtr &ctrlBinderPtr, const HttpControllerRouterItem &routerItem, @@ -127,4 +114,4 @@ private: callback(resp); } }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/HttpFileUploadRequest.cc b/lib/src/HttpFileUploadRequest.cc index 8cfc7535..eb266b7f 100644 --- a/lib/src/HttpFileUploadRequest.cc +++ b/lib/src/HttpFileUploadRequest.cc @@ -2,7 +2,7 @@ * * HttpFileUploadRequest.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -18,9 +18,7 @@ using namespace drogon; HttpFileUploadRequest::HttpFileUploadRequest(const std::vector &files) - : HttpRequestImpl(nullptr), - _boundary(utils::genRandomString(32)), - _files(files) + : HttpRequestImpl(nullptr), _boundary(utils::genRandomString(32)), _files(files) { setMethod(drogon::Post); setVersion(drogon::HttpRequest::kHttp11); diff --git a/lib/src/HttpFileUploadRequest.h b/lib/src/HttpFileUploadRequest.h index 21e124e4..88e379b8 100644 --- a/lib/src/HttpFileUploadRequest.h +++ b/lib/src/HttpFileUploadRequest.h @@ -2,7 +2,7 @@ * * HttpFileUploadRequest.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -19,12 +19,17 @@ namespace drogon { - class HttpFileUploadRequest : public HttpRequestImpl { public: - const std::string &boundary() const { return _boundary; } - const std::vector &files() const { return _files; } + const std::string &boundary() const + { + return _boundary; + } + const std::vector &files() const + { + return _files; + } explicit HttpFileUploadRequest(const std::vector &files); private: @@ -32,4 +37,4 @@ class HttpFileUploadRequest : public HttpRequestImpl std::vector _files; }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc old mode 100755 new mode 100644 index 263fcb01..65b8e5a1 --- a/lib/src/HttpRequestImpl.cc +++ b/lib/src/HttpRequestImpl.cc @@ -2,7 +2,7 @@ * * HttpRequestImpl.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,8 +15,8 @@ #include "HttpRequestImpl.h" #include "HttpFileUploadRequest.h" #include -#include #include +#include #include using namespace drogon; @@ -28,9 +28,9 @@ void HttpRequestImpl::parseParameters() const return; std::string type = getHeaderBy("content-type"); std::transform(type.begin(), type.end(), type.begin(), tolower); - if (_method == Get || (_method == Post && (type == "" || type.find("application/x-www-form-urlencoded") != std::string::npos))) + if (_method == Get || + (_method == Post && (type == "" || type.find("application/x-www-form-urlencoded") != std::string::npos))) { - std::string::size_type pos = 0; while ((input[pos] == '?' || isspace(input[pos])) && pos < input.length()) { @@ -75,7 +75,7 @@ void HttpRequestImpl::parseParameters() const } if (type.find("application/json") != std::string::npos) { - //parse json data in request + // parse json data in request _jsonPtr = std::make_shared(); Json::CharReaderBuilder builder; builder["collectComments"] = false; @@ -98,26 +98,26 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const { switch (_method) { - case Get: - output->append("GET "); - break; - case Post: - output->append("POST "); - break; - case Head: - output->append("HEAD "); - break; - case Put: - output->append("PUT "); - break; - case Delete: - output->append("DELETE "); - break; - case Options: - output->append("OPTIONS "); - break; - default: - return; + case Get: + output->append("GET "); + break; + case Post: + output->append("POST "); + break; + case Head: + output->append("HEAD "); + break; + case Put: + output->append("PUT "); + break; + case Delete: + output->append("DELETE "); + break; + case Options: + output->append("OPTIONS "); + break; + default: + return; } if (!_path.empty()) @@ -153,7 +153,6 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const } else { - output->append("?"); } output->append(content); @@ -161,9 +160,12 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const } else if (_contentType == CT_APPLICATION_JSON) { - ///Can't set parameters in content in this case - LOG_ERROR << "You can't set parameters in the query string when the request content type is JSON and http method is POST or PUT"; - LOG_ERROR << "Please put these parameters into the path or into the json string"; + /// Can't set parameters in content in this case + LOG_ERROR << "You can't set parameters in the query string when the " + "request content type is JSON and http method " + "is POST or PUT"; + LOG_ERROR << "Please put these parameters into the path or into the json " + "string"; content.clear(); } } @@ -217,7 +219,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const { std::streambuf *pbuf = infile.rdbuf(); std::streamsize filesize = pbuf->pubseekoff(0, infile.end); - pbuf->pubseekoff(0, infile.beg); // rewind + pbuf->pubseekoff(0, infile.beg); // rewind std::string str; str.resize(filesize); pbuf->sgetn(&str[0], filesize); @@ -234,7 +236,8 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const if (!content.empty() || !_content.empty()) { char buf[64]; - auto len = snprintf(buf, sizeof(buf), "Content-Length: %lu\r\n", static_cast(content.length() + _content.length())); + auto len = snprintf( + buf, sizeof(buf), "Content-Length: %lu\r\n", static_cast(content.length() + _content.length())); output->append(buf, len); if (_contentTypeString.empty()) { @@ -263,7 +266,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const output->append(it->second); output->append(";"); } - output->unwrite(1); //delete last ';' + output->unwrite(1); // delete last ';' output->append("\r\n"); } @@ -277,7 +280,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const void HttpRequestImpl::addHeader(const char *start, const char *colon, const char *end) { std::string field(start, colon); - //Field name is case-insensitive.so we transform it to lower;(rfc2616-4.2) + // Field name is case-insensitive.so we transform it to lower;(rfc2616-4.2) std::transform(field.begin(), field.end(), field.begin(), ::tolower); ++colon; while (colon < end && isspace(*colon)) diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h old mode 100755 new mode 100644 index 24851b7d..13bec28b --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -2,7 +2,7 @@ * * HttpRequestImpl.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,44 +15,42 @@ #pragma once #include "HttpUtils.h" -#include #include #include +#include -#include +#include +#include #include #include -#include -#include +#include -#include +#include #include #include -#include #include #include +#include using std::string; using namespace trantor; namespace drogon { - class HttpRequestImpl : public HttpRequest { -public: + public: friend class HttpRequestParser; explicit HttpRequestImpl(trantor::EventLoop *loop) - : _method(Invalid), - _version(kUnknown), - _date(trantor::Date::now()), - _contentLen(0), - _loop(loop) + : _method(Invalid), _version(kUnknown), _date(trantor::Date::now()), _contentLen(0), _loop(loop) { } - trantor::EventLoop *getLoop() { return _loop; } + trantor::EventLoop *getLoop() + { + return _loop; + } void setVersion(Version v) { @@ -66,62 +64,61 @@ public: bool setMethod(const char *start, const char *end) { - assert(_method == Invalid); string_view m(start, end - start); switch (m.length()) { - case 3: - if (m == "GET") - { - _method = Get; - } - else if (m == "PUT") - { - _method = Put; - } - else - { + case 3: + if (m == "GET") + { + _method = Get; + } + else if (m == "PUT") + { + _method = Put; + } + else + { + _method = Invalid; + } + break; + case 4: + if (m == "POST") + { + _method = Post; + } + else if (m == "HEAD") + { + _method = Head; + } + else + { + _method = Invalid; + } + break; + case 6: + if (m == "DELETE") + { + _method = Delete; + } + else + { + _method = Invalid; + } + break; + case 7: + if (m == "OPTIONS") + { + _method = Options; + } + else + { + _method = Invalid; + } + break; + default: _method = Invalid; - } - break; - case 4: - if (m == "POST") - { - _method = Post; - } - else if (m == "HEAD") - { - _method = Head; - } - else - { - _method = Invalid; - } - break; - case 6: - if (m == "DELETE") - { - _method = Delete; - } - else - { - _method = Invalid; - } - break; - case 7: - if (m == "OPTIONS") - { - _method = Options; - } - else - { - _method = Invalid; - } - break; - default: - _method = Invalid; - break; + break; } // if (_method != Invalid) @@ -156,26 +153,26 @@ public: const char *result = "UNKNOWN"; switch (_method) { - case Get: - result = "GET"; - break; - case Post: - result = "POST"; - break; - case Head: - result = "HEAD"; - break; - case Put: - result = "PUT"; - break; - case Delete: - result = "DELETE"; - break; - case Options: - result = "OPTIONS"; - break; - default: - break; + case Get: + result = "GET"; + break; + case Post: + result = "POST"; + break; + case Head: + result = "HEAD"; + break; + case Put: + result = "PUT"; + break; + case Delete: + result = "DELETE"; + break; + case Options: + result = "OPTIONS"; + break; + default: + break; } return result; } @@ -196,7 +193,8 @@ public: return _parameters; } - virtual const std::string &getParameter(const std::string &key, const std::string &defaultVal = std::string()) const override + virtual const std::string &getParameter(const std::string &key, + const std::string &defaultVal = std::string()) const override { parseParametersOnce(); auto iter = _parameters.find(key); @@ -386,7 +384,8 @@ public: setContentType(std::string(typeStr.data(), typeStr.length())); } - // virtual void setContentTypeCodeAndCharacterSet(ContentType type, const std::string &charSet = "utf-8") override + // virtual void setContentTypeCodeAndCharacterSet(ContentType type, const + // std::string &charSet = "utf-8") override // { // _contentType = type; // setContentType(webContentTypeAndCharsetToString(type, charSet)); @@ -407,7 +406,7 @@ public: _matchedPathPattern = pathPattern; } -protected: + protected: friend class HttpRequest; void setContentType(const std::string &contentType) { @@ -418,11 +417,12 @@ protected: _contentTypeString = std::move(contentType); } -private: + private: void parseParameters() const; void parseParametersOnce() const { - //Not multi-thread safe but good, because we basically call this function in a single thread + // Not multi-thread safe but good, because we basically call this function + // in a single thread if (!_flagForParsingParameters) { _flagForParsingParameters = true; @@ -444,7 +444,7 @@ private: trantor::InetAddress _local; trantor::Date _date; -protected: + protected: std::string _content; size_t _contentLen; trantor::EventLoop *_loop; @@ -454,4 +454,4 @@ protected: typedef std::shared_ptr HttpRequestImplPtr; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpRequestParser.cc b/lib/src/HttpRequestParser.cc old mode 100755 new mode 100644 index 153b4338..74c98989 --- a/lib/src/HttpRequestParser.cc +++ b/lib/src/HttpRequestParser.cc @@ -2,7 +2,7 @@ * * HttpRequestParser.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,14 +12,14 @@ * */ -#include -#include -#include -#include "HttpAppFrameworkImpl.h" #include "HttpRequestParser.h" +#include "HttpAppFrameworkImpl.h" #include "HttpResponseImpl.h" #include "HttpUtils.h" +#include #include +#include +#include using namespace trantor; using namespace drogon; @@ -139,7 +139,8 @@ bool HttpRequestParser::parseRequest(MsgBuffer *buf) { if (buf->readableBytes() >= 64 * 1024) { - /// The limit for request line is 64K bytes. respone k414RequestURITooLarge + /// The limit for request line is 64K bytes. respone + /// k414RequestURITooLarge /// TODO: Make this configurable? buf->retrieveAll(); shutdownConnection(k414RequestURITooLarge); @@ -168,8 +169,7 @@ bool HttpRequestParser::parseRequest(MsgBuffer *buf) _request->_contentLen = atoi(len.c_str()); _state = HttpRequestParseState_ExpectBody; auto &expect = _request->getHeaderBy("expect"); - if (expect == "100-continue" && - _request->getVersion() >= HttpRequest::kHttp11) + if (expect == "100-continue" && _request->getVersion() >= HttpRequest::kHttp11) { if (_request->_contentLen == 0) { @@ -177,7 +177,7 @@ bool HttpRequestParser::parseRequest(MsgBuffer *buf) shutdownConnection(k400BadRequest); return false; } - //rfc2616-8.2.3 + // rfc2616-8.2.3 auto connPtr = _conn.lock(); if (connPtr) { @@ -330,8 +330,7 @@ void HttpRequestParser::popFirstRequest() _requestPipelining.pop_front(); } -void HttpRequestParser::pushResponseToPipelining(const HttpRequestPtr &req, - const HttpResponsePtr &resp) +void HttpRequestParser::pushResponseToPipelining(const HttpRequestPtr &req, const HttpResponsePtr &resp) { #ifndef NDEBUG auto conn = _conn.lock(); diff --git a/lib/src/HttpRequestParser.h b/lib/src/HttpRequestParser.h old mode 100755 new mode 100644 index b7ed634b..cb5bd957 --- a/lib/src/HttpRequestParser.h +++ b/lib/src/HttpRequestParser.h @@ -2,7 +2,7 @@ * * HttpRequestParser.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -16,11 +16,11 @@ #include "HttpRequestImpl.h" #include "WebSocketConnectionImpl.h" -#include -#include #include +#include #include #include +#include using namespace trantor; namespace drogon @@ -80,16 +80,28 @@ class HttpRequestParser { _websockConnPtr = conn; } - //to support request pipelining(rfc2616-8.1.2.2) + // to support request pipelining(rfc2616-8.1.2.2) void pushRquestToPipelining(const HttpRequestPtr &req); HttpRequestPtr getFirstRequest() const; HttpResponsePtr getFirstResponse() const; void popFirstRequest(); void pushResponseToPipelining(const HttpRequestPtr &req, const HttpResponsePtr &resp); - size_t numberOfRequestsInPipelining() const { return _requestPipelining.size(); } - bool isStop() const { return _stopWorking; } - void stop() { _stopWorking = true; } - size_t numberOfRequestsParsed() const { return _requestsCounter; } + size_t numberOfRequestsInPipelining() const + { + return _requestPipelining.size(); + } + bool isStop() const + { + return _stopWorking; + } + void stop() + { + _stopWorking = true; + } + size_t numberOfRequestsParsed() const + { + return _requestsCounter; + } private: void shutdownConnection(HttpStatusCode code); @@ -105,4 +117,4 @@ class HttpRequestParser bool _stopWorking = false; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc old mode 100755 new mode 100644 index e9ed9552..f9a8dee8 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -2,7 +2,7 @@ * * HttpResponseImpl.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,17 +12,17 @@ * */ -#include "HttpAppFrameworkImpl.h" #include "HttpResponseImpl.h" +#include "HttpAppFrameworkImpl.h" #include "HttpUtils.h" +#include #include #include -#include -#include -#include #include +#include #include #include +#include using namespace trantor; using namespace drogon; @@ -79,9 +79,10 @@ HttpResponsePtr HttpResponse::newHttpViewResponse(const std::string &viewName, c return HttpViewBase::genHttpResponse(viewName, data); } -HttpResponsePtr HttpResponse::newFileResponse(const std::string &fullPath, const std::string &attachmentFileName, ContentType type) +HttpResponsePtr HttpResponse::newFileResponse(const std::string &fullPath, + const std::string &attachmentFileName, + ContentType type) { - std::ifstream infile(fullPath, std::ifstream::binary); LOG_TRACE << "send http file:" << fullPath; if (!infile) @@ -92,12 +93,12 @@ HttpResponsePtr HttpResponse::newFileResponse(const std::string &fullPath, const auto resp = std::make_shared(); std::streambuf *pbuf = infile.rdbuf(); std::streamsize filesize = pbuf->pubseekoff(0, infile.end); - pbuf->pubseekoff(0, infile.beg); // rewind - if (HttpAppFrameworkImpl::instance().useSendfile() && - filesize > 1024 * 200) - //TODO : Is 200k an appropriate value? Or set it to be configurable + pbuf->pubseekoff(0, infile.beg); // rewind + if (HttpAppFrameworkImpl::instance().useSendfile() && filesize > 1024 * 200) + // TODO : Is 200k an appropriate value? Or set it to be configurable { - //The advantages of sendfile() can only be reflected in sending large files. + // The advantages of sendfile() can only be reflected in sending large + // files. resp->setSendfile(fullPath); } else @@ -166,8 +167,7 @@ void HttpResponseImpl::makeHeaderString(const std::shared_ptr &head } else { - - //output->append("Connection: Keep-Alive\r\n"); + // output->append("Connection: Keep-Alive\r\n"); } } headerStringPtr->append(_contentTypeString.data(), _contentTypeString.length()); @@ -214,17 +214,16 @@ std::shared_ptr HttpResponseImpl::renderToString() const httpString->append(*_fullHeaderString); } - //output cookies + // output cookies if (_cookies.size() > 0) { for (auto it = _cookies.begin(); it != _cookies.end(); ++it) { - httpString->append(it->second.cookieString()); } } - //output Date header + // output Date header httpString->append("Date: "); auto datePos = httpString->length(); httpString->append(utils::getHttpFullDate(trantor::Date::date())); @@ -253,17 +252,16 @@ std::shared_ptr HttpResponseImpl::renderHeaderForHeadMethod() const httpString->append(*_fullHeaderString); } - //output cookies + // output cookies if (_cookies.size() > 0) { for (auto it = _cookies.begin(); it != _cookies.end(); ++it) { - httpString->append(it->second.cookieString()); } } - //output Date header + // output Date header httpString->append("Date: "); httpString->append(utils::getHttpFullDate(trantor::Date::date())); httpString->append("\r\n\r\n"); diff --git a/lib/src/HttpResponseImpl.h b/lib/src/HttpResponseImpl.h old mode 100755 new mode 100644 index 26b29084..ad42a063 --- a/lib/src/HttpResponseImpl.h +++ b/lib/src/HttpResponseImpl.h @@ -2,7 +2,7 @@ * * HttpResponseImpl.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,16 +15,16 @@ #pragma once #include "HttpUtils.h" +#include #include -#include -#include -#include #include -#include -#include #include #include -#include +#include +#include +#include +#include +#include using namespace trantor; namespace drogon @@ -33,7 +33,7 @@ class HttpResponseImpl : public HttpResponse { friend class HttpResponseParser; -public: + public: HttpResponseImpl() : _statusCode(kUnknown), _creationDate(trantor::Date::now()), @@ -71,7 +71,8 @@ public: setStatusMessage(statusCodeToString(code)); } - // virtual void setStatusCode(HttpStatusCode code, const std::string &status_message) override + // virtual void setStatusCode(HttpStatusCode code, const std::string + // &status_message) override // { // _statusCode = code; // setStatusMessage(status_message); @@ -98,7 +99,8 @@ public: setContentType(webContentTypeToString(type)); } - // virtual void setContentTypeCodeAndCharacterSet(ContentType type, const std::string &charSet = "utf-8") override + // virtual void setContentTypeCodeAndCharacterSet(ContentType type, const + // std::string &charSet = "utf-8") override // { // _contentType = type; // setContentType(webContentTypeAndCharsetToString(type, charSet)); @@ -189,7 +191,7 @@ public: if (field == "set-cookie") { - //LOG_INFO<<"cookies!!!:"<(); Json::CharReaderBuilder builder; builder["collectComments"] = false; @@ -400,10 +405,10 @@ public: } } -protected: + protected: void makeHeaderString(const std::shared_ptr &headerStringPtr) const; -private: + private: std::unordered_map _headers; std::unordered_map _cookies; @@ -440,4 +445,4 @@ private: } }; typedef std::shared_ptr HttpResponseImplPtr; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpResponseParser.cc b/lib/src/HttpResponseParser.cc old mode 100755 new mode 100644 index 4cdce60c..d0fa052e --- a/lib/src/HttpResponseParser.cc +++ b/lib/src/HttpResponseParser.cc @@ -2,7 +2,7 @@ * * HttpResponseParser.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,15 +12,14 @@ * */ -#include -#include #include "HttpResponseParser.h" #include +#include +#include using namespace trantor; using namespace drogon; HttpResponseParser::HttpResponseParser(const trantor::TcpConnectionPtr &connPtr) - : _state(HttpResponseParseState::kExpectResponseLine), - _response(new HttpResponseImpl) + : _state(HttpResponseParseState::kExpectResponseLine), _response(new HttpResponseImpl) { } @@ -102,7 +101,7 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) else { const std::string &len = _response->getHeaderBy("content-length"); - //LOG_INFO << "content len=" << len; + // LOG_INFO << "content len=" << len; if (!len.empty()) { _response->_leftBodyLength = atoi(len.c_str()); @@ -118,10 +117,11 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) } else { - if (_response->statusCode() == k204NoContent || (_response->statusCode() == k101SwitchingProtocols && - _response->getHeaderBy("upgrade") == "websocket")) + if (_response->statusCode() == k204NoContent || + (_response->statusCode() == k101SwitchingProtocols && + _response->getHeaderBy("upgrade") == "websocket")) { - //The Websocket response may not have a content-length header. + // The Websocket response may not have a content-length header. _state = HttpResponseParseState::kGotAll; hasMore = false; } @@ -142,8 +142,8 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) } else if (_state == HttpResponseParseState::kExpectBody) { - //LOG_INFO << "expectBody:len=" << request_->contentLen; - //LOG_INFO << "expectBody:buf=" << buf; + // LOG_INFO << "expectBody:len=" << request_->contentLen; + // LOG_INFO << "expectBody:buf=" << buf; if (buf->readableBytes() == 0) { if (_response->_leftBodyLength == 0) @@ -168,7 +168,7 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) { _state = HttpResponseParseState::kGotAll; LOG_TRACE << "post got all:len=" << _response->_leftBodyLength; - //LOG_INFO<<"content:"<content_; + // LOG_INFO<<"content:"<content_; LOG_TRACE << "content(END)"; hasMore = false; } @@ -184,11 +184,11 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) const char *crlf = buf->findCRLF(); if (crlf) { - //chunk length line + // chunk length line std::string len(buf->peek(), crlf - buf->peek()); char *end; _response->_currentChunkLength = strtol(len.c_str(), &end, 16); - //LOG_TRACE << "chun length : " << _response->_currentChunkLength; + // LOG_TRACE << "chun length : " << _response->_currentChunkLength; if (_response->_currentChunkLength != 0) { _state = HttpResponseParseState::kExpectChunkBody; @@ -206,7 +206,7 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) } else if (_state == HttpResponseParseState::kExpectChunkBody) { - //LOG_TRACE<<"expect chunk len="<<_response->_currentChunkLength; + // LOG_TRACE<<"expect chunk len="<<_response->_currentChunkLength; if (buf->readableBytes() >= (_response->_currentChunkLength + 2)) { if (*(buf->peek() + _response->_currentChunkLength) == '\r' && @@ -219,7 +219,7 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) } else { - //error! + // error! buf->retrieveAll(); return false; } @@ -231,7 +231,7 @@ bool HttpResponseParser::parseResponse(MsgBuffer *buf) } else if (_state == HttpResponseParseState::kExpectLastEmptyChunk) { - //last empty chunk + // last empty chunk const char *crlf = buf->findCRLF(); if (crlf) { diff --git a/lib/src/HttpResponseParser.h b/lib/src/HttpResponseParser.h old mode 100755 new mode 100644 index 3506520c..4c9b3b22 --- a/lib/src/HttpResponseParser.h +++ b/lib/src/HttpResponseParser.h @@ -2,7 +2,7 @@ * * HttpResponseParser.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,11 +15,11 @@ #pragma once #include "HttpResponseImpl.h" -#include #include #include #include #include +#include using namespace trantor; namespace drogon @@ -69,4 +69,4 @@ class HttpResponseParser HttpResponseImplPtr _response; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpServer.cc b/lib/src/HttpServer.cc old mode 100755 new mode 100644 index 960005a8..3564a8bd --- a/lib/src/HttpServer.cc +++ b/lib/src/HttpServer.cc @@ -2,7 +2,7 @@ * * HttpServer.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,14 +14,14 @@ #include "HttpServer.h" -#include +#include "HttpAppFrameworkImpl.h" #include "HttpRequestParser.h" #include "HttpResponseImpl.h" -#include "HttpAppFrameworkImpl.h" #include #include #include #include +#include using namespace std::placeholders; using namespace drogon; @@ -32,8 +32,7 @@ static bool isWebSocket(const HttpRequestImplPtr &req) auto &headers = req->headers(); if (headers.find("upgrade") == headers.end() || headers.find("connection") == headers.end()) return false; - if (req->getHeaderBy("connection").find("Upgrade") != std::string::npos && - req->getHeaderBy("upgrade") == "websocket") + if (req->getHeaderBy("connection").find("Upgrade") != std::string::npos && req->getHeaderBy("upgrade") == "websocket") { LOG_TRACE << "new websocket request"; @@ -63,9 +62,7 @@ static void defaultConnectionCallback(const trantor::TcpConnectionPtr &conn) return; } -HttpServer::HttpServer(EventLoop *loop, - const InetAddress &listenAddr, - const std::string &name) +HttpServer::HttpServer(EventLoop *loop, const InetAddress &listenAddr, const std::string &name) #ifdef __linux__ : _server(loop, listenAddr, name.c_str()), #else @@ -75,10 +72,8 @@ HttpServer::HttpServer(EventLoop *loop, _newWebsocketCallback(defaultWebSockAsyncCallback), _connectionCallback(defaultConnectionCallback) { - _server.setConnectionCallback( - std::bind(&HttpServer::onConnection, this, _1)); - _server.setRecvMessageCallback( - std::bind(&HttpServer::onMessage, this, _1, _2)); + _server.setConnectionCallback(std::bind(&HttpServer::onConnection, this, _1)); + _server.setRecvMessageCallback(std::bind(&HttpServer::onMessage, this, _1, _2)); } HttpServer::~HttpServer() @@ -87,8 +82,7 @@ HttpServer::~HttpServer() void HttpServer::start() { - LOG_TRACE << "HttpServer[" << _server.name() - << "] starts listenning on " << _server.ipPort(); + LOG_TRACE << "HttpServer[" << _server.name() << "] starts listenning on " << _server.ipPort(); _server.start(); } @@ -111,7 +105,7 @@ void HttpServer::onConnection(const TcpConnectionPtr &conn) requestParser->webSocketConn()->onClose(); } #if (CXX_STD > 14) - conn->getMutableContext()->reset(); //reset(): since c++17 + conn->getMutableContext()->reset(); // reset(): since c++17 #else conn->getMutableContext()->clear(); #endif @@ -119,25 +113,24 @@ void HttpServer::onConnection(const TcpConnectionPtr &conn) } } -void HttpServer::onMessage(const TcpConnectionPtr &conn, - MsgBuffer *buf) +void HttpServer::onMessage(const TcpConnectionPtr &conn, MsgBuffer *buf) { HttpRequestParser *requestParser = any_cast(conn->getMutableContext()); - // With the pipelining feature or web socket, it is possible to receice multiple messages at once, so + // With the pipelining feature or web socket, it is possible to receice + // multiple messages at once, so // the while loop is necessary if (requestParser->webSocketConn()) { - //Websocket payload + // Websocket payload requestParser->webSocketConn()->onNewMessage(conn, buf); } else { while (buf->readableBytes() > 0) { - if (requestParser->isStop()) { - //The number of requests has reached the limit. + // The number of requests has reached the limit. buf->retrieveAll(); return; } @@ -160,7 +153,8 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, { requestParser->setWebsockConnection(wsConn); } - auto httpString = std::dynamic_pointer_cast(resp)->renderToString(); + auto httpString = + std::dynamic_pointer_cast(resp)->renderToString(); conn->send(httpString); }, wsConn); @@ -180,8 +174,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn, void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestImplPtr &req) { const std::string &connection = req->getHeaderBy("connection"); - bool _close = connection == "close" || - (req->getVersion() == HttpRequestImpl::kHttp10 && connection != "Keep-Alive"); + bool _close = connection == "close" || (req->getVersion() == HttpRequestImpl::kHttp10 && connection != "Keep-Alive"); bool isHeadMethod = (req->method() == Head); if (isHeadMethod) @@ -210,18 +203,15 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestImplPt auto newResp = response; auto &sendfileName = std::dynamic_pointer_cast(newResp)->sendfileName(); - if (HttpAppFramework::instance().isGzipEnabled() && - sendfileName.empty() && + if (HttpAppFramework::instance().isGzipEnabled() && sendfileName.empty() && req->getHeaderBy("accept-encoding").find("gzip") != std::string::npos && std::dynamic_pointer_cast(response)->getHeaderBy("content-encoding").empty() && - response->getContentType() < CT_APPLICATION_OCTET_STREAM && - response->getBody().length() > 1024) + response->getContentType() < CT_APPLICATION_OCTET_STREAM && response->getBody().length() > 1024) { - //use gzip + // use gzip LOG_TRACE << "Use gzip to compress the body"; size_t zlen = response->getBody().length(); - auto strCompress = utils::gzipCompress(response->getBody().data(), - response->getBody().length()); + auto strCompress = utils::gzipCompress(response->getBody().data(), response->getBody().length()); if (strCompress) { if (zlen > 0) @@ -229,7 +219,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestImplPt LOG_TRACE << "length after compressing:" << zlen; if (response->expiredTime() >= 0) { - //cached response,we need to make a clone + // cached response,we need to make a clone newResp = std::make_shared(*std::dynamic_pointer_cast(response)); newResp->setExpiredTime(-1); } @@ -275,7 +265,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestImplPt } else { - //some earlier requests are waiting for responses; + // some earlier requests are waiting for responses; requestParser->pushResponseToPipelining(req, newResp); } } @@ -307,7 +297,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestImplPt } else { - //some earlier requests are waiting for responses; + // some earlier requests are waiting for responses; requestParser->pushResponseToPipelining(req, newResp); } } @@ -316,9 +306,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestImplPt }); } -void HttpServer::sendResponse(const TcpConnectionPtr &conn, - const HttpResponsePtr &response, - bool isHeadMethod) +void HttpServer::sendResponse(const TcpConnectionPtr &conn, const HttpResponsePtr &response, bool isHeadMethod) { conn->getLoop()->assertInLoopThread(); auto respImplPtr = std::dynamic_pointer_cast(response); diff --git a/lib/src/HttpServer.h b/lib/src/HttpServer.h old mode 100755 new mode 100644 index 1b8290af..89031e6b --- a/lib/src/HttpServer.h +++ b/lib/src/HttpServer.h @@ -2,7 +2,7 @@ * * HttpServer.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,15 +14,15 @@ #pragma once -#include "WebSocketConnectionImpl.h" #include "HttpRequestImpl.h" -#include +#include "WebSocketConnectionImpl.h" #include +#include +#include +#include #include #include #include -#include -#include using namespace trantor; namespace drogon @@ -34,18 +34,18 @@ class HttpServer : trantor::NonCopyable { public: typedef std::function &&)> HttpAsyncCallback; - typedef std::function &&, - const WebSocketConnectionImplPtr &)> + typedef std::function< + void(const HttpRequestImplPtr &, std::function &&, const WebSocketConnectionImplPtr &)> WebSocketNewAsyncCallback; - HttpServer(EventLoop *loop, - const InetAddress &listenAddr, - const std::string &name); + HttpServer(EventLoop *loop, const InetAddress &listenAddr, const std::string &name); ~HttpServer(); - EventLoop *getLoop() const { return _server.getLoop(); } + EventLoop *getLoop() const + { + return _server.getLoop(); + } void setHttpAsyncCallback(const HttpAsyncCallback &cb) { @@ -86,8 +86,7 @@ class HttpServer : trantor::NonCopyable private: void onConnection(const TcpConnectionPtr &conn); - void onMessage(const TcpConnectionPtr &, - MsgBuffer *); + void onMessage(const TcpConnectionPtr &, MsgBuffer *); void onRequest(const TcpConnectionPtr &, const HttpRequestImplPtr &); void sendResponse(const TcpConnectionPtr &, const HttpResponsePtr &, bool isHeadMethod); trantor::TcpServer _server; @@ -96,4 +95,4 @@ class HttpServer : trantor::NonCopyable trantor::ConnectionCallback _connectionCallback; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpSimpleControllersRouter.cc b/lib/src/HttpSimpleControllersRouter.cc index 7538c15a..af8226dd 100644 --- a/lib/src/HttpSimpleControllersRouter.cc +++ b/lib/src/HttpSimpleControllersRouter.cc @@ -2,7 +2,7 @@ * * HttpSimpleControllersRouter.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,10 +12,10 @@ * */ -#include "FiltersFunction.h" #include "HttpSimpleControllersRouter.h" -#include "HttpAppFrameworkImpl.h" #include "AOPAdvice.h" +#include "FiltersFunction.h" +#include "HttpAppFrameworkImpl.h" using namespace drogon; @@ -72,7 +72,7 @@ void HttpSimpleControllersRouter::registerHttpSimpleController(const std::string } else { - //All HTTP methods are valid + // All HTTP methods are valid for (size_t i = 0; i < Invalid; i++) { item._binders[i] = binder; @@ -96,7 +96,7 @@ void HttpSimpleControllersRouter::route(const HttpRequestImplPtr &req, auto &binder = ctrlInfo._binders[req->method()]; if (!binder) { - //Invalid Http Method + // Invalid Http Method auto res = drogon::HttpResponse::newHttpResponse(); if (req->method() != Options) { @@ -109,7 +109,7 @@ void HttpSimpleControllersRouter::route(const HttpRequestImplPtr &req, callback(res); return; } - //Do post routing advices. + // Do post routing advices. if (!_postRoutingObservers.empty()) { for (auto &observer : _postRoutingObservers) @@ -125,7 +125,8 @@ void HttpSimpleControllersRouter::route(const HttpRequestImplPtr &req, auto sessionIdPtr = std::make_shared(std::move(sessionId)); auto callbackPtr = std::make_shared>(std::move(callback)); FiltersFunction::doFilters(filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder]() mutable { - doPreHandlingAdvices(binder, ctrlInfo, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); + doPreHandlingAdvices( + binder, ctrlInfo, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); }); } else @@ -136,25 +137,32 @@ void HttpSimpleControllersRouter::route(const HttpRequestImplPtr &req, } else { - auto callbackPtr = std::make_shared>(std::move(callback)); - doAdvicesChain(_postRoutingAdvices, - 0, - req, - callbackPtr, - [callbackPtr, &filters, sessionId = std::move(sessionId), req, needSetJsessionid, &ctrlInfo, this, &binder]() mutable { - if (!filters.empty()) - { - auto sessionIdPtr = std::make_shared(std::move(sessionId)); - FiltersFunction::doFilters(filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder]() mutable { - doPreHandlingAdvices(binder, ctrlInfo, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); - }); - } - else - { - doPreHandlingAdvices(binder, ctrlInfo, req, std::move(*callbackPtr), needSetJsessionid, std::move(sessionId)); - } - }); + doAdvicesChain(_postRoutingAdvices, 0, req, callbackPtr, [ + callbackPtr, + &filters, + sessionId = std::move(sessionId), + req, + needSetJsessionid, + &ctrlInfo, + this, + &binder + ]() mutable { + if (!filters.empty()) + { + auto sessionIdPtr = std::make_shared(std::move(sessionId)); + FiltersFunction::doFilters( + filters, req, callbackPtr, needSetJsessionid, sessionIdPtr, [=, &binder]() mutable { + doPreHandlingAdvices( + binder, ctrlInfo, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); + }); + } + else + { + doPreHandlingAdvices( + binder, ctrlInfo, req, std::move(*callbackPtr), needSetJsessionid, std::move(sessionId)); + } + }); } return; } @@ -172,17 +180,18 @@ void HttpSimpleControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlB if (controller) { HttpResponsePtr &responsePtr = ctrlBinderPtr->_responsePtrMap[req->getLoop()]; - if (responsePtr && (responsePtr->expiredTime() == 0 || (trantor::Date::now() < responsePtr->creationDate().after(responsePtr->expiredTime())))) + if (responsePtr && (responsePtr->expiredTime() == 0 || + (trantor::Date::now() < responsePtr->creationDate().after(responsePtr->expiredTime())))) { - //use cached response! + // use cached response! LOG_TRACE << "Use cached response"; if (!needSetJsessionid || responsePtr->statusCode() == k404NotFound) invokeCallback(callback, req, responsePtr); else { - //make a copy response; + // make a copy response; auto newResp = std::make_shared(*std::dynamic_pointer_cast(responsePtr)); - newResp->setExpiredTime(-1); //make it temporary + newResp->setExpiredTime(-1); // make it temporary newResp->addCookie("JSESSIONID", sessionId); invokeCallback(callback, req, newResp); } @@ -190,36 +199,37 @@ void HttpSimpleControllersRouter::doControllerHandler(const CtrlBinderPtr &ctrlB } else { - controller->asyncHandleHttpRequest(req, [=, callback = std::move(callback), &ctrlBinderPtr, sessionId = std::move(sessionId)](const HttpResponsePtr &resp) { - auto newResp = resp; - if (resp->expiredTime() >= 0) - { - //cache the response; - std::dynamic_pointer_cast(resp)->makeHeaderString(); - auto loop = req->getLoop(); - if (loop->isInLoopThread()) - { - ctrlBinderPtr->_responsePtrMap[loop] = resp; - } - else - { - loop->queueInLoop([loop, resp, &ctrlBinderPtr]() { - ctrlBinderPtr->_responsePtrMap[loop] = resp; - }); - } - } - if (needSetJsessionid && resp->statusCode() != k404NotFound) - { + controller->asyncHandleHttpRequest( + req, + [ =, callback = std::move(callback), &ctrlBinderPtr, sessionId = std::move(sessionId) ]( + const HttpResponsePtr &resp) { + auto newResp = resp; if (resp->expiredTime() >= 0) { - //make a copy - newResp = std::make_shared(*std::dynamic_pointer_cast(resp)); - newResp->setExpiredTime(-1); //make it temporary + // cache the response; + std::dynamic_pointer_cast(resp)->makeHeaderString(); + auto loop = req->getLoop(); + if (loop->isInLoopThread()) + { + ctrlBinderPtr->_responsePtrMap[loop] = resp; + } + else + { + loop->queueInLoop([loop, resp, &ctrlBinderPtr]() { ctrlBinderPtr->_responsePtrMap[loop] = resp; }); + } } - newResp->addCookie("JSESSIONID", sessionId); - } - invokeCallback(callback, req, newResp); - }); + if (needSetJsessionid && resp->statusCode() != k404NotFound) + { + if (resp->expiredTime() >= 0) + { + // make a copy + newResp = std::make_shared(*std::dynamic_pointer_cast(resp)); + newResp->setExpiredTime(-1); // make it temporary + } + newResp->addCookie("JSESSIONID", sessionId); + } + invokeCallback(callback, req, newResp); + }); } return; @@ -242,9 +252,10 @@ std::vector> HttpSimpleControll { if (item.second._binders[i]) { - auto info = std::tuple(item.first, - (HttpMethod)i, - std::string("HttpSimpleController: ") + item.second._binders[i]->_controllerName); + auto info = std::tuple( + item.first, + (HttpMethod)i, + std::string("HttpSimpleController: ") + item.second._binders[i]->_controllerName); ret.emplace_back(std::move(info)); } } @@ -320,20 +331,23 @@ void HttpSimpleControllersRouter::doPreHandlingAdvices(const CtrlBinderPtr &ctrl { auto callbackPtr = std::make_shared>(std::move(callback)); auto sessionIdPtr = std::make_shared(std::move(sessionId)); - doAdvicesChain(_preHandlingAdvices, - 0, - req, - std::make_shared>([callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) { - if (!needSetJsessionid || resp->statusCode() == k404NotFound) - (*callbackPtr)(resp); - else - { - resp->addCookie("JSESSIONID", *sessionIdPtr); - (*callbackPtr)(resp); - } - }), - [this, ctrlBinderPtr, &routerItem, req, callbackPtr, needSetJsessionid, sessionIdPtr]() { - doControllerHandler(ctrlBinderPtr, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); - }); + doAdvicesChain( + _preHandlingAdvices, + 0, + req, + std::make_shared>( + [callbackPtr, needSetJsessionid, sessionIdPtr](const HttpResponsePtr &resp) { + if (!needSetJsessionid || resp->statusCode() == k404NotFound) + (*callbackPtr)(resp); + else + { + resp->addCookie("JSESSIONID", *sessionIdPtr); + (*callbackPtr)(resp); + } + }), + [this, ctrlBinderPtr, &routerItem, req, callbackPtr, needSetJsessionid, sessionIdPtr]() { + doControllerHandler( + ctrlBinderPtr, routerItem, req, std::move(*callbackPtr), needSetJsessionid, std::move(*sessionIdPtr)); + }); } } \ No newline at end of file diff --git a/lib/src/HttpSimpleControllersRouter.h b/lib/src/HttpSimpleControllersRouter.h index 557b4f39..9a96e42d 100644 --- a/lib/src/HttpSimpleControllersRouter.h +++ b/lib/src/HttpSimpleControllersRouter.h @@ -2,7 +2,7 @@ * * HttpSimpleControllersRouter.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,43 +13,35 @@ */ #pragma once +#include "HttpControllersRouter.h" #include "HttpRequestImpl.h" #include "HttpResponseImpl.h" -#include "HttpControllersRouter.h" -#include -#include +#include #include #include -#include -#include -#include -#include +#include #include +#include +#include #include -#include +#include +#include +#include namespace drogon { - class HttpSimpleControllersRouter : public trantor::NonCopyable { public: - HttpSimpleControllersRouter(HttpControllersRouter &httpCtrlRouter, - const std::deque> - &postRoutingAdvices, - const std::deque> - &postRoutingObservers, - const std::vector> - &preHandlingAdvices, - const std::vector> - &preHandlingObservers, - const std::deque> - &postHandlingAdvices) + HttpSimpleControllersRouter( + HttpControllersRouter &httpCtrlRouter, + const std::deque> + &postRoutingAdvices, + const std::deque> &postRoutingObservers, + const std::vector> + &preHandlingAdvices, + const std::vector> &preHandlingObservers, + const std::deque> &postHandlingAdvices) : _httpCtrlsRouter(httpCtrlRouter), _postRoutingAdvices(postRoutingAdvices), _preHandlingAdvices(preHandlingAdvices), @@ -70,24 +62,16 @@ class HttpSimpleControllersRouter : public trantor::NonCopyable std::vector> getHandlersInfo() const; -private: + private: HttpControllersRouter &_httpCtrlsRouter; - const std::deque> + const std::deque> &_postRoutingAdvices; - const std::vector> + const std::vector> &_preHandlingAdvices; - const std::deque> - &_postRoutingObservers; - const std::vector> - &_preHandlingObservers; + const std::deque> &_postRoutingObservers; + const std::vector> &_preHandlingObservers; - const std::deque> - &_postHandlingAdvices; + const std::deque> &_postHandlingAdvices; struct CtrlBinder { std::shared_ptr _controller; @@ -124,9 +108,9 @@ private: { for (auto &advice : _postHandlingAdvices) { - advice(req,resp); + advice(req, resp); } callback(resp); } }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpUtils.cc b/lib/src/HttpUtils.cc index e3e9c273..5aac1332 100644 --- a/lib/src/HttpUtils.cc +++ b/lib/src/HttpUtils.cc @@ -2,7 +2,7 @@ * * HttpUtils.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -18,122 +18,121 @@ namespace drogon { - const string_view &webContentTypeToString(ContentType contenttype) { switch (contenttype) { - case CT_TEXT_HTML: - { - static string_view sv = "Content-Type: text/html; charset=utf-8\r\n"; - return sv; - } - case CT_APPLICATION_X_FORM: - { - static string_view sv = "Content-Type: application/x-www-form-urlencoded\r\n"; - return sv; - } - case CT_APPLICATION_XML: - { - static string_view sv = "Content-Type: application/xml; charset=utf-8\r\n"; - return sv; - } - case CT_APPLICATION_JSON: - { - static string_view sv = "Content-Type: application/json; charset=utf-8\r\n"; - return sv; - } - case CT_APPLICATION_X_JAVASCRIPT: - { - static string_view sv = "Content-Type: application/x-javascript; charset=utf-8\r\n"; - return sv; - } - case CT_TEXT_CSS: - { - static string_view sv = "Content-Type: text/css; charset=utf-8\r\n"; - return sv; - } - case CT_TEXT_XML: - { - static string_view sv = "Content-Type: text/xml; charset=utf-8\r\n"; - return sv; - } - case CT_TEXT_XSL: - { - static string_view sv = "Content-Type: text/xsl; charset=utf-8\r\n"; - return sv; - } - case CT_APPLICATION_OCTET_STREAM: - { - static string_view sv = "Content-Type: application/octet-stream\r\n"; - return sv; - } - case CT_IMAGE_SVG_XML: - { - static string_view sv = "Content-Type: image/svg+xml\r\n"; - return sv; - } - case CT_APPLICATION_X_FONT_TRUETYPE: - { - static string_view sv = "Content-Type: application/x-font-truetype\r\n"; - return sv; - } - case CT_APPLICATION_X_FONT_OPENTYPE: - { - static string_view sv = "Content-Type: application/x-font-opentype\r\n"; - return sv; - } - case CT_APPLICATION_FONT_WOFF: - { - static string_view sv = "Content-Type: application/font-woff\r\n"; - return sv; - } - case CT_APPLICATION_FONT_WOFF2: - { - static string_view sv = "Content-Type: application/font-woff2\r\n"; - return sv; - } - case CT_APPLICATION_VND_MS_FONTOBJ: - { - static string_view sv = "Content-Type: application/vnd.ms-fontobject\r\n"; - return sv; - } - case CT_IMAGE_PNG: - { - static string_view sv = "Content-Type: image/png\r\n"; - return sv; - } - case CT_IMAGE_JPG: - { - static string_view sv = "Content-Type: image/jpeg\r\n"; - return sv; - } - case CT_IMAGE_GIF: - { - static string_view sv = "Content-Type: image/gif\r\n"; - return sv; - } - case CT_IMAGE_XICON: - { - static string_view sv = "Content-Type: image/x-icon\r\n"; - return sv; - } - case CT_IMAGE_BMP: - { - static string_view sv = "Content-Type: image/bmp\r\n"; - return sv; - } - case CT_IMAGE_ICNS: - { - static string_view sv = "Content-Type: image/icns\r\n"; - return sv; - } - default: - case CT_TEXT_PLAIN: - { - static string_view sv = "Content-Type: text/plain; charset=utf-8\r\n"; - return sv; - } + case CT_TEXT_HTML: + { + static string_view sv = "Content-Type: text/html; charset=utf-8\r\n"; + return sv; + } + case CT_APPLICATION_X_FORM: + { + static string_view sv = "Content-Type: application/x-www-form-urlencoded\r\n"; + return sv; + } + case CT_APPLICATION_XML: + { + static string_view sv = "Content-Type: application/xml; charset=utf-8\r\n"; + return sv; + } + case CT_APPLICATION_JSON: + { + static string_view sv = "Content-Type: application/json; charset=utf-8\r\n"; + return sv; + } + case CT_APPLICATION_X_JAVASCRIPT: + { + static string_view sv = "Content-Type: application/x-javascript; charset=utf-8\r\n"; + return sv; + } + case CT_TEXT_CSS: + { + static string_view sv = "Content-Type: text/css; charset=utf-8\r\n"; + return sv; + } + case CT_TEXT_XML: + { + static string_view sv = "Content-Type: text/xml; charset=utf-8\r\n"; + return sv; + } + case CT_TEXT_XSL: + { + static string_view sv = "Content-Type: text/xsl; charset=utf-8\r\n"; + return sv; + } + case CT_APPLICATION_OCTET_STREAM: + { + static string_view sv = "Content-Type: application/octet-stream\r\n"; + return sv; + } + case CT_IMAGE_SVG_XML: + { + static string_view sv = "Content-Type: image/svg+xml\r\n"; + return sv; + } + case CT_APPLICATION_X_FONT_TRUETYPE: + { + static string_view sv = "Content-Type: application/x-font-truetype\r\n"; + return sv; + } + case CT_APPLICATION_X_FONT_OPENTYPE: + { + static string_view sv = "Content-Type: application/x-font-opentype\r\n"; + return sv; + } + case CT_APPLICATION_FONT_WOFF: + { + static string_view sv = "Content-Type: application/font-woff\r\n"; + return sv; + } + case CT_APPLICATION_FONT_WOFF2: + { + static string_view sv = "Content-Type: application/font-woff2\r\n"; + return sv; + } + case CT_APPLICATION_VND_MS_FONTOBJ: + { + static string_view sv = "Content-Type: application/vnd.ms-fontobject\r\n"; + return sv; + } + case CT_IMAGE_PNG: + { + static string_view sv = "Content-Type: image/png\r\n"; + return sv; + } + case CT_IMAGE_JPG: + { + static string_view sv = "Content-Type: image/jpeg\r\n"; + return sv; + } + case CT_IMAGE_GIF: + { + static string_view sv = "Content-Type: image/gif\r\n"; + return sv; + } + case CT_IMAGE_XICON: + { + static string_view sv = "Content-Type: image/x-icon\r\n"; + return sv; + } + case CT_IMAGE_BMP: + { + static string_view sv = "Content-Type: image/bmp\r\n"; + return sv; + } + case CT_IMAGE_ICNS: + { + static string_view sv = "Content-Type: image/icns\r\n"; + return sv; + } + default: + case CT_TEXT_PLAIN: + { + static string_view sv = "Content-Type: text/plain; charset=utf-8\r\n"; + return sv; + } } } @@ -141,237 +140,237 @@ const string_view &statusCodeToString(int code) { switch (code) { - case 100: - { - static string_view sv = "Continue"; - return sv; - } - case 101: - { - static string_view sv = "Switching Protocols"; - return sv; - } - case 200: - { - static string_view sv = "OK"; - return sv; - } - case 201: - { - static string_view sv = "Created"; - return sv; - } - case 202: - { - static string_view sv = "Accepted"; - return sv; - } - case 203: - { - static string_view sv = "Non-Authoritative Information"; - return sv; - } - case 204: - { - static string_view sv = "No Content"; - return sv; - } - case 205: - { - static string_view sv = "Reset Content"; - return sv; - } - case 206: - { - static string_view sv = "Partial Content"; - return sv; - } - case 300: - { - static string_view sv = "Multiple Choices"; - return sv; - } - case 301: - { - static string_view sv = "Moved Permanently"; - return sv; - } - case 302: - { - static string_view sv = "Found"; - return sv; - } - case 303: - { - static string_view sv = "See Other"; - return sv; - } - case 304: - { - static string_view sv = "Not Modified"; - return sv; - } - case 305: - { - static string_view sv = "Use Proxy"; - return sv; - } - case 307: - { - static string_view sv = "Temporary Redirect"; - return sv; - } - case 400: - { - static string_view sv = "Bad Request"; - return sv; - } - case 401: - { - static string_view sv = "Unauthorized"; - return sv; - } - case 402: - { - static string_view sv = "Payment Required"; - return sv; - } - case 403: - { - static string_view sv = "Forbidden"; - return sv; - } - case 404: - { - static string_view sv = "Not Found"; - return sv; - } - case 405: - { - static string_view sv = "Method Not Allowed"; - return sv; - } - case 406: - { - static string_view sv = "Not Acceptable"; - return sv; - } - case 407: - { - static string_view sv = "Proxy Authentication Required"; - return sv; - } - case 408: - { - static string_view sv = "Request Time-out"; - return sv; - } - case 409: - { - static string_view sv = "Conflict"; - return sv; - } - case 410: - { - static string_view sv = "Gone"; - return sv; - } - case 411: - { - static string_view sv = "Length Required"; - return sv; - } - case 412: - { - static string_view sv = "Precondition Failed"; - return sv; - } - case 413: - { - static string_view sv = "Request Entity Too Large"; - return sv; - } - case 414: - { - static string_view sv = "Request-URI Too Large"; - return sv; - } - case 415: - { - static string_view sv = "Unsupported Media Type"; - return sv; - } - case 416: - { - static string_view sv = "Requested range not satisfiable"; - return sv; - } - case 417: - { - static string_view sv = "Expectation Failed"; - return sv; - } - case 500: - { - static string_view sv = "Internal Server Error"; - return sv; - } - case 501: - { - static string_view sv = "Not Implemented"; - return sv; - } - case 502: - { - static string_view sv = "Bad Gateway"; - return sv; - } - case 503: - { - static string_view sv = "Service Unavailable"; - return sv; - } - case 504: - { - static string_view sv = "Gateway Time-out"; - return sv; - } - case 505: - { - static string_view sv = "HTTP Version not supported"; - return sv; - } - default: - if (code >= 100 && code < 200) + case 100: { - static string_view sv = "Informational"; + static string_view sv = "Continue"; return sv; } - else if (code >= 200 && code < 300) + case 101: { - static string_view sv = "Successful"; + static string_view sv = "Switching Protocols"; return sv; } - else if (code >= 300 && code < 400) + case 200: { - static string_view sv = "Redirection"; + static string_view sv = "OK"; return sv; } - else if (code >= 400 && code < 500) + case 201: + { + static string_view sv = "Created"; + return sv; + } + case 202: + { + static string_view sv = "Accepted"; + return sv; + } + case 203: + { + static string_view sv = "Non-Authoritative Information"; + return sv; + } + case 204: + { + static string_view sv = "No Content"; + return sv; + } + case 205: + { + static string_view sv = "Reset Content"; + return sv; + } + case 206: + { + static string_view sv = "Partial Content"; + return sv; + } + case 300: + { + static string_view sv = "Multiple Choices"; + return sv; + } + case 301: + { + static string_view sv = "Moved Permanently"; + return sv; + } + case 302: + { + static string_view sv = "Found"; + return sv; + } + case 303: + { + static string_view sv = "See Other"; + return sv; + } + case 304: + { + static string_view sv = "Not Modified"; + return sv; + } + case 305: + { + static string_view sv = "Use Proxy"; + return sv; + } + case 307: + { + static string_view sv = "Temporary Redirect"; + return sv; + } + case 400: { static string_view sv = "Bad Request"; return sv; } - else if (code >= 500 && code < 600) + case 401: { - static string_view sv = "Server Error"; + static string_view sv = "Unauthorized"; return sv; } - else + case 402: { - static string_view sv = "Undefined Error"; + static string_view sv = "Payment Required"; return sv; } + case 403: + { + static string_view sv = "Forbidden"; + return sv; + } + case 404: + { + static string_view sv = "Not Found"; + return sv; + } + case 405: + { + static string_view sv = "Method Not Allowed"; + return sv; + } + case 406: + { + static string_view sv = "Not Acceptable"; + return sv; + } + case 407: + { + static string_view sv = "Proxy Authentication Required"; + return sv; + } + case 408: + { + static string_view sv = "Request Time-out"; + return sv; + } + case 409: + { + static string_view sv = "Conflict"; + return sv; + } + case 410: + { + static string_view sv = "Gone"; + return sv; + } + case 411: + { + static string_view sv = "Length Required"; + return sv; + } + case 412: + { + static string_view sv = "Precondition Failed"; + return sv; + } + case 413: + { + static string_view sv = "Request Entity Too Large"; + return sv; + } + case 414: + { + static string_view sv = "Request-URI Too Large"; + return sv; + } + case 415: + { + static string_view sv = "Unsupported Media Type"; + return sv; + } + case 416: + { + static string_view sv = "Requested range not satisfiable"; + return sv; + } + case 417: + { + static string_view sv = "Expectation Failed"; + return sv; + } + case 500: + { + static string_view sv = "Internal Server Error"; + return sv; + } + case 501: + { + static string_view sv = "Not Implemented"; + return sv; + } + case 502: + { + static string_view sv = "Bad Gateway"; + return sv; + } + case 503: + { + static string_view sv = "Service Unavailable"; + return sv; + } + case 504: + { + static string_view sv = "Gateway Time-out"; + return sv; + } + case 505: + { + static string_view sv = "HTTP Version not supported"; + return sv; + } + default: + if (code >= 100 && code < 200) + { + static string_view sv = "Informational"; + return sv; + } + else if (code >= 200 && code < 300) + { + static string_view sv = "Successful"; + return sv; + } + else if (code >= 300 && code < 400) + { + static string_view sv = "Redirection"; + return sv; + } + else if (code >= 400 && code < 500) + { + static string_view sv = "Bad Request"; + return sv; + } + else if (code >= 500 && code < 600) + { + static string_view sv = "Server Error"; + return sv; + } + else + { + static string_view sv = "Undefined Error"; + return sv; + } } } @@ -386,92 +385,92 @@ ContentType getContentType(const std::string &filename) } switch (extName.length()) { - case 0: - return CT_APPLICATION_OCTET_STREAM; - case 2: - { - if (extName == "js") - return CT_APPLICATION_X_JAVASCRIPT; - return CT_APPLICATION_OCTET_STREAM; - } - case 3: - { - switch (extName[0]) + case 0: + return CT_APPLICATION_OCTET_STREAM; + case 2: { - case 'b': - if (extName == "bmp") - return CT_IMAGE_BMP; - break; - case 'c': - if (extName == "css") - return CT_TEXT_CSS; - break; - case 'e': - if (extName == "eot") - return CT_APPLICATION_VND_MS_FONTOBJ; - break; - case 'g': - if (extName == "gif") - return CT_IMAGE_GIF; - break; - case 'i': - if (extName == "ico") - return CT_IMAGE_XICON; - break; - case 'j': - if (extName == "jpg") - return CT_IMAGE_JPG; - break; - case 'o': - if (extName == "otf") - return CT_APPLICATION_X_FONT_OPENTYPE; - break; - case 'p': - if (extName == "png") - return CT_IMAGE_PNG; - break; - case 's': - if (extName == "svg") - return CT_IMAGE_SVG_XML; - break; - case 't': - if (extName == "txt") - return CT_TEXT_PLAIN; - else if (extName == "ttf") - return CT_APPLICATION_X_FONT_TRUETYPE; - break; - case 'x': - if (extName == "xml") - return CT_TEXT_XML; - else if (extName == "xsl") - return CT_TEXT_XSL; - break; - default: - break; + if (extName == "js") + return CT_APPLICATION_X_JAVASCRIPT; + return CT_APPLICATION_OCTET_STREAM; } - return CT_APPLICATION_OCTET_STREAM; - } - case 4: - { - if (extName == "html") - return CT_TEXT_HTML; - else if (extName == "jpeg") - return CT_IMAGE_JPG; - else if (extName == "icns") - return CT_IMAGE_ICNS; - else if (extName == "woff") - return CT_APPLICATION_FONT_WOFF; - return CT_APPLICATION_OCTET_STREAM; - } - case 5: - { - if (extName == "woff2") - return CT_APPLICATION_FONT_WOFF2; - return CT_APPLICATION_OCTET_STREAM; - } - default: - return CT_APPLICATION_OCTET_STREAM; + case 3: + { + switch (extName[0]) + { + case 'b': + if (extName == "bmp") + return CT_IMAGE_BMP; + break; + case 'c': + if (extName == "css") + return CT_TEXT_CSS; + break; + case 'e': + if (extName == "eot") + return CT_APPLICATION_VND_MS_FONTOBJ; + break; + case 'g': + if (extName == "gif") + return CT_IMAGE_GIF; + break; + case 'i': + if (extName == "ico") + return CT_IMAGE_XICON; + break; + case 'j': + if (extName == "jpg") + return CT_IMAGE_JPG; + break; + case 'o': + if (extName == "otf") + return CT_APPLICATION_X_FONT_OPENTYPE; + break; + case 'p': + if (extName == "png") + return CT_IMAGE_PNG; + break; + case 's': + if (extName == "svg") + return CT_IMAGE_SVG_XML; + break; + case 't': + if (extName == "txt") + return CT_TEXT_PLAIN; + else if (extName == "ttf") + return CT_APPLICATION_X_FONT_TRUETYPE; + break; + case 'x': + if (extName == "xml") + return CT_TEXT_XML; + else if (extName == "xsl") + return CT_TEXT_XSL; + break; + default: + break; + } + return CT_APPLICATION_OCTET_STREAM; + } + case 4: + { + if (extName == "html") + return CT_TEXT_HTML; + else if (extName == "jpeg") + return CT_IMAGE_JPG; + else if (extName == "icns") + return CT_IMAGE_ICNS; + else if (extName == "woff") + return CT_APPLICATION_FONT_WOFF; + return CT_APPLICATION_OCTET_STREAM; + } + case 5: + { + if (extName == "woff2") + return CT_APPLICATION_FONT_WOFF2; + return CT_APPLICATION_OCTET_STREAM; + } + default: + return CT_APPLICATION_OCTET_STREAM; } } -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/HttpUtils.h b/lib/src/HttpUtils.h index 26cf2cb8..572105d7 100644 --- a/lib/src/HttpUtils.h +++ b/lib/src/HttpUtils.h @@ -2,7 +2,7 @@ * * HttpUtils.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,17 +13,16 @@ */ #pragma once -#include #include -#include -#include #include +#include +#include +#include namespace drogon { - const string_view &webContentTypeToString(ContentType contenttype); const string_view &statusCodeToString(int code); ContentType getContentType(const std::string &extName); -} // namespace drogon +} // namespace drogon diff --git a/lib/src/HttpViewBase.cc b/lib/src/HttpViewBase.cc old mode 100755 new mode 100644 index 3704dd5b..286e6f4b --- a/lib/src/HttpViewBase.cc +++ b/lib/src/HttpViewBase.cc @@ -2,7 +2,7 @@ * * HttpViewBase.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,12 +12,12 @@ * */ -#include #include +#include #include -#include #include +#include using namespace drogon; HttpResponsePtr HttpViewBase::genHttpResponse(std::string viewName, const HttpViewData &data) { diff --git a/lib/src/HttpViewData.cc b/lib/src/HttpViewData.cc index 1778e2c5..757ab8aa 100644 --- a/lib/src/HttpViewData.cc +++ b/lib/src/HttpViewData.cc @@ -24,21 +24,21 @@ std::string HttpViewData::htmlTranslate(const std::string &str) { switch (ch) { - case '"': - ret.append("""); - break; - case '<': - ret.append("<"); - break; - case '>': - ret.append(">"); - break; - case '&': - ret.append("&"); - break; - default: - ret.push_back(ch); - break; + case '"': + ret.append("""); + break; + case '<': + ret.append("<"); + break; + case '>': + ret.append(">"); + break; + case '&': + ret.append("&"); + break; + default: + ret.push_back(ch); + break; } } return ret; diff --git a/lib/src/IntranetIpFilter.cc b/lib/src/IntranetIpFilter.cc index e023bffe..b7ce704a 100644 --- a/lib/src/IntranetIpFilter.cc +++ b/lib/src/IntranetIpFilter.cc @@ -2,7 +2,7 @@ * * IntranetIpFilter.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,12 +12,10 @@ * */ -#include #include "HttpResponseImpl.h" +#include using namespace drogon; -void IntranetIpFilter::doFilter(const HttpRequestPtr &req, - FilterCallback &&fcb, - FilterChainCallback &&fccb) +void IntranetIpFilter::doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) { if (req->peerAddr().isIntranetIp()) { diff --git a/lib/src/LocalHostFilter.cc b/lib/src/LocalHostFilter.cc index 01036c19..698fef58 100644 --- a/lib/src/LocalHostFilter.cc +++ b/lib/src/LocalHostFilter.cc @@ -2,7 +2,7 @@ * * LocalHostFilter.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,14 +12,11 @@ * */ -#include #include "HttpResponseImpl.h" +#include using namespace drogon; -void LocalHostFilter::doFilter(const HttpRequestPtr &req, - FilterCallback &&fcb, - FilterChainCallback &&fccb) +void LocalHostFilter::doFilter(const HttpRequestPtr &req, FilterCallback &&fcb, FilterChainCallback &&fccb) { - if (req->peerAddr().isLoopbackIp()) { fccb(); diff --git a/lib/src/MultiPart.cc b/lib/src/MultiPart.cc old mode 100755 new mode 100644 index c55e1bbd..438b13f7 --- a/lib/src/MultiPart.cc +++ b/lib/src/MultiPart.cc @@ -2,7 +2,7 @@ * * MultiPart.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,20 +14,20 @@ #include "HttpRequestImpl.h" #include "HttpUtils.h" -#include -#include #include +#include +#include #ifdef USE_OPENSSL #include #else #include "ssl_funcs/Md5.h" #endif -#include +#include +#include #include +#include #include #include -#include -#include using namespace drogon; @@ -62,7 +62,7 @@ int MultiPartParser::parse(const HttpRequestPtr &req) if (pos == std::string::npos) return -1; std::string boundary = contentType.substr(pos + 9); - //std::cout << "boundary[" << boundary << "]" << std::endl; + // std::cout << "boundary[" << boundary << "]" << std::endl; auto &content = req->query(); return parse(content, boundary); } @@ -135,7 +135,7 @@ int MultiPartParser::parse(const std::string &content, const std::string &bounda pos2 -= 4; if (parseEntity(content.c_str() + pos1, content.c_str() + pos2) != 0) return -1; - //pos2+=boundary.length(); + // pos2+=boundary.length(); } return 0; } @@ -147,12 +147,10 @@ int HttpFile::save(const std::string &path) const return -1; std::string filename; auto tmpPath = path; - if (path[0] == '/' || - (path.length() >= 2 && path[0] == '.' && path[1] == '/') || - (path.length() >= 3 && path[0] == '.' && path[1] == '.' && path[2] == '/') || - path == "." || path == "..") + if (path[0] == '/' || (path.length() >= 2 && path[0] == '.' && path[1] == '/') || + (path.length() >= 3 && path[0] == '.' && path[1] == '.' && path[2] == '/') || path == "." || path == "..") { - //Absolute or relative path + // Absolute or relative path } else { @@ -183,11 +181,10 @@ int HttpFile::saveAs(const std::string &filename) const { assert(!filename.empty()); auto pathAndFileName = filename; - if (filename[0] == '/' || - (filename.length() >= 2 && filename[0] == '.' && filename[1] == '/') || + if (filename[0] == '/' || (filename.length() >= 2 && filename[0] == '.' && filename[1] == '/') || (filename.length() >= 3 && filename[0] == '.' && filename[1] == '.' && filename[2] == '/')) { - //Absolute or relative path + // Absolute or relative path } else { diff --git a/lib/src/NotFound.cc b/lib/src/NotFound.cc old mode 100755 new mode 100644 index b07e5efa..0f58b3c0 --- a/lib/src/NotFound.cc +++ b/lib/src/NotFound.cc @@ -1,10 +1,10 @@ -//this file is generated by program automatically,don't modify it! +// this file is generated by program automatically,don't modify it! /** * * NotFound.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -15,32 +15,32 @@ */ #include -#include -#include #include -#include #include +#include +#include +#include using namespace std; using namespace drogon; std::string NotFound::genText(const HttpViewData &NotFound_view_data) { - std::stringstream NotFound_tmp_stream; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "404 Not Found\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "

404 Not Found

\n"; - NotFound_tmp_stream << "
drogon/"; - NotFound_tmp_stream << NotFound_view_data.get("version"); - NotFound_tmp_stream << "
\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - NotFound_tmp_stream << "\n"; - return NotFound_tmp_stream.str(); + std::stringstream NotFound_tmp_stream; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "404 Not Found\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "

404 Not Found

\n"; + NotFound_tmp_stream << "
drogon/"; + NotFound_tmp_stream << NotFound_view_data.get("version"); + NotFound_tmp_stream << "
\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + NotFound_tmp_stream << "\n"; + return NotFound_tmp_stream.str(); } diff --git a/lib/src/PluginsManager.cc b/lib/src/PluginsManager.cc index e2fd2412..af308d5f 100644 --- a/lib/src/PluginsManager.cc +++ b/lib/src/PluginsManager.cc @@ -2,7 +2,7 @@ * * PluginsManager.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -26,8 +26,7 @@ PluginsManager::~PluginsManager() } } -void PluginsManager::initializeAllPlugins(const Json::Value &configs, - const std::function &forEachCallback) +void PluginsManager::initializeAllPlugins(const Json::Value &configs, const std::function &forEachCallback) { assert(configs.isArray()); std::vector plugins; @@ -47,7 +46,7 @@ void PluginsManager::initializeAllPlugins(const Json::Value &configs, assert(dependencies.isArray() || dependencies.isNull()); if (dependencies.isArray()) { - //Is not null and is an array + // Is not null and is an array for (auto &depName : dependencies) { auto *dp = getPlugin(depName.asString()); @@ -68,7 +67,7 @@ void PluginsManager::initializeAllPlugins(const Json::Value &configs, }); plugins.push_back(pluginPtr); } - //Initialize them, Depth first + // Initialize them, Depth first for (auto plugin : plugins) { plugin->initialize(); diff --git a/lib/src/PluginsManager.h b/lib/src/PluginsManager.h index bf86e55a..243ac2f5 100644 --- a/lib/src/PluginsManager.h +++ b/lib/src/PluginsManager.h @@ -2,7 +2,7 @@ * * PluginsManager.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -18,14 +18,12 @@ namespace drogon { - typedef std::unique_ptr PluginBasePtr; class PluginsManager : trantor::NonCopyable { public: - void initializeAllPlugins(const Json::Value &configs, - const std::function &forEachCallback); + void initializeAllPlugins(const Json::Value &configs, const std::function &forEachCallback); PluginBase *getPlugin(const std::string &pluginName); ~PluginsManager(); @@ -35,4 +33,4 @@ class PluginsManager : trantor::NonCopyable std::vector _initializedPlugins; }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/SharedLibManager.cc b/lib/src/SharedLibManager.cc old mode 100755 new mode 100644 index 735d50e0..4ae16000 --- a/lib/src/SharedLibManager.cc +++ b/lib/src/SharedLibManager.cc @@ -2,7 +2,7 @@ * * SharedLibManager.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,14 +13,14 @@ */ #include "SharedLibManager.h" -#include -#include -#include -#include -#include #include #include +#include #include +#include +#include +#include +#include static void forEachFileIn(const std::string &path, const std::function &cb) { DIR *dp; @@ -30,7 +30,7 @@ static void forEachFileIn(const std::string &path, const std::function &libPaths) : _loop(loop), - _libPaths(libPaths) +SharedLibManager::SharedLibManager(trantor::EventLoop *loop, const std::vector &libPaths) + : _loop(loop), _libPaths(libPaths) { - _timeId = _loop->runEvery(5.0, [=]() { - managerLibs(); - }); + _timeId = _loop->runEvery(5.0, [=]() { managerLibs(); }); } SharedLibManager::~SharedLibManager() { @@ -86,7 +84,7 @@ void SharedLibManager::managerLibs() auto exName = filename.substr(pos + 1); if (exName == "csp") { - //compile + // compile auto lockFile = filename + ".lock"; std::ifstream fin(lockFile); if (fin) @@ -100,7 +98,8 @@ void SharedLibManager::managerLibs() #ifdef __linux__ if (st.st_mtim.tv_sec > _dlMap[filename].mTime.tv_sec) #else - if(st.st_mtimespec.tv_sec>_dlMap[filename].mTime.tv_sec) + if (st.st_mtimespec.tv_sec > + _dlMap[filename].mTime.tv_sec) #endif { LOG_TRACE << "new csp file:" << filename; @@ -117,7 +116,7 @@ void SharedLibManager::managerLibs() cmd.append(filename).append(" -o ").append(libPath); LOG_TRACE << cmd; auto r = system(cmd.c_str()); - //TODO: handle r + // TODO: handle r (void)(r); auto srcFile = filename.substr(0, pos); srcFile.append(".cc"); @@ -126,7 +125,7 @@ void SharedLibManager::managerLibs() #ifdef __linux__ dlStat.mTime = st.st_mtim; #else - dlStat.mTime=st.st_mtimespec; + dlStat.mTime = st.st_mtimespec; #endif if (dlStat.handle) { @@ -182,7 +181,7 @@ void *SharedLibManager::loadLibs(const std::string &sourceFile, void *oldHld) } } - //loading so file; + // loading so file; Handle = dlopen(soFile.c_str(), RTLD_LAZY); if (!Handle) { diff --git a/lib/src/SharedLibManager.h b/lib/src/SharedLibManager.h old mode 100755 new mode 100644 index 69406fb5..45a4bb07 --- a/lib/src/SharedLibManager.h +++ b/lib/src/SharedLibManager.h @@ -2,7 +2,7 @@ * * SharedLibManager.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -14,8 +14,8 @@ #pragma once -#include #include +#include #include #include namespace drogon @@ -39,4 +39,4 @@ class SharedLibManager : public trantor::NonCopyable void *loadLibs(const std::string &sourceFile, void *oldHld); trantor::TimerId _timeId; }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/SpinLock.h b/lib/src/SpinLock.h index addb41ec..9222d756 100644 --- a/lib/src/SpinLock.h +++ b/lib/src/SpinLock.h @@ -1,7 +1,7 @@ /** * SpinLock.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,19 +13,17 @@ #pragma once #include -#include #include +#include #define LOCK_SPIN 2048 namespace drogon { - class SpinLock { public: - inline SpinLock(std::atomic &flag) - : _flag(flag) + inline SpinLock(std::atomic &flag) : _flag(flag) { const static int cpu = std::thread::hardware_concurrency(); int n, i; @@ -65,8 +63,7 @@ class SpinLock class SimpleSpinLock { public: - inline SimpleSpinLock(std::atomic_flag &flag) - : _flag(flag) + inline SimpleSpinLock(std::atomic_flag &flag) : _flag(flag) { while (_flag.test_and_set(std::memory_order_acquire)) { @@ -83,4 +80,4 @@ class SimpleSpinLock std::atomic_flag &_flag; }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc old mode 100755 new mode 100644 index 3a8c3e66..17abad6e --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -2,7 +2,7 @@ * * Utilities.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,31 +12,30 @@ * */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include #include +#include #include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include namespace drogon { namespace utils { - static const std::string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" @@ -63,9 +62,7 @@ std::string genRandomString(int length) static std::once_flag once; static const int len = strlen(char_space); static const int randMax = RAND_MAX - (RAND_MAX % len); - std::call_once(once, []() { - std::srand(time(nullptr)); - }); + std::call_once(once, []() { std::srand(time(nullptr)); }); int i; std::string str; @@ -352,94 +349,94 @@ std::string urlEncode(const std::string &src) { switch (*iter) { - case ' ': - result.append(1, '+'); - break; + case ' ': + result.append(1, '+'); + break; // alnum - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - case 'G': - case 'H': - case 'I': - case 'J': - case 'K': - case 'L': - case 'M': - case 'N': - case 'O': - case 'P': - case 'Q': - case 'R': - case 'S': - case 'T': - case 'U': - case 'V': - case 'W': - case 'X': - case 'Y': - case 'Z': - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - case 'g': - case 'h': - case 'i': - case 'j': - case 'k': - case 'l': - case 'm': - case 'n': - case 'o': - case 'p': - case 'q': - case 'r': - case 's': - case 't': - case 'u': - case 'v': - case 'w': - case 'x': - case 'y': - case 'z': - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': // mark - case '-': - case '_': - case '.': - case '!': - case '~': - case '*': - case '\'': - case '(': - case ')': - case '&': - case '=': - case '/': - case '\\': - case '?': - result.append(1, *iter); - break; + case '-': + case '_': + case '.': + case '!': + case '~': + case '*': + case '\'': + case '(': + case ')': + case '&': + case '=': + case '/': + case '\\': + case '?': + result.append(1, *iter); + break; // escape - default: - result.append(1, '%'); - result.append(charToHex(*iter)); - break; + default: + result.append(1, '%'); + result.append(charToHex(*iter)); + break; } } @@ -461,61 +458,65 @@ std::string urlDecode(const char *begin, const char *end) { switch (begin[i]) { - case '+': - result += ' '; - break; - case '%': - if ((i + 2) < len && isxdigit(begin[i + 1]) && isxdigit(begin[i + 2])) - { - uint x1 = begin[i + 1]; - if (x1 >= '0' && x1 <= '9') + case '+': + result += ' '; + break; + case '%': + if ((i + 2) < len && isxdigit(begin[i + 1]) && isxdigit(begin[i + 2])) { - x1 -= '0'; - } - else if (x1 >= 'a' && x1 <= 'f') - { - x1 = x1 - 'a' + 10; - } - else if (x1 >= 'A' && x1 <= 'F') - { - x1 = x1 - 'A' + 10; - } - uint x2 = begin[i + 2]; - if (x2 >= '0' && x2 <= '9') - { - x2 -= '0'; - } - else if (x2 >= 'a' && x2 <= 'f') - { - x2 = x2 - 'a' + 10; - } - else if (x2 >= 'A' && x2 <= 'F') - { - x2 = x2 - 'A' + 10; - } - hex = x1 * 16 + x2; - //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@] - //可以不经过编码直接用于URL - if (!((hex >= 48 && hex <= 57) || //0-9 - (hex >= 97 && hex <= 122) || //a-z - (hex >= 65 && hex <= 90) || //A-Z - //一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;?@] - hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29 || hex == 0x2a || hex == 0x2b || hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f || hex == 0x3A || hex == 0x3B || hex == 0x3f || hex == 0x40 || hex == 0x5f)) - { - result += char(hex); - i += 2; + uint x1 = begin[i + 1]; + if (x1 >= '0' && x1 <= '9') + { + x1 -= '0'; + } + else if (x1 >= 'a' && x1 <= 'f') + { + x1 = x1 - 'a' + 10; + } + else if (x1 >= 'A' && x1 <= 'F') + { + x1 = x1 - 'A' + 10; + } + uint x2 = begin[i + 2]; + if (x2 >= '0' && x2 <= '9') + { + x2 -= '0'; + } + else if (x2 >= 'a' && x2 <= 'f') + { + x2 = x2 - 'a' + 10; + } + else if (x2 >= 'A' && x2 <= 'F') + { + x2 = x2 - 'A' + 10; + } + hex = x1 * 16 + x2; + //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] + //、以及某些保留字[$&+,/:;=?@] + //可以不经过编码直接用于URL + if (!((hex >= 48 && hex <= 57) || // 0-9 + (hex >= 97 && hex <= 122) || // a-z + (hex >= 65 && hex <= 90) || // A-Z + //一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;?@] + hex == 0x21 || + hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29 || hex == 0x2a || + hex == 0x2b || hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f || hex == 0x3A || + hex == 0x3B || hex == 0x3f || hex == 0x40 || hex == 0x5f)) + { + result += char(hex); + i += 2; + } + else + result += '%'; } else + { result += '%'; - } - else - { - result += '%'; - } - break; - default: - result += begin[i]; - break; + } + break; + default: + result += begin[i]; + break; } } return result; @@ -527,8 +528,7 @@ std::shared_ptr gzipCompress(const char *data, const size_t ndata) z_stream strm = {0}; if (data && ndata > 0) { - if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, - MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) + if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) return nullptr; auto outstr = std::make_shared(); outstr->resize(compressBound(ndata)); @@ -548,7 +548,6 @@ std::shared_ptr gzipCompress(const char *data, const size_t ndata) std::shared_ptr gzipDecompress(const std::shared_ptr &compressedData) { - if (compressedData->length() == 0) return compressedData; @@ -713,5 +712,5 @@ int createPath(const std::string &path) return 0; } -} // namespace utils -} // namespace drogon +} // namespace utils +} // namespace drogon diff --git a/lib/src/WebSocketClientImpl.cc b/lib/src/WebSocketClientImpl.cc index adb6a954..d1704673 100644 --- a/lib/src/WebSocketClientImpl.cc +++ b/lib/src/WebSocketClientImpl.cc @@ -14,11 +14,11 @@ #include "WebSocketClientImpl.h" #include "HttpRequestImpl.h" -#include "HttpUtils.h" #include "HttpResponseParser.h" +#include "HttpUtils.h" #include -#include #include +#include #ifdef USE_OPENSSL #include #else @@ -65,12 +65,10 @@ void WebSocketClientImpl::connectToServerInLoop() } } - if (_server.ipNetEndian() == 0 && !hasIpv6Address && - !_domain.empty() && - _server.portNetEndian() != 0) + if (_server.ipNetEndian() == 0 && !hasIpv6Address && !_domain.empty() && _server.portNetEndian() != 0) { - //dns - //TODO: timeout should be set by user + // dns + // TODO: timeout should be set by user if (InetAddress::resolve(_domain, &_server) == false) { _requestCallback(ReqResult::BadServerAddress, nullptr, shared_from_this()); @@ -100,7 +98,7 @@ void WebSocketClientImpl::connectToServerInLoop() if (connPtr->connected()) { connPtr->setContext(HttpResponseParser(connPtr)); - //send request; + // send request; LOG_TRACE << "Connection established!"; thisPtr->sendReq(connPtr); } @@ -109,20 +107,16 @@ void WebSocketClientImpl::connectToServerInLoop() LOG_TRACE << "connection disconnect"; thisPtr->_connectionClosedCallback(thisPtr); thisPtr->_websockConnPtr.reset(); - thisPtr->_loop->runAfter(1.0, [thisPtr]() { - thisPtr->reconnect(); - }); + thisPtr->_loop->runAfter(1.0, [thisPtr]() { thisPtr->reconnect(); }); } }); _tcpClient->setConnectionErrorCallback([weakPtr]() { auto thisPtr = weakPtr.lock(); if (!thisPtr) return; - //can't connect to server + // can't connect to server thisPtr->_requestCallback(ReqResult::NetworkFailure, nullptr, thisPtr); - thisPtr->_loop->runAfter(1.0, [thisPtr]() { - thisPtr->reconnect(); - }); + thisPtr->_loop->runAfter(1.0, [thisPtr]() { thisPtr->reconnect(); }); }); _tcpClient->setMessageCallback([weakPtr](const trantor::TcpConnectionPtr &connPtr, trantor::MsgBuffer *msg) { auto thisPtr = weakPtr.lock(); @@ -155,7 +149,7 @@ void WebSocketClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr } HttpResponseParser *responseParser = any_cast(connPtr->getMutableContext()); - //LOG_TRACE << "###:" << msg->readableBytes(); + // LOG_TRACE << "###:" << msg->readableBytes(); if (!responseParser->parseResponse(msgBuffer)) { @@ -195,11 +189,10 @@ void WebSocketClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr _upgraded = true; _websockConnPtr = std::make_shared(connPtr, false); auto thisPtr = shared_from_this(); - _websockConnPtr->setMessageCallback([thisPtr](std::string &&message, - const WebSocketConnectionImplPtr &connPtr, - const WebSocketMessageType &type) { - thisPtr->_messageCallback(std::move(message), thisPtr, type); - }); + _websockConnPtr->setMessageCallback( + [thisPtr](std::string &&message, const WebSocketConnectionImplPtr &connPtr, const WebSocketMessageType &type) { + thisPtr->_messageCallback(std::move(message), thisPtr, type); + }); _requestCallback(ReqResult::Ok, resp, shared_from_this()); if (msgBuffer->readableBytes() > 0) { @@ -221,14 +214,11 @@ void WebSocketClientImpl::reconnect() } WebSocketClientImpl::WebSocketClientImpl(trantor::EventLoop *loop, const trantor::InetAddress &addr, bool useSSL) - : _loop(loop), - _server(addr), - _useSSL(useSSL) + : _loop(loop), _server(addr), _useSSL(useSSL) { } -WebSocketClientImpl::WebSocketClientImpl(trantor::EventLoop *loop, const std::string &hostString) - : _loop(loop) +WebSocketClientImpl::WebSocketClientImpl(trantor::EventLoop *loop, const std::string &hostString) : _loop(loop) { auto lowerHost = hostString; std::transform(lowerHost.begin(), lowerHost.end(), lowerHost.begin(), tolower); @@ -298,11 +288,11 @@ WebSocketClientPtr WebSocketClient::newWebSocketClient(const std::string &ip, trantor::EventLoop *loop) { bool isIpv6 = ip.find(":") == std::string::npos ? false : true; - return std::make_shared(loop == nullptr ? app().getLoop() : loop, trantor::InetAddress(ip, port, isIpv6), useSSL); + return std::make_shared( + loop == nullptr ? app().getLoop() : loop, trantor::InetAddress(ip, port, isIpv6), useSSL); } -WebSocketClientPtr WebSocketClient::newWebSocketClient(const std::string &hostString, - trantor::EventLoop *loop) +WebSocketClientPtr WebSocketClient::newWebSocketClient(const std::string &hostString, trantor::EventLoop *loop) { return std::make_shared(loop == nullptr ? app().getLoop() : loop, hostString); } diff --git a/lib/src/WebSocketClientImpl.h b/lib/src/WebSocketClientImpl.h index ed60437d..549288b8 100644 --- a/lib/src/WebSocketClientImpl.h +++ b/lib/src/WebSocketClientImpl.h @@ -16,16 +16,15 @@ #include "WebSocketConnectionImpl.h" #include -#include #include #include +#include -#include #include +#include namespace drogon { - class WebSocketClientImpl : public WebSocketClient, public std::enable_shared_from_this { public: @@ -34,9 +33,9 @@ class WebSocketClientImpl : public WebSocketClient, public std::enable_shared_fr return _websockConnPtr; } - virtual void setMessageHandler(const std::function &callback) override + virtual void setMessageHandler( + const std::function &callback) + override { _messageCallback = callback; } @@ -46,7 +45,6 @@ class WebSocketClientImpl : public WebSocketClient, public std::enable_shared_fr _connectionClosedCallback = callback; } - virtual void connectToServer(const HttpRequestPtr &request, const WebSocketRequestCallback &callback) override { assert(callback); @@ -67,7 +65,10 @@ class WebSocketClientImpl : public WebSocketClient, public std::enable_shared_fr } } - virtual trantor::EventLoop *getLoop() override { return _loop; } + virtual trantor::EventLoop *getLoop() override + { + return _loop; + } WebSocketClientImpl(trantor::EventLoop *loop, const trantor::InetAddress &addr, bool useSSL = false); @@ -86,7 +87,8 @@ class WebSocketClientImpl : public WebSocketClient, public std::enable_shared_fr std::string _wsAccept; HttpRequestPtr _upgradeRequest; - std::function _messageCallback = [](std::string &&message, const WebSocketClientPtr &, const WebSocketMessageType &) {}; + std::function _messageCallback = + [](std::string &&message, const WebSocketClientPtr &, const WebSocketMessageType &) {}; std::function _connectionClosedCallback = [](const WebSocketClientPtr &) {}; WebSocketRequestCallback _requestCallback; WebSocketConnectionImplPtr _websockConnPtr; @@ -98,4 +100,4 @@ class WebSocketClientImpl : public WebSocketClient, public std::enable_shared_fr void reconnect(); }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/WebSocketConnectionImpl.cc b/lib/src/WebSocketConnectionImpl.cc index 20ca5419..6282a2da 100644 --- a/lib/src/WebSocketConnectionImpl.cc +++ b/lib/src/WebSocketConnectionImpl.cc @@ -2,7 +2,7 @@ * * WebSocketConnectionImpl.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,18 +12,15 @@ * */ -#include "HttpAppFrameworkImpl.h" #include "WebSocketConnectionImpl.h" +#include "HttpAppFrameworkImpl.h" +#include #include #include -#include using namespace drogon; WebSocketConnectionImpl::WebSocketConnectionImpl(const trantor::TcpConnectionPtr &conn, bool isServer) - : _tcpConn(conn), - _localAddr(conn->localAddr()), - _peerAddr(conn->peerAddr()), - _isServer(isServer) + : _tcpConn(conn), _localAddr(conn->localAddr()), _peerAddr(conn->peerAddr()), _isServer(isServer) { } @@ -59,10 +56,9 @@ void WebSocketConnectionImpl::send(const char *msg, uint64_t len, const WebSocke void WebSocketConnectionImpl::sendWsData(const char *msg, size_t len, unsigned char opcode) { - LOG_TRACE << "send " << len << " bytes"; - //Format the frame + // Format the frame std::string bytesFormatted; bytesFormatted.resize(len + 10); bytesFormatted[0] = char(0x80 | (opcode & 0x0f)); @@ -99,11 +95,9 @@ void WebSocketConnectionImpl::sendWsData(const char *msg, size_t len, unsigned c } if (!_isServer) { - //Add masking key; + // Add masking key; static std::once_flag once; - std::call_once(once, []() { - std::srand(time(nullptr)); - }); + std::call_once(once, []() { std::srand(time(nullptr)); }); int random = std::rand(); bytesFormatted[1] = (bytesFormatted[1] | 0x80); @@ -178,7 +172,7 @@ void WebSocketConnectionImpl::setPingMessage(const std::string &message, const s bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) { - //According to the rfc6455 + // According to the rfc6455 _gotAll = false; if (buffer->readableBytes() >= 2) { @@ -186,37 +180,37 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) bool isControlFrame = false; switch (opcode) { - case 0: - //continuation frame - break; - case 1: - _type = WebSocketMessageType::Text; - break; - case 2: - _type = WebSocketMessageType::Binary; - break; - case 8: - _type = WebSocketMessageType::Close; - isControlFrame = true; - break; - case 9: - _type = WebSocketMessageType::Ping; - isControlFrame = true; - break; - case 10: - _type = WebSocketMessageType::Pong; - isControlFrame = true; - break; - default: - LOG_ERROR << "Unknown frame type"; - return false; - break; + case 0: + // continuation frame + break; + case 1: + _type = WebSocketMessageType::Text; + break; + case 2: + _type = WebSocketMessageType::Binary; + break; + case 8: + _type = WebSocketMessageType::Close; + isControlFrame = true; + break; + case 9: + _type = WebSocketMessageType::Ping; + isControlFrame = true; + break; + case 10: + _type = WebSocketMessageType::Pong; + isControlFrame = true; + break; + default: + LOG_ERROR << "Unknown frame type"; + return false; + break; } bool isFin = (((*buffer)[0] & 0x80) == 0x80); if (!isFin && isControlFrame) { - //rfc6455-5.5 + // rfc6455-5.5 LOG_ERROR << "Bad frame: all control frames MUST NOT be fragmented"; return false; } @@ -243,8 +237,9 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) { if (isControlFrame) { - //rfc6455-5.5 - LOG_ERROR << "Bad frame: all control frames MUST have a payload length of 125 bytes or less"; + // rfc6455-5.5 + LOG_ERROR << "Bad frame: all control frames MUST have a payload length " + "of 125 bytes or less"; return false; } if (indexFirstMask == 4) @@ -271,7 +266,7 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) } if (isMasked != 0) { - //The message is sent by the client, check the length + // The message is sent by the client, check the length if (length > HttpAppFrameworkImpl::instance().getClientMaxWebSocketMessageSize()) { LOG_ERROR << "The size of the WebSocket message is too large!"; diff --git a/lib/src/WebSocketConnectionImpl.h b/lib/src/WebSocketConnectionImpl.h index 4918deea..8d483ec8 100644 --- a/lib/src/WebSocketConnectionImpl.h +++ b/lib/src/WebSocketConnectionImpl.h @@ -2,7 +2,7 @@ * * WebSocketConnectionImpl.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -20,13 +20,12 @@ namespace drogon { - class WebSocketConnectionImpl; typedef std::shared_ptr WebSocketConnectionImplPtr; class WebSocketMessageParser { -public: + public: bool parse(trantor::MsgBuffer *buffer); bool gotAll(std::string &message, WebSocketMessageType &type) { @@ -38,7 +37,7 @@ public: return true; } -private: + private: std::string _message; WebSocketMessageType _type; bool _gotAll = false; @@ -48,7 +47,7 @@ class WebSocketConnectionImpl : public WebSocketConnection, public std::enable_shared_from_this, public trantor::NonCopyable { -public: + public: explicit WebSocketConnectionImpl(const trantor::TcpConnectionPtr &conn, bool isServer = true); virtual void send(const char *msg, uint64_t len, const WebSocketMessageType &type = WebSocketMessageType::Text) override; @@ -60,8 +59,8 @@ public: virtual bool connected() const override; virtual bool disconnected() const override; - virtual void shutdown() override; //close write - virtual void forceClose() override; //close + virtual void shutdown() override; // close write + virtual void forceClose() override; // close virtual void setContext(const any &context) override; virtual const any &getContext() const override; @@ -69,9 +68,8 @@ public: virtual void setPingMessage(const std::string &message, const std::chrono::duration &interval) override; - void setMessageCallback(const std::function &callback) + void setMessageCallback( + const std::function &callback) { _messageCallback = callback; } @@ -85,7 +83,6 @@ public: { while (buffer->readableBytes() > 0) { - auto success = _parser.parse(buffer); if (success) { @@ -95,12 +92,12 @@ public: { if (type == WebSocketMessageType::Ping) { - //ping + // ping send(message, WebSocketMessageType::Pong); } else if (type == WebSocketMessageType::Close) { - //close + // close connPtr->shutdown(); } else if (type == WebSocketMessageType::Unknown) @@ -116,7 +113,7 @@ public: } else { - //Websock error! + // Websock error! connPtr->shutdown(); return; } @@ -131,7 +128,7 @@ public: _closeCallback(shared_from_this()); } -private: + private: trantor::TcpConnectionPtr _tcpConn; trantor::InetAddress _localAddr; trantor::InetAddress _peerAddr; @@ -140,14 +137,10 @@ private: WebSocketMessageParser _parser; trantor::TimerId _pingTimerId = trantor::InvalidTimerId; - std::function - _messageCallback = [](std::string &&, - const WebSocketConnectionImplPtr &, - const WebSocketMessageType &) {}; + std::function _messageCallback = + [](std::string &&, const WebSocketConnectionImplPtr &, const WebSocketMessageType &) {}; std::function _closeCallback = [](const WebSocketConnectionImplPtr &) {}; void sendWsData(const char *msg, size_t len, unsigned char opcode); }; -} // namespace drogon +} // namespace drogon diff --git a/lib/src/WebsocketControllersRouter.cc b/lib/src/WebsocketControllersRouter.cc index b5b054b2..da96160b 100644 --- a/lib/src/WebsocketControllersRouter.cc +++ b/lib/src/WebsocketControllersRouter.cc @@ -2,7 +2,7 @@ * * WebsocketControllersRouter.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -12,8 +12,8 @@ * */ -#include "FiltersFunction.h" #include "WebsocketControllersRouter.h" +#include "FiltersFunction.h" #include "HttpAppFrameworkImpl.h" #ifdef USE_OPENSSL #include @@ -82,9 +82,8 @@ std::vector> WebsocketControlle std::vector> ret; for (auto &item : _websockCtrlMap) { - auto info = std::tuple(item.first, - Get, - std::string("WebsocketController: ") + item.second._controller->className()); + auto info = std::tuple( + item.first, Get, std::string("WebsocketController: ") + item.second._controller->className()); ret.emplace_back(std::move(info)); } return ret; @@ -106,14 +105,12 @@ void WebsocketControllersRouter::doControllerHandler(const WebSocketControllerBa resp->addHeader("Connection", "Upgrade"); resp->addHeader("Sec-WebSocket-Accept", base64Key); callback(resp); - wsConnPtr->setMessageCallback([ctrlPtr](std::string &&message, - const WebSocketConnectionImplPtr &connPtr, - const WebSocketMessageType &type) { - ctrlPtr->handleNewMessage(connPtr, std::move(message), type); - }); - wsConnPtr->setCloseCallback([ctrlPtr](const WebSocketConnectionImplPtr &connPtr) { - ctrlPtr->handleConnectionClosed(connPtr); - }); + wsConnPtr->setMessageCallback( + [ctrlPtr](std::string &&message, const WebSocketConnectionImplPtr &connPtr, const WebSocketMessageType &type) { + ctrlPtr->handleNewMessage(connPtr, std::move(message), type); + }); + wsConnPtr->setCloseCallback( + [ctrlPtr](const WebSocketConnectionImplPtr &connPtr) { ctrlPtr->handleConnectionClosed(connPtr); }); ctrlPtr->handleNewConnection(req, wsConnPtr); return; } diff --git a/lib/src/WebsocketControllersRouter.h b/lib/src/WebsocketControllersRouter.h index f516dee8..5397bdfd 100644 --- a/lib/src/WebsocketControllersRouter.h +++ b/lib/src/WebsocketControllersRouter.h @@ -2,7 +2,7 @@ * * WebsocketControllersRouter.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -16,14 +16,14 @@ #include "HttpRequestImpl.h" #include "HttpResponseImpl.h" #include "WebSocketConnectionImpl.h" -#include -#include #include -#include +#include +#include +#include #include #include -#include -#include +#include +#include namespace drogon { @@ -31,7 +31,9 @@ class HttpAppFrameworkImpl; class WebsocketControllersRouter : public trantor::NonCopyable { public: - WebsocketControllersRouter() {} + WebsocketControllersRouter() + { + } void registerWebSocketController(const std::string &pathName, const std::string &ctrlName, const std::vector &filters); @@ -58,4 +60,4 @@ class WebsocketControllersRouter : public trantor::NonCopyable std::function &&callback, const WebSocketConnectionImplPtr &wsConnPtr); }; -} // namespace drogon \ No newline at end of file +} // namespace drogon \ No newline at end of file diff --git a/lib/src/ssl_funcs/Md5.cc b/lib/src/ssl_funcs/Md5.cc index fda66603..fcf8b2ec 100644 --- a/lib/src/ssl_funcs/Md5.cc +++ b/lib/src/ssl_funcs/Md5.cc @@ -2,7 +2,7 @@ * * Md5.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -13,9 +13,9 @@ */ #include "Md5.h" +#include #include #include -#include const int Md5Encode::kA = 0x67452301; const int Md5Encode::kB = 0xefcdab89; @@ -60,7 +60,7 @@ UInt32 Md5Encode::FillData(const char *in_data_ptr, int data_byte_len, char **ou } else { - bit_need_fill = (BIT_OF_GROUP - SRC_DATA_LEN) - mod_bit_num; + bit_need_fill = (BIT_OF_GROUP - SRC_DATA_LEN) - mod_bit_num; } int all_bit = bit_num + bit_need_fill; if (0 < bit_need_fill) @@ -245,7 +245,7 @@ std::string Md5Encode::encode(const std::string &src_info) std::string result; char *out_data_ptr = NULL; int total_byte = FillData(src_info.c_str(), src_info.length(), &out_data_ptr); - //char * data_BIT_OF_GROUP = out_data_ptr; + // char * data_BIT_OF_GROUP = out_data_ptr; for (int i = 0; i < total_byte / (BIT_OF_GROUP / BIT_OF_BYTE); ++i) { char *data_BIT_OF_GROUP = out_data_ptr; diff --git a/lib/src/ssl_funcs/Md5.h b/lib/src/ssl_funcs/Md5.h index 44c176a4..19501cfe 100644 --- a/lib/src/ssl_funcs/Md5.h +++ b/lib/src/ssl_funcs/Md5.h @@ -9,7 +9,7 @@ * * Md5.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -60,7 +60,6 @@ class Md5Encode }; public: - static std::string encode(const std::string &src_info); protected: diff --git a/lib/src/ssl_funcs/Sha1.cc b/lib/src/ssl_funcs/Sha1.cc index 21a50905..6bdcd7d2 100644 --- a/lib/src/ssl_funcs/Sha1.cc +++ b/lib/src/ssl_funcs/Sha1.cc @@ -2,7 +2,7 @@ * * Sha1.cc * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license @@ -17,137 +17,136 @@ static unsigned int FromBigEndian(unsigned int v) { - return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | - ((v & 0xff000000) >> 24); + return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24); } static void WriteBigEndian64(unsigned char *p, unsigned int v) { - memset(p, 0, 8); - memcpy(p, &v, 4); - int i = 0; - for (i = 0; i < 4; i++) - { - unsigned char t = p[i]; - p[i] = p[7 - i]; - p[7 - i] = t; - } + memset(p, 0, 8); + memcpy(p, &v, 4); + int i = 0; + for (i = 0; i < 4; i++) + { + unsigned char t = p[i]; + p[i] = p[7 - i]; + p[7 - i] = t; + } } static unsigned int LeftRol(unsigned int v, int n) { - return (v << n) | (v >> (32 - n)); + return (v << n) | (v >> (32 - n)); } unsigned char *SHA1(const unsigned char *data, size_t dataLen, unsigned char *md) { - unsigned char *pbytes = (unsigned char *)data; - unsigned int nbyte = dataLen; + unsigned char *pbytes = (unsigned char *)data; + unsigned int nbyte = dataLen; - static unsigned int words[80]; - unsigned int H[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; - unsigned int a, b, c, d, e, f, k, temp, bitlen[2], word; - unsigned int i, j, index, p1, p2, maxlen; - unsigned char spec[4] = {0}; - i = nbyte % 4; + static unsigned int words[80]; + unsigned int H[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; + unsigned int a, b, c, d, e, f, k, temp, bitlen[2], word; + unsigned int i, j, index, p1, p2, maxlen; + unsigned char spec[4] = {0}; + i = nbyte % 4; - p1 = nbyte - i; - spec[i] = 1 << 7; - while (i--) - { - spec[i] = pbytes[p1 + i]; - } + p1 = nbyte - i; + spec[i] = 1 << 7; + while (i--) + { + spec[i] = pbytes[p1 + i]; + } - maxlen = (nbyte + 1) % 64; - if (maxlen <= 56) - { - maxlen = (nbyte + 1) - maxlen + 64; - } - else - { - maxlen = (nbyte + 1) - maxlen + 128; - } - p2 = maxlen - 8; - WriteBigEndian64((unsigned char *)bitlen, nbyte * 8); + maxlen = (nbyte + 1) % 64; + if (maxlen <= 56) + { + maxlen = (nbyte + 1) - maxlen + 64; + } + else + { + maxlen = (nbyte + 1) - maxlen + 128; + } + p2 = maxlen - 8; + WriteBigEndian64((unsigned char *)bitlen, nbyte * 8); - for (j = 0; j < maxlen; j += 64) - { - a = H[0]; - b = H[1]; - c = H[2]; - d = H[3]; - e = H[4]; - for (i = 0; i < 80; i++) - { - if (i < 16) - { - index = j + (i << 2); - if (index < p1) - { - word = *((unsigned int *)(pbytes + index)); - } - else if (index == p1) - { - word = *(unsigned int *)spec; - } - else if (index < p2) - { - word = 0; - } - else - { - word = (index < maxlen - 4) ? bitlen[0] : bitlen[1]; - } - words[i] = FromBigEndian(word); - } - else - { - words[i] = LeftRol(words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16], 1); - } - if (i < 20) - { - f = (b & c) | ((~b) & d); - k = 0x5A827999; - } - else if (i < 40) - { - f = b ^ c ^ d; - k = 0x6ED9EBA1; - } - else if (i < 60) - { - f = (b & c) | (b & d) | (c & d); - k = 0x8F1BBCDC; - } - else - { - f = b ^ c ^ d; - k = 0xCA62C1D6; - } - temp = LeftRol(a, 5) + f + e + k + words[i]; - e = d; - d = c; - c = LeftRol(b, 30); - b = a; - a = temp; - } - H[0] += a; - H[1] += b; - H[2] += c; - H[3] += d; - H[4] += e; - } - int ct = 0; - for (i = 0; i < 5; i++) - { - unsigned char buf[4] = {0}; - memcpy(buf, &(H[i]), 4); - for (int r = 3; r >= 0; r--) - { - md[ct] = buf[r]; - ct++; - } - } + for (j = 0; j < maxlen; j += 64) + { + a = H[0]; + b = H[1]; + c = H[2]; + d = H[3]; + e = H[4]; + for (i = 0; i < 80; i++) + { + if (i < 16) + { + index = j + (i << 2); + if (index < p1) + { + word = *((unsigned int *)(pbytes + index)); + } + else if (index == p1) + { + word = *(unsigned int *)spec; + } + else if (index < p2) + { + word = 0; + } + else + { + word = (index < maxlen - 4) ? bitlen[0] : bitlen[1]; + } + words[i] = FromBigEndian(word); + } + else + { + words[i] = LeftRol(words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16], 1); + } + if (i < 20) + { + f = (b & c) | ((~b) & d); + k = 0x5A827999; + } + else if (i < 40) + { + f = b ^ c ^ d; + k = 0x6ED9EBA1; + } + else if (i < 60) + { + f = (b & c) | (b & d) | (c & d); + k = 0x8F1BBCDC; + } + else + { + f = b ^ c ^ d; + k = 0xCA62C1D6; + } + temp = LeftRol(a, 5) + f + e + k + words[i]; + e = d; + d = c; + c = LeftRol(b, 30); + b = a; + a = temp; + } + H[0] += a; + H[1] += b; + H[2] += c; + H[3] += d; + H[4] += e; + } + int ct = 0; + for (i = 0; i < 5; i++) + { + unsigned char buf[4] = {0}; + memcpy(buf, &(H[i]), 4); + for (int r = 3; r >= 0; r--) + { + md[ct] = buf[r]; + ct++; + } + } - return md; + return md; } diff --git a/lib/src/ssl_funcs/Sha1.h b/lib/src/ssl_funcs/Sha1.h index 2c6a5fd2..e82ebb74 100644 --- a/lib/src/ssl_funcs/Sha1.h +++ b/lib/src/ssl_funcs/Sha1.h @@ -2,7 +2,7 @@ * * Sha1.h * An Tao - * + * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license diff --git a/lib/tests/CacheMapTest.cc b/lib/tests/CacheMapTest.cc old mode 100755 new mode 100644 index a35d159d..87dd84d8 --- a/lib/tests/CacheMapTest.cc +++ b/lib/tests/CacheMapTest.cc @@ -1,11 +1,11 @@ #include -#include -#include #include -#include +#include #include #include -#include +#include +#include +#include int main() { @@ -21,9 +21,7 @@ int main() std::cout << i << " cache item erased!" << std::endl; }); } - cache.insert("1", "first", 20, [=] { - LOG_DEBUG << "first item in cache timeout,erase!"; - }); + cache.insert("1", "first", 20, [=] { LOG_DEBUG << "first item in cache timeout,erase!"; }); cache.insert("2", "second", 5); cache.insert("3", "third", 5); std::thread thread1([&] { diff --git a/lib/tests/CacheMapTest2.cc b/lib/tests/CacheMapTest2.cc old mode 100755 new mode 100644 index 9fe81626..be39ec6b --- a/lib/tests/CacheMapTest2.cc +++ b/lib/tests/CacheMapTest2.cc @@ -1,16 +1,15 @@ #include -#include -#include #include -#include -#include -#include #include #include +#include +#include +#include +#include +#include int main() { - trantor::Logger::setLogLevel(trantor::Logger::TRACE); trantor::EventLoopThread loopThread; loopThread.run(); @@ -19,19 +18,16 @@ int main() std::shared_ptr> main_cachePtr; auto now = trantor::Date::date(); loop->runAt(now.after(1).roundSecond(), [=, &main_cachePtr]() { - std::shared_ptr> cachePtr = std::make_shared>(loop, 0.1, 3, 50); + std::shared_ptr> cachePtr = + std::make_shared>(loop, 0.1, 3, 50); main_cachePtr = cachePtr; LOG_DEBUG << "insert :usecount=" << main_cachePtr.use_count(); - cachePtr->insert("1", "1", 3, [=]() { - LOG_DEBUG << "timeout!erase 1!"; - }); - cachePtr->insert("2", "2", 10, []() { - LOG_DEBUG << "2 timeout"; - }); + cachePtr->insert("1", "1", 3, [=]() { LOG_DEBUG << "timeout!erase 1!"; }); + cachePtr->insert("2", "2", 10, []() { LOG_DEBUG << "2 timeout"; }); }); trantor::EventLoop mainLoop; mainLoop.runAt(now.after(3).roundSecond().after(0.0013), [&]() { - (*main_cachePtr)["new"]="new"; + (*main_cachePtr)["new"] = "new"; if (main_cachePtr->find("1")) { LOG_DEBUG << "find item 1:" << (*main_cachePtr)["1"]; @@ -43,8 +39,6 @@ int main() LOG_DEBUG << "can't find item 1"; } }); - mainLoop.runAfter(8, [&]() { - mainLoop.quit(); - }); + mainLoop.runAfter(8, [&]() { mainLoop.quit(); }); mainLoop.loop(); } diff --git a/lib/tests/ClassNameTest.cc b/lib/tests/ClassNameTest.cc index 334c8828..c94d9a81 100644 --- a/lib/tests/ClassNameTest.cc +++ b/lib/tests/ClassNameTest.cc @@ -16,8 +16,8 @@ class handler : public drogon::DrObject class hh : public handler { }; -} // namespace v1 -} // namespace api +} // namespace v1 +} // namespace api int main() { api::v1::hh h; diff --git a/lib/tests/CookiesTest.cc b/lib/tests/CookiesTest.cc old mode 100755 new mode 100644 diff --git a/lib/tests/GzipTest.cc b/lib/tests/GzipTest.cc index d458277f..08050f75 100644 --- a/lib/tests/GzipTest.cc +++ b/lib/tests/GzipTest.cc @@ -5,310 +5,323 @@ using namespace drogon; int main() { - const std::string inStr = "Applications\n" - "Developer\n" - "Library\n" - "Network\n" - "System\n" - "Users\n" - "Volumes\n" - "bin\n" - "cores\n" - "dev\n" - "etc\n" - "home\n" - "installer.failurerequests\n" - "net\n" - "opt\n" - "private\n" - "sbin\n" - "tmp\n" - "usb\n" - "usr\n" - "var\n" - "vm\n" - "用户信息\n" - "\n" - "/Applications:\n" - "Adobe\n" - "Adobe Creative Cloud\n" - "Adobe Photoshop CC\n" - "AirPlayer Pro.app\n" - "Android Studio.app\n" - "App Store.app\n" - "Autodesk\n" - "Automator.app\n" - "Axure RP Pro 7.0.app\n" - "BaiduNetdisk_mac.app\n" - "CLion.app\n" - "Calculator.app\n" - "Calendar.app\n" - "Chess.app\n" - "CleanApp.app\n" - "Contacts.app\n" - "DVD Player.app\n" - "Dashboard.app\n" - "Dictionary.app\n" - "Docs for Xcode.app\n" - "FaceTime.app\n" - "FinalShell\n" - "Firefox.app\n" - "Folx.app\n" - "Font Book.app\n" - "GitHub.app\n" - "Google Chrome.app\n" - "Grammarly.app\n" - "Image Capture.app\n" - "Lantern.app\n" - "Launchpad.app\n" - "License.rtf\n" - "MacPorts\n" - "Mail.app\n" - "Maps.app\n" - "Messages.app\n" - "Microsoft Excel.app\n" - "Microsoft Office 2011\n" - "Microsoft OneNote.app\n" - "Microsoft Outlook.app\n" - "Microsoft PowerPoint.app\n" - "Microsoft Word.app\n" - "Mindjet MindManager.app\n" - "Mission Control.app\n" - "Mockplus.app\n" - "MyEclipse 2015\n" - "Notes.app\n" - "OmniGraffle.app\n" - "PP助手.app\n" - "Pages.app\n" - "Photo Booth.app\n" - "Photos.app\n" - "Preview.app\n" - "QJVPN.app\n" - "QQ.app\n" - "QuickTime Player.app\n" - "RAR Extractor Lite.app\n" - "Reminders.app\n" - "Remote Desktop Connection.app\n" - "Renee Undeleter.app\n" - "Sabaki.app\n" - "Safari.app\n" - "ShadowsocksX.app\n" - "Siri.app\n" - "SogouInputPad.app\n" - "Stickies.app\n" - "System Preferences.app\n" - "TeX\n" - "Telegram.app\n" - "Termius.app\n" - "Tesumego - How to Make a Professional Go Player.app\n" - "TextEdit.app\n" - "Thunder.app\n" - "Time Machine.app\n" - "Tunnelblick.app\n" - "Utilities\n" - "VPN Shield.appdownload\n" - "VirtualBox.app\n" - "WeChat.app\n" - "WinOnX2.app\n" - "Wireshark.app\n" - "Xcode.app\n" - "Yose.app\n" - "YoudaoNote.localized\n" - "finalshelldata\n" - "iBooks.app\n" - "iPhoto.app\n" - "iTools.app\n" - "iTunes.app\n" - "pgAdmin 4.app\n" - "wechatwebdevtools.app\n" - "搜狐影音.appdownload\n" - "网易有道词典.app\n" - "万能数据恢复大师.app\n" - "\n" - "/Applications/Adobe:\n" - "Flash Player\n" - "\n" - "/Applications/Adobe/Flash Player:\n" - "AddIns\n" - "\n" - "/Applications/Adobe/Flash Player/AddIns:\n" - "airappinstaller\n" - "\n" - "/Applications/Adobe/Flash Player/AddIns/airappinstaller:\n" - "airappinstaller\n" - "digest.s\n" - "\n" - "/Applications/Adobe Creative Cloud:\n" - "Adobe Creative Cloud\n" - "Icon\n" - "Uninstall Adobe Creative Cloud\n" - "\n" - "/Applications/Adobe Photoshop CC:\n" - "Adobe Photoshop CC.app\n" - "Configuration\n" - "Icon\n" - "Legal\n" - "LegalNotices.pdf\n" - "Locales\n" - "Plug-ins\n" - "Presets\n" - "卸载 Adobe Photoshop CC\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app:\n" - "Contents\n" - "Linguistics\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents:\n" - "Application Data\n" - "Frameworks\n" - "Info.plist\n" - "MacOS\n" - "PkgInfo\n" - "Required\n" - "Resources\n" - "_CodeSignature\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data:\n" - "Custom File Info Panels\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels:\n" - "4.0\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels/4.0:\n" - "bin\n" - "custom\n" - "panels\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels/4.0/bin:\n" - "FileInfoFoundation.swf\n" - "FileInfoUI.swf\n" - "framework.swf\n" - "loc\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels/4.0/bin/loc:\n" - "FileInfo_ar_AE.dat\n" - "FileInfo_bg_BG.dat\n" - "FileInfo_cs_CZ.dat\n" - "FileInfo_da_DK.dat\n" - "FileInfo_de_DE.dat\n" - "FileInfo_el_GR.dat\n" - "FileInfo_en_US.dat\n" - "FileInfo_es_ES.dat\n" - "FileInfo_et_EE.dat\n" - "FileInfo_fi_FI.dat\n" - "FileInfo_fr_FR.dat\n" - "FileInfo_he_IL.dat\n" - "FileInfo_hr_HR.dat\n" - "FileInfo_hu_HU.dat\n" - "FileInfo_it_IT.dat\n" - "FileInfo_ja_JP.dat\n" - "FileInfo_ko_KR.dat\n" - "FileInfo_lt_LT.dat\n" - "FileInfo_lv_LV.dat\n" - "FileInfo_nb_NO.dat\n" - "FileInfo_nl_NL.dat\n" - "FileInfo_pl_PL.dat\n" - "FileInfo_pt_BR.dat\n" - "FileInfo_ro_RO.dat\n" - "FileInfo_ru_RU.dat\n" - "FileInfo_sk_SK.dat\n" - "FileInfo_sl_SI.dat\n" - "FileInfo_sv_SE.dat\n" - "FileInfo_tr_TR.dat\n" - "FileInfo_uk_UA.dat\n" - "FileInfo_zh_CN.dat\n" - "FileInfo_zh_TW.dat\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels/4.0/custom:\n" - "DICOM.xml\n" - "Mobile.xml\n" - "loc\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels/4.0/custom/loc:\n" - "DICOM_ar_AE.dat\n" - "DICOM_bg_BG.dat\n" - "DICOM_cs_CZ.dat\n" - "DICOM_da_DK.dat\n" - "DICOM_de_DE.dat\n" - "DICOM_el_GR.dat\n" - "DICOM_en_US.dat\n" - "DICOM_es_ES.dat\n" - "DICOM_et_EE.dat\n" - "DICOM_fi_FI.dat\n" - "DICOM_fr_FR.dat\n" - "DICOM_he_IL.dat\n" - "DICOM_hr_HR.dat\n" - "DICOM_hu_HU.dat\n" - "DICOM_it_IT.dat\n" - "DICOM_ja_JP.dat\n" - "DICOM_ko_KR.dat\n" - "DICOM_lt_LT.dat\n" - "DICOM_lv_LV.dat\n" - "DICOM_nb_NO.dat\n" - "DICOM_nl_NL.dat\n" - "DICOM_pl_PL.dat\n" - "DICOM_pt_BR.dat\n" - "DICOM_ro_RO.dat\n" - "DICOM_ru_RU.dat\n" - "DICOM_sk_SK.dat\n" - "DICOM_sl_SI.dat\n" - "DICOM_sv_SE.dat\n" - "DICOM_tr_TR.dat\n" - "DICOM_uk_UA.dat\n" - "DICOM_zh_CN.dat\n" - "DICOM_zh_TW.dat\n" - "Mobile_ar_AE.dat\n" - "Mobile_bg_BG.dat\n" - "Mobile_cs_CZ.dat\n" - "Mobile_da_DK.dat\n" - "Mobile_de_DE.dat\n" - "Mobile_el_GR.dat\n" - "Mobile_en_US.dat\n" - "Mobile_es_ES.dat\n" - "Mobile_et_EE.dat\n" - "Mobile_fi_FI.dat\n" - "Mobile_fr_FR.dat\n" - "Mobile_he_IL.dat\n" - "Mobile_hr_HR.dat\n" - "Mobile_hu_HU.dat\n" - "Mobile_it_IT.dat\n" - "Mobile_ja_JP.dat\n" - "Mobile_ko_KR.dat\n" - "Mobile_lt_LT.dat\n" - "Mobile_lv_LV.dat\n" - "Mobile_nb_NO.dat\n" - "Mobile_nl_NL.dat\n" - "Mobile_pl_PL.dat\n" - "Mobile_pt_BR.dat\n" - "Mobile_ro_RO.dat\n" - "Mobile_ru_RU.dat\n" - "Mobile_sk_SK.dat\n" - "Mobile_sl_SI.dat\n" - "Mobile_sv_SE.dat\n" - "Mobile_tr_TR.dat\n" - "Mobile_uk_UA.dat\n" - "Mobile_zh_CN.dat\n" - "Mobile_zh_TW.dat\n" - "\n" - "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents/Application Data/Custom File Info Panels/4.0/panels:\n" - "IPTC\n" - "IPTCExt\n" - "advanced\n" - "audioData\n" - "camera\n" - "categories\n" - "description\n" - "dicom\n" - "gpsData\n" - "history\n" - "mobile\n" - "origin\n" - "rawpacket"; - auto ret=utils::gzipCompress(inStr.c_str(), inStr.length()); - if(ret) + const std::string inStr = + "Applications\n" + "Developer\n" + "Library\n" + "Network\n" + "System\n" + "Users\n" + "Volumes\n" + "bin\n" + "cores\n" + "dev\n" + "etc\n" + "home\n" + "installer.failurerequests\n" + "net\n" + "opt\n" + "private\n" + "sbin\n" + "tmp\n" + "usb\n" + "usr\n" + "var\n" + "vm\n" + "用户信息\n" + "\n" + "/Applications:\n" + "Adobe\n" + "Adobe Creative Cloud\n" + "Adobe Photoshop CC\n" + "AirPlayer Pro.app\n" + "Android Studio.app\n" + "App Store.app\n" + "Autodesk\n" + "Automator.app\n" + "Axure RP Pro 7.0.app\n" + "BaiduNetdisk_mac.app\n" + "CLion.app\n" + "Calculator.app\n" + "Calendar.app\n" + "Chess.app\n" + "CleanApp.app\n" + "Contacts.app\n" + "DVD Player.app\n" + "Dashboard.app\n" + "Dictionary.app\n" + "Docs for Xcode.app\n" + "FaceTime.app\n" + "FinalShell\n" + "Firefox.app\n" + "Folx.app\n" + "Font Book.app\n" + "GitHub.app\n" + "Google Chrome.app\n" + "Grammarly.app\n" + "Image Capture.app\n" + "Lantern.app\n" + "Launchpad.app\n" + "License.rtf\n" + "MacPorts\n" + "Mail.app\n" + "Maps.app\n" + "Messages.app\n" + "Microsoft Excel.app\n" + "Microsoft Office 2011\n" + "Microsoft OneNote.app\n" + "Microsoft Outlook.app\n" + "Microsoft PowerPoint.app\n" + "Microsoft Word.app\n" + "Mindjet MindManager.app\n" + "Mission Control.app\n" + "Mockplus.app\n" + "MyEclipse 2015\n" + "Notes.app\n" + "OmniGraffle.app\n" + "PP助手.app\n" + "Pages.app\n" + "Photo Booth.app\n" + "Photos.app\n" + "Preview.app\n" + "QJVPN.app\n" + "QQ.app\n" + "QuickTime Player.app\n" + "RAR Extractor Lite.app\n" + "Reminders.app\n" + "Remote Desktop Connection.app\n" + "Renee Undeleter.app\n" + "Sabaki.app\n" + "Safari.app\n" + "ShadowsocksX.app\n" + "Siri.app\n" + "SogouInputPad.app\n" + "Stickies.app\n" + "System Preferences.app\n" + "TeX\n" + "Telegram.app\n" + "Termius.app\n" + "Tesumego - How to Make a Professional Go Player.app\n" + "TextEdit.app\n" + "Thunder.app\n" + "Time Machine.app\n" + "Tunnelblick.app\n" + "Utilities\n" + "VPN Shield.appdownload\n" + "VirtualBox.app\n" + "WeChat.app\n" + "WinOnX2.app\n" + "Wireshark.app\n" + "Xcode.app\n" + "Yose.app\n" + "YoudaoNote.localized\n" + "finalshelldata\n" + "iBooks.app\n" + "iPhoto.app\n" + "iTools.app\n" + "iTunes.app\n" + "pgAdmin 4.app\n" + "wechatwebdevtools.app\n" + "搜狐影音.appdownload\n" + "网易有道词典.app\n" + "万能数据恢复大师.app\n" + "\n" + "/Applications/Adobe:\n" + "Flash Player\n" + "\n" + "/Applications/Adobe/Flash Player:\n" + "AddIns\n" + "\n" + "/Applications/Adobe/Flash Player/AddIns:\n" + "airappinstaller\n" + "\n" + "/Applications/Adobe/Flash Player/AddIns/airappinstaller:\n" + "airappinstaller\n" + "digest.s\n" + "\n" + "/Applications/Adobe Creative Cloud:\n" + "Adobe Creative Cloud\n" + "Icon\n" + "Uninstall Adobe Creative Cloud\n" + "\n" + "/Applications/Adobe Photoshop CC:\n" + "Adobe Photoshop CC.app\n" + "Configuration\n" + "Icon\n" + "Legal\n" + "LegalNotices.pdf\n" + "Locales\n" + "Plug-ins\n" + "Presets\n" + "卸载 Adobe Photoshop CC\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app:\n" + "Contents\n" + "Linguistics\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop CC.app/Contents:\n" + "Application Data\n" + "Frameworks\n" + "Info.plist\n" + "MacOS\n" + "PkgInfo\n" + "Required\n" + "Resources\n" + "_CodeSignature\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data:\n" + "Custom File Info Panels\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info Panels:\n" + "4.0\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info Panels/4.0:\n" + "bin\n" + "custom\n" + "panels\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info Panels/4.0/bin:\n" + "FileInfoFoundation.swf\n" + "FileInfoUI.swf\n" + "framework.swf\n" + "loc\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info " + "Panels/4.0/bin/loc:\n" + "FileInfo_ar_AE.dat\n" + "FileInfo_bg_BG.dat\n" + "FileInfo_cs_CZ.dat\n" + "FileInfo_da_DK.dat\n" + "FileInfo_de_DE.dat\n" + "FileInfo_el_GR.dat\n" + "FileInfo_en_US.dat\n" + "FileInfo_es_ES.dat\n" + "FileInfo_et_EE.dat\n" + "FileInfo_fi_FI.dat\n" + "FileInfo_fr_FR.dat\n" + "FileInfo_he_IL.dat\n" + "FileInfo_hr_HR.dat\n" + "FileInfo_hu_HU.dat\n" + "FileInfo_it_IT.dat\n" + "FileInfo_ja_JP.dat\n" + "FileInfo_ko_KR.dat\n" + "FileInfo_lt_LT.dat\n" + "FileInfo_lv_LV.dat\n" + "FileInfo_nb_NO.dat\n" + "FileInfo_nl_NL.dat\n" + "FileInfo_pl_PL.dat\n" + "FileInfo_pt_BR.dat\n" + "FileInfo_ro_RO.dat\n" + "FileInfo_ru_RU.dat\n" + "FileInfo_sk_SK.dat\n" + "FileInfo_sl_SI.dat\n" + "FileInfo_sv_SE.dat\n" + "FileInfo_tr_TR.dat\n" + "FileInfo_uk_UA.dat\n" + "FileInfo_zh_CN.dat\n" + "FileInfo_zh_TW.dat\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info " + "Panels/4.0/custom:\n" + "DICOM.xml\n" + "Mobile.xml\n" + "loc\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info " + "Panels/4.0/custom/loc:\n" + "DICOM_ar_AE.dat\n" + "DICOM_bg_BG.dat\n" + "DICOM_cs_CZ.dat\n" + "DICOM_da_DK.dat\n" + "DICOM_de_DE.dat\n" + "DICOM_el_GR.dat\n" + "DICOM_en_US.dat\n" + "DICOM_es_ES.dat\n" + "DICOM_et_EE.dat\n" + "DICOM_fi_FI.dat\n" + "DICOM_fr_FR.dat\n" + "DICOM_he_IL.dat\n" + "DICOM_hr_HR.dat\n" + "DICOM_hu_HU.dat\n" + "DICOM_it_IT.dat\n" + "DICOM_ja_JP.dat\n" + "DICOM_ko_KR.dat\n" + "DICOM_lt_LT.dat\n" + "DICOM_lv_LV.dat\n" + "DICOM_nb_NO.dat\n" + "DICOM_nl_NL.dat\n" + "DICOM_pl_PL.dat\n" + "DICOM_pt_BR.dat\n" + "DICOM_ro_RO.dat\n" + "DICOM_ru_RU.dat\n" + "DICOM_sk_SK.dat\n" + "DICOM_sl_SI.dat\n" + "DICOM_sv_SE.dat\n" + "DICOM_tr_TR.dat\n" + "DICOM_uk_UA.dat\n" + "DICOM_zh_CN.dat\n" + "DICOM_zh_TW.dat\n" + "Mobile_ar_AE.dat\n" + "Mobile_bg_BG.dat\n" + "Mobile_cs_CZ.dat\n" + "Mobile_da_DK.dat\n" + "Mobile_de_DE.dat\n" + "Mobile_el_GR.dat\n" + "Mobile_en_US.dat\n" + "Mobile_es_ES.dat\n" + "Mobile_et_EE.dat\n" + "Mobile_fi_FI.dat\n" + "Mobile_fr_FR.dat\n" + "Mobile_he_IL.dat\n" + "Mobile_hr_HR.dat\n" + "Mobile_hu_HU.dat\n" + "Mobile_it_IT.dat\n" + "Mobile_ja_JP.dat\n" + "Mobile_ko_KR.dat\n" + "Mobile_lt_LT.dat\n" + "Mobile_lv_LV.dat\n" + "Mobile_nb_NO.dat\n" + "Mobile_nl_NL.dat\n" + "Mobile_pl_PL.dat\n" + "Mobile_pt_BR.dat\n" + "Mobile_ro_RO.dat\n" + "Mobile_ru_RU.dat\n" + "Mobile_sk_SK.dat\n" + "Mobile_sl_SI.dat\n" + "Mobile_sv_SE.dat\n" + "Mobile_tr_TR.dat\n" + "Mobile_uk_UA.dat\n" + "Mobile_zh_CN.dat\n" + "Mobile_zh_TW.dat\n" + "\n" + "/Applications/Adobe Photoshop CC/Adobe Photoshop " + "CC.app/Contents/Application Data/Custom File Info " + "Panels/4.0/panels:\n" + "IPTC\n" + "IPTCExt\n" + "advanced\n" + "audioData\n" + "camera\n" + "categories\n" + "description\n" + "dicom\n" + "gpsData\n" + "history\n" + "mobile\n" + "origin\n" + "rawpacket"; + auto ret = utils::gzipCompress(inStr.c_str(), inStr.length()); + if (ret) { std::cout << "origin length=" << inStr.length() << " compressing length=" << ret->length() << std::endl; - + auto decompressStr = utils::gzipDecompress(ret); if (decompressStr) { diff --git a/lib/tests/HttpViewDataTest.cc b/lib/tests/HttpViewDataTest.cc index d7bf3483..f75b1105 100644 --- a/lib/tests/HttpViewDataTest.cc +++ b/lib/tests/HttpViewDataTest.cc @@ -5,8 +5,6 @@ int main() drogon::HttpViewData data; std::cout << (data.insert("1", 1), data.get("1")) << std::endl; std::cout << (data.insertAsString("2", 2.0), data.get("2")) << std::endl; - std::cout << (data.insertFormattedString("3", "third value is %d", 3), - data.get("3")) - << std::endl; + std::cout << (data.insertFormattedString("3", "third value is %d", 3), data.get("3")) << std::endl; std::cout << (data.insertAsString("4", "4"), data.get("4")) << std::endl; } diff --git a/lib/tests/MainLoopTest.cc b/lib/tests/MainLoopTest.cc index c8034b25..73e05713 100644 --- a/lib/tests/MainLoopTest.cc +++ b/lib/tests/MainLoopTest.cc @@ -4,11 +4,6 @@ int main() { - std::thread([]() { - drogon::app().getLoop()->runEvery(1, []() { - std::cout << "!" << std::endl; - }); - }) - .detach(); + std::thread([]() { drogon::app().getLoop()->runEvery(1, []() { std::cout << "!" << std::endl; }); }).detach(); drogon::app().run(); } \ No newline at end of file diff --git a/lib/tests/Md5Test.cc b/lib/tests/Md5Test.cc index a26f27e0..baa4a683 100644 --- a/lib/tests/Md5Test.cc +++ b/lib/tests/Md5Test.cc @@ -2,5 +2,9 @@ #include int main() { - std::cout << Md5Encode::encode("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890") << std::endl; + std::cout << Md5Encode::encode( + "123456789012345678901234567890123456789012345" + "678901234567890123456789012345678901234567890" + "1234567890") + << std::endl; } \ No newline at end of file diff --git a/lib/tests/Sha1Test.cc b/lib/tests/Sha1Test.cc index 67c705f9..2f63b037 100644 --- a/lib/tests/Sha1Test.cc +++ b/lib/tests/Sha1Test.cc @@ -9,11 +9,13 @@ int main() { - unsigned char in[] = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - unsigned char out[SHA_DIGEST_LENGTH] = {0}; - SHA1(in, strlen((const char *)in), out); - /// fecfd28bbc9345891a66d7c1b8ff46e60192d284 - for (int i = 0; i < SHA_DIGEST_LENGTH; i++) - printf("%02x", out[i]); - putchar('\n'); + unsigned char in[] = + "1234567890123456789012345678901234567890123456789012345" + "678901234567890123456789012345678901234567890"; + unsigned char out[SHA_DIGEST_LENGTH] = {0}; + SHA1(in, strlen((const char *)in), out); + /// fecfd28bbc9345891a66d7c1b8ff46e60192d284 + for (int i = 0; i < SHA_DIGEST_LENGTH; i++) + printf("%02x", out[i]); + putchar('\n'); } diff --git a/orm_lib/inc/drogon/orm/ArrayParser.h b/orm_lib/inc/drogon/orm/ArrayParser.h index e14c5b40..e2b35a61 100644 --- a/orm_lib/inc/drogon/orm/ArrayParser.h +++ b/orm_lib/inc/drogon/orm/ArrayParser.h @@ -8,7 +8,7 @@ * COPYING with this source code, please notify the distributor of this mistake, * or contact the author. */ -//Taken from libpqxx and modified +// Taken from libpqxx and modified #pragma once @@ -68,5 +68,5 @@ class ArrayParser const char *m_pos; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/Criteria.h b/orm_lib/inc/drogon/orm/Criteria.h index 8d446854..248b5f9b 100644 --- a/orm_lib/inc/drogon/orm/Criteria.h +++ b/orm_lib/inc/drogon/orm/Criteria.h @@ -9,7 +9,7 @@ * that can be found in the License file. * * Drogon - * + * */ #pragma once @@ -17,17 +17,16 @@ #include #include +#include +#include #include #include -#include #include -#include namespace drogon { namespace orm { - enum class CompareOperator { EQ, @@ -43,71 +42,74 @@ enum class CompareOperator class Criteria { public: - explicit operator bool() const { return !_condString.empty(); } - std::string criteriaString() const { return _condString; } + explicit operator bool() const + { + return !_condString.empty(); + } + std::string criteriaString() const + { + return _condString; + } template Criteria(const std::string &colName, const CompareOperator &opera, T &&arg) { - assert(opera != CompareOperator::IsNotNull && - opera != CompareOperator::IsNull); + assert(opera != CompareOperator::IsNotNull && opera != CompareOperator::IsNull); _condString = colName; switch (opera) { - case CompareOperator::EQ: - _condString += " = $?"; - break; - case CompareOperator::NE: - _condString += " != $?"; - break; - case CompareOperator::GT: - _condString += " > $?"; - break; - case CompareOperator::GE: - _condString += " >= $?"; - break; - case CompareOperator::LT: - _condString += " < $?"; - break; - case CompareOperator::LE: - _condString += " <= $?"; - break; - case CompareOperator::LIKE: - _condString += " like $?"; - break; - case CompareOperator::IsNull: - case CompareOperator::IsNotNull: - default: - break; + case CompareOperator::EQ: + _condString += " = $?"; + break; + case CompareOperator::NE: + _condString += " != $?"; + break; + case CompareOperator::GT: + _condString += " > $?"; + break; + case CompareOperator::GE: + _condString += " >= $?"; + break; + case CompareOperator::LT: + _condString += " < $?"; + break; + case CompareOperator::LE: + _condString += " <= $?"; + break; + case CompareOperator::LIKE: + _condString += " like $?"; + break; + case CompareOperator::IsNull: + case CompareOperator::IsNotNull: + default: + break; } - _outputArgumentsFunc = [=](internal::SqlBinder &binder) { - binder << arg; - }; + _outputArgumentsFunc = [=](internal::SqlBinder &binder) { binder << arg; }; } template - Criteria(const std::string &colName, T &&arg) - : Criteria(colName, CompareOperator::EQ, arg) + Criteria(const std::string &colName, T &&arg) : Criteria(colName, CompareOperator::EQ, arg) { } Criteria(const std::string &colName, const CompareOperator &opera) { - assert(opera == CompareOperator::IsNotNull || - opera == CompareOperator::IsNull); + assert(opera == CompareOperator::IsNotNull || opera == CompareOperator::IsNull); _condString = colName; switch (opera) { - case CompareOperator::IsNull: - _condString += " is null"; - break; - case CompareOperator::IsNotNull: - _condString += " is not null"; - break; - default: - break; + case CompareOperator::IsNull: + _condString += " is null"; + break; + case CompareOperator::IsNotNull: + _condString += " is not null"; + break; + default: + break; } } - Criteria() {} + Criteria() + { + } void outputArgs(internal::SqlBinder &binder) const { if (_outputArgumentsFunc) @@ -119,10 +121,10 @@ class Criteria friend const Criteria operator||(Criteria cond1, Criteria cond2); std::string _condString; std::function _outputArgumentsFunc; -}; // namespace orm +}; // namespace orm const Criteria operator&&(Criteria cond1, Criteria cond2); const Criteria operator||(Criteria cond1, Criteria cond2); -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/DbClient.h b/orm_lib/inc/drogon/orm/DbClient.h index 7228d492..1811fa48 100644 --- a/orm_lib/inc/drogon/orm/DbClient.h +++ b/orm_lib/inc/drogon/orm/DbClient.h @@ -15,24 +15,23 @@ #pragma once #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include namespace drogon { namespace orm { - typedef std::function ResultCallback; typedef std::function ExceptionCallback; @@ -43,28 +42,33 @@ class DbClient : public trantor::NonCopyable { public: virtual ~DbClient(){}; - /// Create new database client with multiple connections; - /** - * @param connInfo: Connection string with some parameters, - * each parameter setting is in the form keyword = value. Spaces around the equal sign are optional. - * To write an empty value, or a value containing spaces, surround it with single quotes, e.g., - * keyword = 'a value'. Single quotes and backslashes within the value must be escaped with a backslash, - * i.e., \' and \\. - * Example: - * host=localhost port=5432 dbname=mydb connect_timeout=10 password='' - * The currently recognized parameter key words are: - * - host: can be either a host name or an IP address. - * - port: Port number to connect to at the server host. - * - dbname: The database name. Defaults to be the same as the user name. - * - user: user name to connect as. With PostgreSQL defaults to be the same as - * the operating system name of the user running the application. - * - password: Password to be used if the server demands password authentication. - * - * For other key words on PostgreSQL, see the PostgreSQL documentation. - * Only a pair of key values ​​is valid for Sqlite3, and its keyword is 'filename'. - * - * @param connNum: The number of connections to database server; - */ +/// Create new database client with multiple connections; +/** + * @param connInfo: Connection string with some parameters, + * each parameter setting is in the form keyword = value. Spaces around the + * equal sign are optional. + * To write an empty value, or a value containing spaces, surround it with + * single quotes, e.g., + * keyword = 'a value'. Single quotes and backslashes within the value must be + * escaped with a backslash, + * i.e., \' and \\. + * Example: + * host=localhost port=5432 dbname=mydb connect_timeout=10 password='' + * The currently recognized parameter key words are: + * - host: can be either a host name or an IP address. + * - port: Port number to connect to at the server host. + * - dbname: The database name. Defaults to be the same as the user name. + * - user: user name to connect as. With PostgreSQL defaults to be the same as + * the operating system name of the user running the application. + * - password: Password to be used if the server demands password + * authentication. + * + * For other key words on PostgreSQL, see the PostgreSQL documentation. + * Only a pair of key values ​​is valid for Sqlite3, and its keyword is + * 'filename'. + * + * @param connNum: The number of connections to database server; + */ #if USE_POSTGRESQL static std::shared_ptr newPgClient(const std::string &connInfo, const size_t connNum); #endif @@ -79,29 +83,30 @@ class DbClient : public trantor::NonCopyable /** * FUNCTION1 is usually the ResultCallback type; * FUNCTION2 is usually the ExceptionCallback type; - * @param args are parameters that are bound to placeholders in the @param sql. - * NOTE: - * + * @param args are parameters that are bound to placeholders in the @param + * sql. + * NOTE: + * * If the number of @param args is not zero, make sure that all criteria - * in @param sql are set by bind parameters, for example: - * - * 1. select * from users where user_id > 10 limit 10 offset 10; //Not bad, no bind parameters are used. - * 2. select * from users where user_id > ? limit ? offset ?; //Good, fully use bind parameters. - * 3. select * from users where user_id > ? limit ? offset 10; //Bad, partially use bind parameters. - * - * Strictly speaking, try not to splice SQL statements dynamically, Instead, use the constant sql string + * in @param sql are set by bind parameters, for example: + * + * 1. select * from users where user_id > 10 limit 10 offset 10; //Not bad, + * no bind parameters are used. + * 2. select * from users where user_id > ? limit ? offset ?; //Good, fully + * use bind parameters. + * 3. select * from users where user_id > ? limit ? offset 10; //Bad, + * partially use bind parameters. + * + * Strictly speaking, try not to splice SQL statements dynamically, Instead, + * use the constant sql string * with placeholders and the bind parameters to execute sql. - * This rule makes the sql execute faster and more securely, and users should follow this rule when calling + * This rule makes the sql execute faster and more securely, and users should + * follow this rule when calling * all methods of DbClient. - * + * */ - template - void execSqlAsync(const std::string &sql, - FUNCTION1 &&rCallback, - FUNCTION2 &&exceptCallback, - Arguments &&... args) noexcept + template + void execSqlAsync(const std::string &sql, FUNCTION1 &&rCallback, FUNCTION2 &&exceptCallback, Arguments &&... args) noexcept { auto binder = *this << sql; (void)std::initializer_list{(binder << std::forward(args), 0)...}; @@ -111,64 +116,67 @@ class DbClient : public trantor::NonCopyable /// Async and nonblocking method template - std::future execSqlAsyncFuture(const std::string &sql, - Arguments &&... args) noexcept + std::future execSqlAsyncFuture(const std::string &sql, Arguments &&... args) noexcept { auto binder = *this << sql; (void)std::initializer_list{(binder << std::forward(args), 0)...}; std::shared_ptr> prom = std::make_shared>(); - binder >> [=](const Result &r) { - prom->set_value(r); - }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const Result &r) { prom->set_value(r); }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } - //Sync and blocking method + // Sync and blocking method template - const Result execSqlSync(const std::string &sql, - Arguments &&... args) noexcept(false) + const Result execSqlSync(const std::string &sql, Arguments &&... args) noexcept(false) { Result r(nullptr); { auto binder = *this << sql; (void)std::initializer_list{(binder << std::forward(args), 0)...}; - //Use blocking mode + // Use blocking mode binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //exec may be throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // exec may be throw exception; } return r; } - /// Streaming-like method for sql execution. For more information, see the wiki page. + /// Streaming-like method for sql execution. For more information, see the + /// wiki page. internal::SqlBinder operator<<(const std::string &sql); internal::SqlBinder operator<<(std::string &&sql); /// Create a transaction object. /** - * @param commitCallback: the callback with which user can get the submitting result, - * The Boolean type parameter in the callback function indicates whether the + * @param commitCallback: the callback with which user can get the submitting + * result, + * The Boolean type parameter in the callback function indicates whether the * transaction was submitted successfully. * NOTE: - * The callback only indicates the result of the 'commit' command, which is the last - * step of the transaction. If the transaction has been automatically or manually rolled back, + * The callback only indicates the result of the 'commit' command, which is + * the last + * step of the transaction. If the transaction has been automatically or + * manually rolled back, * the callback will never be executed. - * You can also use the setCommitCallback() method of a transaction object to set the callback. + * You can also use the setCommitCallback() method of a transaction object to + * set the callback. */ virtual std::shared_ptr newTransaction(const std::function &commitCallback = nullptr) = 0; /// Create a transaction object in asynchronous mode. virtual void newTransactionAsync(const std::function &)> &callback) = 0; - ClientType type() const { return _type; } - const std::string &connectionInfo() { return _connInfo; } + ClientType type() const + { + return _type; + } + const std::string &connectionInfo() + { + return _connInfo; + } private: friend internal::SqlBinder; @@ -190,9 +198,9 @@ class Transaction : public DbClient { public: virtual void rollback() = 0; - //virtual void commit() = 0; + // virtual void commit() = 0; virtual void setCommitCallback(const std::function &commitCallback) = 0; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/Exception.h b/orm_lib/inc/drogon/orm/Exception.h index 075b4652..471b8107 100644 --- a/orm_lib/inc/drogon/orm/Exception.h +++ b/orm_lib/inc/drogon/orm/Exception.h @@ -7,7 +7,7 @@ * or contact the author. */ -//taken from libpqxx and modified +// taken from libpqxx and modified /** * @@ -25,18 +25,18 @@ #pragma once +#include #include #include -#include namespace drogon { namespace orm { - /// Mixin base class to identify drogon-db-specific exception types /** - * If you wish to catch all exception types specific to drogon db for some reason, + * If you wish to catch all exception types specific to drogon db for some + * reason, * catch this type. All of drogon db's exception classes are derived from it * through multiple-inheritance (they also fit into the standard library's * exception hierarchy in more fitting places). @@ -70,7 +70,8 @@ class DrogonDbException * catch (const drogon::orm::DrogonDbException &e) * { * std::cerr << e.base().what() << std::endl; - * const drogon::orm::SqlError *s=dynamic_cast(&e.base()); + * const drogon::orm::SqlError *s=dynamic_cast(&e.base()); * if (s) std::cerr << "Query was: " << s->query() << std::endl; * } * @endcode @@ -79,10 +80,11 @@ class DrogonDbException { static std::exception except; return except; - } //[t00] + } //[t00] }; -/// Run-time Failure encountered by drogon orm lib, similar to std::runtime_error +/// Run-time Failure encountered by drogon orm lib, similar to +/// std::runtime_error class Failure : public DrogonDbException, public std::runtime_error { virtual const std::exception &base() const noexcept override @@ -132,10 +134,7 @@ class SqlError : public Failure const std::string _sqlState; public: - explicit SqlError( - const std::string &msg = "", - const std::string &Q = "", - const char sqlstate[] = nullptr); + explicit SqlError(const std::string &msg = "", const std::string &Q = "", const char sqlstate[] = nullptr); virtual ~SqlError() noexcept; /// The query whose execution triggered the exception @@ -261,108 +260,110 @@ class UnexpectedRows : public RangeError } public: - explicit UnexpectedRows(const std::string &msg) : RangeError(msg) {} + explicit UnexpectedRows(const std::string &msg) : RangeError(msg) + { + } }; /// Database feature not supported in current setup class FeatureNotSupported : public SqlError { public: - explicit FeatureNotSupported( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit FeatureNotSupported(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; /// Error in data provided to SQL statement class DataException : public SqlError { public: - explicit DataException( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit DataException(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; class IntegrityConstraintViolation : public SqlError { public: - explicit IntegrityConstraintViolation( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit IntegrityConstraintViolation(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; class RestrictViolation : public IntegrityConstraintViolation { public: - explicit RestrictViolation( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : IntegrityConstraintViolation(err, Q, sqlstate) {} + explicit RestrictViolation(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : IntegrityConstraintViolation(err, Q, sqlstate) + { + } }; class NotNullViolation : public IntegrityConstraintViolation { public: - explicit NotNullViolation( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : IntegrityConstraintViolation(err, Q, sqlstate) {} + explicit NotNullViolation(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : IntegrityConstraintViolation(err, Q, sqlstate) + { + } }; class ForeignKeyViolation : public IntegrityConstraintViolation { public: - explicit ForeignKeyViolation( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : IntegrityConstraintViolation(err, Q, sqlstate) {} + explicit ForeignKeyViolation(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : IntegrityConstraintViolation(err, Q, sqlstate) + { + } }; class UniqueViolation : public IntegrityConstraintViolation { public: - explicit UniqueViolation( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : IntegrityConstraintViolation(err, Q, sqlstate) {} + explicit UniqueViolation(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : IntegrityConstraintViolation(err, Q, sqlstate) + { + } }; class CheckViolation : public IntegrityConstraintViolation { public: - explicit CheckViolation( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : IntegrityConstraintViolation(err, Q, sqlstate) {} + explicit CheckViolation(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : IntegrityConstraintViolation(err, Q, sqlstate) + { + } }; class InvalidCursorState : public SqlError { public: - explicit InvalidCursorState( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit InvalidCursorState(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; class InvalidSqlStatementName : public SqlError { public: - explicit InvalidSqlStatementName( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit InvalidSqlStatementName(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; class InvalidCursorName : public SqlError { public: - explicit InvalidCursorName( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit InvalidCursorName(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; class SyntaxError : public SqlError @@ -371,81 +372,82 @@ class SyntaxError : public SqlError /// Approximate position in string where error occurred, or -1 if unknown. const int _errorPosition; - explicit SyntaxError( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr, - int pos = -1) : SqlError(err, Q, sqlstate), _errorPosition(pos) {} + explicit SyntaxError(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr, int pos = -1) + : SqlError(err, Q, sqlstate), _errorPosition(pos) + { + } }; class UndefinedColumn : public SyntaxError { public: - explicit UndefinedColumn( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SyntaxError(err, Q, sqlstate) {} + explicit UndefinedColumn(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SyntaxError(err, Q, sqlstate) + { + } }; class UndefinedFunction : public SyntaxError { public: - explicit UndefinedFunction( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SyntaxError(err, Q, sqlstate) {} + explicit UndefinedFunction(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SyntaxError(err, Q, sqlstate) + { + } }; class UndefinedTable : public SyntaxError { public: - explicit UndefinedTable( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SyntaxError(err, Q, sqlstate) {} + explicit UndefinedTable(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SyntaxError(err, Q, sqlstate) + { + } }; class InsufficientPrivilege : public SqlError { public: - explicit InsufficientPrivilege( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit InsufficientPrivilege(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; /// Resource shortage on the server class InsufficientResources : public SqlError { public: - explicit InsufficientResources( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : SqlError(err, Q, sqlstate) {} + explicit InsufficientResources(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : SqlError(err, Q, sqlstate) + { + } }; class DiskFull : public InsufficientResources { public: - explicit DiskFull( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : InsufficientResources(err, Q, sqlstate) {} + explicit DiskFull(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : InsufficientResources(err, Q, sqlstate) + { + } }; class OutOfMemory : public InsufficientResources { public: - explicit OutOfMemory( - const std::string &err, - const std::string &Q = "", - const char sqlstate[] = nullptr) : InsufficientResources(err, Q, sqlstate) {} + explicit OutOfMemory(const std::string &err, const std::string &Q = "", const char sqlstate[] = nullptr) + : InsufficientResources(err, Q, sqlstate) + { + } }; class TooManyConnections : public BrokenConnection { public: - explicit TooManyConnections(const std::string &err) : BrokenConnection(err) {} + explicit TooManyConnections(const std::string &err) : BrokenConnection(err) + { + } }; // /// PL/pgSQL error @@ -488,5 +490,5 @@ class TooManyConnections : public BrokenConnection // const char sqlstate[] = nullptr) : plpgsql_error(err, Q, sqlstate) {} // }; typedef std::function DrogonDbExceptionCallback; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/Field.h b/orm_lib/inc/drogon/orm/Field.h index f8bb62d1..11a35b5f 100644 --- a/orm_lib/inc/drogon/orm/Field.h +++ b/orm_lib/inc/drogon/orm/Field.h @@ -12,24 +12,23 @@ * */ -//Taken from libpqxx and modified. -//The license for libpqxx can be found in the COPYING file. +// Taken from libpqxx and modified. +// The license for libpqxx can be found in the COPYING file. #pragma once -#include +#include #include #include -#include -#include -#include -#include #include +#include +#include +#include +#include #ifdef __linux__ #include -inline uint64_t -ntohll(const uint64_t &input) +inline uint64_t ntohll(const uint64_t &input) { uint64_t rval; uint8_t *data = (uint8_t *)&rval; @@ -46,15 +45,13 @@ ntohll(const uint64_t &input) return rval; } -inline uint64_t -htonll(const uint64_t &input) +inline uint64_t htonll(const uint64_t &input) { return (ntohll(input)); } #endif #ifdef _WIN32 -inline uint64_t -ntohll(const uint64_t &input) +inline uint64_t ntohll(const uint64_t &input) { uint64_t rval; uint8_t *data = (uint8_t *)&rval; @@ -71,8 +68,7 @@ ntohll(const uint64_t &input) return rval; } -inline uint64_t -htonll(const uint64_t &input) +inline uint64_t htonll(const uint64_t &input) { return (ntohll(input)); } @@ -81,9 +77,8 @@ namespace drogon { namespace orm { - /// Reference to a field in a result set. -/** +/** * A field represents one entry in a row. It represents an actual value * in the result set, and can be converted to various types. */ @@ -99,7 +94,7 @@ class Field bool isNull() const; /// Read as plain C string - /** + /** * Since the field's data is stored internally in the form of a * zero-terminated C string, this is the fastest way to read it. Use the * to() or as() functions to convert the string to other types such as @@ -112,7 +107,7 @@ class Field T as() const { auto _data = _result.getValue(_row, _column); - //auto _dataLength = _result.getLength(_row, _column); + // auto _dataLength = _result.getLength(_row, _column); // For binary format! // if (_dataLength == 1) // { @@ -139,7 +134,7 @@ class Field } /// Parse the field as an SQL array. - /** + /** * Call the parser to retrieve values (and structure) from the array. * * Make sure the @c result object stays alive until parsing is finished. If @@ -205,5 +200,5 @@ string_view Field::as() const; // std::vector Field::as>() const; // template <> // std::vector Field::as>() const; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/FunctionTraits.h b/orm_lib/inc/drogon/orm/FunctionTraits.h index 1cf64cc7..27922ef6 100644 --- a/orm_lib/inc/drogon/orm/FunctionTraits.h +++ b/orm_lib/inc/drogon/orm/FunctionTraits.h @@ -13,21 +13,19 @@ */ #pragma once +#include +#include #include #include -#include -#include namespace drogon { namespace orm { - class Result; class DrogonDbException; namespace internal { - template struct FunctionTraits; template <> @@ -41,34 +39,24 @@ struct FunctionTraits static const bool isSqlCallback = false; static const bool isExceptCallback = false; }; -//functor,lambda +// functor,lambda template -struct FunctionTraits : public FunctionTraits< - decltype(&std::remove_reference::type::operator())> +struct FunctionTraits : public FunctionTraits::type::operator())> { }; -template < - typename ClassType, - typename ReturnType, - typename... Arguments> -struct FunctionTraits< - ReturnType (ClassType::*)(Arguments...) const> : FunctionTraits +template +struct FunctionTraits : FunctionTraits { }; -template < - typename ClassType, - typename ReturnType, - typename... Arguments> -struct FunctionTraits< - ReturnType (ClassType::*)(Arguments...)> : FunctionTraits +template +struct FunctionTraits : FunctionTraits { }; template <> -struct FunctionTraits - : public FunctionTraits +struct FunctionTraits : public FunctionTraits { static const bool isSqlCallback = true; static const bool isStepResultCallback = false; @@ -76,8 +64,7 @@ struct FunctionTraits }; template <> -struct FunctionTraits - : public FunctionTraits +struct FunctionTraits : public FunctionTraits { static const bool isExceptCallback = true; static const bool isSqlCallback = false; @@ -86,8 +73,7 @@ struct FunctionTraits }; template <> -struct FunctionTraits - : public FunctionTraits +struct FunctionTraits : public FunctionTraits { static const bool isExceptCallback = true; static const bool isSqlCallback = false; @@ -95,35 +81,27 @@ struct FunctionTraits static const bool isPtr = true; }; -template < - typename ReturnType, - typename... Arguments> -struct FunctionTraits< - ReturnType (*)(bool, Arguments...)> : FunctionTraits +template +struct FunctionTraits : FunctionTraits { static const bool isSqlCallback = true; static const bool isStepResultCallback = true; }; -template < - typename ReturnType, - typename... Arguments> -struct FunctionTraits< - ReturnType (*)(Arguments...)> +template +struct FunctionTraits { typedef ReturnType result_type; template - using argument = typename std::tuple_element< - Index, - std::tuple>::type; + using argument = typename std::tuple_element>::type; static const std::size_t arity = sizeof...(Arguments); - //static const bool isSqlCallback = false; + // static const bool isSqlCallback = false; static const bool isSqlCallback = true; static const bool isStepResultCallback = true; }; -} // namespace internal -} // namespace orm -} // namespace drogon +} // namespace internal +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/Mapper.h b/orm_lib/inc/drogon/orm/Mapper.h index 00656ade..a7216f59 100644 --- a/orm_lib/inc/drogon/orm/Mapper.h +++ b/orm_lib/inc/drogon/orm/Mapper.h @@ -13,18 +13,17 @@ */ #pragma once -#include #include +#include #include -#include #include #include +#include namespace drogon { namespace orm { - enum class SortOrder { ASC, @@ -42,7 +41,7 @@ struct Traits { typedef int type; }; -} // namespace internal +} // namespace internal template class Mapper @@ -58,63 +57,48 @@ class Mapper typedef std::function)> MultipleRowsCallback; typedef std::function CountCallback; - Mapper(const DbClientPtr &client) : _client(client) {} + Mapper(const DbClientPtr &client) : _client(client) + { + } typedef typename internal::Traits::value>::type TraitsPKType; T findByPrimaryKey(const TraitsPKType &key) noexcept(false); - void findByPrimaryKey(const TraitsPKType &key, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void findByPrimaryKey(const TraitsPKType &key, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future findFutureByPrimaryKey(const TraitsPKType &key) noexcept; std::vector findAll() noexcept(false); - void findAll(const MultipleRowsCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void findAll(const MultipleRowsCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future> findFutureAll() noexcept; size_t count(const Criteria &criteria = Criteria()) noexcept(false); - void count(const Criteria &criteria, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void count(const Criteria &criteria, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future countFuture(const Criteria &criteria = Criteria()) noexcept; T findOne(const Criteria &criteria) noexcept(false); - void findOne(const Criteria &criteria, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void findOne(const Criteria &criteria, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future findFutureOne(const Criteria &criteria) noexcept; std::vector findBy(const Criteria &criteria) noexcept(false); - void findBy(const Criteria &criteria, - const MultipleRowsCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void findBy(const Criteria &criteria, const MultipleRowsCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future> findFutureBy(const Criteria &criteria) noexcept; void insert(T &obj) noexcept(false); - void insert(const T &obj, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void insert(const T &obj, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future insertFuture(const T &) noexcept; size_t update(const T &obj) noexcept(false); - void update(const T &obj, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void update(const T &obj, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future updateFuture(const T &obj) noexcept; size_t deleteOne(const T &obj) noexcept(false); - void deleteOne(const T &obj, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void deleteOne(const T &obj, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future deleteFutureOne(const T &obj) noexcept; size_t deleteBy(const Criteria &criteria) noexcept(false); - void deleteBy(const Criteria &criteria, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept; + void deleteBy(const Criteria &criteria, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept; std::future deleteFutureBy(const Criteria &criteria) noexcept; private: @@ -138,7 +122,8 @@ class Mapper sql += " = $?"; } template - typename std::enable_if, PKType>::value, void>::type makePrimaryKeyCriteria(std::string &sql) + typename std::enable_if, PKType>::value, void>::type makePrimaryKeyCriteria( + std::string &sql) { sql += " where "; for (size_t i = 0; i < T::primaryKeyName.size(); i++) @@ -152,14 +137,16 @@ class Mapper } } template - typename std::enable_if::value, void>::type - outputPrimeryKeyToBinder(const TraitsPKType &pk, internal::SqlBinder &binder) + typename std::enable_if::value, void>::type outputPrimeryKeyToBinder( + const TraitsPKType &pk, + internal::SqlBinder &binder) { binder << pk; } template - typename std::enable_if, PKType>::value, void>::type - outputPrimeryKeyToBinder(const TraitsPKType &pk, internal::SqlBinder &binder) + typename std::enable_if, PKType>::value, void>::type outputPrimeryKeyToBinder( + const TraitsPKType &pk, + internal::SqlBinder &binder) { tupleToBinder(pk, binder); } @@ -198,10 +185,8 @@ inline T Mapper::findByPrimaryKey(const typename Mapper::TraitsPKType &key auto binder = *_client << std::move(sql); outputPrimeryKeyToBinder(key, binder); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //exec may be throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // exec may be throw exception; } if (r.size() == 0) { @@ -251,11 +236,10 @@ inline void Mapper::findByPrimaryKey(const typename Mapper::TraitsPKType & } template -inline std::future -Mapper::findFutureByPrimaryKey(const typename Mapper::TraitsPKType &key) noexcept +inline std::future Mapper::findFutureByPrimaryKey(const typename Mapper::TraitsPKType &key) noexcept { static_assert(!std::is_same::value, "No primary key in the table!"); - //return findFutureOne(Criteria(T::primaryKeyName, key)); + // return findFutureOne(Criteria(T::primaryKeyName, key)); std::string sql = "select * from "; sql += T::tableName; makePrimaryKeyCriteria(sql); @@ -297,9 +281,7 @@ Mapper::findFutureByPrimaryKey(const typename Mapper::TraitsPKType &key) n prom->set_value(T(r[0])); } }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -344,10 +326,8 @@ inline T Mapper::findOne(const Criteria &criteria) noexcept(false) binder << _offset; clear(); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //exec may be throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // exec may be throw exception; } if (r.size() == 0) { @@ -362,9 +342,7 @@ inline T Mapper::findOne(const Criteria &criteria) noexcept(false) } template -inline void Mapper::findOne(const Criteria &criteria, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::findOne(const Criteria &criteria, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept { std::string sql = "select * from "; sql += T::tableName; @@ -483,9 +461,7 @@ inline std::future Mapper::findFutureOne(const Criteria &criteria) noexcep prom->set_value(T(r[0])); } }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -529,10 +505,8 @@ inline std::vector Mapper::findBy(const Criteria &criteria) noexcept(false binder << _offset; clear(); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //exec may be throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // exec may be throw exception; } std::vector ret; for (auto const &row : r) @@ -542,9 +516,7 @@ inline std::vector Mapper::findBy(const Criteria &criteria) noexcept(false return ret; } template -inline void Mapper::findBy(const Criteria &criteria, - const MultipleRowsCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::findBy(const Criteria &criteria, const MultipleRowsCallback &rcb, const ExceptionCallback &ecb) noexcept { std::string sql = "select * from "; sql += T::tableName; @@ -636,9 +608,7 @@ inline std::future> Mapper::findFutureBy(const Criteria &crite } prom->set_value(ret); }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -648,8 +618,7 @@ inline std::vector Mapper::findAll() noexcept(false) return findBy(Criteria()); } template -inline void Mapper::findAll(const MultipleRowsCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::findAll(const MultipleRowsCallback &rcb, const ExceptionCallback &ecb) noexcept { findBy(Criteria(), rcb, ecb); } @@ -676,18 +645,14 @@ inline size_t Mapper::count(const Criteria &criteria) noexcept(false) if (criteria) criteria.outputArgs(binder); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //exec may be throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // exec may be throw exception; } assert(r.size() == 1); return r[0]["count"].as(); } template -inline void Mapper::count(const Criteria &criteria, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::count(const Criteria &criteria, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept { std::string sql = "select count(*) from "; sql += T::tableName; @@ -728,9 +693,7 @@ inline std::future Mapper::countFuture(const Criteria &criteria) noex assert(r.size() == 1); prom->set_value(r[0]["count"].as()); }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -746,13 +709,13 @@ inline void Mapper::insert(T &obj) noexcept(false) sql += colName; sql += ","; } - sql[sql.length() - 1] = ')'; //Replace the last ',' + sql[sql.length() - 1] = ')'; // Replace the last ',' sql += " values ("; for (size_t i = 0; i < T::insertColumns().size(); i++) { sql += "$?,"; } - sql[sql.length() - 1] = ')'; //Replace the last ',' + sql[sql.length() - 1] = ')'; // Replace the last ',' if (_client->type() == ClientType::PostgreSQL) { sql += " returning *"; @@ -763,26 +726,22 @@ inline void Mapper::insert(T &obj) noexcept(false) auto binder = *_client << std::move(sql); obj.outputArgs(binder); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //Maybe throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // Maybe throw exception; } if (_client->type() == ClientType::PostgreSQL) { assert(r.size() == 1); obj = T(r[0]); } - else // Mysql or Sqlite3 + else // Mysql or Sqlite3 { auto id = r.insertId(); obj.updateId(id); } } template -inline void Mapper::insert(const T &obj, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::insert(const T &obj, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept { clear(); std::string sql = "insert into "; @@ -793,13 +752,13 @@ inline void Mapper::insert(const T &obj, sql += colName; sql += ","; } - sql[sql.length() - 1] = ')'; //Replace the last ',' + sql[sql.length() - 1] = ')'; // Replace the last ',' sql += " values ("; for (int i = 0; i < T::insertColumns().size(); i++) { sql += "$?,"; } - sql[sql.length() - 1] = ')'; //Replace the last ',' + sql[sql.length() - 1] = ')'; // Replace the last ',' if (_client->type() == ClientType::PostgreSQL) { sql += " returning *"; @@ -814,7 +773,7 @@ inline void Mapper::insert(const T &obj, assert(r.size() == 1); rcb(T(r[0])); } - else //Mysql or Sqlite3 + else // Mysql or Sqlite3 { auto id = r.insertId(); auto newObj = obj; @@ -836,13 +795,13 @@ inline std::future Mapper::insertFuture(const T &obj) noexcept sql += colName; sql += ","; } - sql[sql.length() - 1] = ')'; //Replace the last ',' + sql[sql.length() - 1] = ')'; // Replace the last ',' sql += " values ("; for (int i = 0; i < T::insertColumns().size(); i++) { sql += "$?,"; } - sql[sql.length() - 1] = ')'; //Replace the last ',' + sql[sql.length() - 1] = ')'; // Replace the last ',' if (_client->type() == ClientType::PostgreSQL) { sql += " returning *"; @@ -859,7 +818,7 @@ inline std::future Mapper::insertFuture(const T &obj) noexcept assert(r.size() == 1); prom->set_value(T(r[0])); } - else //Mysql or Sqlite3 + else // Mysql or Sqlite3 { auto id = r.insertId(); auto newObj = obj; @@ -867,9 +826,7 @@ inline std::future Mapper::insertFuture(const T &obj) noexcept prom->set_value(newObj); } }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -886,7 +843,7 @@ inline size_t Mapper::update(const T &obj) noexcept(false) sql += colName; sql += " = $?,"; } - sql[sql.length() - 1] = ' '; //Replace the last ',' + sql[sql.length() - 1] = ' '; // Replace the last ',' makePrimaryKeyCriteria(sql); @@ -897,17 +854,13 @@ inline size_t Mapper::update(const T &obj) noexcept(false) obj.updateArgs(binder); outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //Maybe throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // Maybe throw exception; } return r.affectedRows(); } template -inline void Mapper::update(const T &obj, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::update(const T &obj, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept { clear(); static_assert(!std::is_same::value, "No primary key in the table!"); @@ -919,7 +872,7 @@ inline void Mapper::update(const T &obj, sql += colName; sql += " = $?,"; } - sql[sql.length() - 1] = ' '; //Replace the last ',' + sql[sql.length() - 1] = ' '; // Replace the last ',' makePrimaryKeyCriteria(sql); @@ -927,9 +880,7 @@ inline void Mapper::update(const T &obj, auto binder = *_client << std::move(sql); obj.updateArgs(binder); outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder); - binder >> [=](const Result &r) { - rcb(r.affectedRows()); - }; + binder >> [=](const Result &r) { rcb(r.affectedRows()); }; binder >> ecb; } template @@ -945,7 +896,7 @@ inline std::future Mapper::updateFuture(const T &obj) noexcept sql += colName; sql += " = $?,"; } - sql[sql.length() - 1] = ' '; //Replace the last ',' + sql[sql.length() - 1] = ' '; // Replace the last ',' makePrimaryKeyCriteria(sql); @@ -955,12 +906,8 @@ inline std::future Mapper::updateFuture(const T &obj) noexcept outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder); std::shared_ptr> prom = std::make_shared>(); - binder >> [=](const Result &r) { - prom->set_value(r.affectedRows()); - }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const Result &r) { prom->set_value(r.affectedRows()); }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -973,7 +920,7 @@ inline size_t Mapper::deleteOne(const T &obj) noexcept(false) std::string sql = "delete from "; sql += T::tableName; - sql += " "; //Replace the last ',' + sql += " "; // Replace the last ',' makePrimaryKeyCriteria(sql); @@ -983,17 +930,13 @@ inline size_t Mapper::deleteOne(const T &obj) noexcept(false) auto binder = *_client << std::move(sql); outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder); binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //Maybe throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // Maybe throw exception; } return r.affectedRows(); } template -inline void Mapper::deleteOne(const T &obj, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::deleteOne(const T &obj, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept { clear(); static_assert(!std::is_same::value, "No primary key in the table!"); @@ -1006,9 +949,7 @@ inline void Mapper::deleteOne(const T &obj, sql = replaceSqlPlaceHolder(sql, "$?"); auto binder = *_client << std::move(sql); outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder); - binder >> [=](const Result &r) { - rcb(r.affectedRows()); - }; + binder >> [=](const Result &r) { rcb(r.affectedRows()); }; binder >> ecb; } template @@ -1027,12 +968,8 @@ inline std::future Mapper::deleteFutureOne(const T &obj) noexcept outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder); std::shared_ptr> prom = std::make_shared>(); - binder >> [=](const Result &r) { - prom->set_value(r.affectedRows()); - }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const Result &r) { prom->set_value(r.affectedRows()); }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -1060,17 +997,13 @@ inline size_t Mapper::deleteBy(const Criteria &criteria) noexcept(false) criteria.outputArgs(binder); } binder << Mode::Blocking; - binder >> [&r](const Result &result) { - r = result; - }; - binder.exec(); //Maybe throw exception; + binder >> [&r](const Result &result) { r = result; }; + binder.exec(); // Maybe throw exception; } return r.affectedRows(); } template -inline void Mapper::deleteBy(const Criteria &criteria, - const CountCallback &rcb, - const ExceptionCallback &ecb) noexcept +inline void Mapper::deleteBy(const Criteria &criteria, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept { clear(); static_assert(!std::is_same::value, "No primary key in the table!"); @@ -1089,9 +1022,7 @@ inline void Mapper::deleteBy(const Criteria &criteria, { criteria.outputArgs(binder); } - binder >> [=](const Result &r) { - rcb(r.affectedRows()); - }; + binder >> [=](const Result &r) { rcb(r.affectedRows()); }; binder >> ecb; } template @@ -1114,12 +1045,8 @@ inline std::future Mapper::deleteFutureBy(const Criteria &criteria) n } std::shared_ptr> prom = std::make_shared>(); - binder >> [=](const Result &r) { - prom->set_value(r.affectedRows()); - }; - binder >> [=](const std::exception_ptr &e) { - prom->set_exception(e); - }; + binder >> [=](const Result &r) { prom->set_value(r.affectedRows()); }; + binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); }; binder.exec(); return prom->get_future(); } @@ -1219,5 +1146,5 @@ inline std::string Mapper::replaceSqlPlaceHolder(const std::string &sqlStr, c } } -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/Result.h b/orm_lib/inc/drogon/orm/Result.h index e2335398..e8f73977 100644 --- a/orm_lib/inc/drogon/orm/Result.h +++ b/orm_lib/inc/drogon/orm/Result.h @@ -12,8 +12,8 @@ * */ -//Taken from libpqxx and modified. -//The license for libpqxx can be found in the COPYING file. +// Taken from libpqxx and modified. +// The license for libpqxx can be found in the COPYING file. #pragma once @@ -55,8 +55,9 @@ enum class SqlStatus class Result { public: - Result(const ResultImplPtr &ptr) - : _resultPtr(ptr) {} + Result(const ResultImplPtr &ptr) : _resultPtr(ptr) + { + } using difference_type = long; using size_type = unsigned long; using reference = Row; @@ -69,7 +70,10 @@ class Result using ReverseIterator = ConstReverseIterator; size_type size() const noexcept; - size_type capacity() const noexcept { return size(); } + size_type capacity() const noexcept + { + return size(); + } ConstIterator begin() const noexcept; ConstIterator cbegin() const noexcept; ConstIterator end() const noexcept; @@ -80,7 +84,10 @@ class Result ConstReverseIterator rend() const; ConstReverseIterator crend() const; - bool empty() const noexcept { return size() == 0; } + bool empty() const noexcept + { + return size() == 0; + } reference front() const noexcept; reference back() const noexcept; @@ -102,11 +109,12 @@ class Result */ size_type affectedRows() const noexcept; - /// For Mysql, Sqlite3 databases, return the auto-incrementing primary key after inserting + /// For Mysql, Sqlite3 databases, return the auto-incrementing primary key + /// after inserting /** - * For postgreSQL databases, this method always returns zero, One can use + * For postgreSQL databases, this method always returns zero, One can use * the following sql to get auto-incrementing id: - * insert into table_name volumn1, volumn2 values(....) returning id; + * insert into table_name volumn1, volumn2 values(....) returning id; */ unsigned long long insertId() const noexcept; @@ -133,7 +141,9 @@ class Result field_size_type getLength(size_type row, row_size_type column) const; protected: - Result() {} + Result() + { + } }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/ResultIterator.h b/orm_lib/inc/drogon/orm/ResultIterator.h index 769f339b..e7e681e6 100644 --- a/orm_lib/inc/drogon/orm/ResultIterator.h +++ b/orm_lib/inc/drogon/orm/ResultIterator.h @@ -12,8 +12,8 @@ * */ -//Taken from libpqxx and modified. -//The license for libpqxx can be found in the COPYING file. +// Taken from libpqxx and modified. +// The license for libpqxx can be found in the COPYING file. #pragma once @@ -24,12 +24,7 @@ namespace drogon namespace orm { class ConstResultIterator - : public std::iterator< - std::random_access_iterator_tag, - const Row, - Result::difference_type, - ConstResultIterator, - Row>, + : public std::iterator, protected Row { public: @@ -37,10 +32,16 @@ class ConstResultIterator using reference = Row; using size_type = Result::size_type; using difference_type = Result::difference_type; - //ConstResultIterator(const Row &t) noexcept : Row(t) {} + // ConstResultIterator(const Row &t) noexcept : Row(t) {} - pointer operator->() { return this; } - reference operator*() { return Row(*this); } + pointer operator->() + { + return this; + } + reference operator*() + { + return Row(*this); + } ConstResultIterator operator++(int); ConstResultIterator &operator++() @@ -92,7 +93,9 @@ class ConstResultIterator private: friend class Result; - ConstResultIterator(const Result &r, size_type index) noexcept : Row(r, index) {} + ConstResultIterator(const Result &r, size_type index) noexcept : Row(r, index) + { + } }; class ConstReverseResultIterator : private ConstResultIterator @@ -106,10 +109,13 @@ class ConstReverseResultIterator : private ConstResultIterator using value_type = iterator_type::value_type; using reference = iterator_type::reference; - ConstReverseResultIterator( - const ConstReverseResultIterator &rhs) : ConstResultIterator(rhs) {} - explicit ConstReverseResultIterator( - const ConstResultIterator &rhs) : ConstResultIterator(rhs) { super::operator--(); } + ConstReverseResultIterator(const ConstReverseResultIterator &rhs) : ConstResultIterator(rhs) + { + } + explicit ConstReverseResultIterator(const ConstResultIterator &rhs) : ConstResultIterator(rhs) + { + super::operator--(); + } ConstResultIterator base() const noexcept; @@ -165,5 +171,5 @@ class ConstReverseResultIterator : private ConstResultIterator } }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/Row.h b/orm_lib/inc/drogon/orm/Row.h index 8c7437ae..2b457e6d 100644 --- a/orm_lib/inc/drogon/orm/Row.h +++ b/orm_lib/inc/drogon/orm/Row.h @@ -12,8 +12,8 @@ * */ -//Taken from libpqxx and modified. -//The license for libpqxx can be found in the COPYING file. +// Taken from libpqxx and modified. +// The license for libpqxx can be found in the COPYING file. #pragma once @@ -23,19 +23,19 @@ namespace drogon { namespace orm { - class Field; class ConstRowIterator; class ConstReverseRowIterator; /// Reference to one row in a result. -/** +/** * A row represents one row (also called a row) in a query result set. * It also acts as a container mapping column numbers or names to field * values (see below): * * @code - * cout << row["date"].as() << ": " << row["name"].as() << endl; + * cout << row["date"].as() << ": " << + *row["name"].as() << endl; * @endcode * * The row itself acts like a (non-modifyable) container, complete with its @@ -56,7 +56,10 @@ class Row reference operator[](const char columnName[]) const; reference operator[](const std::string &columnName) const; size_type size() const; - size_type capacity() const noexcept { return size(); } + size_type capacity() const noexcept + { + return size(); + } ConstIterator begin() const noexcept; ConstIterator cbegin() const noexcept; ConstIterator end() const noexcept; @@ -83,5 +86,5 @@ class Row Row(const Result &r, size_type index) noexcept; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/RowIterator.h b/orm_lib/inc/drogon/orm/RowIterator.h index 3655a7da..a9830d52 100644 --- a/orm_lib/inc/drogon/orm/RowIterator.h +++ b/orm_lib/inc/drogon/orm/RowIterator.h @@ -12,34 +12,36 @@ * */ -//Taken from libpqxx and modified. -//The license for libpqxx can be found in the COPYING file. +// Taken from libpqxx and modified. +// The license for libpqxx can be found in the COPYING file. #pragma once -#include #include +#include namespace drogon { namespace orm { -class ConstRowIterator : public std::iterator< - std::random_access_iterator_tag, - const Field, - Row::difference_type, - ConstRowIterator, - Field>, - protected Field +class ConstRowIterator + : public std::iterator, + protected Field { public: using pointer = const Field *; using reference = Field; using size_type = Row::size_type; using difference_type = Row::difference_type; - //ConstRowIterator(const Field &t) noexcept : Field(t) {} + // ConstRowIterator(const Field &t) noexcept : Field(t) {} - pointer operator->() { return this; } - reference operator*() { return Field(*this); } + pointer operator->() + { + return this; + } + reference operator*() + { + return Field(*this); + } ConstRowIterator operator++(int); ConstRowIterator &operator++() @@ -91,7 +93,9 @@ class ConstRowIterator : public std::iterator< private: friend class Row; - ConstRowIterator(const Row &r, size_type column) noexcept : Field(r, column) {} + ConstRowIterator(const Row &r, size_type column) noexcept : Field(r, column) + { + } }; class ConstReverseRowIterator : private ConstRowIterator @@ -105,10 +109,13 @@ class ConstReverseRowIterator : private ConstRowIterator using value_type = iterator_type::value_type; using reference = iterator_type::reference; - ConstReverseRowIterator( - const ConstReverseRowIterator &rhs) : ConstRowIterator(rhs) {} - explicit ConstReverseRowIterator( - const ConstRowIterator &rhs) : ConstRowIterator(rhs) { super::operator--(); } + ConstReverseRowIterator(const ConstReverseRowIterator &rhs) : ConstRowIterator(rhs) + { + } + explicit ConstReverseRowIterator(const ConstRowIterator &rhs) : ConstRowIterator(rhs) + { + super::operator--(); + } ConstRowIterator base() const noexcept; @@ -164,5 +171,5 @@ class ConstReverseRowIterator : private ConstRowIterator } }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/inc/drogon/orm/SqlBinder.h b/orm_lib/inc/drogon/orm/SqlBinder.h index 2ff5d418..0efe42c8 100644 --- a/orm_lib/inc/drogon/orm/SqlBinder.h +++ b/orm_lib/inc/drogon/orm/SqlBinder.h @@ -14,24 +14,24 @@ #pragma once #include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include #include #if USE_MYSQL #include #endif -#include -#include -#include #include +#include #include -#include #include #include +#include +#include +#include namespace drogon { @@ -95,7 +95,8 @@ struct VectorTypeTraits typedef std::string ItemsType; }; -//we only accept value type or const lreference type or rreference type as handle method parameters type +// we only accept value type or const lreference type or rreference type as +// handle method parameters type template struct CallbackArgTypeTraits { @@ -147,8 +148,7 @@ class CallbackHolder : public CallbackHolderBase private: Function _function; typedef FunctionTraits traits; - template < - std::size_t Index> + template using NthArgumentType = typename traits::template argument; static const size_t argumentCount = traits::arity; @@ -172,37 +172,35 @@ class CallbackHolder : public CallbackHolderBase static_assert(argumentCount == 0, "Your sql callback function type is wrong!"); _function(result); } - template < - typename... Values, - std::size_t Boundary = argumentCount> - typename std::enable_if<(sizeof...(Values) < Boundary), void>::type run( - const Row *const row, - bool isNull, - Values &&... values) + template + typename std::enable_if<(sizeof...(Values) < Boundary), void>::type run(const Row *const row, + bool isNull, + Values &&... values) { - //call this function recursively until parameter's count equals to the count of target function parameters + // call this function recursively until parameter's count equals to the + // count of target function parameters static_assert(CallbackArgTypeTraits>::isValid, - "your sql callback function argument type must be value type or const left-reference type"); - typedef typename std::remove_cv>::type>::type ValueType; + "your sql callback function argument type must be value type or const " + "left-reference type"); + typedef + typename std::remove_cv>::type>::type ValueType; ValueType value = ValueType(); if (row && row->size() > sizeof...(Values)) { // if(!VectorTypeTraits::isVector) // value = (*row)[sizeof...(Values)].as(); // else - // ; // value = (*row)[sizeof...(Values)].asArray::ItemsType>(); + // ; // value = + // (*row)[sizeof...(Values)].asArray::ItemsType>(); value = makeValue((*row)[(Row::size_type)sizeof...(Values)]); } run(row, isNull, std::forward(values)..., std::move(value)); } - template < - typename... Values, - std::size_t Boundary = argumentCount> - typename std::enable_if<(sizeof...(Values) == Boundary), void>::type run( - const Row *const row, - bool isNull, - Values &&... values) + template + typename std::enable_if<(sizeof...(Values) == Boundary), void>::type run(const Row *const row, + bool isNull, + Values &&... values) { _function(isNull, std::move(values)...); } @@ -231,18 +229,16 @@ class SqlBinder { } ~SqlBinder(); - template > + template > typename std::enable_if::type &operator>>(CallbackType &&callback) { - //LOG_DEBUG << "ptr callback"; + // LOG_DEBUG << "ptr callback"; _isExceptPtr = true; _exceptPtrCallback = std::forward(callback); return *this; } - template > + template > typename std::enable_if::type &operator>>(CallbackType &&callback) { _isExceptPtr = false; @@ -250,16 +246,18 @@ class SqlBinder return *this; } - template > + template > typename std::enable_if::type &operator>>(CallbackType &&callback) { - _callbackHolder = std::shared_ptr(new CallbackHolder(std::forward(callback))); + _callbackHolder = + std::shared_ptr(new CallbackHolder(std::forward(callback))); return *this; } template - typename std::enable_if::type>::type, trantor::Date>::value, self &>::type + typename std::enable_if< + !std::is_same::type>::type, trantor::Date>::value, + self &>::type operator<<(T &¶meter) { _paraNum++; @@ -269,19 +267,19 @@ class SqlBinder { switch (sizeof(T)) { - case 2: - *std::static_pointer_cast(obj) = ntohs(parameter); - break; - case 4: - *std::static_pointer_cast(obj) = ntohl(parameter); - break; - case 8: - *std::static_pointer_cast(obj) = ntohll(parameter); - break; - case 1: - default: + case 2: + *std::static_pointer_cast(obj) = ntohs(parameter); + break; + case 4: + *std::static_pointer_cast(obj) = ntohl(parameter); + break; + case 8: + *std::static_pointer_cast(obj) = ntohll(parameter); + break; + case 1: + default: - break; + break; } _objs.push_back(obj); _parameters.push_back((char *)obj.get()); @@ -296,19 +294,19 @@ class SqlBinder _length.push_back(0); switch (sizeof(T)) { - case 1: - _format.push_back(MYSQL_TYPE_TINY); - break; - case 2: - _format.push_back(MYSQL_TYPE_SHORT); - break; - case 4: - _format.push_back(MYSQL_TYPE_LONG); - break; - case 8: - _format.push_back(MYSQL_TYPE_LONGLONG); - default: - break; + case 1: + _format.push_back(MYSQL_TYPE_TINY); + break; + case 2: + _format.push_back(MYSQL_TYPE_SHORT); + break; + case 4: + _format.push_back(MYSQL_TYPE_LONG); + break; + case 8: + _format.push_back(MYSQL_TYPE_LONGLONG); + default: + break; } #endif } @@ -319,25 +317,25 @@ class SqlBinder _length.push_back(0); switch (sizeof(T)) { - case 1: - _format.push_back(Sqlite3TypeChar); - break; - case 2: - _format.push_back(Sqlite3TypeShort); - break; - case 4: - _format.push_back(Sqlite3TypeInt); - break; - case 8: - _format.push_back(Sqlite3TypeInt64); - default: - break; + case 1: + _format.push_back(Sqlite3TypeChar); + break; + case 2: + _format.push_back(Sqlite3TypeShort); + break; + case 4: + _format.push_back(Sqlite3TypeInt); + break; + case 8: + _format.push_back(Sqlite3TypeInt64); + default: + break; } } - //LOG_TRACE << "Bind parameter:" << parameter; + // LOG_TRACE << "Bind parameter:" << parameter; return *this; } - //template <> + // template <> self &operator<<(const char str[]) { return operator<<(std::string(str)); @@ -524,6 +522,6 @@ class SqlBinder ClientType _type; }; -} // namespace internal -} // namespace orm -} // namespace drogon +} // namespace internal +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/ArrayParser.cc b/orm_lib/src/ArrayParser.cc index 70f75d21..09e33eb8 100644 --- a/orm_lib/src/ArrayParser.cc +++ b/orm_lib/src/ArrayParser.cc @@ -7,7 +7,7 @@ * or contact the author. */ -//Taken from libpqxx and modified +// Taken from libpqxx and modified /** * @@ -50,28 +50,26 @@ const char *scan_single_quoted_string(const char begin[]) { switch (*here) { - case '\'': - // Escaped quote, or closing quote. - here++; - // If the next character is a quote, we've got a double single quote. - // That's how SQL escapes embedded quotes in a string. Terrible idea, - // but it's what we have. - if (*here != '\'') - return here; - // Otherwise, we've found the end of the string. Return the address of - // the very next byte. - break; - case '\\': - // Backslash escape. Skip ahead by one more character. - here++; - if (!*here) - throw ArgumentError( - "SQL string ends in escape: " + std::string(begin)); - break; + case '\'': + // Escaped quote, or closing quote. + here++; + // If the next character is a quote, we've got a double single quote. + // That's how SQL escapes embedded quotes in a string. Terrible idea, + // but it's what we have. + if (*here != '\'') + return here; + // Otherwise, we've found the end of the string. Return the address of + // the very next byte. + break; + case '\\': + // Backslash escape. Skip ahead by one more character. + here++; + if (!*here) + throw ArgumentError("SQL string ends in escape: " + std::string(begin)); + break; } } - throw ArgumentError( - "Null byte in SQL string: " + std::string(begin)); + throw ArgumentError("Null byte in SQL string: " + std::string(begin)); } /// Parse a single-quoted SQL string: un-quote it and un-escape it. @@ -112,19 +110,17 @@ const char *scan_double_quoted_string(const char begin[]) { switch (*here) { - case '\\': - // Backslash escape. Skip ahead by one more character. - here++; - if (!*here) - throw ArgumentError( - "SQL string ends in escape: " + std::string(begin)); - break; - case '"': - return here + 1; + case '\\': + // Backslash escape. Skip ahead by one more character. + here++; + if (!*here) + throw ArgumentError("SQL string ends in escape: " + std::string(begin)); + break; + case '"': + return here + 1; } } - throw ArgumentError( - "Null byte in SQL string: " + std::string(begin)); + throw ArgumentError("Null byte in SQL string: " + std::string(begin)); } /// Parse a double-quoted SQL string: un-quote it and un-escape it. @@ -163,10 +159,7 @@ const char *scan_unquoted_string(const char begin[]) assert(*begin != '"'); const char *p; - for ( - p = begin; - *p != ',' && *p != ';' && *p != '}'; - p++) + for (p = begin; *p != ',' && *p != ';' && *p != '}'; p++) ; return p; } @@ -180,14 +173,13 @@ std::string parse_unquoted_string(const char begin[], const char end[]) return std::string(begin, end); } -} // namespace +} // namespace ArrayParser::ArrayParser(const char input[]) : m_pos(input) { } -std::pair -ArrayParser::getNext() +std::pair ArrayParser::getNext() { ArrayParser::juncture found; std::string value; @@ -201,45 +193,45 @@ ArrayParser::getNext() else switch (*m_pos) { - case '\0': - found = juncture::done; - end = m_pos; - break; - case '{': - found = juncture::row_start; - end = m_pos + 1; - break; - case '}': - found = juncture::row_end; - end = m_pos + 1; - break; - case '\'': - found = juncture::string_value; - end = scan_single_quoted_string(m_pos); - value = parse_single_quoted_string(m_pos, end); - break; - case '"': - found = juncture::string_value; - end = scan_double_quoted_string(m_pos); - value = parse_double_quoted_string(m_pos, end); - break; - default: - end = scan_unquoted_string(m_pos); - value = parse_unquoted_string(m_pos, end); - if (value == "NULL") - { - // In this one situation, as a special case, NULL means a null field, - // not a string that happens to spell "NULL". - value.clear(); - found = juncture::null_value; - } - else - { - // The normal case: we just parsed an unquoted string. The value is - // what we need. + case '\0': + found = juncture::done; + end = m_pos; + break; + case '{': + found = juncture::row_start; + end = m_pos + 1; + break; + case '}': + found = juncture::row_end; + end = m_pos + 1; + break; + case '\'': found = juncture::string_value; - } - break; + end = scan_single_quoted_string(m_pos); + value = parse_single_quoted_string(m_pos, end); + break; + case '"': + found = juncture::string_value; + end = scan_double_quoted_string(m_pos); + value = parse_double_quoted_string(m_pos, end); + break; + default: + end = scan_unquoted_string(m_pos); + value = parse_unquoted_string(m_pos, end); + if (value == "NULL") + { + // In this one situation, as a special case, NULL means a null field, + // not a string that happens to spell "NULL". + value.clear(); + found = juncture::null_value; + } + else + { + // The normal case: we just parsed an unquoted string. The value is + // what we need. + found = juncture::string_value; + } + break; } // Skip a field separator following a string (or null). diff --git a/orm_lib/src/Criteria.cc b/orm_lib/src/Criteria.cc index 05833998..d2104cf7 100644 --- a/orm_lib/src/Criteria.cc +++ b/orm_lib/src/Criteria.cc @@ -18,7 +18,6 @@ namespace drogon { namespace orm { - const Criteria operator&&(Criteria cond1, Criteria cond2) { assert(cond1); @@ -68,5 +67,5 @@ const Criteria operator||(Criteria cond1, Criteria cond2) return cond; } -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/DbClient.cc b/orm_lib/src/DbClient.cc index 67063538..2c7a3865 100644 --- a/orm_lib/src/DbClient.cc +++ b/orm_lib/src/DbClient.cc @@ -12,8 +12,8 @@ * */ -#include #include "DbClientImpl.h" +#include using namespace drogon::orm; using namespace drogon; diff --git a/orm_lib/src/DbClientImpl.cc b/orm_lib/src/DbClientImpl.cc index 99b02230..d99e3ac9 100644 --- a/orm_lib/src/DbClientImpl.cc +++ b/orm_lib/src/DbClientImpl.cc @@ -24,31 +24,34 @@ #include "sqlite3_impl/Sqlite3Connection.h" #endif #include "TransactionImpl.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include +#include +#include using namespace drogon::orm; DbClientImpl::DbClientImpl(const std::string &connInfo, const size_t connNum, ClientType type) : _connectNum(connNum), - _loops(type == ClientType::Sqlite3 ? 1 : (connNum < std::thread::hardware_concurrency() ? connNum : std::thread::hardware_concurrency()), "DbLoop") + _loops(type == ClientType::Sqlite3 + ? 1 + : (connNum < std::thread::hardware_concurrency() ? connNum : std::thread::hardware_concurrency()), + "DbLoop") { _type = type; _connInfo = connInfo; LOG_TRACE << "type=" << (int)type; - //LOG_DEBUG << _loops.getLoopNum(); + // LOG_DEBUG << _loops.getLoopNum(); assert(connNum > 0); _loops.start(); if (type == ClientType::PostgreSQL) @@ -62,8 +65,7 @@ DbClientImpl::DbClientImpl(const std::string &connInfo, const size_t connNum, Cl _connections.insert(newConnection(loop)); }); } - }) - .detach(); + }).detach(); } else if (type == ClientType::Mysql) { @@ -76,8 +78,7 @@ DbClientImpl::DbClientImpl(const std::string &connInfo, const size_t connNum, Cl _connections.insert(newConnection(loop)); }); } - }) - .detach(); + }).detach(); } else if (type == ClientType::Sqlite3) { @@ -176,7 +177,14 @@ void DbClientImpl::execSql(std::string &&sql, } if (conn) { - execSql(conn, std::move(sql), paraNum, std::move(parameters), std::move(length), std::move(format), std::move(rcb), std::move(exceptCallback)); + execSql(conn, + std::move(sql), + paraNum, + std::move(parameters), + std::move(length), + std::move(format), + std::move(rcb), + std::move(exceptCallback)); return; } bool busy = false; @@ -184,7 +192,7 @@ void DbClientImpl::execSql(std::string &&sql, std::lock_guard guard(_bufferMutex); if (_sqlCmdBuffer.size() > 200000) { - //too many queries in buffer; + // too many queries in buffer; busy = true; } } @@ -200,7 +208,7 @@ void DbClientImpl::execSql(std::string &&sql, } return; } - //LOG_TRACE << "Push query to buffer"; + // LOG_TRACE << "Push query to buffer"; std::shared_ptr cmd = std::make_shared(std::move(sql), paraNum, std::move(parameters), @@ -239,52 +247,49 @@ void DbClientImpl::newTransactionAsync(const std::function &)> &&callback) { std::weak_ptr weakThis = shared_from_this(); - auto trans = std::shared_ptr(new TransactionImpl(_type, conn, std::function(), [weakThis, conn]() { - auto thisPtr = weakThis.lock(); - if (!thisPtr) - return; - if (conn->status() == ConnectStatus_Bad) - { - return; - } - { - std::lock_guard guard(thisPtr->_connectionsMutex); - if (thisPtr->_connections.find(conn) == thisPtr->_connections.end() && - thisPtr->_busyConnections.find(conn) == thisPtr->_busyConnections.find(conn)) - { - //connection is broken and removed - return; - } - } - conn->loop()->queueInLoop([weakThis, conn]() { + auto trans = + std::shared_ptr(new TransactionImpl(_type, conn, std::function(), [weakThis, conn]() { auto thisPtr = weakThis.lock(); if (!thisPtr) return; - std::weak_ptr weakConn = conn; - conn->setIdleCallback([weakThis, weakConn]() { + if (conn->status() == ConnectStatus_Bad) + { + return; + } + { + std::lock_guard guard(thisPtr->_connectionsMutex); + if (thisPtr->_connections.find(conn) == thisPtr->_connections.end() && + thisPtr->_busyConnections.find(conn) == thisPtr->_busyConnections.find(conn)) + { + // connection is broken and removed + return; + } + } + conn->loop()->queueInLoop([weakThis, conn]() { auto thisPtr = weakThis.lock(); if (!thisPtr) return; - auto connPtr = weakConn.lock(); - if (!connPtr) - return; - thisPtr->handleNewTask(connPtr); + std::weak_ptr weakConn = conn; + conn->setIdleCallback([weakThis, weakConn]() { + auto thisPtr = weakThis.lock(); + if (!thisPtr) + return; + auto connPtr = weakConn.lock(); + if (!connPtr) + return; + thisPtr->handleNewTask(connPtr); + }); + thisPtr->handleNewTask(conn); }); - thisPtr->handleNewTask(conn); - }); - })); + })); trans->doBegin(); - conn->loop()->queueInLoop([callback = std::move(callback), trans]() { - callback(trans); - }); + conn->loop()->queueInLoop([ callback = std::move(callback), trans ]() { callback(trans); }); } std::shared_ptr DbClientImpl::newTransaction(const std::function &commitCallback) { std::promise> pro; auto f = pro.get_future(); - newTransactionAsync([&pro](const std::shared_ptr &trans) { - pro.set_value(trans); - }); + newTransactionAsync([&pro](const std::shared_ptr &trans) { pro.set_value(trans); }); auto trans = f.get(); trans->setCommitCallback(commitCallback); return trans; @@ -306,7 +311,7 @@ void DbClientImpl::handleNewTask(const DbConnectionPtr &connPtr) makeTrans(connPtr, std::move(transCallback)); return; } - //Then check if there are some sql queries in the buffer + // Then check if there are some sql queries in the buffer std::shared_ptr cmd; { std::lock_guard guard(_bufferMutex); @@ -318,10 +323,17 @@ void DbClientImpl::handleNewTask(const DbConnectionPtr &connPtr) } if (cmd) { - execSql(connPtr, std::move(cmd->_sql), cmd->_paraNum, std::move(cmd->_parameters), std::move(cmd->_length), std::move(cmd->_format), std::move(cmd->_cb), std::move(cmd->_exceptCb)); + execSql(connPtr, + std::move(cmd->_sql), + cmd->_paraNum, + std::move(cmd->_parameters), + std::move(cmd->_length), + std::move(cmd->_format), + std::move(cmd->_cb), + std::move(cmd->_exceptCb)); return; } - //Connection is idle, put it into the _readyConnections set; + // Connection is idle, put it into the _readyConnections set; { std::lock_guard guard(_connectionsMutex); _busyConnections.erase(connPtr); @@ -363,7 +375,7 @@ DbConnectionPtr DbClientImpl::newConnection(trantor::EventLoop *loop) std::weak_ptr weakPtr = shared_from_this(); connPtr->setCloseCallback([weakPtr](const DbConnectionPtr &closeConnPtr) { - //Erase the connection + // Erase the connection auto thisPtr = weakPtr.lock(); if (!thisPtr) return; @@ -374,7 +386,7 @@ DbConnectionPtr DbClientImpl::newConnection(trantor::EventLoop *loop) assert(thisPtr->_connections.find(closeConnPtr) != thisPtr->_connections.end()); thisPtr->_connections.erase(closeConnPtr); } - //Reconnect after 1 second + // Reconnect after 1 second auto loop = closeConnPtr->loop(); loop->runAfter(1, [weakPtr, loop] { auto thisPtr = weakPtr.lock(); @@ -391,7 +403,7 @@ DbConnectionPtr DbClientImpl::newConnection(trantor::EventLoop *loop) return; { std::lock_guard guard(thisPtr->_connectionsMutex); - thisPtr->_busyConnections.insert(okConnPtr); //For new connections, this sentence is necessary + thisPtr->_busyConnections.insert(okConnPtr); // For new connections, this sentence is necessary } thisPtr->handleNewTask(okConnPtr); }); @@ -405,6 +417,6 @@ DbConnectionPtr DbClientImpl::newConnection(trantor::EventLoop *loop) return; thisPtr->handleNewTask(connPtr); }); - //std::cout<<"newConn end"< #include -#include -#include -#include #include -#include -#include #include +#include +#include +#include +#include +#include namespace drogon { namespace orm { - class DbClientImpl : public DbClient, public std::enable_shared_from_this { public: @@ -62,7 +61,7 @@ class DbClientImpl : public DbClient, public std::enable_shared_from_this &)> &&callback); - + std::mutex _connectionsMutex; std::unordered_set _connections; std::unordered_set _readyConnections; @@ -103,5 +102,5 @@ class DbClientImpl : public DbClient, public std::enable_shared_from_this +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include +#include +#include using namespace drogon::orm; DbClientLockFree::DbClientLockFree(const std::string &connInfo, trantor::EventLoop *loop, ClientType type) - : _connInfo(connInfo), - _loop(loop) + : _connInfo(connInfo), _loop(loop) { _type = type; LOG_TRACE << "type=" << (int)type; @@ -55,9 +54,7 @@ DbClientLockFree::DbClientLockFree(const std::string &connInfo, trantor::EventLo else if (type == ClientType::Mysql) { for (size_t i = 0; i < _connectionNum; i++) - _loop->runAfter(0.1 * (i + 1), [this]() { - _connectionHolders.push_back(newConnection()); - }); + _loop->runAfter(0.1 * (i + 1), [this]() { _connectionHolders.push_back(newConnection()); }); } else { @@ -118,7 +115,7 @@ void DbClientLockFree::execSql(std::string &&sql, if (_sqlCmdBuffer.size() > 20000) { - //too many queries in buffer; + // too many queries in buffer; try { throw Failure("Too many queries in buffer"); @@ -130,7 +127,7 @@ void DbClientLockFree::execSql(std::string &&sql, return; } - //LOG_TRACE << "Push query to buffer"; + // LOG_TRACE << "Push query to buffer"; _sqlCmdBuffer.emplace_back(std::make_shared(std::move(sql), paraNum, std::move(parameters), @@ -161,56 +158,56 @@ void DbClientLockFree::newTransactionAsync(const std::function &)> &&callback) +void DbClientLockFree::makeTrans(const DbConnectionPtr &conn, + std::function &)> &&callback) { std::weak_ptr weakThis = shared_from_this(); - auto trans = std::shared_ptr(new TransactionImpl(_type, conn, std::function(), [weakThis, conn]() { - auto thisPtr = weakThis.lock(); - if (!thisPtr) - return; + auto trans = + std::shared_ptr(new TransactionImpl(_type, conn, std::function(), [weakThis, conn]() { + auto thisPtr = weakThis.lock(); + if (!thisPtr) + return; - if (conn->status() == ConnectStatus_Bad) - { - return; - } - if (!thisPtr->_transCallbacks.empty()) - { - auto callback = std::move(thisPtr->_transCallbacks.front()); - thisPtr->_transCallbacks.pop(); - thisPtr->makeTrans(conn, std::move(callback)); - return; - } - - for (auto &connPtr : thisPtr->_connections) - { - if (connPtr == conn) + if (conn->status() == ConnectStatus_Bad) { - conn->loop()->queueInLoop([weakThis, conn]() { - auto thisPtr = weakThis.lock(); - if (!thisPtr) - return; - std::weak_ptr weakConn = conn; - conn->setIdleCallback([weakThis, weakConn]() { + return; + } + if (!thisPtr->_transCallbacks.empty()) + { + auto callback = std::move(thisPtr->_transCallbacks.front()); + thisPtr->_transCallbacks.pop(); + thisPtr->makeTrans(conn, std::move(callback)); + return; + } + + for (auto &connPtr : thisPtr->_connections) + { + if (connPtr == conn) + { + conn->loop()->queueInLoop([weakThis, conn]() { auto thisPtr = weakThis.lock(); if (!thisPtr) return; - auto connPtr = weakConn.lock(); - if (!connPtr) - return; - thisPtr->handleNewTask(connPtr); + std::weak_ptr weakConn = conn; + conn->setIdleCallback([weakThis, weakConn]() { + auto thisPtr = weakThis.lock(); + if (!thisPtr) + return; + auto connPtr = weakConn.lock(); + if (!connPtr) + return; + thisPtr->handleNewTask(connPtr); + }); + thisPtr->_transSet.erase(conn); + thisPtr->handleNewTask(conn); }); - thisPtr->_transSet.erase(conn); - thisPtr->handleNewTask(conn); - }); - break; + break; + } } - } - })); + })); _transSet.insert(conn); trans->doBegin(); - conn->loop()->queueInLoop([callback = std::move(callback), trans] { - callback(trans); - }); + conn->loop()->queueInLoop([ callback = std::move(callback), trans ] { callback(trans); }); } void DbClientLockFree::handleNewTask(const DbConnectionPtr &conn) @@ -267,7 +264,7 @@ DbConnectionPtr DbClientLockFree::newConnection() std::weak_ptr weakPtr = shared_from_this(); connPtr->setCloseCallback([weakPtr](const DbConnectionPtr &closeConnPtr) { - //Erase the connection + // Erase the connection auto thisPtr = weakPtr.lock(); if (!thisPtr) return; @@ -290,7 +287,7 @@ DbConnectionPtr DbClientLockFree::newConnection() } thisPtr->_transSet.erase(closeConnPtr); - //Reconnect after 1 second + // Reconnect after 1 second thisPtr->_loop->runAfter(1, [weakPtr] { auto thisPtr = weakPtr.lock(); if (!thisPtr) @@ -316,6 +313,6 @@ DbConnectionPtr DbClientLockFree::newConnection() return; thisPtr->handleNewTask(connPtr); }); - //std::cout<<"newConn end"< #include -#include -#include -#include #include -#include +#include #include +#include +#include +#include #include namespace drogon { namespace orm { - class DbClientLockFree : public DbClient, public std::enable_shared_from_this { public: @@ -79,7 +78,7 @@ class DbClientLockFree : public DbClient, public std::enable_shared_from_this> _sqlCmdBuffer; std::queue &)>> _transCallbacks; @@ -89,5 +88,5 @@ class DbClientLockFree : public DbClient, public std::enable_shared_from_this -#include #include -#include -#include -#include #include #include +#include #include #include +#include +#include +#include +#include namespace drogon { namespace orm { - #if (CXX_STD > 14) typedef std::shared_mutex SharedMutex; #else @@ -50,7 +49,9 @@ class DbConnection : public trantor::NonCopyable { public: typedef std::function DbConnectionCallback; - DbConnection(trantor::EventLoop *loop) : _loop(loop) {} + DbConnection(trantor::EventLoop *loop) : _loop(loop) + { + } void setOkCallback(const DbConnectionCallback &cb) { _okCb = cb; @@ -74,10 +75,19 @@ class DbConnection : public trantor::NonCopyable { LOG_TRACE << "Destruct DbConn" << this; } - ConnectStatus status() const { return _status; } - trantor::EventLoop *loop() { return _loop; } + ConnectStatus status() const + { + return _status; + } + trantor::EventLoop *loop() + { + return _loop; + } virtual void disconnect() = 0; - bool isWorking() { return _isWorking; } + bool isWorking() + { + return _isWorking; + } protected: QueryCallback _cb; @@ -91,5 +101,5 @@ class DbConnection : public trantor::NonCopyable std::string _sql = ""; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/Exception.cc b/orm_lib/src/Exception.cc index e12f462f..ee13cae8 100644 --- a/orm_lib/src/Exception.cc +++ b/orm_lib/src/Exception.cc @@ -1,4 +1,4 @@ -/** +/** * * Copyright (c) 2005-2017, Jeroen T. Vermeulen. * @@ -6,7 +6,7 @@ * COPYING with this source code, please notify the distributor of this mistake, * or contact the author. */ -//taken from libpqxx and modified +// taken from libpqxx and modified /** * @@ -42,12 +42,8 @@ BrokenConnection::BrokenConnection(const std::string &whatarg) : Failure(whatarg { } -SqlError::SqlError( - const std::string &whatarg, - const std::string &Q, - const char sqlstate[]) : Failure(whatarg), - _query(Q), - _sqlState(sqlstate ? sqlstate : "") +SqlError::SqlError(const std::string &whatarg, const std::string &Q, const char sqlstate[]) + : Failure(whatarg), _query(Q), _sqlState(sqlstate ? sqlstate : "") { } @@ -65,52 +61,42 @@ const std::string &SqlError::sqlState() const noexcept return _sqlState; } -InDoubtError::InDoubtError(const std::string &whatarg) - : Failure(whatarg) +InDoubtError::InDoubtError(const std::string &whatarg) : Failure(whatarg) { } -TransactionRollback::TransactionRollback(const std::string &whatarg) - : Failure(whatarg) +TransactionRollback::TransactionRollback(const std::string &whatarg) : Failure(whatarg) { } -SerializationFailure::SerializationFailure(const std::string &whatarg) - : TransactionRollback(whatarg) +SerializationFailure::SerializationFailure(const std::string &whatarg) : TransactionRollback(whatarg) { } -StatementCompletionUnknown::StatementCompletionUnknown(const std::string &whatarg) - : TransactionRollback(whatarg) +StatementCompletionUnknown::StatementCompletionUnknown(const std::string &whatarg) : TransactionRollback(whatarg) { } -DeadlockDetected::DeadlockDetected(const std::string &whatarg) - : TransactionRollback(whatarg) +DeadlockDetected::DeadlockDetected(const std::string &whatarg) : TransactionRollback(whatarg) { } -InternalError::InternalError(const std::string &whatarg) - : logic_error("libpqxx internal error: " + whatarg) +InternalError::InternalError(const std::string &whatarg) : logic_error("libpqxx internal error: " + whatarg) { } -UsageError::UsageError(const std::string &whatarg) - : logic_error(whatarg) +UsageError::UsageError(const std::string &whatarg) : logic_error(whatarg) { } -ArgumentError::ArgumentError(const std::string &whatarg) - : invalid_argument(whatarg) +ArgumentError::ArgumentError(const std::string &whatarg) : invalid_argument(whatarg) { } -ConversionError::ConversionError(const std::string &whatarg) - : domain_error(whatarg) +ConversionError::ConversionError(const std::string &whatarg) : domain_error(whatarg) { } -RangeError::RangeError(const std::string &whatarg) - : out_of_range(whatarg) +RangeError::RangeError(const std::string &whatarg) : out_of_range(whatarg) { } diff --git a/orm_lib/src/Field.cc b/orm_lib/src/Field.cc index 957f62ad..a82a8942 100644 --- a/orm_lib/src/Field.cc +++ b/orm_lib/src/Field.cc @@ -18,9 +18,7 @@ using namespace drogon::orm; Field::Field(const Row &row, Row::size_type columnNum) noexcept - : _row(Result::size_type(row._index)), - _column(columnNum), - _result(row._result) + : _row(Result::size_type(row._index)), _column(columnNum), _result(row._result) { } @@ -113,7 +111,8 @@ const char *Field::c_str() const // { // auto _data = _result.getValue(_row, _column); // auto _dataLength = _result.getLength(_row, _column); -// return std::vector((int32_t *)_data,(int32_t *)(_data + _dataLength)); +// return std::vector((int32_t *)_data,(int32_t *)(_data + +// _dataLength)); // } // template <> @@ -121,5 +120,6 @@ const char *Field::c_str() const // { // auto _data = _result.getValue(_row, _column); // auto _dataLength = _result.getLength(_row, _column); -// return std::vector((int64_t *)_data,(int64_t *)(_data + _dataLength)); +// return std::vector((int64_t *)_data,(int64_t *)(_data + +// _dataLength)); // } diff --git a/orm_lib/src/Result.cc b/orm_lib/src/Result.cc index 90137f35..0c4b36ca 100644 --- a/orm_lib/src/Result.cc +++ b/orm_lib/src/Result.cc @@ -13,10 +13,10 @@ */ #include "ResultImpl.h" -#include -#include -#include #include +#include +#include +#include using namespace drogon::orm; diff --git a/orm_lib/src/ResultImpl.h b/orm_lib/src/ResultImpl.h index e17aa83d..69998d91 100644 --- a/orm_lib/src/ResultImpl.h +++ b/orm_lib/src/ResultImpl.h @@ -14,17 +14,18 @@ #pragma once -#include #include +#include namespace drogon { namespace orm { - class ResultImpl : public trantor::NonCopyable, public Result { public: - ResultImpl(const std::string &query) : _query(query) {} + ResultImpl(const std::string &query) : _query(query) + { + } virtual size_type size() const noexcept = 0; virtual row_size_type columns() const noexcept = 0; virtual const char *columnName(row_size_type Number) const = 0; @@ -33,14 +34,25 @@ class ResultImpl : public trantor::NonCopyable, public Result virtual const char *getValue(size_type row, row_size_type column) const = 0; virtual bool isNull(size_type row, row_size_type column) const = 0; virtual field_size_type getLength(size_type row, row_size_type column) const = 0; - virtual const std::string &sql() const { return _query; } - virtual unsigned long long insertId() const noexcept { return 0; } - virtual int oid(row_size_type column) const { return 0; } - virtual ~ResultImpl() {} + virtual const std::string &sql() const + { + return _query; + } + virtual unsigned long long insertId() const noexcept + { + return 0; + } + virtual int oid(row_size_type column) const + { + return 0; + } + virtual ~ResultImpl() + { + } private: std::string _query; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/Row.cc b/orm_lib/src/Row.cc index 35227346..ab4e632d 100644 --- a/orm_lib/src/Row.cc +++ b/orm_lib/src/Row.cc @@ -12,16 +12,13 @@ * */ -#include -#include -#include #include +#include +#include +#include using namespace drogon::orm; -Row::Row(const Result &r, size_type index) noexcept - : _result(r), - _index(long(index)), - _end(r.columns()) +Row::Row(const Result &r, size_type index) noexcept : _result(r), _index(long(index)), _end(r.columns()) { } Row::size_type Row::size() const diff --git a/orm_lib/src/SqlBinder.cc b/orm_lib/src/SqlBinder.cc index 10afb8a1..6882269e 100644 --- a/orm_lib/src/SqlBinder.cc +++ b/orm_lib/src/SqlBinder.cc @@ -13,11 +13,11 @@ */ #include -#include #include -#include +#include #include #include +#include using namespace drogon::orm; using namespace drogon::orm::internal; void SqlBinder::exec() @@ -25,51 +25,57 @@ void SqlBinder::exec() _execed = true; if (_mode == Mode::NonBlocking) { - //nonblocking mode,default mode - //Retain shared_ptrs of parameters until we get the result; - _client.execSql(std::move(_sql), _paraNum, std::move(_parameters), std::move(_length), std::move(_format), - [holder = std::move(_callbackHolder), objs = std::move(_objs)](const Result &r) mutable { - objs.clear(); - if (holder) - { - holder->execCallback(r); - } - }, - [exceptCb = std::move(_exceptCallback), - exceptPtrCb = std::move(_exceptPtrCallback), - isExceptPtr = _isExceptPtr](const std::exception_ptr &exception) { - //LOG_DEBUG<<"exp callback "<execCallback(r); + } + }, + [ exceptCb = std::move(_exceptCallback), exceptPtrCb = std::move(_exceptPtrCallback), isExceptPtr = _isExceptPtr ]( + const std::exception_ptr &exception) { + // LOG_DEBUG<<"exp callback "<> pro(new std::promise); auto f = pro->get_future(); - _client.execSql(std::move(_sql), _paraNum, std::move(_parameters), std::move(_length), std::move(_format), - [pro](const Result &r) { - pro->set_value(r); - }, + _client.execSql(std::move(_sql), + _paraNum, + std::move(_parameters), + std::move(_length), + std::move(_format), + [pro](const Result &r) { pro->set_value(r); }, [pro](const std::exception_ptr &exception) { try { @@ -94,7 +100,7 @@ void SqlBinder::exec() { if (!_destructed) { - //throw exception + // throw exception std::rethrow_exception(std::current_exception()); } else diff --git a/orm_lib/src/TransactionImpl.cc b/orm_lib/src/TransactionImpl.cc index 5e0ec57d..b3d7fa56 100644 --- a/orm_lib/src/TransactionImpl.cc +++ b/orm_lib/src/TransactionImpl.cc @@ -17,13 +17,11 @@ using namespace drogon::orm; -TransactionImpl::TransactionImpl(ClientType type, const DbConnectionPtr &connPtr, +TransactionImpl::TransactionImpl(ClientType type, + const DbConnectionPtr &connPtr, const std::function &commitCallback, const std::function &usedUpCallback) - : _connectionPtr(connPtr), - _usedUpCallback(usedUpCallback), - _loop(connPtr->loop()), - _commitCallback(commitCallback) + : _connectionPtr(connPtr), _usedUpCallback(usedUpCallback), _loop(connPtr->loop()), _commitCallback(commitCallback) { _type = type; } @@ -34,7 +32,7 @@ TransactionImpl::~TransactionImpl() if (!_isCommitedOrRolledback) { auto loop = _connectionPtr->loop(); - loop->queueInLoop([conn = _connectionPtr, ucb = std::move(_usedUpCallback), commitCb = std::move(_commitCallback)]() { + loop->queueInLoop([ conn = _connectionPtr, ucb = std::move(_usedUpCallback), commitCb = std::move(_commitCallback) ]() { conn->setIdleCallback([ucb = std::move(ucb)]() { if (ucb) ucb(); @@ -69,7 +67,7 @@ TransactionImpl::~TransactionImpl() } else { - if(_usedUpCallback) + if (_usedUpCallback) { _usedUpCallback(); } @@ -105,7 +103,7 @@ void TransactionImpl::execSqlInLoop(std::string &&sql, } else { - //push sql cmd to buffer; + // push sql cmd to buffer; SqlCmd cmd; cmd._sql = std::move(sql); cmd._paraNum = paraNum; @@ -120,7 +118,7 @@ void TransactionImpl::execSqlInLoop(std::string &&sql, } else { - //The transaction has been rolled back; + // The transaction has been rolled back; try { throw TransactionRollback("The transaction has been rolled back"); @@ -141,7 +139,7 @@ void TransactionImpl::rollback() return; if (thisPtr->_isWorking) { - //push sql cmd to buffer; + // push sql cmd to buffer; SqlCmd cmd; cmd._sql = "rollback"; cmd._paraNum = 0; @@ -150,12 +148,13 @@ void TransactionImpl::rollback() thisPtr->_isCommitedOrRolledback = true; }; cmd._exceptCb = [thisPtr](const std::exception_ptr &ePtr) { - //clearupCb(); + // clearupCb(); thisPtr->_isCommitedOrRolledback = true; LOG_ERROR << "Transaction rool back error"; }; cmd._isRollbackCmd = true; - //Rollback cmd should be executed firstly, so we push it in front of the list + // Rollback cmd should be executed firstly, so we push it in front of the + // list thisPtr->_sqlCmdBuffer.push_front(std::move(cmd)); return; } @@ -169,10 +168,10 @@ void TransactionImpl::rollback() [thisPtr](const Result &r) { LOG_TRACE << "Transaction roll back!"; thisPtr->_isCommitedOrRolledback = true; - //clearupCb(); + // clearupCb(); }, [thisPtr](const std::exception_ptr &ePtr) { - //clearupCb(); + // clearupCb(); LOG_ERROR << "Transaction rool back error"; thisPtr->_isCommitedOrRolledback = true; }); @@ -197,7 +196,7 @@ void TransactionImpl::execNewTask() std::move(cmd._parameters), std::move(cmd._length), std::move(cmd._format), - [callback = std::move(cmd._cb), cmd, thisPtr](const Result &r) { + [ callback = std::move(cmd._cb), cmd, thisPtr ](const Result &r) { if (cmd._isRollbackCmd) { thisPtr->_isCommitedOrRolledback = true; @@ -261,16 +260,13 @@ void TransactionImpl::doBegin() assert(!thisPtr->_isCommitedOrRolledback); thisPtr->_isWorking = true; thisPtr->_thisPtr = thisPtr; - thisPtr->_connectionPtr->execSql("begin", - 0, - std::vector(), - std::vector(), - std::vector(), - [](const Result &r) { - LOG_TRACE << "Transaction begin!"; - }, - [thisPtr](const std::exception_ptr &ePtr) { - thisPtr->_isCommitedOrRolledback = true; - }); + thisPtr->_connectionPtr->execSql( + "begin", + 0, + std::vector(), + std::vector(), + std::vector(), + [](const Result &r) { LOG_TRACE << "Transaction begin!"; }, + [thisPtr](const std::exception_ptr &ePtr) { thisPtr->_isCommitedOrRolledback = true; }); }); } diff --git a/orm_lib/src/TransactionImpl.h b/orm_lib/src/TransactionImpl.h index b464ccf0..600af483 100644 --- a/orm_lib/src/TransactionImpl.h +++ b/orm_lib/src/TransactionImpl.h @@ -26,7 +26,10 @@ namespace orm class TransactionImpl : public Transaction, public std::enable_shared_from_this { public: - TransactionImpl(ClientType type, const DbConnectionPtr &connPtr, const std::function &commitCallback, const std::function &usedUpCallback); + TransactionImpl(ClientType type, + const DbConnectionPtr &connPtr, + const std::function &commitCallback, + const std::function &usedUpCallback); ~TransactionImpl(); void rollback() override; virtual void setCommitCallback(const std::function &commitCallback) override @@ -56,14 +59,16 @@ class TransactionImpl : public Transaction, public std::enable_shared_from_this< } else { - _loop->queueInLoop([thisPtr = shared_from_this(), - sql = std::move(sql), - paraNum, - parameters = std::move(parameters), - length = std::move(length), - format = std::move(format), - rcb = std::move(rcb), - exceptCallback = std::move(exceptCallback)]() mutable { + _loop->queueInLoop([ + thisPtr = shared_from_this(), + sql = std::move(sql), + paraNum, + parameters = std::move(parameters), + length = std::move(length), + format = std::move(format), + rcb = std::move(rcb), + exceptCallback = std::move(exceptCallback) + ]() mutable { thisPtr->execSqlInLoop(std::move(sql), paraNum, std::move(parameters), @@ -116,5 +121,5 @@ class TransactionImpl : public Transaction, public std::enable_shared_from_this< std::function _commitCallback; std::shared_ptr _thisPtr; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/mysql_impl/MysqlConnection.cc b/orm_lib/src/mysql_impl/MysqlConnection.cc index ef2715b9..42c58fbe 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.cc +++ b/orm_lib/src/mysql_impl/MysqlConnection.cc @@ -14,17 +14,16 @@ #include "MysqlConnection.h" #include "MysqlResultImpl.h" -#include -#include #include +#include #include +#include using namespace drogon::orm; namespace drogon { namespace orm { - Result makeResult(const std::shared_ptr &r = std::shared_ptr(nullptr), const std::string &query = "", Result::size_type affectedRows = 0, @@ -33,19 +32,16 @@ Result makeResult(const std::shared_ptr &r = std::shared_ptr(new MysqlResultImpl(r, query, affectedRows, insertId))); } -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon MysqlConnection::MysqlConnection(trantor::EventLoop *loop, const std::string &connInfo) - : DbConnection(loop), - _mysqlPtr(std::shared_ptr(new MYSQL, [](MYSQL *p) { - mysql_close(p); - })) + : DbConnection(loop), _mysqlPtr(std::shared_ptr(new MYSQL, [](MYSQL *p) { mysql_close(p); })) { mysql_init(_mysqlPtr.get()); mysql_options(_mysqlPtr.get(), MYSQL_OPT_NONBLOCK, 0); - //Get the key and value + // Get the key and value std::regex r(" *= *"); auto tmpStr = std::regex_replace(connInfo, r, "="); std::string host, user, passwd, dbname, port; @@ -61,7 +57,7 @@ MysqlConnection::MysqlConnection(trantor::EventLoop *loop, const std::string &co value = value.substr(1, value.length() - 2); } std::transform(key.begin(), key.end(), key.begin(), tolower); - //LOG_TRACE << key << "=" << value; + // LOG_TRACE << key << "=" << value; if (key == "host") { host = value; @@ -72,7 +68,7 @@ MysqlConnection::MysqlConnection(trantor::EventLoop *loop, const std::string &co } else if (key == "dbname") { - //LOG_DEBUG << "database:[" << value << "]"; + // LOG_DEBUG << "database:[" << value << "]"; dbname = value; } else if (key == "port") @@ -96,16 +92,14 @@ MysqlConnection::MysqlConnection(trantor::EventLoop *loop, const std::string &co port.empty() ? 3306 : atol(port.c_str()), NULL, 0); - //LOG_DEBUG << ret; + // LOG_DEBUG << ret; auto fd = mysql_get_socket(_mysqlPtr.get()); _channelPtr = std::unique_ptr(new trantor::Channel(loop, fd)); _channelPtr->setCloseCallback([=]() { perror("sock close"); handleClosed(); }); - _channelPtr->setEventCallback([=]() { - handleEvent(); - }); + _channelPtr->setEventCallback([=]() { handleEvent(); }); setChannel(); }); } @@ -131,9 +125,7 @@ void MysqlConnection::setChannel() { auto timeout = mysql_get_timeout_value(_mysqlPtr.get()); auto thisPtr = shared_from_this(); - _loop->runAfter(timeout, [thisPtr]() { - thisPtr->handleTimeout(); - }); + _loop->runAfter(timeout, [thisPtr]() { thisPtr->handleTimeout(); }); } } @@ -180,7 +172,7 @@ void MysqlConnection::handleTimeout() LOG_ERROR << "Failed to mysql_real_connect()"; return; } - //I don't think the programe can run to here. + // I don't think the programe can run to here. _status = ConnectStatus_Ok; if (_okCb) { @@ -216,7 +208,7 @@ void MysqlConnection::handleEvent() if (!ret) { handleClosed(); - //perror(""); + // perror(""); LOG_ERROR << "Failed to mysql_real_connect()"; return; } @@ -233,67 +225,67 @@ void MysqlConnection::handleEvent() { switch (_execStatus) { - case ExecStatus_RealQuery: - { - int err = 0; - _waitStatus = mysql_real_query_cont(&err, _mysqlPtr.get(), status); - LOG_TRACE << "real_query:" << _waitStatus; - if (_waitStatus == 0) + case ExecStatus_RealQuery: { - if (err) - { - _execStatus = ExecStatus_None; - LOG_ERROR << "error:" << err << " status:" << status; - outputError(); - return; - } - _execStatus = ExecStatus_StoreResult; - MYSQL_RES *ret; - _waitStatus = mysql_store_result_start(&ret, _mysqlPtr.get()); - LOG_TRACE << "store_result_start:" << _waitStatus; + int err = 0; + _waitStatus = mysql_real_query_cont(&err, _mysqlPtr.get(), status); + LOG_TRACE << "real_query:" << _waitStatus; if (_waitStatus == 0) { - _execStatus = ExecStatus_None; if (err) { + _execStatus = ExecStatus_None; + LOG_ERROR << "error:" << err << " status:" << status; + outputError(); + return; + } + _execStatus = ExecStatus_StoreResult; + MYSQL_RES *ret; + _waitStatus = mysql_store_result_start(&ret, _mysqlPtr.get()); + LOG_TRACE << "store_result_start:" << _waitStatus; + if (_waitStatus == 0) + { + _execStatus = ExecStatus_None; + if (err) + { + LOG_ERROR << "error"; + outputError(); + return; + } + getResult(ret); + } + } + setChannel(); + break; + } + case ExecStatus_StoreResult: + { + MYSQL_RES *ret; + _waitStatus = mysql_store_result_cont(&ret, _mysqlPtr.get(), status); + LOG_TRACE << "store_result:" << _waitStatus; + if (_waitStatus == 0) + { + if (!ret) + { + _execStatus = ExecStatus_None; LOG_ERROR << "error"; outputError(); return; } getResult(ret); } + setChannel(); + break; } - setChannel(); - break; - } - case ExecStatus_StoreResult: - { - MYSQL_RES *ret; - _waitStatus = mysql_store_result_cont(&ret, _mysqlPtr.get(), status); - LOG_TRACE << "store_result:" << _waitStatus; - if (_waitStatus == 0) + case ExecStatus_None: { - if (!ret) - { - _execStatus = ExecStatus_None; - LOG_ERROR << "error"; - outputError(); - return; - } - getResult(ret); + // Connection closed! + if (_waitStatus == 0) + handleClosed(); + break; } - setChannel(); - break; - } - case ExecStatus_None: - { - //Connection closed! - if (_waitStatus == 0) - handleClosed(); - break; - } - default: - return; + default: + return; } } } @@ -337,33 +329,33 @@ void MysqlConnection::execSqlInLoop(std::string &&sql, pos = seekPos + 1; switch (format[i]) { - case MYSQL_TYPE_TINY: - _sql.append(std::to_string(*((char *)parameters[i]))); - break; - case MYSQL_TYPE_SHORT: - _sql.append(std::to_string(*((short *)parameters[i]))); - break; - case MYSQL_TYPE_LONG: - _sql.append(std::to_string(*((int32_t *)parameters[i]))); - break; - case MYSQL_TYPE_LONGLONG: - _sql.append(std::to_string(*((int64_t *)parameters[i]))); - break; - case MYSQL_TYPE_NULL: - _sql.append("NULL"); - break; - case MYSQL_TYPE_STRING: - { - _sql.append("'"); - std::string to(length[i] * 2, '\0'); - auto len = mysql_real_escape_string(_mysqlPtr.get(), (char *)to.c_str(), parameters[i], length[i]); - to.resize(len); - _sql.append(to); - _sql.append("'"); - } - break; - default: + case MYSQL_TYPE_TINY: + _sql.append(std::to_string(*((char *)parameters[i]))); + break; + case MYSQL_TYPE_SHORT: + _sql.append(std::to_string(*((short *)parameters[i]))); + break; + case MYSQL_TYPE_LONG: + _sql.append(std::to_string(*((int32_t *)parameters[i]))); + break; + case MYSQL_TYPE_LONGLONG: + _sql.append(std::to_string(*((int64_t *)parameters[i]))); + break; + case MYSQL_TYPE_NULL: + _sql.append("NULL"); + break; + case MYSQL_TYPE_STRING: + { + _sql.append("'"); + std::string to(length[i] * 2, '\0'); + auto len = mysql_real_escape_string(_mysqlPtr.get(), (char *)to.c_str(), parameters[i], length[i]); + to.resize(len); + _sql.append(to); + _sql.append("'"); + } break; + default: + break; } } } @@ -378,7 +370,8 @@ void MysqlConnection::execSqlInLoop(std::string &&sql, } LOG_TRACE << _sql; int err; - //int mysql_real_query_start(int *ret, MYSQL *mysql, const char *q, unsigned long length) + // int mysql_real_query_start(int *ret, MYSQL *mysql, const char *q, unsigned + // long length) _waitStatus = mysql_real_query_start(&err, _mysqlPtr.get(), _sql.c_str(), _sql.length()); LOG_TRACE << "real_query:" << _waitStatus; _execStatus = ExecStatus_RealQuery; @@ -414,17 +407,14 @@ void MysqlConnection::execSqlInLoop(std::string &&sql, void MysqlConnection::outputError() { _channelPtr->disableAll(); - LOG_ERROR << "Error(" - << mysql_errno(_mysqlPtr.get()) << ") [" << mysql_sqlstate(_mysqlPtr.get()) << "] \"" + LOG_ERROR << "Error(" << mysql_errno(_mysqlPtr.get()) << ") [" << mysql_sqlstate(_mysqlPtr.get()) << "] \"" << mysql_error(_mysqlPtr.get()) << "\""; if (_isWorking) { - try { - //TODO: exception type - throw SqlError(mysql_error(_mysqlPtr.get()), - _sql); + // TODO: exception type + throw SqlError(mysql_error(_mysqlPtr.get()), _sql); } catch (...) { @@ -440,9 +430,7 @@ void MysqlConnection::outputError() void MysqlConnection::getResult(MYSQL_RES *res) { - auto resultPtr = std::shared_ptr(res, [](MYSQL_RES *r) { - mysql_free_result(r); - }); + auto resultPtr = std::shared_ptr(res, [](MYSQL_RES *r) { mysql_free_result(r); }); auto Result = makeResult(resultPtr, _sql, mysql_affected_rows(_mysqlPtr.get()), mysql_insert_id(_mysqlPtr.get())); if (_isWorking) { diff --git a/orm_lib/src/mysql_impl/MysqlConnection.h b/orm_lib/src/mysql_impl/MysqlConnection.h index 6846d40a..5ef104a3 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.h +++ b/orm_lib/src/mysql_impl/MysqlConnection.h @@ -15,28 +15,29 @@ #pragma once #include "../DbConnection.h" -#include -#include #include -#include -#include -#include -#include #include #include +#include +#include +#include +#include +#include +#include namespace drogon { namespace orm { - class MysqlConnection; typedef std::shared_ptr MysqlConnectionPtr; class MysqlConnection : public DbConnection, public std::enable_shared_from_this { public: MysqlConnection(trantor::EventLoop *loop, const std::string &connInfo); - ~MysqlConnection() {} + ~MysqlConnection() + { + } virtual void execSql(std::string &&sql, size_t paraNum, std::vector &¶meters, @@ -58,14 +59,16 @@ class MysqlConnection : public DbConnection, public std::enable_shared_from_this else { auto thisPtr = shared_from_this(); - _loop->queueInLoop([thisPtr, - sql = std::move(sql), - paraNum, - parameters = std::move(parameters), - length = std::move(length), - format = std::move(format), - rcb = std::move(rcb), - exceptCallback = std::move(exceptCallback)]() mutable { + _loop->queueInLoop([ + thisPtr, + sql = std::move(sql), + paraNum, + parameters = std::move(parameters), + length = std::move(length), + format = std::move(format), + rcb = std::move(rcb), + exceptCallback = std::move(exceptCallback) + ]() mutable { thisPtr->execSqlInLoop(std::move(sql), paraNum, std::move(parameters), @@ -86,7 +89,7 @@ class MysqlConnection : public DbConnection, public std::enable_shared_from_this std::vector &&format, ResultCallback &&rcb, std::function &&exceptCallback); - + std::unique_ptr _channelPtr; std::shared_ptr _mysqlPtr; @@ -111,5 +114,5 @@ class MysqlConnection : public DbConnection, public std::enable_shared_from_this std::vector _isNulls; }; -} // namespace orm -} // namespace drogon \ No newline at end of file +} // namespace orm +} // namespace drogon \ No newline at end of file diff --git a/orm_lib/src/mysql_impl/MysqlResultImpl.cc b/orm_lib/src/mysql_impl/MysqlResultImpl.cc index b3b42cb6..49e47329 100644 --- a/orm_lib/src/mysql_impl/MysqlResultImpl.cc +++ b/orm_lib/src/mysql_impl/MysqlResultImpl.cc @@ -9,12 +9,12 @@ * that can be found in the License file. * * Drogon - * + * */ #include "MysqlResultImpl.h" -#include #include +#include using namespace drogon::orm; diff --git a/orm_lib/src/mysql_impl/MysqlResultImpl.h b/orm_lib/src/mysql_impl/MysqlResultImpl.h index 0e12bcbc..b1f3a915 100644 --- a/orm_lib/src/mysql_impl/MysqlResultImpl.h +++ b/orm_lib/src/mysql_impl/MysqlResultImpl.h @@ -14,19 +14,18 @@ #pragma once #include "../ResultImpl.h" -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include #include namespace drogon { namespace orm { - class MysqlResultImpl : public ResultImpl { public: @@ -87,5 +86,5 @@ class MysqlResultImpl : public ResultImpl std::shared_ptr>>> _rowsPtr; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/mysql_impl/test/test1.cc b/orm_lib/src/mysql_impl/test/test1.cc index a5f27e28..9809f4fa 100644 --- a/orm_lib/src/mysql_impl/test/test1.cc +++ b/orm_lib/src/mysql_impl/test/test1.cc @@ -1,9 +1,9 @@ #include -#include #include -#include -#include #include +#include +#include +#include using namespace drogon::orm; using namespace drogon; @@ -15,7 +15,9 @@ int main() sleep(1); // for (int i = 0; i < 10; i++) // { - // std::string str = formattedString("insert into users (user_id,user_name,org_name) values('%d','antao','default')", i); + // std::string str = formattedString("insert into users + // (user_id,user_name,org_name) values('%d','antao','default')", + // i); // *clientPtr << str >> [](const Result &r) { // std::cout << "insert rows:" << r.affectedRows() << std::endl; // } >> [](const DrogonDbException &e) { @@ -36,18 +38,14 @@ int main() // { // for (auto f : row) // { - // std::cout << f.name() << ":" << (f.isNull() ? "NULL" : f.as()) << std::endl; + // std::cout << f.name() << ":" << (f.isNull() ? "NULL" : + // f.as()) << std::endl; // } // } - } >> [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; + } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; LOG_TRACE << "end"; LOG_TRACE << "begin"; - *clientPtr << "select * from users where id=? and user_id=? order by id" - << 139 - << "233" - << Mode::Blocking >> + *clientPtr << "select * from users where id=? and user_id=? order by id" << 139 << "233" << Mode::Blocking >> [](const Result &r) { std::cout << "rows:" << r.size() << std::endl; std::cout << "column num:" << r.columns() << std::endl; @@ -57,14 +55,10 @@ int main() std::cout << " time=" << row["time"].as() << std::endl; } } >> - [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; - *clientPtr << "update users set time=? where id>?" << trantor::Date::date() << 1000 << Mode::Blocking >> [](const Result &r) { - std::cout << "update " << r.affectedRows() << " rows" << std::endl; - } >> [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; + [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; + *clientPtr << "update users set time=? where id>?" << trantor::Date::date() << 1000 << Mode::Blocking >> + [](const Result &r) { std::cout << "update " << r.affectedRows() << " rows" << std::endl; } >> + [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; // std::ifstream infile("Makefile", std::ifstream::binary); // std::streambuf *pbuf = infile.rdbuf(); @@ -85,28 +79,18 @@ int main() << "hehaha" << 1000 >> [](const Result &r) { std::cout << "hahaha update " << r.affectedRows() << " rows" << std::endl; - //trans->rollback(); + // trans->rollback(); } >> - [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; + [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; } LOG_DEBUG << "out of transaction block"; *clientPtr << "select * from users where id=1000" >> [](const Result &r) { std::cout << "file:" << r[0]["file"].as() << std::endl; - } >> [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; + } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; - *clientPtr << "select * from users limit ? offset ?" - << 2 - << 2 >> - [](const Result &r) { - std::cout << "select " << r.size() << " records" << std::endl; - } >> - [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; + *clientPtr << "select * from users limit ? offset ?" << 2 << 2 >> [](const Result &r) { + std::cout << "select " << r.size() << " records" << std::endl; + } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; LOG_TRACE << "end"; getchar(); } \ No newline at end of file diff --git a/orm_lib/src/postgresql_impl/PgConnection.cc b/orm_lib/src/postgresql_impl/PgConnection.cc index 9b683214..1926589c 100644 --- a/orm_lib/src/postgresql_impl/PgConnection.cc +++ b/orm_lib/src/postgresql_impl/PgConnection.cc @@ -14,11 +14,11 @@ #include "PgConnection.h" #include "PostgreSQLResultImpl.h" -#include #include #include #include #include +#include using namespace drogon::orm; @@ -26,24 +26,21 @@ namespace drogon { namespace orm { - Result makeResult(const std::shared_ptr &r = std::shared_ptr(nullptr), const std::string &query = "") { return Result(std::shared_ptr(new PostgreSQLResultImpl(r, query))); } -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon PgConnection::PgConnection(trantor::EventLoop *loop, const std::string &connInfo) : DbConnection(loop), - _connPtr(std::shared_ptr(PQconnectStart(connInfo.c_str()), [](PGconn *conn) { - PQfinish(conn); - })), + _connPtr(std::shared_ptr(PQconnectStart(connInfo.c_str()), [](PGconn *conn) { PQfinish(conn); })), _channel(loop, PQsocket(_connPtr.get())) { PQsetnonblocking(_connPtr.get(), 1); - //assert(PQisnonblocking(_connPtr.get())); + // assert(PQisnonblocking(_connPtr.get())); _channel.setReadCallback([=]() { if (_status != ConnectStatus_Ok) { @@ -111,41 +108,41 @@ void PgConnection::pgPoll() switch (connStatus) { - case PGRES_POLLING_FAILED: - LOG_ERROR << "!!!Pg connection failed: " << PQerrorMessage(_connPtr.get()); - if (_status == ConnectStatus_None) - { - handleClosed(); - } - break; - case PGRES_POLLING_WRITING: - if (!_channel.isWriting()) - _channel.enableWriting(); - break; - case PGRES_POLLING_READING: - if (!_channel.isReading()) - _channel.enableReading(); - if (_channel.isWriting()) - _channel.disableWriting(); - break; + case PGRES_POLLING_FAILED: + LOG_ERROR << "!!!Pg connection failed: " << PQerrorMessage(_connPtr.get()); + if (_status == ConnectStatus_None) + { + handleClosed(); + } + break; + case PGRES_POLLING_WRITING: + if (!_channel.isWriting()) + _channel.enableWriting(); + break; + case PGRES_POLLING_READING: + if (!_channel.isReading()) + _channel.enableReading(); + if (_channel.isWriting()) + _channel.disableWriting(); + break; - case PGRES_POLLING_OK: - if (_status != ConnectStatus_Ok) - { - _status = ConnectStatus_Ok; - assert(_okCb); - _okCb(shared_from_this()); - } - if (!_channel.isReading()) - _channel.enableReading(); - if (_channel.isWriting()) - _channel.disableWriting(); - break; - case PGRES_POLLING_ACTIVE: - //unused! - break; - default: - break; + case PGRES_POLLING_OK: + if (_status != ConnectStatus_Ok) + { + _status = ConnectStatus_Ok; + assert(_okCb); + _okCb(shared_from_this()); + } + if (!_channel.isReading()) + _channel.enableReading(); + if (_channel.isWriting()) + _channel.disableWriting(); + break; + case PGRES_POLLING_ACTIVE: + // unused! + break; + default: + break; } } @@ -201,13 +198,8 @@ void PgConnection::execSqlInLoop(std::string &&sql, if (iter != _preparedStatementMap.end()) { _isRreparingStatement = false; - if (PQsendQueryPrepared(_connPtr.get(), - iter->second.c_str(), - paraNum, - parameters.data(), - length.data(), - format.data(), - 0) == 0) + if (PQsendQueryPrepared( + _connPtr.get(), iter->second.c_str(), paraNum, parameters.data(), length.data(), format.data(), 0) == 0) { LOG_ERROR << "send query error: " << PQerrorMessage(_connPtr.get()); if (_isWorking) @@ -292,14 +284,12 @@ void PgConnection::handleRead() } if (PQisBusy(_connPtr.get())) { - //need read more data from socket; + // need read more data from socket; return; } if (_channel.isWriting()) _channel.disableWriting(); - while ((res = std::shared_ptr(PQgetResult(_connPtr.get()), [](PGresult *p) { - PQclear(p); - }))) + while ((res = std::shared_ptr(PQgetResult(_connPtr.get()), [](PGresult *p) { PQclear(p); }))) { auto type = PQresultStatus(res.get()); if (type == PGRES_BAD_RESPONSE || type == PGRES_FATAL_ERROR) @@ -309,7 +299,7 @@ void PgConnection::handleRead() { try { - //TODO: exception type + // TODO: exception type throw SqlError(PQerrorMessage(_connPtr.get()), _sql); } catch (...) @@ -353,13 +343,8 @@ void PgConnection::doAfterPreparing() { _isRreparingStatement = false; _preparedStatementMap[_sql] = _statementName; - if (PQsendQueryPrepared(_connPtr.get(), - _statementName.c_str(), - _paraNum, - _parameters.data(), - _length.data(), - _format.data(), - 0) == 0) + if (PQsendQueryPrepared( + _connPtr.get(), _statementName.c_str(), _paraNum, _parameters.data(), _length.data(), _format.data(), 0) == 0) { LOG_ERROR << "send query error: " << PQerrorMessage(_connPtr.get()); if (_isWorking) diff --git a/orm_lib/src/postgresql_impl/PgConnection.h b/orm_lib/src/postgresql_impl/PgConnection.h index 6a8b976b..046b3c98 100644 --- a/orm_lib/src/postgresql_impl/PgConnection.h +++ b/orm_lib/src/postgresql_impl/PgConnection.h @@ -15,22 +15,21 @@ #pragma once #include "../DbConnection.h" -#include -#include #include -#include +#include +#include #include #include #include -#include -#include +#include +#include +#include #include namespace drogon { namespace orm { - class PgConnection; typedef std::shared_ptr PgConnectionPtr; class PgConnection : public DbConnection, public std::enable_shared_from_this @@ -59,14 +58,16 @@ class PgConnection : public DbConnection, public std::enable_shared_from_thisqueueInLoop([thisPtr, - sql = std::move(sql), - paraNum, - parameters = std::move(parameters), - length = std::move(length), - format = std::move(format), - rcb = std::move(rcb), - exceptCallback = std::move(exceptCallback)]() mutable { + _loop->queueInLoop([ + thisPtr, + sql = std::move(sql), + paraNum, + parameters = std::move(parameters), + length = std::move(length), + format = std::move(format), + rcb = std::move(rcb), + exceptCallback = std::move(exceptCallback) + ]() mutable { thisPtr->execSqlInLoop(std::move(sql), paraNum, std::move(parameters), @@ -95,7 +96,7 @@ class PgConnection : public DbConnection, public std::enable_shared_from_this &&format, ResultCallback &&rcb, std::function &&exceptCallback); - //std::function _preparingCallback; + // std::function _preparingCallback; void doAfterPreparing(); std::string _statementName; int _paraNum; @@ -104,5 +105,5 @@ class PgConnection : public DbConnection, public std::enable_shared_from_this _format; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.cc b/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.cc index 8fa75448..9be33070 100644 --- a/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.cc +++ b/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.cc @@ -9,7 +9,7 @@ * that can be found in the License file. * * Drogon - * + * */ #include "PostgreSQLResultImpl.h" @@ -35,7 +35,7 @@ const char *PostgreSQLResultImpl::columnName(row_size_type number) const assert(N); return N; } - throw "nullptr result"; //The program will never execute here + throw "nullptr result"; // The program will never execute here } Result::size_type PostgreSQLResultImpl::affectedRows() const noexcept { @@ -51,10 +51,10 @@ Result::row_size_type PostgreSQLResultImpl::columnNumber(const char colName[]) c { auto N = PQfnumber(ptr, colName); if (N == -1) - throw std::string("there is no column named ") + colName; // TODO throw detail exception here; + throw std::string("there is no column named ") + colName; // TODO throw detail exception here; return N; } - throw "nullptr result"; //The program will never execute here + throw "nullptr result"; // The program will never execute here } const char *PostgreSQLResultImpl::getValue(size_type row, row_size_type column) const { diff --git a/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.h b/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.h index fd5a98ff..dca3a887 100644 --- a/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.h +++ b/orm_lib/src/postgresql_impl/PostgreSQLResultImpl.h @@ -24,13 +24,10 @@ namespace drogon { namespace orm { - class PostgreSQLResultImpl : public ResultImpl { public: - PostgreSQLResultImpl(const std::shared_ptr &r, const std::string &query) noexcept - :ResultImpl(query), - _result(r) + PostgreSQLResultImpl(const std::shared_ptr &r, const std::string &query) noexcept : ResultImpl(query), _result(r) { } virtual size_type size() const noexcept override; @@ -47,5 +44,5 @@ class PostgreSQLResultImpl : public ResultImpl std::shared_ptr _result; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/postgresql_impl/test/test1.cc b/orm_lib/src/postgresql_impl/test/test1.cc index 8281e680..816fe51c 100644 --- a/orm_lib/src/postgresql_impl/test/test1.cc +++ b/orm_lib/src/postgresql_impl/test/test1.cc @@ -1,6 +1,6 @@ #include -#include #include +#include #include using namespace drogon::orm; @@ -10,16 +10,11 @@ int main() auto clientPtr = DbClient::newPgClient("host=127.0.0.1 port=5432 dbname=test user=antao", 3); LOG_DEBUG << "start!"; sleep(1); - *clientPtr << "update group_users set join_date=$1,relationship=$2 where g_uuid=420040 and u_uuid=2" - << nullptr - << nullptr - << Mode::Blocking >> - [](const Result &r) { - std::cout << "update " << r.affectedRows() << " lines" << std::endl; - } >> - [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - }; + *clientPtr << "update group_users set join_date=$1,relationship=$2 where " + "g_uuid=420040 and u_uuid=2" + << nullptr << nullptr << Mode::Blocking >> + [](const Result &r) { std::cout << "update " << r.affectedRows() << " lines" << std::endl; } >> + [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; }; try { auto r = clientPtr->execSqlSync("select * from users where user_uuid=$1;", 1); @@ -36,7 +31,8 @@ int main() LOG_DEBUG << "catch:" << e.base().what(); } - // client << "select count(*) from users" >> [](const drogon::orm::Result &r) { + // client << "select count(*) from users" >> [](const drogon::orm::Result &r) + // { // for (auto row : r) // { // for (auto f : row) @@ -48,7 +44,8 @@ int main() // LOG_DEBUG << "except callback:" << e.base().what(); // }; - // client << "select * from users limit 5" >> [](const drogon::orm::Result &r) { + // client << "select * from users limit 5" >> [](const drogon::orm::Result &r) + // { // for (auto row : r) // { // for (auto f : row) @@ -74,10 +71,12 @@ int main() // client.execSqlAsync("", // [](const drogon::orm::Result &r) {}, // [](const drogon::orm::DrogonDbException &e) { - // LOG_DEBUG << "async blocking except callback:" << e.base().what(); + // LOG_DEBUG << "async blocking except callback:" << + // e.base().what(); // }, // true); - auto f = clientPtr->execSqlAsyncFuture("select * from users where user_uuid > $1 limit $2 offset $3", 100, (size_t)2, (size_t)2); + auto f = + clientPtr->execSqlAsyncFuture("select * from users where user_uuid > $1 limit $2 offset $3", 100, (size_t)2, (size_t)2); try { auto r = f.get(); @@ -99,14 +98,11 @@ int main() // { // std::cout<< e.base().what()<execSqlAsync("select * from users where user_uuid=$1;", - [](const drogon::orm::Result &r) { - LOG_DEBUG << "row count:" << r.size(); - }, - [](const drogon::orm::DrogonDbException &e) { - LOG_DEBUG << "async nonblocking except callback:" << e.base().what(); - }, - 1); + clientPtr->execSqlAsync( + "select * from users where user_uuid=$1;", + [](const drogon::orm::Result &r) { LOG_DEBUG << "row count:" << r.size(); }, + [](const drogon::orm::DrogonDbException &e) { LOG_DEBUG << "async nonblocking except callback:" << e.base().what(); }, + 1); clientPtr->execSqlAsync("select * from users where org_name=$1", [](const Result &r) { std::cout << r.size() << " rows selected!" << std::endl; @@ -116,9 +112,7 @@ int main() std::cout << i++ << ": user name is " << row["user_name"].as() << std::endl; } }, - [](const DrogonDbException &e) { - std::cerr << "error:" << e.base().what() << std::endl; - }, + [](const DrogonDbException &e) { std::cerr << "error:" << e.base().what() << std::endl; }, "default"); *clientPtr << "select t2,t9 from ttt where t7=2" >> [](const Result &r) { std::cout << r.size() << " rows selected!" << std::endl; @@ -127,8 +121,6 @@ int main() std::cout << row["t9"].as() << std::endl; std::cout << row["t2"].as() << std::endl; } - } >> [](const DrogonDbException &e) { - std::cerr << "error:" << e.base().what() << std::endl; - }; + } >> [](const DrogonDbException &e) { std::cerr << "error:" << e.base().what() << std::endl; }; getchar(); } diff --git a/orm_lib/src/postgresql_impl/test/test2.cc b/orm_lib/src/postgresql_impl/test/test2.cc index bc2d90e0..c4e2e499 100644 --- a/orm_lib/src/postgresql_impl/test/test2.cc +++ b/orm_lib/src/postgresql_impl/test/test2.cc @@ -1,10 +1,10 @@ #include #include -#include #include -#include #include +#include +#include using namespace drogon::orm; class User @@ -15,9 +15,7 @@ class User const static std::string tableName; typedef int PrimaryKeyType; - explicit User(const Row &r) - : _userId(r["user_id"].as()), - _userName(r["user_name"].as()) + explicit User(const Row &r) : _userId(r["user_id"].as()), _userName(r["user_name"].as()) { } std::string _userId; @@ -37,50 +35,38 @@ int main() auto trans = client->newTransaction([](bool committed) { std::cout << "The transaction submission " << (committed ? "succeeded" : "failed") << std::endl; }); - *trans << "delete from users where user_uuid=201" >> - [trans](const Result &r) { - std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl; - trans->rollback(); - } >> - [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; + *trans << "delete from users where user_uuid=201" >> [trans](const Result &r) { + std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl; + trans->rollback(); + } >> [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; - *trans << "delete from users where user_uuid=201" >> - [](const Result &r) { - std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl; - } >> - [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; + *trans << "delete from users where user_uuid=201" >> [](const Result &r) { + std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl; + } >> [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; } Mapper mapper(client); auto U = mapper.findByPrimaryKey(2); std::cout << "id=" << U._userId << std::endl; std::cout << "name=" << U._userName << std::endl; - *client << "select * from array_test" >> [=](bool isNull, const std::vector> &a, const std::string &b, int c) { - if (!isNull) - { - std::cout << "a.len=" << a.size() << std::endl; - for (size_t i = 0; i < a.size(); i++) + *client << "select * from array_test" >> + [=](bool isNull, const std::vector> &a, const std::string &b, int c) { + if (!isNull) { - std::cout << "a[" << i << "]=" << *a[i] << " "; + std::cout << "a.len=" << a.size() << std::endl; + for (size_t i = 0; i < a.size(); i++) + { + std::cout << "a[" << i << "]=" << *a[i] << " "; + } + std::cout << std::endl; + std::cout << "b=" << b << " b.len=" << b.length() << std::endl; + std::cout << "c=" << c << std::endl; } - std::cout << std::endl; - std::cout << "b=" << b << " b.len=" << b.length() << std::endl; - std::cout << "c=" << c << std::endl; - } - } >> [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; + } >> + [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; for (int i = 0; i < 100; i++) mapper.findByPrimaryKey(2, - [](User u) { - std::cout << "get a user by pk" << std::endl; - }, - [](const DrogonDbException &e) { - throw std::exception(); - }); + [](User u) { std::cout << "get a user by pk" << std::endl; }, + [](const DrogonDbException &e) { throw std::exception(); }); getchar(); } diff --git a/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc b/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc index bab94d70..de63b0d7 100644 --- a/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc +++ b/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc @@ -34,9 +34,10 @@ void Sqlite3Connection::onError(const std::string &sql, const std::function &sharedMutex) - : DbConnection(loop), - _sharedMutexPtr(sharedMutex) +Sqlite3Connection::Sqlite3Connection(trantor::EventLoop *loop, + const std::string &connInfo, + const std::shared_ptr &sharedMutex) + : DbConnection(loop), _sharedMutexPtr(sharedMutex) { _loopThread.run(); _loop = _loopThread.getLoop(); @@ -47,7 +48,7 @@ Sqlite3Connection::Sqlite3Connection(trantor::EventLoop *loop, const std::string LOG_FATAL << sqlite3_errstr(ret); } }); - //Get the key and value + // Get the key and value std::regex r(" *= *"); auto tmpStr = std::regex_replace(connInfo, r, "="); std::string host, user, passwd, dbname, port; @@ -69,7 +70,7 @@ Sqlite3Connection::Sqlite3Connection(trantor::EventLoop *loop, const std::string filename = value; } } - _loop->runInLoop([this, filename = std::move(filename)]() { + _loop->runInLoop([ this, filename = std::move(filename) ]() { sqlite3 *tmp = nullptr; auto ret = sqlite3_open(filename.data(), &tmp); _conn = std::shared_ptr(tmp, [=](sqlite3 *ptr) { sqlite3_close(ptr); }); @@ -96,16 +97,16 @@ void Sqlite3Connection::execSql(std::string &&sql, std::function &&exceptCallback) { auto thisPtr = shared_from_this(); - _loopThread.getLoop()->runInLoop([thisPtr, - sql = std::move(sql), - paraNum, - parameters = std::move(parameters), - length = std::move(length), - format = std::move(format), - rcb = std::move(rcb), - exceptCallback = std::move(exceptCallback)]() mutable { - thisPtr->execSqlInQueue(sql, paraNum, parameters, length, format, rcb, exceptCallback); - }); + _loopThread.getLoop()->runInLoop([ + thisPtr, + sql = std::move(sql), + paraNum, + parameters = std::move(parameters), + length = std::move(length), + format = std::move(format), + rcb = std::move(rcb), + exceptCallback = std::move(exceptCallback) + ]() mutable { thisPtr->execSqlInQueue(sql, paraNum, parameters, length, format, rcb, exceptCallback); }); } void Sqlite3Connection::execSqlInQueue(const std::string &sql, @@ -121,7 +122,6 @@ void Sqlite3Connection::execSqlInQueue(const std::string &sql, bool newStmt = false; if (paraNum > 0) { - auto iter = _stmtMap.find(sql); if (iter != _stmtMap.end()) { @@ -134,11 +134,7 @@ void Sqlite3Connection::execSqlInQueue(const std::string &sql, newStmt = true; const char *remaining; auto ret = sqlite3_prepare_v2(_conn.get(), sql.data(), -1, &stmt, &remaining); - stmtPtr = stmt ? std::shared_ptr(stmt, - [](sqlite3_stmt *p) { - sqlite3_finalize(p); - }) - : nullptr; + stmtPtr = stmt ? std::shared_ptr(stmt, [](sqlite3_stmt *p) { sqlite3_finalize(p); }) : nullptr; if (ret != SQLITE_OK || !stmtPtr) { onError(sql, exceptCallback); @@ -165,30 +161,30 @@ void Sqlite3Connection::execSqlInQueue(const std::string &sql, int bindRet; switch (format[i]) { - case Sqlite3TypeChar: - bindRet = sqlite3_bind_int(stmt, i + 1, *(char *)parameters[i]); - break; - case Sqlite3TypeShort: - bindRet = sqlite3_bind_int(stmt, i + 1, *(short *)parameters[i]); - break; - case Sqlite3TypeInt: - bindRet = sqlite3_bind_int(stmt, i + 1, *(int32_t *)parameters[i]); - break; - case Sqlite3TypeInt64: - bindRet = sqlite3_bind_int64(stmt, i + 1, *(int64_t *)parameters[i]); - break; - case Sqlite3TypeDouble: - bindRet = sqlite3_bind_double(stmt, i + 1, *(double *)parameters[i]); - break; - case Sqlite3TypeText: - bindRet = sqlite3_bind_text(stmt, i + 1, parameters[i], -1, SQLITE_STATIC); - break; - case Sqlite3TypeBlob: - bindRet = sqlite3_bind_blob(stmt, i + 1, parameters[i], length[i], SQLITE_STATIC); - break; - case Sqlite3TypeNull: - bindRet = sqlite3_bind_null(stmt, i + 1); - break; + case Sqlite3TypeChar: + bindRet = sqlite3_bind_int(stmt, i + 1, *(char *)parameters[i]); + break; + case Sqlite3TypeShort: + bindRet = sqlite3_bind_int(stmt, i + 1, *(short *)parameters[i]); + break; + case Sqlite3TypeInt: + bindRet = sqlite3_bind_int(stmt, i + 1, *(int32_t *)parameters[i]); + break; + case Sqlite3TypeInt64: + bindRet = sqlite3_bind_int64(stmt, i + 1, *(int64_t *)parameters[i]); + break; + case Sqlite3TypeDouble: + bindRet = sqlite3_bind_double(stmt, i + 1, *(double *)parameters[i]); + break; + case Sqlite3TypeText: + bindRet = sqlite3_bind_text(stmt, i + 1, parameters[i], -1, SQLITE_STATIC); + break; + case Sqlite3TypeBlob: + bindRet = sqlite3_bind_blob(stmt, i + 1, parameters[i], length[i], SQLITE_STATIC); + break; + case Sqlite3TypeNull: + bindRet = sqlite3_bind_null(stmt, i + 1); + break; } if (bindRet != SQLITE_OK) { @@ -211,14 +207,14 @@ void Sqlite3Connection::execSqlInQueue(const std::string &sql, if (sqlite3_stmt_readonly(stmt)) { - //Readonly, hold read lock; + // Readonly, hold read lock; std::shared_lock lock(*_sharedMutexPtr); r = stmtStep(stmt, resultPtr, columnNum); sqlite3_reset(stmt); } else { - //Hold write lock + // Hold write lock std::unique_lock lock(*_sharedMutexPtr); r = stmtStep(stmt, resultPtr, columnNum); if (r == SQLITE_DONE) @@ -249,28 +245,28 @@ int Sqlite3Connection::stmtStep(sqlite3_stmt *stmt, const std::shared_ptr> row; for (int i = 0; i < columnNum; i++) { - switch (sqlite3_column_type(stmt, i)) { - case SQLITE_INTEGER: - row.push_back(std::make_shared(std::to_string(sqlite3_column_int64(stmt, i)))); - break; - case SQLITE_FLOAT: - row.push_back(std::make_shared(std::to_string(sqlite3_column_double(stmt, i)))); - break; - case SQLITE_TEXT: - row.push_back(std::make_shared((const char *)sqlite3_column_text(stmt, i), (size_t)sqlite3_column_bytes(stmt, i))); - break; - case SQLITE_BLOB: - { - const char *buf = (const char *)sqlite3_column_blob(stmt, i); - size_t len = sqlite3_column_bytes(stmt, i); - row.push_back(buf ? std::make_shared(buf, len) : std::make_shared()); - } - break; - case SQLITE_NULL: - row.push_back(nullptr); + case SQLITE_INTEGER: + row.push_back(std::make_shared(std::to_string(sqlite3_column_int64(stmt, i)))); + break; + case SQLITE_FLOAT: + row.push_back(std::make_shared(std::to_string(sqlite3_column_double(stmt, i)))); + break; + case SQLITE_TEXT: + row.push_back(std::make_shared((const char *)sqlite3_column_text(stmt, i), + (size_t)sqlite3_column_bytes(stmt, i))); + break; + case SQLITE_BLOB: + { + const char *buf = (const char *)sqlite3_column_blob(stmt, i); + size_t len = sqlite3_column_bytes(stmt, i); + row.push_back(buf ? std::make_shared(buf, len) : std::make_shared()); + } break; + case SQLITE_NULL: + row.push_back(nullptr); + break; } } resultPtr->_result.push_back(std::move(row)); diff --git a/orm_lib/src/sqlite3_impl/Sqlite3Connection.h b/orm_lib/src/sqlite3_impl/Sqlite3Connection.h index a0dbd80f..c26e93f1 100644 --- a/orm_lib/src/sqlite3_impl/Sqlite3Connection.h +++ b/orm_lib/src/sqlite3_impl/Sqlite3Connection.h @@ -16,24 +16,23 @@ #include "../DbConnection.h" #include "Sqlite3ResultImpl.h" -#include #include -#include -#include -#include -#include -#include -#include +#include #include #include -#include +#include #include +#include +#include +#include +#include +#include +#include namespace drogon { namespace orm { - class Sqlite3Connection; typedef std::shared_ptr Sqlite3ConnectionPtr; class Sqlite3Connection : public DbConnection, public std::enable_shared_from_this @@ -64,8 +63,8 @@ class Sqlite3Connection : public DbConnection, public std::enable_shared_from_th trantor::EventLoopThread _loopThread; std::shared_ptr _conn; std::shared_ptr _sharedMutexPtr; - std::unordered_map> _stmtMap; + std::unordered_map> _stmtMap; }; -} // namespace orm -} // namespace drogon +} // namespace orm +} // namespace drogon diff --git a/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.cc b/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.cc index 892068c3..da8257ca 100644 --- a/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.cc +++ b/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.cc @@ -9,12 +9,12 @@ * that can be found in the License file. * * Drogon - * + * */ #include "Sqlite3ResultImpl.h" -#include #include +#include using namespace drogon::orm; diff --git a/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.h b/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.h index fb4ffe1c..51649b33 100644 --- a/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.h +++ b/orm_lib/src/sqlite3_impl/Sqlite3ResultImpl.h @@ -16,11 +16,11 @@ #include "../ResultImpl.h" -#include #include +#include #include -#include #include +#include namespace drogon { @@ -29,8 +29,7 @@ namespace orm class Sqlite3ResultImpl : public ResultImpl { public: - explicit Sqlite3ResultImpl(const std::string &query) noexcept - : ResultImpl(query) + explicit Sqlite3ResultImpl(const std::string &query) noexcept : ResultImpl(query) { } virtual size_type size() const noexcept override; @@ -52,5 +51,5 @@ class Sqlite3ResultImpl : public ResultImpl size_t _affectedRows = 0; size_t _insertId = 0; }; -} // namespace orm -} // namespace drogon \ No newline at end of file +} // namespace orm +} // namespace drogon \ No newline at end of file diff --git a/orm_lib/src/sqlite3_impl/test/Groups.cc b/orm_lib/src/sqlite3_impl/test/Groups.cc index 1dd46103..ef0a4730 100644 --- a/orm_lib/src/sqlite3_impl/test/Groups.cc +++ b/orm_lib/src/sqlite3_impl/test/Groups.cc @@ -25,18 +25,16 @@ const std::string Groups::primaryKeyName = "group_id"; const bool Groups::hasPrimaryKey = true; const std::string Groups::tableName = "GROUPS"; -const std::vector Groups::_metaData={ -{"group_id","uint64_t","integer",8,1,1,0}, -{"group_name","std::string","text",0,0,0,0}, -{"creater_id","uint64_t","integer",8,0,0,0}, -{"create_time","std::string","text",0,0,0,0}, -{"inviting","uint64_t","integer",8,0,0,0}, -{"inviting_user_id","uint64_t","integer",8,0,0,0}, -{"avatar_id","std::string","text",0,0,0,0}, -{"uuu","double","double",8,0,0,0}, -{"text","std::string","varchar(255)",0,0,0,0}, -{"avatar","std::vector","blob",0,0,0,0} -}; +const std::vector Groups::_metaData = {{"group_id", "uint64_t", "integer", 8, 1, 1, 0}, + {"group_name", "std::string", "text", 0, 0, 0, 0}, + {"creater_id", "uint64_t", "integer", 8, 0, 0, 0}, + {"create_time", "std::string", "text", 0, 0, 0, 0}, + {"inviting", "uint64_t", "integer", 8, 0, 0, 0}, + {"inviting_user_id", "uint64_t", "integer", 8, 0, 0, 0}, + {"avatar_id", "std::string", "text", 0, 0, 0, 0}, + {"uuu", "double", "double", 8, 0, 0, 0}, + {"text", "std::string", "varchar(255)", 0, 0, 0, 0}, + {"avatar", "std::vector", "blob", 0, 0, 0, 0}}; const std::string &Groups::getColumnName(size_t index) noexcept(false) { assert(index < _metaData.size()); @@ -44,50 +42,50 @@ const std::string &Groups::getColumnName(size_t index) noexcept(false) } Groups::Groups(const Row &r) noexcept { - if(!r["group_id"].isNull()) - { - _groupId=std::make_shared(r["group_id"].as()); - } - if(!r["group_name"].isNull()) - { - _groupName=std::make_shared(r["group_name"].as()); - } - if(!r["creater_id"].isNull()) - { - _createrId=std::make_shared(r["creater_id"].as()); - } - if(!r["create_time"].isNull()) - { - _createTime=std::make_shared(r["create_time"].as()); - } - if(!r["inviting"].isNull()) - { - _inviting=std::make_shared(r["inviting"].as()); - } - if(!r["inviting_user_id"].isNull()) - { - _invitingUserId=std::make_shared(r["inviting_user_id"].as()); - } - if(!r["avatar_id"].isNull()) - { - _avatarId=std::make_shared(r["avatar_id"].as()); - } - if(!r["uuu"].isNull()) - { - _uuu=std::make_shared(r["uuu"].as()); - } - if(!r["text"].isNull()) - { - _text=std::make_shared(r["text"].as()); - } - if(!r["avatar"].isNull()) - { - _avatar=std::make_shared>(r["avatar"].as>()); - } + if (!r["group_id"].isNull()) + { + _groupId = std::make_shared(r["group_id"].as()); + } + if (!r["group_name"].isNull()) + { + _groupName = std::make_shared(r["group_name"].as()); + } + if (!r["creater_id"].isNull()) + { + _createrId = std::make_shared(r["creater_id"].as()); + } + if (!r["create_time"].isNull()) + { + _createTime = std::make_shared(r["create_time"].as()); + } + if (!r["inviting"].isNull()) + { + _inviting = std::make_shared(r["inviting"].as()); + } + if (!r["inviting_user_id"].isNull()) + { + _invitingUserId = std::make_shared(r["inviting_user_id"].as()); + } + if (!r["avatar_id"].isNull()) + { + _avatarId = std::make_shared(r["avatar_id"].as()); + } + if (!r["uuu"].isNull()) + { + _uuu = std::make_shared(r["uuu"].as()); + } + if (!r["text"].isNull()) + { + _text = std::make_shared(r["text"].as()); + } + if (!r["avatar"].isNull()) + { + _avatar = std::make_shared>(r["avatar"].as>()); + } } -const uint64_t & Groups::getValueOfGroupId(const uint64_t &defaultValue) const noexcept +const uint64_t &Groups::getValueOfGroupId(const uint64_t &defaultValue) const noexcept { - if(_groupId) + if (_groupId) return *_groupId; return defaultValue; } @@ -95,15 +93,15 @@ std::shared_ptr Groups::getGroupId() const noexcept { return _groupId; } -const typename Groups::PrimaryKeyType & Groups::getPrimaryKey() const +const typename Groups::PrimaryKeyType &Groups::getPrimaryKey() const { assert(_groupId); return *_groupId; } -const std::string & Groups::getValueOfGroupName(const std::string &defaultValue) const noexcept +const std::string &Groups::getValueOfGroupName(const std::string &defaultValue) const noexcept { - if(_groupName) + if (_groupName) return *_groupName; return defaultValue; } @@ -122,10 +120,9 @@ void Groups::setGroupName(std::string &&groupName) noexcept _dirtyFlag[1] = true; } - -const uint64_t & Groups::getValueOfCreaterId(const uint64_t &defaultValue) const noexcept +const uint64_t &Groups::getValueOfCreaterId(const uint64_t &defaultValue) const noexcept { - if(_createrId) + if (_createrId) return *_createrId; return defaultValue; } @@ -139,10 +136,9 @@ void Groups::setCreaterId(const uint64_t &createrId) noexcept _dirtyFlag[2] = true; } - -const std::string & Groups::getValueOfCreateTime(const std::string &defaultValue) const noexcept +const std::string &Groups::getValueOfCreateTime(const std::string &defaultValue) const noexcept { - if(_createTime) + if (_createTime) return *_createTime; return defaultValue; } @@ -161,10 +157,9 @@ void Groups::setCreateTime(std::string &&createTime) noexcept _dirtyFlag[3] = true; } - -const uint64_t & Groups::getValueOfInviting(const uint64_t &defaultValue) const noexcept +const uint64_t &Groups::getValueOfInviting(const uint64_t &defaultValue) const noexcept { - if(_inviting) + if (_inviting) return *_inviting; return defaultValue; } @@ -178,10 +173,9 @@ void Groups::setInviting(const uint64_t &inviting) noexcept _dirtyFlag[4] = true; } - -const uint64_t & Groups::getValueOfInvitingUserId(const uint64_t &defaultValue) const noexcept +const uint64_t &Groups::getValueOfInvitingUserId(const uint64_t &defaultValue) const noexcept { - if(_invitingUserId) + if (_invitingUserId) return *_invitingUserId; return defaultValue; } @@ -195,10 +189,9 @@ void Groups::setInvitingUserId(const uint64_t &invitingUserId) noexcept _dirtyFlag[5] = true; } - -const std::string & Groups::getValueOfAvatarId(const std::string &defaultValue) const noexcept +const std::string &Groups::getValueOfAvatarId(const std::string &defaultValue) const noexcept { - if(_avatarId) + if (_avatarId) return *_avatarId; return defaultValue; } @@ -217,10 +210,9 @@ void Groups::setAvatarId(std::string &&avatarId) noexcept _dirtyFlag[6] = true; } - -const double & Groups::getValueOfUuu(const double &defaultValue) const noexcept +const double &Groups::getValueOfUuu(const double &defaultValue) const noexcept { - if(_uuu) + if (_uuu) return *_uuu; return defaultValue; } @@ -234,10 +226,9 @@ void Groups::setUuu(const double &uuu) noexcept _dirtyFlag[7] = true; } - -const std::string & Groups::getValueOfText(const std::string &defaultValue) const noexcept +const std::string &Groups::getValueOfText(const std::string &defaultValue) const noexcept { - if(_text) + if (_text) return *_text; return defaultValue; } @@ -256,17 +247,16 @@ void Groups::setText(std::string &&text) noexcept _dirtyFlag[8] = true; } - -const std::vector & Groups::getValueOfAvatar(const std::vector &defaultValue) const noexcept +const std::vector &Groups::getValueOfAvatar(const std::vector &defaultValue) const noexcept { - if(_avatar) + if (_avatar) return *_avatar; return defaultValue; } std::string Groups::getValueOfAvatarAsString(const std::string &defaultValue) const noexcept { - if(_avatar) - return std::string(_avatar->data(),_avatar->size()); + if (_avatar) + return std::string(_avatar->data(), _avatar->size()); return defaultValue; } std::shared_ptr> Groups::getAvatar() const noexcept @@ -281,7 +271,7 @@ void Groups::setAvatar(const std::vector &avatar) noexcept void Groups::setAvatar(const std::string &avatar) noexcept { - _avatar = std::make_shared>(avatar.c_str(),avatar.c_str()+avatar.length()); + _avatar = std::make_shared>(avatar.c_str(), avatar.c_str() + avatar.length()); _dirtyFlag[9] = true; } @@ -292,23 +282,14 @@ void Groups::updateId(const uint64_t id) const std::vector &Groups::insertColumns() noexcept { - static const std::vector _inCols={ - "group_name", - "creater_id", - "create_time", - "inviting", - "inviting_user_id", - "avatar_id", - "uuu", - "text", - "avatar" - }; + static const std::vector _inCols = { + "group_name", "creater_id", "create_time", "inviting", "inviting_user_id", "avatar_id", "uuu", "text", "avatar"}; return _inCols; } void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { - if(getGroupName()) + if (getGroupName()) { binder << getValueOfGroupName(); } @@ -316,7 +297,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getCreaterId()) + if (getCreaterId()) { binder << getValueOfCreaterId(); } @@ -324,7 +305,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getCreateTime()) + if (getCreateTime()) { binder << getValueOfCreateTime(); } @@ -332,7 +313,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getInviting()) + if (getInviting()) { binder << getValueOfInviting(); } @@ -340,7 +321,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getInvitingUserId()) + if (getInvitingUserId()) { binder << getValueOfInvitingUserId(); } @@ -348,7 +329,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getAvatarId()) + if (getAvatarId()) { binder << getValueOfAvatarId(); } @@ -356,7 +337,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getUuu()) + if (getUuu()) { binder << getValueOfUuu(); } @@ -364,7 +345,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getText()) + if (getText()) { binder << getValueOfText(); } @@ -372,7 +353,7 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getAvatar()) + if (getAvatar()) { binder << getValueOfAvatar(); } @@ -385,9 +366,9 @@ void Groups::outputArgs(drogon::orm::internal::SqlBinder &binder) const const std::vector Groups::updateColumns() const { std::vector ret; - for(size_t i=0;i Groups::updateColumns() const void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const { - if(_dirtyFlag[1]) + if (_dirtyFlag[1]) { - if(getGroupName()) + if (getGroupName()) { binder << getValueOfGroupName(); } @@ -408,9 +389,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[2]) + if (_dirtyFlag[2]) { - if(getCreaterId()) + if (getCreaterId()) { binder << getValueOfCreaterId(); } @@ -419,9 +400,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[3]) + if (_dirtyFlag[3]) { - if(getCreateTime()) + if (getCreateTime()) { binder << getValueOfCreateTime(); } @@ -430,9 +411,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[4]) + if (_dirtyFlag[4]) { - if(getInviting()) + if (getInviting()) { binder << getValueOfInviting(); } @@ -441,9 +422,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[5]) + if (_dirtyFlag[5]) { - if(getInvitingUserId()) + if (getInvitingUserId()) { binder << getValueOfInvitingUserId(); } @@ -452,9 +433,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[6]) + if (_dirtyFlag[6]) { - if(getAvatarId()) + if (getAvatarId()) { binder << getValueOfAvatarId(); } @@ -463,9 +444,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[7]) + if (_dirtyFlag[7]) { - if(getUuu()) + if (getUuu()) { binder << getValueOfUuu(); } @@ -474,9 +455,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[8]) + if (_dirtyFlag[8]) { - if(getText()) + if (getText()) { binder << getValueOfText(); } @@ -485,9 +466,9 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[9]) + if (_dirtyFlag[9]) { - if(getAvatar()) + if (getAvatar()) { binder << getValueOfAvatar(); } @@ -500,85 +481,85 @@ void Groups::updateArgs(drogon::orm::internal::SqlBinder &binder) const Json::Value Groups::toJson() const { Json::Value ret; - if(getGroupId()) + if (getGroupId()) { - ret["group_id"]=(Json::UInt64)getValueOfGroupId(); + ret["group_id"] = (Json::UInt64)getValueOfGroupId(); } else { - ret["group_id"]=Json::Value(); + ret["group_id"] = Json::Value(); } - if(getGroupName()) + if (getGroupName()) { - ret["group_name"]=getValueOfGroupName(); + ret["group_name"] = getValueOfGroupName(); } else { - ret["group_name"]=Json::Value(); + ret["group_name"] = Json::Value(); } - if(getCreaterId()) + if (getCreaterId()) { - ret["creater_id"]=(Json::UInt64)getValueOfCreaterId(); + ret["creater_id"] = (Json::UInt64)getValueOfCreaterId(); } else { - ret["creater_id"]=Json::Value(); + ret["creater_id"] = Json::Value(); } - if(getCreateTime()) + if (getCreateTime()) { - ret["create_time"]=getValueOfCreateTime(); + ret["create_time"] = getValueOfCreateTime(); } else { - ret["create_time"]=Json::Value(); + ret["create_time"] = Json::Value(); } - if(getInviting()) + if (getInviting()) { - ret["inviting"]=(Json::UInt64)getValueOfInviting(); + ret["inviting"] = (Json::UInt64)getValueOfInviting(); } else { - ret["inviting"]=Json::Value(); + ret["inviting"] = Json::Value(); } - if(getInvitingUserId()) + if (getInvitingUserId()) { - ret["inviting_user_id"]=(Json::UInt64)getValueOfInvitingUserId(); + ret["inviting_user_id"] = (Json::UInt64)getValueOfInvitingUserId(); } else { - ret["inviting_user_id"]=Json::Value(); + ret["inviting_user_id"] = Json::Value(); } - if(getAvatarId()) + if (getAvatarId()) { - ret["avatar_id"]=getValueOfAvatarId(); + ret["avatar_id"] = getValueOfAvatarId(); } else { - ret["avatar_id"]=Json::Value(); + ret["avatar_id"] = Json::Value(); } - if(getUuu()) + if (getUuu()) { - ret["uuu"]=getValueOfUuu(); + ret["uuu"] = getValueOfUuu(); } else { - ret["uuu"]=Json::Value(); + ret["uuu"] = Json::Value(); } - if(getText()) + if (getText()) { - ret["text"]=getValueOfText(); + ret["text"] = getValueOfText(); } else { - ret["text"]=Json::Value(); + ret["text"] = Json::Value(); } - if(getAvatar()) + if (getAvatar()) { - ret["avatar"]=drogon::utils::base64Encode((const unsigned char *)getAvatar()->data(),getAvatar()->size()); + ret["avatar"] = drogon::utils::base64Encode((const unsigned char *)getAvatar()->data(), getAvatar()->size()); } else { - ret["avatar"]=Json::Value(); + ret["avatar"] = Json::Value(); } return ret; } diff --git a/orm_lib/src/sqlite3_impl/test/Groups.h b/orm_lib/src/sqlite3_impl/test/Groups.h index c7372501..6c962322 100644 --- a/orm_lib/src/sqlite3_impl/test/Groups.h +++ b/orm_lib/src/sqlite3_impl/test/Groups.h @@ -6,26 +6,25 @@ */ #pragma once +#include +#include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include using namespace drogon::orm; namespace drogon_model { -namespace sqlite3 +namespace sqlite3 { - class Groups { public: @@ -48,97 +47,119 @@ class Groups const static bool hasPrimaryKey; const static std::string primaryKeyName; typedef uint64_t PrimaryKeyType; - const PrimaryKeyType & getPrimaryKey() const; + const PrimaryKeyType &getPrimaryKey() const; Groups(const Row &r) noexcept; Groups() = default; - + /** For column group_id */ - ///Get the value of the column group_id, returns the default value if the column is null - const uint64_t &getValueOfGroupId(const uint64_t &defaultValue=uint64_t()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column group_id, returns the default value if the + /// column is null + const uint64_t &getValueOfGroupId(const uint64_t &defaultValue = uint64_t()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getGroupId() const noexcept; /** For column group_name */ - ///Get the value of the column group_name, returns the default value if the column is null - const std::string &getValueOfGroupName(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column group_name, returns the default value if the + /// column is null + const std::string &getValueOfGroupName(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getGroupName() const noexcept; - ///Set the value of the column group_name + /// Set the value of the column group_name void setGroupName(const std::string &groupName) noexcept; void setGroupName(std::string &&groupName) noexcept; /** For column creater_id */ - ///Get the value of the column creater_id, returns the default value if the column is null - const uint64_t &getValueOfCreaterId(const uint64_t &defaultValue=uint64_t()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column creater_id, returns the default value if the + /// column is null + const uint64_t &getValueOfCreaterId(const uint64_t &defaultValue = uint64_t()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getCreaterId() const noexcept; - ///Set the value of the column creater_id + /// Set the value of the column creater_id void setCreaterId(const uint64_t &createrId) noexcept; /** For column create_time */ - ///Get the value of the column create_time, returns the default value if the column is null - const std::string &getValueOfCreateTime(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column create_time, returns the default value if the + /// column is null + const std::string &getValueOfCreateTime(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getCreateTime() const noexcept; - ///Set the value of the column create_time + /// Set the value of the column create_time void setCreateTime(const std::string &createTime) noexcept; void setCreateTime(std::string &&createTime) noexcept; /** For column inviting */ - ///Get the value of the column inviting, returns the default value if the column is null - const uint64_t &getValueOfInviting(const uint64_t &defaultValue=uint64_t()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column inviting, returns the default value if the + /// column is null + const uint64_t &getValueOfInviting(const uint64_t &defaultValue = uint64_t()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getInviting() const noexcept; - ///Set the value of the column inviting + /// Set the value of the column inviting void setInviting(const uint64_t &inviting) noexcept; /** For column inviting_user_id */ - ///Get the value of the column inviting_user_id, returns the default value if the column is null - const uint64_t &getValueOfInvitingUserId(const uint64_t &defaultValue=uint64_t()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column inviting_user_id, returns the default value if + /// the column is null + const uint64_t &getValueOfInvitingUserId(const uint64_t &defaultValue = uint64_t()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getInvitingUserId() const noexcept; - ///Set the value of the column inviting_user_id + /// Set the value of the column inviting_user_id void setInvitingUserId(const uint64_t &invitingUserId) noexcept; /** For column avatar_id */ - ///Get the value of the column avatar_id, returns the default value if the column is null - const std::string &getValueOfAvatarId(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column avatar_id, returns the default value if the + /// column is null + const std::string &getValueOfAvatarId(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getAvatarId() const noexcept; - ///Set the value of the column avatar_id + /// Set the value of the column avatar_id void setAvatarId(const std::string &avatarId) noexcept; void setAvatarId(std::string &&avatarId) noexcept; /** For column uuu */ - ///Get the value of the column uuu, returns the default value if the column is null - const double &getValueOfUuu(const double &defaultValue=double()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column uuu, returns the default value if the column + /// is null + const double &getValueOfUuu(const double &defaultValue = double()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getUuu() const noexcept; - ///Set the value of the column uuu + /// Set the value of the column uuu void setUuu(const double &uuu) noexcept; /** For column text */ - ///Get the value of the column text, returns the default value if the column is null - const std::string &getValueOfText(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column text, returns the default value if the column + /// is null + const std::string &getValueOfText(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getText() const noexcept; - ///Set the value of the column text + /// Set the value of the column text void setText(const std::string &text) noexcept; void setText(std::string &&text) noexcept; /** For column avatar */ - ///Get the value of the column avatar, returns the default value if the column is null - const std::vector &getValueOfAvatar(const std::vector &defaultValue=std::vector()) const noexcept; - ///Return the column value by std::string with binary data - std::string getValueOfAvatarAsString(const std::string &defaultValue="") const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column avatar, returns the default value if the + /// column is null + const std::vector &getValueOfAvatar(const std::vector &defaultValue = std::vector()) const noexcept; + /// Return the column value by std::string with binary data + std::string getValueOfAvatarAsString(const std::string &defaultValue = "") const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr> getAvatar() const noexcept; - ///Set the value of the column avatar + /// Set the value of the column avatar void setAvatar(const std::vector &avatar) noexcept; void setAvatar(const std::string &avatar) noexcept; - - static size_t getColumnNumber() noexcept { return 10; } + static size_t getColumnNumber() noexcept + { + return 10; + } static const std::string &getColumnName(size_t index) noexcept(false); Json::Value toJson() const; @@ -149,7 +170,7 @@ class Groups void outputArgs(drogon::orm::internal::SqlBinder &binder) const; const std::vector updateColumns() const; void updateArgs(drogon::orm::internal::SqlBinder &binder) const; - ///For mysql or sqlite3 + /// For mysql or sqlite3 void updateId(const uint64_t id); std::shared_ptr _groupId; std::shared_ptr _groupName; @@ -172,8 +193,8 @@ class Groups const bool _notNull; }; static const std::vector _metaData; - bool _dirtyFlag[10]={ false }; + bool _dirtyFlag[10] = {false}; }; -} // namespace sqlite3 -} // namespace drogon_model +} // namespace sqlite3 +} // namespace drogon_model diff --git a/orm_lib/src/sqlite3_impl/test/test1.cc b/orm_lib/src/sqlite3_impl/test/test1.cc index 04b896b2..209bee28 100644 --- a/orm_lib/src/sqlite3_impl/test/test1.cc +++ b/orm_lib/src/sqlite3_impl/test/test1.cc @@ -1,8 +1,8 @@ #include "Groups.h" #include #include -#include #include +#include #include using namespace drogon::orm; @@ -29,67 +29,50 @@ int main() INVITING_USER_ID INTEGER,\ AVATAR_ID TEXT, uuu double, text VARCHAR(255),avatar blob)" << Mode::Blocking >> - [](const Result &r) { - LOG_DEBUG << "created"; - } >> - [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; + [](const Result &r) { LOG_DEBUG << "created"; } >> + [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; *clientPtr << "insert into GROUPS (group_name) values(?)" << "test_group" << Mode::Blocking >> [](const Result &r) { LOG_DEBUG << "inserted:" << r.affectedRows(); LOG_DEBUG << "id:" << r.insertId(); } >> - [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; + [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; *clientPtr << "insert into GROUPS (group_name) values(?)" << "test_group" << Mode::Blocking >> [](const Result &r) { LOG_DEBUG << "inserted:" << r.affectedRows(); LOG_DEBUG << "id:" << r.insertId(); } >> - [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; - *clientPtr << "select * from GROUPS " >> - [](const Result &r) { - LOG_DEBUG << "affected rows:" << r.affectedRows(); - LOG_DEBUG << "select " << r.size() << " rows"; - LOG_DEBUG << "id:" << r.insertId(); - for (auto const &row : r) - { - LOG_DEBUG << "group_id:" << row["group_id"].as(); - } - } >> - [](const DrogonDbException &e) { - std::cout << e.base().what() << std::endl; - }; - { - auto trans = clientPtr->newTransaction([](bool success) { - LOG_DEBUG << (success ? "commit success!" : "commit failed!"); - }); - Mapper mapper(trans); - mapper.limit(2).offset(1).findAll([trans](const std::vector &v) { - Mapper mapper(trans); - for(auto group:v) + [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; + *clientPtr << "select * from GROUPS " >> [](const Result &r) { + LOG_DEBUG << "affected rows:" << r.affectedRows(); + LOG_DEBUG << "select " << r.size() << " rows"; + LOG_DEBUG << "id:" << r.insertId(); + for (auto const &row : r) { - LOG_DEBUG << "group_id=" << group.getValueOfGroupId(); - std::cout << group.toJson() << std::endl; - std::cout << "avatar:" << group.getValueOfAvatarAsString() << std::endl; - group.setAvatarId("xixi"); - mapper.update(group, - [=](const size_t count) - { - LOG_DEBUG << "update " << count << " rows"; - }, - [](const DrogonDbException &e) - { - LOG_ERROR << e.base().what(); - }); - } }, - [](const DrogonDbException &e) { LOG_ERROR << e.base().what(); }); + LOG_DEBUG << "group_id:" << row["group_id"].as(); + } + } >> [](const DrogonDbException &e) { std::cout << e.base().what() << std::endl; }; + { + auto trans = + clientPtr->newTransaction([](bool success) { LOG_DEBUG << (success ? "commit success!" : "commit failed!"); }); + Mapper mapper(trans); + mapper.limit(2).offset(1).findAll( + [trans](const std::vector &v) { + Mapper mapper(trans); + for (auto group : v) + { + LOG_DEBUG << "group_id=" << group.getValueOfGroupId(); + std::cout << group.toJson() << std::endl; + std::cout << "avatar:" << group.getValueOfAvatarAsString() << std::endl; + group.setAvatarId("xixi"); + mapper.update(group, + [=](const size_t count) { LOG_DEBUG << "update " << count << " rows"; }, + [](const DrogonDbException &e) { LOG_ERROR << e.base().what(); }); + } + }, + [](const DrogonDbException &e) { LOG_ERROR << e.base().what(); }); drogon_model::sqlite3::Groups group; group.setAvatar("hahahaha,xixixixix"); try diff --git a/orm_lib/tests/Users.cc b/orm_lib/tests/Users.cc index 1e6221ff..47f5c055 100644 --- a/orm_lib/tests/Users.cc +++ b/orm_lib/tests/Users.cc @@ -6,9 +6,9 @@ */ #include "Users.h" -#include #include #include +#include using namespace drogon_model::postgres; @@ -25,17 +25,15 @@ const std::string Users::primaryKeyName = "id"; const bool Users::hasPrimaryKey = true; const std::string Users::tableName = "users"; -const std::vector Users::_metaData={ -{"user_id","std::string","character varying",32,0,0,0}, -{"user_name","std::string","character varying",64,0,0,0}, -{"password","std::string","character varying",64,0,0,0}, -{"org_name","std::string","character varying",20,0,0,0}, -{"signature","std::string","character varying",50,0,0,0}, -{"avatar_id","std::string","character varying",32,0,0,0}, -{"id","int32_t","integer",4,1,1,1}, -{"salt","std::string","character varying",20,0,0,0}, -{"admin","bool","boolean",1,0,0,0} -}; +const std::vector Users::_metaData = {{"user_id", "std::string", "character varying", 32, 0, 0, 0}, + {"user_name", "std::string", "character varying", 64, 0, 0, 0}, + {"password", "std::string", "character varying", 64, 0, 0, 0}, + {"org_name", "std::string", "character varying", 20, 0, 0, 0}, + {"signature", "std::string", "character varying", 50, 0, 0, 0}, + {"avatar_id", "std::string", "character varying", 32, 0, 0, 0}, + {"id", "int32_t", "integer", 4, 1, 1, 1}, + {"salt", "std::string", "character varying", 20, 0, 0, 0}, + {"admin", "bool", "boolean", 1, 0, 0, 0}}; const std::string &Users::getColumnName(size_t index) noexcept(false) { assert(index < _metaData.size()); @@ -43,46 +41,46 @@ const std::string &Users::getColumnName(size_t index) noexcept(false) } Users::Users(const Row &r) noexcept { - if(!r["user_id"].isNull()) - { - _userId=std::make_shared(r["user_id"].as()); - } - if(!r["user_name"].isNull()) - { - _userName=std::make_shared(r["user_name"].as()); - } - if(!r["password"].isNull()) - { - _password=std::make_shared(r["password"].as()); - } - if(!r["org_name"].isNull()) - { - _orgName=std::make_shared(r["org_name"].as()); - } - if(!r["signature"].isNull()) - { - _signature=std::make_shared(r["signature"].as()); - } - if(!r["avatar_id"].isNull()) - { - _avatarId=std::make_shared(r["avatar_id"].as()); - } - if(!r["id"].isNull()) - { - _id=std::make_shared(r["id"].as()); - } - if(!r["salt"].isNull()) - { - _salt=std::make_shared(r["salt"].as()); - } - if(!r["admin"].isNull()) - { - _admin=std::make_shared(r["admin"].as()); - } + if (!r["user_id"].isNull()) + { + _userId = std::make_shared(r["user_id"].as()); + } + if (!r["user_name"].isNull()) + { + _userName = std::make_shared(r["user_name"].as()); + } + if (!r["password"].isNull()) + { + _password = std::make_shared(r["password"].as()); + } + if (!r["org_name"].isNull()) + { + _orgName = std::make_shared(r["org_name"].as()); + } + if (!r["signature"].isNull()) + { + _signature = std::make_shared(r["signature"].as()); + } + if (!r["avatar_id"].isNull()) + { + _avatarId = std::make_shared(r["avatar_id"].as()); + } + if (!r["id"].isNull()) + { + _id = std::make_shared(r["id"].as()); + } + if (!r["salt"].isNull()) + { + _salt = std::make_shared(r["salt"].as()); + } + if (!r["admin"].isNull()) + { + _admin = std::make_shared(r["admin"].as()); + } } -const std::string & Users::getValueOfUserId(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfUserId(const std::string &defaultValue) const noexcept { - if(_userId) + if (_userId) return *_userId; return defaultValue; } @@ -101,10 +99,9 @@ void Users::setUserId(std::string &&userId) noexcept _dirtyFlag[0] = true; } - -const std::string & Users::getValueOfUserName(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfUserName(const std::string &defaultValue) const noexcept { - if(_userName) + if (_userName) return *_userName; return defaultValue; } @@ -123,10 +120,9 @@ void Users::setUserName(std::string &&userName) noexcept _dirtyFlag[1] = true; } - -const std::string & Users::getValueOfPassword(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfPassword(const std::string &defaultValue) const noexcept { - if(_password) + if (_password) return *_password; return defaultValue; } @@ -145,10 +141,9 @@ void Users::setPassword(std::string &&password) noexcept _dirtyFlag[2] = true; } - -const std::string & Users::getValueOfOrgName(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfOrgName(const std::string &defaultValue) const noexcept { - if(_orgName) + if (_orgName) return *_orgName; return defaultValue; } @@ -167,10 +162,9 @@ void Users::setOrgName(std::string &&orgName) noexcept _dirtyFlag[3] = true; } - -const std::string & Users::getValueOfSignature(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfSignature(const std::string &defaultValue) const noexcept { - if(_signature) + if (_signature) return *_signature; return defaultValue; } @@ -189,10 +183,9 @@ void Users::setSignature(std::string &&signature) noexcept _dirtyFlag[4] = true; } - -const std::string & Users::getValueOfAvatarId(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfAvatarId(const std::string &defaultValue) const noexcept { - if(_avatarId) + if (_avatarId) return *_avatarId; return defaultValue; } @@ -211,10 +204,9 @@ void Users::setAvatarId(std::string &&avatarId) noexcept _dirtyFlag[5] = true; } - -const int32_t & Users::getValueOfId(const int32_t &defaultValue) const noexcept +const int32_t &Users::getValueOfId(const int32_t &defaultValue) const noexcept { - if(_id) + if (_id) return *_id; return defaultValue; } @@ -222,15 +214,15 @@ std::shared_ptr Users::getId() const noexcept { return _id; } -const typename Users::PrimaryKeyType & Users::getPrimaryKey() const +const typename Users::PrimaryKeyType &Users::getPrimaryKey() const { assert(_id); return *_id; } -const std::string & Users::getValueOfSalt(const std::string &defaultValue) const noexcept +const std::string &Users::getValueOfSalt(const std::string &defaultValue) const noexcept { - if(_salt) + if (_salt) return *_salt; return defaultValue; } @@ -249,10 +241,9 @@ void Users::setSalt(std::string &&salt) noexcept _dirtyFlag[7] = true; } - -const bool & Users::getValueOfAdmin(const bool &defaultValue) const noexcept +const bool &Users::getValueOfAdmin(const bool &defaultValue) const noexcept { - if(_admin) + if (_admin) return *_admin; return defaultValue; } @@ -266,29 +257,20 @@ void Users::setAdmin(const bool &admin) noexcept _dirtyFlag[8] = true; } - void Users::updateId(const uint64_t id) { } const std::vector &Users::insertColumns() noexcept { - static const std::vector _inCols={ - "user_id", - "user_name", - "password", - "org_name", - "signature", - "avatar_id", - "salt", - "admin" - }; + static const std::vector _inCols = { + "user_id", "user_name", "password", "org_name", "signature", "avatar_id", "salt", "admin"}; return _inCols; } void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { - if(getUserId()) + if (getUserId()) { binder << getValueOfUserId(); } @@ -296,7 +278,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getUserName()) + if (getUserName()) { binder << getValueOfUserName(); } @@ -304,7 +286,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getPassword()) + if (getPassword()) { binder << getValueOfPassword(); } @@ -312,7 +294,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getOrgName()) + if (getOrgName()) { binder << getValueOfOrgName(); } @@ -320,7 +302,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getSignature()) + if (getSignature()) { binder << getValueOfSignature(); } @@ -328,7 +310,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getAvatarId()) + if (getAvatarId()) { binder << getValueOfAvatarId(); } @@ -336,7 +318,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getSalt()) + if (getSalt()) { binder << getValueOfSalt(); } @@ -344,7 +326,7 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const { binder << nullptr; } - if(getAdmin()) + if (getAdmin()) { binder << getValueOfAdmin(); } @@ -357,9 +339,9 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const const std::vector Users::updateColumns() const { std::vector ret; - for(size_t i=0;i Users::updateColumns() const void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const { - if(_dirtyFlag[0]) + if (_dirtyFlag[0]) { - if(getUserId()) + if (getUserId()) { binder << getValueOfUserId(); } @@ -380,9 +362,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[1]) + if (_dirtyFlag[1]) { - if(getUserName()) + if (getUserName()) { binder << getValueOfUserName(); } @@ -391,9 +373,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[2]) + if (_dirtyFlag[2]) { - if(getPassword()) + if (getPassword()) { binder << getValueOfPassword(); } @@ -402,9 +384,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[3]) + if (_dirtyFlag[3]) { - if(getOrgName()) + if (getOrgName()) { binder << getValueOfOrgName(); } @@ -413,9 +395,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[4]) + if (_dirtyFlag[4]) { - if(getSignature()) + if (getSignature()) { binder << getValueOfSignature(); } @@ -424,9 +406,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[5]) + if (_dirtyFlag[5]) { - if(getAvatarId()) + if (getAvatarId()) { binder << getValueOfAvatarId(); } @@ -435,9 +417,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[7]) + if (_dirtyFlag[7]) { - if(getSalt()) + if (getSalt()) { binder << getValueOfSalt(); } @@ -446,9 +428,9 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } - if(_dirtyFlag[8]) + if (_dirtyFlag[8]) { - if(getAdmin()) + if (getAdmin()) { binder << getValueOfAdmin(); } @@ -461,77 +443,77 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const Json::Value Users::toJson() const { Json::Value ret; - if(getUserId()) + if (getUserId()) { - ret["user_id"]=getValueOfUserId(); + ret["user_id"] = getValueOfUserId(); } else { - ret["user_id"]=Json::Value(); + ret["user_id"] = Json::Value(); } - if(getUserName()) + if (getUserName()) { - ret["user_name"]=getValueOfUserName(); + ret["user_name"] = getValueOfUserName(); } else { - ret["user_name"]=Json::Value(); + ret["user_name"] = Json::Value(); } - if(getPassword()) + if (getPassword()) { - ret["password"]=getValueOfPassword(); + ret["password"] = getValueOfPassword(); } else { - ret["password"]=Json::Value(); + ret["password"] = Json::Value(); } - if(getOrgName()) + if (getOrgName()) { - ret["org_name"]=getValueOfOrgName(); + ret["org_name"] = getValueOfOrgName(); } else { - ret["org_name"]=Json::Value(); + ret["org_name"] = Json::Value(); } - if(getSignature()) + if (getSignature()) { - ret["signature"]=getValueOfSignature(); + ret["signature"] = getValueOfSignature(); } else { - ret["signature"]=Json::Value(); + ret["signature"] = Json::Value(); } - if(getAvatarId()) + if (getAvatarId()) { - ret["avatar_id"]=getValueOfAvatarId(); + ret["avatar_id"] = getValueOfAvatarId(); } else { - ret["avatar_id"]=Json::Value(); + ret["avatar_id"] = Json::Value(); } - if(getId()) + if (getId()) { - ret["id"]=getValueOfId(); + ret["id"] = getValueOfId(); } else { - ret["id"]=Json::Value(); + ret["id"] = Json::Value(); } - if(getSalt()) + if (getSalt()) { - ret["salt"]=getValueOfSalt(); + ret["salt"] = getValueOfSalt(); } else { - ret["salt"]=Json::Value(); + ret["salt"] = Json::Value(); } - if(getAdmin()) + if (getAdmin()) { - ret["admin"]=getValueOfAdmin(); + ret["admin"] = getValueOfAdmin(); } else { - ret["admin"]=Json::Value(); + ret["admin"] = Json::Value(); } return ret; } diff --git a/orm_lib/tests/Users.h b/orm_lib/tests/Users.h index 7c9179b1..b2cf736c 100644 --- a/orm_lib/tests/Users.h +++ b/orm_lib/tests/Users.h @@ -6,26 +6,25 @@ */ #pragma once +#include +#include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include using namespace drogon::orm; namespace drogon_model { -namespace postgres +namespace postgres { - class Users { public: @@ -47,89 +46,109 @@ class Users const static bool hasPrimaryKey; const static std::string primaryKeyName; typedef int32_t PrimaryKeyType; - const PrimaryKeyType & getPrimaryKey() const; + const PrimaryKeyType &getPrimaryKey() const; Users(const Row &r) noexcept; Users() = default; - + /** For column user_id */ - ///Get the value of the column user_id, returns the default value if the column is null - const std::string &getValueOfUserId(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column user_id, returns the default value if the + /// column is null + const std::string &getValueOfUserId(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getUserId() const noexcept; - ///Set the value of the column user_id + /// Set the value of the column user_id void setUserId(const std::string &userId) noexcept; void setUserId(std::string &&userId) noexcept; /** For column user_name */ - ///Get the value of the column user_name, returns the default value if the column is null - const std::string &getValueOfUserName(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column user_name, returns the default value if the + /// column is null + const std::string &getValueOfUserName(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getUserName() const noexcept; - ///Set the value of the column user_name + /// Set the value of the column user_name void setUserName(const std::string &userName) noexcept; void setUserName(std::string &&userName) noexcept; /** For column password */ - ///Get the value of the column password, returns the default value if the column is null - const std::string &getValueOfPassword(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column password, returns the default value if the + /// column is null + const std::string &getValueOfPassword(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getPassword() const noexcept; - ///Set the value of the column password + /// Set the value of the column password void setPassword(const std::string &password) noexcept; void setPassword(std::string &&password) noexcept; /** For column org_name */ - ///Get the value of the column org_name, returns the default value if the column is null - const std::string &getValueOfOrgName(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column org_name, returns the default value if the + /// column is null + const std::string &getValueOfOrgName(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getOrgName() const noexcept; - ///Set the value of the column org_name + /// Set the value of the column org_name void setOrgName(const std::string &orgName) noexcept; void setOrgName(std::string &&orgName) noexcept; /** For column signature */ - ///Get the value of the column signature, returns the default value if the column is null - const std::string &getValueOfSignature(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column signature, returns the default value if the + /// column is null + const std::string &getValueOfSignature(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getSignature() const noexcept; - ///Set the value of the column signature + /// Set the value of the column signature void setSignature(const std::string &signature) noexcept; void setSignature(std::string &&signature) noexcept; /** For column avatar_id */ - ///Get the value of the column avatar_id, returns the default value if the column is null - const std::string &getValueOfAvatarId(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column avatar_id, returns the default value if the + /// column is null + const std::string &getValueOfAvatarId(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getAvatarId() const noexcept; - ///Set the value of the column avatar_id + /// Set the value of the column avatar_id void setAvatarId(const std::string &avatarId) noexcept; void setAvatarId(std::string &&avatarId) noexcept; /** For column id */ - ///Get the value of the column id, returns the default value if the column is null - const int32_t &getValueOfId(const int32_t &defaultValue=int32_t()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column id, returns the default value if the column is + /// null + const int32_t &getValueOfId(const int32_t &defaultValue = int32_t()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getId() const noexcept; /** For column salt */ - ///Get the value of the column salt, returns the default value if the column is null - const std::string &getValueOfSalt(const std::string &defaultValue=std::string()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column salt, returns the default value if the column + /// is null + const std::string &getValueOfSalt(const std::string &defaultValue = std::string()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getSalt() const noexcept; - ///Set the value of the column salt + /// Set the value of the column salt void setSalt(const std::string &salt) noexcept; void setSalt(std::string &&salt) noexcept; /** For column admin */ - ///Get the value of the column admin, returns the default value if the column is null - const bool &getValueOfAdmin(const bool &defaultValue=bool()) const noexcept; - ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null + /// Get the value of the column admin, returns the default value if the column + /// is null + const bool &getValueOfAdmin(const bool &defaultValue = bool()) const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an empty + /// shared_ptr object if the column is null std::shared_ptr getAdmin() const noexcept; - ///Set the value of the column admin + /// Set the value of the column admin void setAdmin(const bool &admin) noexcept; - - static size_t getColumnNumber() noexcept { return 9; } + static size_t getColumnNumber() noexcept + { + return 9; + } static const std::string &getColumnName(size_t index) noexcept(false); Json::Value toJson() const; @@ -140,7 +159,7 @@ class Users void outputArgs(drogon::orm::internal::SqlBinder &binder) const; const std::vector updateColumns() const; void updateArgs(drogon::orm::internal::SqlBinder &binder) const; - ///For mysql or sqlite3 + /// For mysql or sqlite3 void updateId(const uint64_t id); std::shared_ptr _userId; std::shared_ptr _userName; @@ -162,8 +181,8 @@ class Users const bool _notNull; }; static const std::vector _metaData; - bool _dirtyFlag[9]={ false }; + bool _dirtyFlag[9] = {false}; }; -} // namespace postgres -} // namespace drogon_model +} // namespace postgres +} // namespace drogon_model diff --git a/orm_lib/tests/db_test.cc b/orm_lib/tests/db_test.cc index 62c2e07c..2517bb2b 100644 --- a/orm_lib/tests/db_test.cc +++ b/orm_lib/tests/db_test.cc @@ -8,14 +8,14 @@ * that can be found in the License file. * * Drogon - * + * * Drogon database test program - * + * */ #include "Users.h" #include -#include #include +#include #include using namespace drogon::orm; #define RESET "\033[0m" @@ -50,12 +50,9 @@ int main() #endif LOG_DEBUG << "start!"; sleep(1); - //Prepare the test environment + // Prepare the test environment *clientPtr << "DROP TABLE IF EXISTS USERS" >> - [](const Result &r) { - testOutput(true, "Prepare the test environment(0)"); - } >> - [](const DrogonDbException &e) { + [](const Result &r) { testOutput(true, "Prepare the test environment(0)"); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; testOutput(false, "Prepare the test environment(0)"); }; @@ -72,10 +69,7 @@ int main() admin boolean DEFAULT false,\ CONSTRAINT user_id_org UNIQUE(user_id, org_name)\ )" >> - [](const Result &r) { - testOutput(true, "Prepare the test environment(1)"); - } >> - [](const DrogonDbException &e) { + [](const Result &r) { testOutput(true, "Prepare the test environment(1)"); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; testOutput(false, "Prepare the test environment(1)"); }; @@ -89,60 +83,53 @@ int main() << "123" << "default" >> [](const Result &r) { - //std::cout << "id=" << r[0]["id"].as() << std::endl; + // std::cout << "id=" << r[0]["id"].as() << std::endl; testOutput(r[0]["id"].as() == 1, "DbClient streaming-type interface(0)"); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; testOutput(false, "DbClient streaming-type interface(0)"); }; - ///1.2 insert,blocking + /// 1.2 insert,blocking *clientPtr << "insert into users \ (user_id,user_name,password,org_name) \ values($1,$2,$3,$4) returning *" << "pg1" << "postgresql1" << "123" - << "default" - << Mode::Blocking >> + << "default" << Mode::Blocking >> [](const Result &r) { - //std::cout << "id=" << r[0]["id"].as() << std::endl; + // std::cout << "id=" << r[0]["id"].as() << std::endl; testOutput(r[0]["id"].as() == 2, "DbClient streaming-type interface(1)"); } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; testOutput(false, "DbClient streaming-type interface(1)"); }; - ///1.3 query,no-blocking - *clientPtr << "select * from users where 1 = 1" - << Mode::NonBlocking >> - [](const Result &r) { - for (Result::size_type i = 0; i < r.size(); ++i) - { - std::cout << r[i]["id"].as() << " " << r[i]["user_id"].as() << " " - << r[i]["user_name"].as() << std::endl; - } - testOutput(true, "DbClient streaming-type interface(0)"); - } >> - [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - testOutput(false, "DbClient streaming-type interface(0)"); - }; - ///1.4 query,blocking - *clientPtr << "select * from users where 1 = 1" - << Mode::Blocking >> - [](const Result &r) { - for (const auto &item : r) - { - std::cout << item["id"].as() << " " << item["user_id"].as() << " " - << item["user_name"].as() << std::endl; - } - testOutput(true, "DbClient streaming-type interface(1)"); - } >> - [](const DrogonDbException &e) { - std::cerr << e.base().what() << std::endl; - testOutput(false, "DbClient streaming-type interface(1)"); - }; + /// 1.3 query,no-blocking + *clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >> [](const Result &r) { + for (Result::size_type i = 0; i < r.size(); ++i) + { + std::cout << r[i]["id"].as() << " " << r[i]["user_id"].as() << " " + << r[i]["user_name"].as() << std::endl; + } + testOutput(true, "DbClient streaming-type interface(0)"); + } >> [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(0)"); + }; + /// 1.4 query,blocking + *clientPtr << "select * from users where 1 = 1" << Mode::Blocking >> [](const Result &r) { + for (const auto &item : r) + { + std::cout << item["id"].as() << " " << item["user_id"].as() << " " + << item["user_name"].as() << std::endl; + } + testOutput(true, "DbClient streaming-type interface(1)"); + } >> [](const DrogonDbException &e) { + std::cerr << e.base().what() << std::endl; + testOutput(false, "DbClient streaming-type interface(1)"); + }; /// 2 DbClient execSqlAsync()... ///