hewei.it 5 gadi atpakaļ
vecāks
revīzija
0a0ea2e858
4 mainītis faili ar 41 papildinājumiem un 28 dzēšanām
  1. 8 8
      README.md
  2. 9 4
      examples/http_client_test.cpp
  3. 15 7
      http/client/http_client.cpp
  4. 9 9
      readme_cn.md

+ 8 - 8
README.md

@@ -37,7 +37,7 @@ but simpler api and richer protocols.
 
 ### HTTP
 #### http server
-see `examples/http_server_test.cpp`
+see [examples/http_server_test.cpp](examples/http_server_test.cpp)
 ```c++
 #include "HttpServer.h"
 
@@ -62,7 +62,7 @@ int main() {
 }
 ```
 #### http client
-see `examples/http_client_test.cpp`
+see [examples/http_client_test.cpp](examples/http_client_test.cpp)
 ```c++
 #include "requests.h"
 
@@ -75,12 +75,12 @@ int main() {
         printf("%s\n", resp->body.c_str());
     }
 
-    auto resp2 = requests::post("127.0.0.1:8080/echo", "hello,world!");
-    if (resp2 == NULL) {
+    auto resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
+    if (resp == NULL) {
         printf("request failed!\n");
     } else {
-        printf("%d %s\r\n", resp2->status_code, resp2->status_message());
-        printf("%s\n", resp2->body.c_str());
+        printf("%d %s\r\n", resp->status_code, resp->status_message());
+        printf("%s\n", resp->body.c_str());
     }
 
     return 0;
@@ -127,7 +127,7 @@ bin/webbench -c 2 -t 60 localhost:8080
 ![libhv-vs-nginx.png](html/downloads/libhv-vs-nginx.png)
 
 ### EventLoop
-see `examples/tcp.c` `examples/udp.c`
+see [examples/tcp.c](examples/tcp.c) [examples/udp.c](examples/udp.c) [examples/nc.c](examples/nc.c)
 ```c
 // TCP echo server
 #include "hloop.h"
@@ -176,7 +176,7 @@ bin/nc 127.0.0.1 10514
 ```
 
 ## BUILD
-see BUILD.md
+see [BUILD.md](BUILD.md)
 
 ### lib
 - make libhv

+ 9 - 4
examples/http_client_test.cpp

@@ -32,6 +32,11 @@ static void test_http_async_client(int* finished) {
 }
 
 static void test_http_sync_client() {
+    // auto resp = requests::get("http://www.example.com");
+    //
+    // make clean && make WITH_OPENSSL=yes
+    // auto resp = requests::get("https://www.baidu.com");
+
     auto resp = requests::get("http://127.0.0.1:8080/ping");
     if (resp == NULL) {
         printf("request failed!\n");
@@ -40,12 +45,12 @@ static void test_http_sync_client() {
         printf("%s\n", resp->body.c_str());
     }
 
-    auto resp2 = requests::post("127.0.0.1:8080/echo", "hello,world!");
-    if (resp2 == NULL) {
+    resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
+    if (resp == NULL) {
         printf("request failed!\n");
     } else {
-        printf("%d %s\r\n", resp2->status_code, resp2->status_message());
-        printf("%s\n", resp2->body.c_str());
+        printf("%d %s\r\n", resp->status_code, resp->status_message());
+        printf("%s\n", resp->body.c_str());
     }
 }
 

+ 15 - 7
http/client/http_client.cpp

@@ -152,6 +152,10 @@ int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
 int http_client_send(HttpRequest* req, HttpResponse* resp) {
     if (!req || !resp) return ERR_NULL_POINTER;
 
+    if (req->timeout == 0) {
+        req->timeout = DEFAULT_HTTP_TIMEOUT;
+    }
+
     http_client_t cli;
     return __http_client_send(&cli, req, resp);
 }
@@ -275,8 +279,8 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
         curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req->body.size());
     }
 
-    if (cli->timeout > 0) {
-        curl_easy_setopt(curl, CURLOPT_TIMEOUT, cli->timeout);
+    if (req->timeout > 0) {
+        curl_easy_setopt(curl, CURLOPT_TIMEOUT, req->timeout);
     }
 
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, s_body_cb);
@@ -330,7 +334,7 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
     }
     tcp_nodelay(connfd, 1);
 
-    if (cli->https) {
+    if (req->https) {
         if (hssl_ctx_instance() == NULL) {
             hssl_ctx_init(NULL);
         }
@@ -361,7 +365,7 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
 int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
     // connect -> send -> recv -> http_parser
     int err = 0;
-    int timeout = cli->timeout;
+    int timeout = req->timeout;
     int connfd = cli->fd;
 
     time_t start_time = time(NULL);
@@ -393,7 +397,7 @@ send:
                 }
                 so_sndtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
             }
-            if (cli->https) {
+            if (req->https) {
                 nsend = hssl_write(cli->ssl, data+total_nsend, len-total_nsend);
             }
             else {
@@ -425,7 +429,7 @@ recv:
             }
             so_rcvtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
         }
-        if (cli->https) {
+        if (req->https) {
             nrecv = hssl_read(cli->ssl, recvbuf, sizeof(recvbuf));
         }
         else {
@@ -604,7 +608,7 @@ static int __http_client_send_async(http_client_t* cli, HttpRequestPtr req, Http
     hevent_set_userdata(connio, ctx);
 
     // timeout
-    if (req->timeout != 0) {
+    if (req->timeout > 0) {
         ctx->timer = htimer_add(cli->loop_, on_timeout, req->timeout * 1000, 1);
         assert(ctx->timer != NULL);
         hevent_set_userdata(ctx->timer, ctx);
@@ -640,6 +644,10 @@ int http_client_send_async(HttpRequestPtr req, HttpResponsePtr resp,
         HttpResponseCallback cb, void* userdata) {
     if (!req || !resp) return ERR_NULL_POINTER;
 
+    if (req->timeout == 0) {
+        req->timeout = DEFAULT_HTTP_TIMEOUT;
+    }
+
     static http_client_t s_default_async_client;
     return __http_client_send_async(&s_default_async_client, req, resp, cb, userdata);
 }

+ 9 - 9
readme_cn.md

@@ -35,7 +35,7 @@
 
 ### HTTP
 #### http server
-see `examples/http_server_test.cpp`
+见[examples/http_server_test.cpp](examples/http_server_test.cpp)
 ```c++
 #include "HttpServer.h"
 
@@ -60,7 +60,7 @@ int main() {
 }
 ```
 #### http client
-see `examples/http_client_test.cpp`
+见[examples/http_client_test.cpp](examples/http_client_test.cpp)
 ```c++
 #include "requests.h"
 
@@ -73,12 +73,12 @@ int main() {
         printf("%s\n", resp->body.c_str());
     }
 
-    auto resp2 = requests::post("127.0.0.1:8080/echo", "hello,world!");
-    if (resp2 == NULL) {
+    resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
+    if (resp == NULL) {
         printf("request failed!\n");
     } else {
-        printf("%d %s\r\n", resp2->status_code, resp2->status_message());
-        printf("%s\n", resp2->body.c_str());
+        printf("%d %s\r\n", resp->status_code, resp->status_message());
+        printf("%s\n", resp->body.c_str());
     }
 
     return 0;
@@ -125,8 +125,8 @@ bin/webbench -c 2 -t 60 localhost:8080
 ![libhv-vs-nginx.png](html/downloads/libhv-vs-nginx.png)
 
 ### EventLoop
-see `examples/tcp.c` `examples/udp.c`
-```c
+见[examples/tcp.c](examples/tcp.c) [examples/udp.c](examples/udp.c) [examples/nc.c](examples/nc.c)
+```c++
 // TCP echo server
 #include "hloop.h"
 
@@ -174,7 +174,7 @@ bin/nc 127.0.0.1 10514
 ```
 
 ## 构建
-见BUILD.md
+见[BUILD.md](BUILD.md)
 
 ### 库
 - make libhv