ithewei 5 ani în urmă
părinte
comite
04e5f4987d
2 a modificat fișierele cu 14 adăugiri și 7 ștergeri
  1. 13 6
      http/client/http_client.cpp
  2. 1 1
      http/client/requests.h

+ 13 - 6
http/client/http_client.cpp

@@ -456,12 +456,12 @@ struct HttpContext {
     HttpResponsePtr resp;
     HttpResponsePtr resp;
     HttpParserPtr   parser;
     HttpParserPtr   parser;
 
 
-    hio_t*          io;
-    htimer_t*       timer;
-
     HttpResponseCallback cb;
     HttpResponseCallback cb;
     void*                userdata;
     void*                userdata;
 
 
+    hio_t*          io;
+    htimer_t*       timer;
+
     HttpContext() {
     HttpContext() {
         io = NULL;
         io = NULL;
         timer = NULL;
         timer = NULL;
@@ -471,6 +471,8 @@ struct HttpContext {
 
 
     ~HttpContext() {
     ~HttpContext() {
         killTimer();
         killTimer();
+        // keep-alive
+        // closeIO();
     }
     }
 
 
     void closeIO() {
     void closeIO() {
@@ -509,10 +511,11 @@ struct HttpContext {
 static void on_close(hio_t* io) {
 static void on_close(hio_t* io) {
     HttpContext* ctx = (HttpContext*)hevent_userdata(io);
     HttpContext* ctx = (HttpContext*)hevent_userdata(io);
     if (ctx) {
     if (ctx) {
+        hevent_set_userdata(io, NULL);
+        ctx->io = NULL;
         int error = hio_error(io);
         int error = hio_error(io);
         ctx->callback(error);
         ctx->callback(error);
         delete ctx;
         delete ctx;
-        hevent_set_userdata(io, NULL);
     }
     }
 }
 }
 
 
@@ -549,7 +552,11 @@ static void on_connect(hio_t* io) {
 
 
 static void on_timeout(htimer_t* timer) {
 static void on_timeout(htimer_t* timer) {
     HttpContext* ctx = (HttpContext*)hevent_userdata(timer);
     HttpContext* ctx = (HttpContext*)hevent_userdata(timer);
-    ctx->errorCallback(ERR_TASK_TIMEOUT);
+    if (ctx) {
+        hevent_set_userdata(timer, NULL);
+        ctx->timer = NULL;
+        ctx->errorCallback(ERR_TASK_TIMEOUT);
+    }
 }
 }
 
 
 static HTHREAD_ROUTINE(http_client_loop_thread) {
 static HTHREAD_ROUTINE(http_client_loop_thread) {
@@ -600,11 +607,11 @@ static int __http_client_send_async(http_client_t* cli, HttpRequestPtr req, Http
     // new HttpContext
     // new HttpContext
     // delete on_close
     // delete on_close
     HttpContext* ctx = new HttpContext;
     HttpContext* ctx = new HttpContext;
-    ctx->io = connio;
     ctx->req = req;
     ctx->req = req;
     ctx->resp = resp;
     ctx->resp = resp;
     ctx->cb = cb;
     ctx->cb = cb;
     ctx->userdata = userdata;
     ctx->userdata = userdata;
+    ctx->io = connio;
     hevent_set_userdata(connio, ctx);
     hevent_set_userdata(connio, ctx);
 
 
     // timeout
     // timeout

+ 1 - 1
http/client/requests.h

@@ -84,7 +84,7 @@ Response patch(const char* url, const http_body& body = NoBody, const http_heade
     return request(HTTP_PATCH, url, body, headers);
     return request(HTTP_PATCH, url, body, headers);
 }
 }
 
 
-// delete is c++ keywrod, we have to replace delete with Delete.
+// delete is c++ keyword, we have to replace delete with Delete.
 Response Delete(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
 Response Delete(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
     return request(HTTP_DELETE, url, body, headers);
     return request(HTTP_DELETE, url, body, headers);
 }
 }