yhirose 8 years ago
parent
commit
ddb454da1b
1 changed files with 12 additions and 0 deletions
  1. 12 0
      httplib.h

+ 12 - 0
httplib.h

@@ -176,6 +176,7 @@ protected:
 
 
 private:
 private:
     bool read_response_line(Stream& strm, Response& res);
     bool read_response_line(Stream& strm, Response& res);
+    void add_default_headers(Request& req);
 
 
     virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res);
     virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res);
 
 
@@ -1078,11 +1079,19 @@ inline bool Client::read_and_close_socket(socket_t sock, const Request& req, Res
     });
     });
 }
 }
 
 
+inline void Client::add_default_headers(Request& req)
+{
+    req.set_header("Host", host_.c_str());
+    req.set_header("Accept", "*/*");
+    req.set_header("User-Agent", "cpp-httplib/0.1");
+}
+
 inline std::shared_ptr<Response> Client::get(const char* path)
 inline std::shared_ptr<Response> Client::get(const char* path)
 {
 {
     Request req;
     Request req;
     req.method = "GET";
     req.method = "GET";
     req.path = path;
     req.path = path;
+    add_default_headers(req);
 
 
     auto res = std::make_shared<Response>();
     auto res = std::make_shared<Response>();
 
 
@@ -1094,6 +1103,7 @@ inline std::shared_ptr<Response> Client::head(const char* path)
     Request req;
     Request req;
     req.method = "HEAD";
     req.method = "HEAD";
     req.path = path;
     req.path = path;
+    add_default_headers(req);
 
 
     auto res = std::make_shared<Response>();
     auto res = std::make_shared<Response>();
 
 
@@ -1106,6 +1116,8 @@ inline std::shared_ptr<Response> Client::post(
     Request req;
     Request req;
     req.method = "POST";
     req.method = "POST";
     req.path = path;
     req.path = path;
+    add_default_headers(req);
+
     req.set_header("Content-Type", content_type);
     req.set_header("Content-Type", content_type);
     req.body = body;
     req.body = body;