From 8e93f1b8385f95396021fa6204d13e7d4ab0ad2d Mon Sep 17 00:00:00 2001 From: XieHan Date: Tue, 25 May 2021 18:48:37 +0800 Subject: [PATCH 1/2] optimize code --- src/factory/HttpTaskImpl.cc | 51 +++++++++++++------------------------ 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/src/factory/HttpTaskImpl.cc b/src/factory/HttpTaskImpl.cc index bce9d06f..8de676a6 100644 --- a/src/factory/HttpTaskImpl.cc +++ b/src/factory/HttpTaskImpl.cc @@ -450,12 +450,12 @@ protected: virtual bool finish_once(); private: - struct ConnContext : public WFConnection + struct SSLConnection : public WFConnection { SSL *ssl_; SSLHandshaker handshaker_; SSLWrapper wrapper_; - ConnContext(SSL *ssl) : handshaker_(ssl), wrapper_(NULL, ssl) + SSLConnection(SSL *ssl) : handshaker_(ssl), wrapper_(NULL, ssl) { ssl_ = ssl; } @@ -463,7 +463,7 @@ private: SSLHandshaker *get_ssl_handshaker(); SSLWrapper *get_ssl_wrapper(ProtocolMessage *msg); - int set_ssl(); + int init_ssl_connection(); std::string proxy_auth_; ParsedURI user_uri_; @@ -478,39 +478,24 @@ WFConnection *ComplexHttpProxyTask::get_connection() const WFConnection *conn = this->WFComplexClientTask::get_connection(); if (conn && is_ssl_) - return (ConnContext *)conn->get_context(); + return (SSLConnection *)conn->get_context(); return conn; } -SSLHandshaker *ComplexHttpProxyTask::get_ssl_handshaker() +inline SSLHandshaker *ComplexHttpProxyTask::get_ssl_handshaker() { - WFConnection *conn = this->WFComplexClientTask::get_connection(); - - if (conn && is_ssl_) - return &((ConnContext *)conn->get_context())->handshaker_; - - return NULL; + return &((SSLConnection *)this->get_connection())->handshaker_; } -SSLWrapper *ComplexHttpProxyTask::get_ssl_wrapper(ProtocolMessage *msg) +inline SSLWrapper *ComplexHttpProxyTask::get_ssl_wrapper(ProtocolMessage *msg) { - WFConnection *conn = this->WFComplexClientTask::get_connection(); - ConnContext *context; - SSLWrapper *wrapper; - - if (conn && is_ssl_) - { - context = (ConnContext *)conn->get_context(); - wrapper = &context->wrapper_; - *wrapper = SSLWrapper(msg, context->ssl_); - return wrapper; - } - - return NULL; + SSLConnection *conn = (SSLConnection *)this->get_connection(); + conn->wrapper_ = SSLWrapper(msg, conn->ssl_); + return &conn->wrapper_; } -int ComplexHttpProxyTask::set_ssl() +int ComplexHttpProxyTask::init_ssl_connection() { SSL *ssl = __create_ssl(WFGlobal::get_ssl_client_ctx()); WFConnection *conn; @@ -522,15 +507,15 @@ int ComplexHttpProxyTask::set_ssl() SSL_set_connect_state(ssl); conn = this->WFComplexClientTask::get_connection(); - ConnContext *ctx = new ConnContext(ssl); + SSLConnection *ssl_conn = new SSLConnection(ssl); - auto&& deleter = [] (void *c) + auto&& deleter = [] (void *ctx) { - ConnContext *ctx = (ConnContext *)c; - SSL_free(ctx->ssl_); - delete ctx; + SSLConnection *ssl_conn = (SSLConnection *)ctx; + SSL_free(ssl_conn->ssl_); + delete ssl_conn; }; - conn->set_context(ctx, std::move(deleter)); + conn->set_context(ssl_conn, std::move(deleter)); return 0; } @@ -620,7 +605,7 @@ int ComplexHttpProxyTask::keep_alive_timeout() this->clear_resp(); - if (is_ssl_ && set_ssl() < 0) + if (is_ssl_ && init_ssl_connection() < 0) { state_ = WFT_STATE_SYS_ERROR; error_ = errno; From 21b047dd38820d4c1e8fb9c7ddf58fa93959e528 Mon Sep 17 00:00:00 2001 From: XieHan Date: Tue, 25 May 2021 19:21:48 +0800 Subject: [PATCH 2/2] simplify code --- src/factory/HttpTaskImpl.cc | 50 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/factory/HttpTaskImpl.cc b/src/factory/HttpTaskImpl.cc index 8de676a6..852a4ee5 100644 --- a/src/factory/HttpTaskImpl.cc +++ b/src/factory/HttpTaskImpl.cc @@ -439,7 +439,6 @@ public: void set_user_uri(ParsedURI&& uri) { user_uri_ = std::move(uri); } void set_user_uri(const ParsedURI& uri) { user_uri_ = uri; } - virtual WFConnection *get_connection() const; virtual const ParsedURI *get_current_uri() const { return &user_uri_; } protected: @@ -449,6 +448,17 @@ protected: virtual bool init_success(); virtual bool finish_once(); +protected: + virtual WFConnection *get_connection() const + { + WFConnection *conn = this->ComplexHttpTask::get_connection(); + + if (conn && is_ssl_) + return (SSLConnection *)conn->get_context(); + + return conn; + } + private: struct SSLConnection : public WFConnection { @@ -461,8 +471,18 @@ private: } }; - SSLHandshaker *get_ssl_handshaker(); - SSLWrapper *get_ssl_wrapper(ProtocolMessage *msg); + SSLHandshaker *get_ssl_handshaker() const + { + return &((SSLConnection *)this->get_connection())->handshaker_; + } + + SSLWrapper *get_ssl_wrapper(ProtocolMessage *msg) const + { + SSLConnection *conn = (SSLConnection *)this->get_connection(); + conn->wrapper_ = SSLWrapper(msg, conn->ssl_); + return &conn->wrapper_; + } + int init_ssl_connection(); std::string proxy_auth_; @@ -473,28 +493,6 @@ private: int error_; }; -WFConnection *ComplexHttpProxyTask::get_connection() const -{ - WFConnection *conn = this->WFComplexClientTask::get_connection(); - - if (conn && is_ssl_) - return (SSLConnection *)conn->get_context(); - - return conn; -} - -inline SSLHandshaker *ComplexHttpProxyTask::get_ssl_handshaker() -{ - return &((SSLConnection *)this->get_connection())->handshaker_; -} - -inline SSLWrapper *ComplexHttpProxyTask::get_ssl_wrapper(ProtocolMessage *msg) -{ - SSLConnection *conn = (SSLConnection *)this->get_connection(); - conn->wrapper_ = SSLWrapper(msg, conn->ssl_); - return &conn->wrapper_; -} - int ComplexHttpProxyTask::init_ssl_connection() { SSL *ssl = __create_ssl(WFGlobal::get_ssl_client_ctx()); @@ -506,7 +504,7 @@ int ComplexHttpProxyTask::init_ssl_connection() SSL_set_tlsext_host_name(ssl, user_uri_.host); SSL_set_connect_state(ssl); - conn = this->WFComplexClientTask::get_connection(); + conn = this->ComplexHttpTask::get_connection(); SSLConnection *ssl_conn = new SSLConnection(ssl); auto&& deleter = [] (void *ctx)