Browse Source

Fix readability warnings (#1722)

* Fix readability warnings

Did not fix readbility-qualified-auto, will do a separate pull request

* Revert changes where meaning is lost

* Revert some style changes
Jean-Francois Simoneau 2 years ago
parent
commit
115a786581
1 changed files with 43 additions and 40 deletions
  1. 43 40
      httplib.h

+ 43 - 40
httplib.h

@@ -868,15 +868,15 @@ private:
   bool routing(Request &req, Response &res, Stream &strm);
   bool routing(Request &req, Response &res, Stream &strm);
   bool handle_file_request(const Request &req, Response &res,
   bool handle_file_request(const Request &req, Response &res,
                            bool head = false);
                            bool head = false);
-  bool dispatch_request(Request &req, Response &res, const Handlers &handlers);
-  bool
-  dispatch_request_for_content_reader(Request &req, Response &res,
-                                      ContentReader content_reader,
-                                      const HandlersForContentReader &handlers);
+  bool dispatch_request(Request &req, Response &res,
+                        const Handlers &handlers) const;
+  bool dispatch_request_for_content_reader(
+      Request &req, Response &res, ContentReader content_reader,
+      const HandlersForContentReader &handlers) const;
 
 
-  bool parse_request_line(const char *s, Request &req);
+  bool parse_request_line(const char *s, Request &req) const;
   void apply_ranges(const Request &req, Response &res,
   void apply_ranges(const Request &req, Response &res,
-                    std::string &content_type, std::string &boundary);
+                    std::string &content_type, std::string &boundary) const;
   bool write_response(Stream &strm, bool close_connection, const Request &req,
   bool write_response(Stream &strm, bool close_connection, const Request &req,
                       Response &res);
                       Response &res);
   bool write_response_with_content(Stream &strm, bool close_connection,
   bool write_response_with_content(Stream &strm, bool close_connection,
@@ -896,7 +896,7 @@ private:
   bool read_content_core(Stream &strm, Request &req, Response &res,
   bool read_content_core(Stream &strm, Request &req, Response &res,
                          ContentReceiver receiver,
                          ContentReceiver receiver,
                          MultipartContentHeader multipart_header,
                          MultipartContentHeader multipart_header,
-                         ContentReceiver multipart_receiver);
+                         ContentReceiver multipart_receiver) const;
 
 
   virtual bool process_and_close_socket(socket_t sock);
   virtual bool process_and_close_socket(socket_t sock);
 
 
@@ -962,7 +962,7 @@ enum class Error {
   SSLPeerCouldBeClosed_,
   SSLPeerCouldBeClosed_,
 };
 };
 
 
-std::string to_string(const Error error);
+std::string to_string(Error error);
 
 
 std::ostream &operator<<(std::ostream &os, const Error &obj);
 std::ostream &operator<<(std::ostream &os, const Error &obj);
 
 
@@ -1220,7 +1220,7 @@ public:
   void set_ca_cert_path(const std::string &ca_cert_file_path,
   void set_ca_cert_path(const std::string &ca_cert_file_path,
                         const std::string &ca_cert_dir_path = std::string());
                         const std::string &ca_cert_dir_path = std::string());
   void set_ca_cert_store(X509_STORE *ca_cert_store);
   void set_ca_cert_store(X509_STORE *ca_cert_store);
-  X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size);
+  X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const;
 #endif
 #endif
 
 
 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -1249,14 +1249,14 @@ protected:
   // Also, shutdown_ssl and close_socket should also NOT be called concurrently
   // Also, shutdown_ssl and close_socket should also NOT be called concurrently
   // with a DIFFERENT thread sending requests using that socket.
   // with a DIFFERENT thread sending requests using that socket.
   virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully);
   virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully);
-  void shutdown_socket(Socket &socket);
+  void shutdown_socket(Socket &socket) const;
   void close_socket(Socket &socket);
   void close_socket(Socket &socket);
 
 
   bool process_request(Stream &strm, Request &req, Response &res,
   bool process_request(Stream &strm, Request &req, Response &res,
                        bool close_connection, Error &error);
                        bool close_connection, Error &error);
 
 
   bool write_content_with_provider(Stream &strm, const Request &req,
   bool write_content_with_provider(Stream &strm, const Request &req,
-                                   Error &error);
+                                   Error &error) const;
 
 
   void copy_settings(const ClientImpl &rhs);
   void copy_settings(const ClientImpl &rhs);
 
 
@@ -1347,7 +1347,8 @@ private:
   Result send_(Request &&req);
   Result send_(Request &&req);
 
 
   socket_t create_client_socket(Error &error) const;
   socket_t create_client_socket(Error &error) const;
-  bool read_response_line(Stream &strm, const Request &req, Response &res);
+  bool read_response_line(Stream &strm, const Request &req,
+                          Response &res) const;
   bool write_request(Stream &strm, Request &req, bool close_connection,
   bool write_request(Stream &strm, Request &req, bool close_connection,
                      Error &error);
                      Error &error);
   bool redirect(Request &req, Response &res, Error &error);
   bool redirect(Request &req, Response &res, Error &error);
@@ -1366,7 +1367,7 @@ private:
       const std::string &content_type);
       const std::string &content_type);
   ContentProviderWithoutLength get_multipart_content_provider(
   ContentProviderWithoutLength get_multipart_content_provider(
       const std::string &boundary, const MultipartFormDataItems &items,
       const std::string &boundary, const MultipartFormDataItems &items,
-      const MultipartFormDataProviderItems &provider_items);
+      const MultipartFormDataProviderItems &provider_items) const;
 
 
   std::string adjust_host_string(const std::string &host) const;
   std::string adjust_host_string(const std::string &host) const;
 
 
@@ -2964,7 +2965,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
 #ifndef _WIN32
 #ifndef _WIN32
   if (hints.ai_family == AF_UNIX) {
   if (hints.ai_family == AF_UNIX) {
     const auto addrlen = host.length();
     const auto addrlen = host.length();
-    if (addrlen > sizeof(sockaddr_un::sun_path)) return INVALID_SOCKET;
+    if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
 
 
     auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
     auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
     if (sock != INVALID_SOCKET) {
     if (sock != INVALID_SOCKET) {
@@ -3168,7 +3169,7 @@ inline socket_t create_client_socket(
 #ifdef USE_IF2IP
 #ifdef USE_IF2IP
           auto ip_from_if = if2ip(address_family, intf);
           auto ip_from_if = if2ip(address_family, intf);
           if (ip_from_if.empty()) { ip_from_if = intf; }
           if (ip_from_if.empty()) { ip_from_if = intf; }
-          if (!bind_ip_address(sock2, ip_from_if.c_str())) {
+          if (!bind_ip_address(sock2, ip_from_if)) {
             error = Error::BindIPAddress;
             error = Error::BindIPAddress;
             return false;
             return false;
           }
           }
@@ -3536,7 +3537,7 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
       }
       }
     }
     }
 
 
-    if (ret != Z_OK && ret != Z_STREAM_END) return false;
+    if (ret != Z_OK && ret != Z_STREAM_END) { return false; }
 
 
   } while (data_length > 0);
   } while (data_length > 0);
 
 
@@ -4149,7 +4150,7 @@ inline bool redirect(T &cli, Request &req, Response &res,
     req = new_req;
     req = new_req;
     res = new_res;
     res = new_res;
 
 
-    if (res.location.empty()) res.location = location;
+    if (res.location.empty()) { res.location = location; }
   }
   }
   return ret;
   return ret;
 }
 }
@@ -4236,7 +4237,7 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try {
     auto len = static_cast<size_t>(m.length(1));
     auto len = static_cast<size_t>(m.length(1));
     auto all_valid_ranges = true;
     auto all_valid_ranges = true;
     split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
     split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
-      if (!all_valid_ranges) return;
+      if (!all_valid_ranges) { return; }
       static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))");
       static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))");
       std::cmatch cm;
       std::cmatch cm;
       if (std::regex_match(b, e, cm, re_another_range)) {
       if (std::regex_match(b, e, cm, re_another_range)) {
@@ -4587,7 +4588,7 @@ serialize_multipart_formdata(const MultipartFormDataItems &items,
     body += item.content + serialize_multipart_formdata_item_end();
     body += item.content + serialize_multipart_formdata_item_end();
   }
   }
 
 
-  if (finish) body += serialize_multipart_formdata_finish(boundary);
+  if (finish) { body += serialize_multipart_formdata_finish(boundary); }
 
 
   return body;
   return body;
 }
 }
@@ -5722,8 +5723,7 @@ inline Server &Server::set_payload_max_length(size_t length) {
 
 
 inline bool Server::bind_to_port(const std::string &host, int port,
 inline bool Server::bind_to_port(const std::string &host, int port,
                                  int socket_flags) {
                                  int socket_flags) {
-  if (bind_internal(host, port, socket_flags) < 0) return false;
-  return true;
+  return bind_internal(host, port, socket_flags) >= 0;
 }
 }
 inline int Server::bind_to_any_port(const std::string &host, int socket_flags) {
 inline int Server::bind_to_any_port(const std::string &host, int socket_flags) {
   return bind_internal(host, 0, socket_flags);
   return bind_internal(host, 0, socket_flags);
@@ -5757,7 +5757,7 @@ inline void Server::stop() {
   }
   }
 }
 }
 
 
-inline bool Server::parse_request_line(const char *s, Request &req) {
+inline bool Server::parse_request_line(const char *s, Request &req) const {
   auto len = strlen(s);
   auto len = strlen(s);
   if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; }
   if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; }
   len -= 2;
   len -= 2;
@@ -6008,10 +6008,11 @@ inline bool Server::read_content_with_content_receiver(
                            std::move(multipart_receiver));
                            std::move(multipart_receiver));
 }
 }
 
 
-inline bool Server::read_content_core(Stream &strm, Request &req, Response &res,
-                                      ContentReceiver receiver,
-                                      MultipartContentHeader multipart_header,
-                                      ContentReceiver multipart_receiver) {
+inline bool
+Server::read_content_core(Stream &strm, Request &req, Response &res,
+                          ContentReceiver receiver,
+                          MultipartContentHeader multipart_header,
+                          ContentReceiver multipart_receiver) const {
   detail::MultipartFormDataParser multipart_form_data_parser;
   detail::MultipartFormDataParser multipart_form_data_parser;
   ContentReceiverWithProgress out;
   ContentReceiverWithProgress out;
 
 
@@ -6302,7 +6303,7 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
 }
 }
 
 
 inline bool Server::dispatch_request(Request &req, Response &res,
 inline bool Server::dispatch_request(Request &req, Response &res,
-                                     const Handlers &handlers) {
+                                     const Handlers &handlers) const {
   for (const auto &x : handlers) {
   for (const auto &x : handlers) {
     const auto &matcher = x.first;
     const auto &matcher = x.first;
     const auto &handler = x.second;
     const auto &handler = x.second;
@@ -6317,7 +6318,7 @@ inline bool Server::dispatch_request(Request &req, Response &res,
 
 
 inline void Server::apply_ranges(const Request &req, Response &res,
 inline void Server::apply_ranges(const Request &req, Response &res,
                                  std::string &content_type,
                                  std::string &content_type,
-                                 std::string &boundary) {
+                                 std::string &boundary) const {
   if (req.ranges.size() > 1) {
   if (req.ranges.size() > 1) {
     boundary = detail::make_multipart_data_boundary();
     boundary = detail::make_multipart_data_boundary();
 
 
@@ -6429,7 +6430,7 @@ inline void Server::apply_ranges(const Request &req, Response &res,
 
 
 inline bool Server::dispatch_request_for_content_reader(
 inline bool Server::dispatch_request_for_content_reader(
     Request &req, Response &res, ContentReader content_reader,
     Request &req, Response &res, ContentReader content_reader,
-    const HandlersForContentReader &handlers) {
+    const HandlersForContentReader &handlers) const {
   for (const auto &x : handlers) {
   for (const auto &x : handlers) {
     const auto &matcher = x.first;
     const auto &matcher = x.first;
     const auto &handler = x.second;
     const auto &handler = x.second;
@@ -6671,7 +6672,7 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const {
   // Check is custom IP specified for host_
   // Check is custom IP specified for host_
   std::string ip;
   std::string ip;
   auto it = addr_map_.find(host_);
   auto it = addr_map_.find(host_);
-  if (it != addr_map_.end()) ip = it->second;
+  if (it != addr_map_.end()) { ip = it->second; }
 
 
   return detail::create_client_socket(
   return detail::create_client_socket(
       host_, ip, port_, address_family_, tcp_nodelay_, socket_options_,
       host_, ip, port_, address_family_, tcp_nodelay_, socket_options_,
@@ -6696,7 +6697,7 @@ inline void ClientImpl::shutdown_ssl(Socket & /*socket*/,
          socket_requests_are_from_thread_ == std::this_thread::get_id());
          socket_requests_are_from_thread_ == std::this_thread::get_id());
 }
 }
 
 
-inline void ClientImpl::shutdown_socket(Socket &socket) {
+inline void ClientImpl::shutdown_socket(Socket &socket) const {
   if (socket.sock == INVALID_SOCKET) { return; }
   if (socket.sock == INVALID_SOCKET) { return; }
   detail::shutdown_socket(socket.sock);
   detail::shutdown_socket(socket.sock);
 }
 }
@@ -6721,7 +6722,7 @@ inline void ClientImpl::close_socket(Socket &socket) {
 }
 }
 
 
 inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
 inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
-                                           Response &res) {
+                                           Response &res) const {
   std::array<char, 2048> buf{};
   std::array<char, 2048> buf{};
 
 
   detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
   detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
@@ -7002,7 +7003,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
 
 
 inline bool ClientImpl::write_content_with_provider(Stream &strm,
 inline bool ClientImpl::write_content_with_provider(Stream &strm,
                                                     const Request &req,
                                                     const Request &req,
-                                                    Error &error) {
+                                                    Error &error) const {
   auto is_shutting_down = []() { return false; };
   auto is_shutting_down = []() { return false; };
 
 
   if (req.is_chunked_content_provider_) {
   if (req.is_chunked_content_provider_) {
@@ -7328,13 +7329,14 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
 
 
 inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
 inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
     const std::string &boundary, const MultipartFormDataItems &items,
     const std::string &boundary, const MultipartFormDataItems &items,
-    const MultipartFormDataProviderItems &provider_items) {
-  size_t cur_item = 0, cur_start = 0;
+    const MultipartFormDataProviderItems &provider_items) const {
+  size_t cur_item = 0;
+  size_t cur_start = 0;
   // cur_item and cur_start are copied to within the std::function and maintain
   // cur_item and cur_start are copied to within the std::function and maintain
   // state between successive calls
   // state between successive calls
   return [&, cur_item, cur_start](size_t offset,
   return [&, cur_item, cur_start](size_t offset,
                                   DataSink &sink) mutable -> bool {
                                   DataSink &sink) mutable -> bool {
-    if (!offset && items.size()) {
+    if (!offset && !items.empty()) {
       sink.os << detail::serialize_multipart_formdata(items, boundary, false);
       sink.os << detail::serialize_multipart_formdata(items, boundary, false);
       return true;
       return true;
     } else if (cur_item < provider_items.size()) {
     } else if (cur_item < provider_items.size()) {
@@ -7351,8 +7353,9 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
       cur_sink.write = sink.write;
       cur_sink.write = sink.write;
       cur_sink.done = [&]() { has_data = false; };
       cur_sink.done = [&]() { has_data = false; };
 
 
-      if (!provider_items[cur_item].provider(offset - cur_start, cur_sink))
+      if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) {
         return false;
         return false;
+      }
 
 
       if (!has_data) {
       if (!has_data) {
         sink.os << detail::serialize_multipart_formdata_item_end();
         sink.os << detail::serialize_multipart_formdata_item_end();
@@ -7989,9 +7992,9 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) {
 }
 }
 
 
 inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
 inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
-                                                    std::size_t size) {
+                                                    std::size_t size) const {
   auto mem = BIO_new_mem_buf(ca_cert, static_cast<int>(size));
   auto mem = BIO_new_mem_buf(ca_cert, static_cast<int>(size));
-  if (!mem) return nullptr;
+  if (!mem) { return nullptr; }
 
 
   auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr);
   auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr);
   if (!inf) {
   if (!inf) {