Browse Source

test wintls for (#412)

ithewei 2 years ago
parent
commit
045f7a64e9
8 changed files with 29 additions and 13 deletions
  1. 7 1
      .github/workflows/CI.yml
  2. 1 1
      CMakeLists.txt
  3. 1 2
      docs/PLAN.md
  4. 7 0
      examples/httpd/httpd.cpp
  5. 5 3
      http/client/HttpClient.cpp
  6. 1 0
      scripts/check.sh
  7. 7 3
      ssl/hssl.h
  8. 0 3
      ssl/wintls.c

+ 7 - 1
.github/workflows/CI.yml

@@ -46,7 +46,8 @@ jobs:
           cd cmake-build-win64
           start bin/Release/http_server_test
           bin/Release/http_client_test
-          bin/Release/curl -v http://127.0.0.1:8080/ping
+          bin/Release/curl -v -X HEAD http://example.com/
+          bin/Release/curl -v -X HEAD https://example.com/
           bin/Release/wrk -c 100 -t 2 -d 10s http://127.0.0.1:8080/ping
 
   build-mac:
@@ -59,6 +60,11 @@ jobs:
           ./configure
           make libhv evpp examples unittest
 
+      - name: test
+        run: |
+          bin/curl -v -X HEAD http://example.com/
+          bin/curl -v -X HEAD https://example.com/
+
   build-android:
     name: build-android
     runs-on: ubuntu-latest

+ 1 - 1
CMakeLists.txt

@@ -158,7 +158,7 @@ endif()
 
 if(WIN32)
     add_definitions(-DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0600)
-    set(LIBS ${LIBS} winmm iphlpapi ws2_32)
+    set(LIBS ${LIBS} secur32 crypt32 winmm iphlpapi ws2_32)
     if(ENABLE_WINDUMP)
         add_definitions(-DENABLE_WINDUMP)
         set(LIBS ${LIBS} dbghelp)

+ 1 - 2
docs/PLAN.md

@@ -2,7 +2,7 @@
 
 - base: cross platfrom infrastructure
 - event: select/poll/epoll/wepoll/kqueue/port
-- ssl: openssl/guntls/mbedtls
+- ssl: openssl/gnutls/mbedtls/wintls/appletls
 - rudp: KCP
 - evpp: c++ EventLoop interface similar to muduo and evpp
 - http client/server: include https http1/x http2
@@ -11,7 +11,6 @@
 
 ## Improving
 
-- wintls: SChannel is so hard :) need help
 - Path router: optimized matching via trie?
 - FileCache use LRUCache
 

+ 7 - 0
examples/httpd/httpd.cpp

@@ -197,8 +197,15 @@ int parse_confile(const char* confile) {
         param.ca_file = ca_file.c_str();
         param.endpoint = HSSL_SERVER;
         if (g_http_server.newSslCtx(&param) != 0) {
+#ifdef OS_WIN
+            if (strcmp(hssl_backend(), "schannel") == 0) {
+                hlogw("schannel needs pkcs12 formatted certificate file.");
+                g_http_server.https_port = 0;
+            }
+#else
             hloge("SSL certificate verify failed!");
             exit(0);
+#endif
         }
         else {
             hlogi("SSL certificate verify ok!");

+ 5 - 3
http/client/HttpClient.cpp

@@ -206,6 +206,7 @@ static int http_client_make_request(http_client_t* cli, HttpRequest* req) {
 }
 
 int http_client_connect(http_client_t* cli, const char* host, int port, int https, int timeout) {
+    cli->Close();
     int blocktime = DEFAULT_CONNECT_TIMEOUT;
     if (timeout > 0) {
         blocktime = MIN(timeout*1000, blocktime);
@@ -240,6 +241,7 @@ int http_client_connect(http_client_t* cli, const char* host, int port, int http
         if (!is_ipaddr(host)) {
             hssl_set_sni_hostname(cli->ssl, host);
         }
+        so_rcvtimeo(connfd, blocktime);
         int ret = hssl_connect(cli->ssl);
         if (ret != 0) {
             fprintf(stderr, "* ssl handshake failed: %d\n", ret);
@@ -319,7 +321,9 @@ static int http_client_exec(http_client_t* cli, HttpRequest* req, HttpResponse*
         }
     }
 
-    if (connfd <= 0) {
+    if (connfd <= 0 || cli->host != req->host || cli->port != req->port) {
+        cli->host = req->host;
+        cli->port = req->port;
 connect:
         connfd = http_client_connect(cli, req->host.c_str(), req->port, https, connect_timeout);
         if (connfd < 0) {
@@ -347,7 +351,6 @@ send:
                 err = socket_errno();
                 if (err == EINTR) continue;
                 if (retry_count-- > 0 && left_time > req->retry_delay + connect_timeout * 1000) {
-                    cli->Close();
                     err = 0;
                     if (req->retry_delay > 0) hv_msleep(req->retry_delay);
                     goto connect;
@@ -375,7 +378,6 @@ recv:
                 goto disconnect;
             }
             if (retry_count-- > 0 && left_time > req->retry_delay + connect_timeout * 1000) {
-                cli->Close();
                 err = 0;
                 if (req->retry_delay > 0) hv_msleep(req->retry_delay);
                 goto connect;

+ 1 - 0
scripts/check.sh

@@ -13,3 +13,4 @@ bin/curl -v http://127.0.0.1:8080/
 if [ $HTTPS -gt 0 ]; then
     bin/curl -v https://127.0.0.1:8443/
 fi
+bin/wrk -c 100 -t 2 -d 10s http://127.0.0.1:8080/ping

+ 7 - 3
ssl/hssl.h

@@ -8,11 +8,15 @@
     !defined(WITH_GNUTLS)  &&   \
     !defined(WITH_MBEDTLS)
 #ifdef OS_WIN
-#define WITH_WINTLS
+    #define WITH_WINTLS
+    #ifdef _MSC_VER
+        #pragma comment(lib, "secur32.lib")
+        #pragma comment(lib, "crypt32.lib")
+    #endif
 #elif defined(OS_DARWIN)
-#define WITH_APPLETLS
+    #define WITH_APPLETLS
 #else
-#define HV_WITHOUT_SSL
+    #define HV_WITHOUT_SSL
 #endif
 #endif
 

+ 0 - 3
ssl/wintls.c

@@ -14,9 +14,6 @@
 #include <security.h>
 #include <sspi.h>
 
-#pragma comment(lib, "Secur32.lib")
-#pragma comment(lib, "crypt32.lib")
-
 #define TLS_SOCKET_BUFFER_SIZE 17000
 
 const char* hssl_backend()