@@ -69,20 +69,20 @@ class coro_http_server {
6969#endif
7070
7171 // only call once, not thread safe.
72- std::errc sync_start () noexcept {
72+ std::error_code sync_start () noexcept {
7373 auto ret = async_start ();
7474 ret.wait ();
7575 return ret.value ();
7676 }
7777
7878 // only call once, not thread safe.
79- async_simple::Future<std::errc > async_start () {
79+ async_simple::Future<std::error_code > async_start () {
8080 errc_ = listen ();
8181
82- async_simple::Promise<std::errc > promise;
82+ async_simple::Promise<std::error_code > promise;
8383 auto future = promise.getFuture ();
8484
85- if (errc_ == std::errc{} ) {
85+ if (! errc_) {
8686 if (out_ctx_ == nullptr ) {
8787 thd_ = std::thread ([this ] {
8888 pool_->run ();
@@ -91,7 +91,7 @@ class coro_http_server {
9191
9292 accept ().start ([p = std::move (promise), this ](auto &&res) mutable {
9393 if (res.hasError ()) {
94- errc_ = std::errc::io_error;
94+ errc_ = std::make_error_code (std:: errc::io_error) ;
9595 p.setValue (errc_);
9696 }
9797 else {
@@ -196,16 +196,14 @@ class coro_http_server {
196196 coro_io::channel<coro_http_client>::create (hosts, {.lba = type},
197197 weights));
198198 auto handler =
199- [this , channel, type, url_path ](
199+ [this , channel, type](
200200 coro_http_request &req,
201201 coro_http_response &response) -> async_simple::coro::Lazy<void > {
202202 co_await channel->send_request (
203203 [this , &req, &response](
204204 coro_http_client &client,
205205 std::string_view host) -> async_simple::coro::Lazy<void > {
206- uri_t uri;
207- uri.parse_from (host.data ());
208- co_await reply (client, uri.get_path (), req, response);
206+ co_await reply (client, host, req, response);
209207 });
210208 };
211209
@@ -503,10 +501,10 @@ class coro_http_server {
503501 }
504502
505503 std::string_view address () { return address_; }
506- std::errc get_errc () { return errc_; }
504+ std::error_code get_errc () { return errc_; }
507505
508506 private:
509- std::errc listen () {
507+ std::error_code listen () {
510508 CINATRA_LOG_INFO << " begin to listen" ;
511509 using asio::ip::tcp;
512510 asio::error_code ec;
@@ -519,25 +517,29 @@ class coro_http_server {
519517 if (ec || it == it_end) {
520518 CINATRA_LOG_ERROR << " bad address: " << address_
521519 << " error: " << ec.message ();
522- return std::errc::bad_address;
520+ if (ec) {
521+ return ec;
522+ }
523+ return std::make_error_code (std::errc::address_not_available);
523524 }
524525
525526 auto endpoint = it->endpoint ();
526527 acceptor_.open (endpoint.protocol (), ec);
527528 if (ec) {
528529 CINATRA_LOG_ERROR << " acceptor open failed"
529530 << " error: " << ec.message ();
530- return std::errc::io_error ;
531+ return ec ;
531532 }
532533#ifdef __GNUC__
533534 acceptor_.set_option (tcp::acceptor::reuse_address (true ), ec);
534535#endif
535536 acceptor_.bind (endpoint, ec);
536537 if (ec) {
537538 CINATRA_LOG_ERROR << " bind port: " << port_ << " error: " << ec.message ();
538- acceptor_.cancel (ec);
539- acceptor_.close (ec);
540- return std::errc::address_in_use;
539+ std::error_code ignore_ec;
540+ acceptor_.cancel (ignore_ec);
541+ acceptor_.close (ignore_ec);
542+ return ec;
541543 }
542544#ifdef _MSC_VER
543545 acceptor_.set_option (tcp::acceptor::reuse_address (true ));
@@ -546,22 +548,22 @@ class coro_http_server {
546548 if (ec) {
547549 CINATRA_LOG_ERROR << " get local endpoint port: " << port_
548550 << " listen error: " << ec.message ();
549- return std::errc::io_error ;
551+ return ec ;
550552 }
551553
552554 auto end_point = acceptor_.local_endpoint (ec);
553555 if (ec) {
554556 CINATRA_LOG_ERROR << " get local endpoint port: " << port_
555557 << " error: " << ec.message ();
556- return std::errc::address_in_use ;
558+ return ec ;
557559 }
558560 port_ = end_point.port ();
559561
560562 CINATRA_LOG_INFO << " listen port " << port_ << " successfully" ;
561563 return {};
562564 }
563565
564- async_simple::coro::Lazy<std::errc > accept () {
566+ async_simple::coro::Lazy<std::error_code > accept () {
565567 for (;;) {
566568 coro_io::ExecutorWrapper<> *executor;
567569 if (out_ctx_ == nullptr ) {
@@ -580,7 +582,7 @@ class coro_http_server {
580582 if (error == asio::error::operation_aborted ||
581583 error == asio::error::bad_descriptor) {
582584 acceptor_close_waiter_.set_value ();
583- co_return std::errc::operation_canceled ;
585+ co_return error ;
584586 }
585587 continue ;
586588 }
@@ -765,17 +767,28 @@ class coro_http_server {
765767 }
766768
767769 async_simple::coro::Lazy<void > reply (coro_http_client &client,
768- std::string url_path ,
770+ std::string_view host ,
769771 coro_http_request &req,
770772 coro_http_response &response) {
773+ uri_t uri;
774+ std::string proxy_host;
775+
776+ if (host.find (" //" ) == std::string_view::npos) {
777+ proxy_host.append (" http://" ).append (host);
778+ uri.parse_from (proxy_host.data ());
779+ }
780+ else {
781+ uri.parse_from (host.data ());
782+ }
771783 std::unordered_map<std::string, std::string> req_headers;
772- for (auto &[k, v] : req_headers ) {
784+ for (auto &[k, v] : req. get_headers () ) {
773785 req_headers.emplace (k, v);
774786 }
787+ req_headers[" Host" ] = uri.host ;
775788
776789 auto ctx = req_context<std::string_view>{.content = req.get_body ()};
777790 auto result = co_await client.async_request (
778- std::move (url_path ), method_type (req.get_method ()), std::move (ctx),
791+ req. full_url ( ), method_type (req.get_method ()), std::move (ctx),
779792 std::move (req_headers));
780793
781794 for (auto &[k, v] : result.resp_headers ) {
@@ -789,6 +802,8 @@ class coro_http_server {
789802 }
790803
791804 void init_address (std::string address) {
805+ CINATRA_LOG_ERROR << " init log" ; // init easylog singleton to make sure
806+ // server destruct before easylog.
792807 if (size_t pos = address.find (' :' ); pos != std::string::npos) {
793808 auto port_sv = std::string_view (address).substr (pos + 1 );
794809
@@ -813,7 +828,7 @@ class coro_http_server {
813828 std::unique_ptr<coro_io::ExecutorWrapper<>> out_executor_ = nullptr ;
814829 uint16_t port_;
815830 std::string address_;
816- std::errc errc_ = {};
831+ std::error_code errc_ = {};
817832 asio::ip::tcp::acceptor acceptor_;
818833 std::thread thd_;
819834 std::promise<void > acceptor_close_waiter_;
0 commit comments