From 20df5fe845d6f46d8b9b762249ce8257aac47f05 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 30 Aug 2019 01:28:12 -0400
Subject: [PATCH] http: cleanup

---
 include/opendht/http.h | 10 +++++-----
 src/http.cpp           | 25 ++++++++++++-------------
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/include/opendht/http.h b/include/opendht/http.h
index 9888c8cf..edb97694 100644
--- a/include/opendht/http.h
+++ b/include/opendht/http.h
@@ -158,7 +158,7 @@ public:
     void add_callback(ResolverCb cb);
 
 private:
-    void resolve(const std::string host, const std::string service);
+    void resolve(const std::string& host, const std::string& service);
 
     std::mutex mutex_;
 
@@ -223,10 +223,10 @@ public:
      */
     void set_header(const restinio::http_request_header_t header);
     void set_method(const restinio::http_method_id_t method);
-    void set_target(const std::string target);
-    void set_header_field(const restinio::http_field_t field, const std::string& value);
+    void set_target(std::string target);
+    void set_header_field(const restinio::http_field_t field, std::string value);
     void set_connection_type(const restinio::http_connection_header_t connection);
-    void set_body(const std::string& body);
+    void set_body(std::string body);
 
     void add_on_status_callback(OnStatusCb cb);
     void add_on_body_callback(OnDataCb cb);
@@ -281,7 +281,7 @@ private:
      * Return how many bytes were parsed.
      * Note: we pass requerst.size()==0 to signal that EOF has been received.
      */
-    size_t parse_request(const std::string request);
+    size_t parse_request(const std::string& request);
 
     restinio::http_request_header_t header_;
     std::map<restinio::http_field_t, std::string> headers_;
diff --git a/src/http.cpp b/src/http.cpp
index ffae9845..729747a9 100644
--- a/src/http.cpp
+++ b/src/http.cpp
@@ -356,7 +356,7 @@ Resolver::add_callback(ResolverCb cb)
 }
 
 void
-Resolver::resolve(const std::string host, const std::string service)
+Resolver::resolve(const std::string& host, const std::string& service)
 {
     asio::ip::tcp::resolver::query query_(host, service);
 
@@ -486,15 +486,15 @@ Request::set_method(const restinio::http_method_id_t method)
 }
 
 void
-Request::set_target(const std::string target)
+Request::set_target(std::string target)
 {
-    header_.request_target(target);
+    header_.request_target(std::move(target));
 }
 
 void
-Request::set_header_field(const restinio::http_field_t field, const std::string& value)
+Request::set_header_field(const restinio::http_field_t field, std::string value)
 {
-    headers_[field] = value;
+    headers_[field] = std::move(value);
 }
 
 void
@@ -504,9 +504,9 @@ Request::set_connection_type(const restinio::http_connection_header_t connection
 }
 
 void
-Request::set_body(const std::string& body)
+Request::set_body(std::string body)
 {
-    body_ = body;
+    body_ = std::move(body);
 }
 
 void
@@ -600,7 +600,7 @@ Request::init_parser()
         };
         auto header_field = std::make_shared<std::string>("");
         auto on_header_field_cb = cbs_->on_header_field;
-        cbs_->on_header_field = [this, header_field, on_header_field_cb](const char* at, size_t length){
+        cbs_->on_header_field = [header_field, on_header_field_cb](const char* at, size_t length) {
             header_field->erase();
             auto field = std::string(at, length);
             header_field->append(field);
@@ -608,7 +608,7 @@ Request::init_parser()
                 on_header_field_cb(at, length);
         };
         auto on_header_value_cb = cbs_->on_header_value;
-        cbs_->on_header_value = [this, header_field, on_header_value_cb](const char* at, size_t length){
+        cbs_->on_header_value = [this, header_field, on_header_value_cb](const char* at, size_t length) {
             response_.headers[*header_field] = std::string(at, length);
             if (on_header_value_cb)
                 on_header_value_cb(at, length);
@@ -617,8 +617,7 @@ Request::init_parser()
             notify_state_change(State::HEADER_RECEIVED);
         };
         auto on_body_cb = cbs_->on_body;
-        cbs_->on_body = [this, on_body_cb](const char* at, size_t length){
-            auto content = std::string(at, length);
+        cbs_->on_body = [on_body_cb](const char* at, size_t length) {
             if (on_body_cb)
                 on_body_cb(at, length);
         };
@@ -863,7 +862,7 @@ Request::handle_response_header(const asio::error_code& ec)
         std::getline(is, response_.body);
         unsigned int content_length = atoi(content_length_it->second.c_str());
         // full body already in the header
-        if ((response_.body.size() + 1) == (content_length)){
+        if (response_.body.size() + 1 == content_length) {
             response_.body.append("\n");
             parse_request(response_.body);
             if (message_complete_.load())
@@ -941,7 +940,7 @@ Request::handle_response_body(const asio::error_code& ec, const size_t bytes)
 }
 
 size_t
-Request::parse_request(const std::string request)
+Request::parse_request(const std::string& request)
 {
     std::lock_guard<std::mutex> lock(cbs_mutex_);
     return http_parser_execute(parser_.get(), parser_s_.get(), request.c_str(), request.size());
-- 
GitLab