Browse Source

Add HttpMessage::head_cb

ithewei 4 years ago
parent
commit
11b223c316
6 changed files with 15 additions and 5 deletions
  1. 4 4
      base/hbase.c
  2. 3 0
      http/Http1Parser.cpp
  3. 4 0
      http/HttpMessage.h
  4. 2 1
      http/README.md
  5. 1 0
      http/client/AsyncHttpClient.cpp
  6. 1 0
      http/client/http_client.cpp

+ 4 - 4
base/hbase.c

@@ -191,7 +191,7 @@ int hv_mkdir_p(const char* dir) {
     if (access(dir, 0) == 0) {
         return EEXIST;
     }
-    char tmp[MAX_PATH];
+    char tmp[MAX_PATH] = {0};
     safe_strncpy(tmp, dir, sizeof(tmp));
     char* p = tmp;
     char delim = '/';
@@ -221,7 +221,7 @@ int hv_rmdir_p(const char* dir) {
     if (rmdir(dir) != 0) {
         return EPERM;
     }
-    char tmp[MAX_PATH];
+    char tmp[MAX_PATH] = {0};
     safe_strncpy(tmp, dir, sizeof(tmp));
     char* p = tmp;
     while (*p) ++p;
@@ -300,7 +300,7 @@ char* get_executable_path(char* buf, int size) {
 }
 
 char* get_executable_dir(char* buf, int size) {
-    char filepath[MAX_PATH];
+    char filepath[MAX_PATH] = {0};
     get_executable_path(filepath, sizeof(filepath));
     char* pos = strrchr_dir(filepath);
     if (pos) {
@@ -311,7 +311,7 @@ char* get_executable_dir(char* buf, int size) {
 }
 
 char* get_executable_file(char* buf, int size) {
-    char filepath[MAX_PATH];
+    char filepath[MAX_PATH] = {0};
     get_executable_path(filepath, sizeof(filepath));
     char* pos = strrchr_dir(filepath);
     if (pos) {

+ 3 - 0
http/Http1Parser.cpp

@@ -129,6 +129,9 @@ int on_headers_complete(http_parser* parser) {
         }
     }
     hp->state = HP_HEADERS_COMPLETE;
+    if (hp->parsed->head_cb) {
+        hp->parsed->head_cb(hp->parsed->headers);
+    }
     return skip_body ? 1 : 0;
 }
 

+ 4 - 0
http/HttpMessage.h

@@ -72,6 +72,8 @@ struct HV_EXPORT HttpCookie {
 typedef std::map<std::string, std::string, hv::StringCaseLess>  http_headers;
 typedef std::vector<HttpCookie>                                 http_cookies;
 typedef std::string                                             http_body;
+
+typedef std::function<void(const http_headers& headers)>        http_head_cb;
 typedef std::function<void(const char* data, size_t size)>      http_body_cb;
 typedef std::function<void(const char* data, size_t size)>      http_chunked_cb;
 
@@ -85,6 +87,8 @@ public:
     http_headers        headers;
     http_cookies        cookies;
     http_body           body;
+
+    http_head_cb        head_cb;
     http_body_cb        body_cb;
     http_chunked_cb     chunked_cb; // Transfer-Encoding: chunked
 

+ 2 - 1
http/README.md

@@ -4,7 +4,8 @@
 .
 ├── client
 │   ├── http_client.h   http客户端对外头文件
-│   └── requests.h      模拟python requests api
+│   ├── requests.h      模拟python requests api
+│   └── axios.h         模拟nodejs axios api
 ├── httpdef.h           http定义
 ├── http2def.h          http2定义
 ├── grpcdef.h           grpc定义

+ 1 - 0
http/client/AsyncHttpClient.cpp

@@ -143,6 +143,7 @@ int AsyncHttpClient::sendRequest(const SocketChannelPtr& channel) {
         resp = new HttpResponse;
         ctx->resp.reset(resp);
     }
+    if (req->head_cb)    resp->head_cb = std::move(req->head_cb);
     if (req->body_cb)    resp->body_cb = std::move(req->body_cb);
     if (req->chunked_cb) resp->chunked_cb = std::move(req->chunked_cb);
 

+ 1 - 0
http/client/http_client.cpp

@@ -150,6 +150,7 @@ int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
         }
     }
 
+    if (req->head_cb)    resp->head_cb = std::move(req->head_cb);
     if (req->body_cb)    resp->body_cb = std::move(req->body_cb);
     if (req->chunked_cb) resp->chunked_cb = std::move(req->chunked_cb);