Kaynağa Gözat

fix #124: add HttpRequest::isHttps

hewei.it 4 yıl önce
ebeveyn
işleme
98cfcad0f8

+ 19 - 20
examples/http_client_test.cpp

@@ -9,18 +9,19 @@
 
 #include "requests.h"
 #include "axios.h"
+using namespace hv;
 
 #include "hthread.h" // import hv_gettid
 
-static void test_http_async_client(http_client_t* cli, int* finished) {
+static void test_http_async_client(HttpClient* cli, int* resp_cnt) {
     printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
     HttpRequestPtr req(new HttpRequest);
     req->method = HTTP_POST;
-    req->url = "127.0.0.1:8080/echo";
+    req->url = "http://127.0.0.1:8080/echo";
     req->headers["Connection"] = "keep-alive";
     req->body = "This is an async request.";
     req->timeout = 10;
-    http_client_send_async(cli, req, [finished](const HttpResponsePtr& resp) {
+    cli->sendAsync(req, [resp_cnt](const HttpResponsePtr& resp) {
         printf("test_http_async_client response thread tid=%ld\n", hv_gettid());
         if (resp == NULL) {
             printf("request failed!\n");
@@ -28,19 +29,19 @@ static void test_http_async_client(http_client_t* cli, int* finished) {
             printf("%d %s\r\n", resp->status_code, resp->status_message());
             printf("%s\n", resp->body.c_str());
         }
-        *finished = 1;
+        *resp_cnt += 1;
     });
 }
 
-static void test_http_sync_client(http_client_t* cli) {
+static void test_http_sync_client(HttpClient* cli) {
     HttpRequest req;
     req.method = HTTP_POST;
-    req.url = "127.0.0.1:8080/echo";
+    req.url = "http://127.0.0.1:8080/echo";
     req.headers["Connection"] = "keep-alive";
     req.body = "This is a sync request.";
     req.timeout = 10;
     HttpResponse resp;
-    int ret = http_client_send(cli, &req, &resp);
+    int ret = cli->send(&req, &resp);
     if (ret != 0) {
         printf("request failed!\n");
     } else {
@@ -69,7 +70,7 @@ static void test_requests() {
     jroot["pswd"] = "123456";
     http_headers headers;
     headers["Content-Type"] = "application/json";
-    resp = requests::post("127.0.0.1:8080/echo", jroot.dump(), headers);
+    resp = requests::post("http://127.0.0.1:8080/echo", jroot.dump(), headers);
     if (resp == NULL) {
         printf("request failed!\n");
     } else {
@@ -158,29 +159,27 @@ static void test_axios() {
 }
 
 int main(int argc, char* argv[]) {
-    int cnt = 0;
-    if (argc > 1) cnt = atoi(argv[1]);
-    if (cnt == 0) cnt = 1;
+    int req_cnt = 0;
+    if (argc > 1) req_cnt = atoi(argv[1]);
+    if (req_cnt == 0) req_cnt = 1;
 
-    http_client_t* sync_client = http_client_new();
-    http_client_t* async_client = http_client_new();
-    int finished = 0;
+    HttpClient sync_client;
+    HttpClient async_client;
+    int resp_cnt = 0;
 
-    for (int i = 0; i < cnt; ++i) {
-        test_http_async_client(async_client, &finished);
+    for (int i = 0; i < req_cnt; ++i) {
+        test_http_async_client(&async_client, &resp_cnt);
 
-        test_http_sync_client(sync_client);
+        test_http_sync_client(&sync_client);
 
         test_requests();
 
         test_axios();
     }
 
-    http_client_del(sync_client);
     // demo wait async finished
-    while (!finished) hv_delay(100);
+    while (resp_cnt < req_cnt) hv_delay(100);
     printf("finished!\n");
-    http_client_del(async_client);
 
     return 0;
 }

+ 6 - 0
http/HttpMessage.h

@@ -418,6 +418,12 @@ public:
         return http_method_str(method);
     }
 
+    // scheme
+    bool isHttps() {
+        return strncmp(scheme.c_str(), "https", 5) == 0 ||
+               strncmp(url.c_str(), "https://", 8) == 0;
+    }
+
     // url
     void SetUrl(const char* url) {
         this->url = url;

+ 1 - 1
http/client/AsyncHttpClient.cpp

@@ -47,7 +47,7 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
         hio_set_peeraddr(connio, &peeraddr.sa, sockaddr_len(&peeraddr));
         addChannel(connio);
         // https
-        if (strcmp(req->scheme.c_str(), "https") == 0) {
+        if (req->isHttps()) {
             hio_enable_ssl(connio);
         }
     }

+ 10 - 7
http/client/http_client.cpp

@@ -71,10 +71,7 @@ struct http_client_s {
             hssl_free(ssl);
             ssl = NULL;
         }
-        if (fd > 0) {
-            closesocket(fd);
-            fd = -1;
-        }
+        SAFE_CLOSESOCKET(fd);
     }
 };
 
@@ -95,6 +92,12 @@ int http_client_del(http_client_t* cli) {
     return 0;
 }
 
+int http_client_close(http_client_t* cli) {
+    if (cli == NULL) return 0;
+    cli->Close();
+    return 0;
+}
+
 int http_client_set_timeout(http_client_t* cli, int timeout) {
     cli->timeout = timeout;
     return 0;
@@ -163,7 +166,7 @@ static int http_client_make_request(http_client_t* cli, HttpRequest* req) {
         req->port = cli->port;
     }
 
-    bool https = strcmp(req->scheme.c_str(), "https") == 0 || strncmp(req->url.c_str(), "https", 5) == 0;
+    bool https = req->isHttps();
     bool use_proxy = https ? (!cli->https_proxy_host.empty()) : (!cli->http_proxy_host.empty());
     if (use_proxy) {
         if (req->host == "127.0.0.1" || req->host == "localhost") {
@@ -410,7 +413,7 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
     }
     tcp_nodelay(connfd, 1);
 
-    if (strcmp(req->scheme.c_str(), "https") == 0) {
+    if (req->isHttps()) {
         hssl_ctx_t ssl_ctx = hssl_ctx_instance();
         if (ssl_ctx == NULL) {
             closesocket(connfd);
@@ -468,7 +471,7 @@ connect:
 send:
     char* data = NULL;
     size_t len  = 0;
-    bool https = strcmp(req->scheme.c_str(), "https") == 0;
+    bool https = req->isHttps();
     while (cli->parser->GetSendData(&data, &len)) {
         total_nsend = 0;
         while (1) {

+ 6 - 0
http/client/http_client.h

@@ -30,6 +30,7 @@ int main(int argc, char* argv[]) {
 typedef struct http_client_s http_client_t;
 
 HV_EXPORT http_client_t* http_client_new(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0);
+HV_EXPORT int http_client_close(http_client_t* cli);
 HV_EXPORT int http_client_del(http_client_t* cli);
 HV_EXPORT const char* http_client_strerror(int errcode);
 
@@ -119,6 +120,11 @@ public:
         return http_client_send_async(_client, req, resp_cb);
     }
 
+    // close
+    int close() {
+        return http_client_close(_client);
+    }
+
 private:
     http_client_t* _client;
 };