Jelajahi Sumber

WITH_OPENSSL

hewei 6 tahun lalu
induk
melakukan
486185bd77
4 mengubah file dengan 36 tambahan dan 19 penghapusan
  1. 1 1
      Makefile
  2. 20 1
      README.md
  3. 4 4
      etc/httpd.conf
  4. 11 13
      examples/httpd.cpp

+ 1 - 1
Makefile

@@ -80,7 +80,7 @@ curl: prepare
 consul_cli: prepare
 	$(RM) $(TMPDIR)/*.o $(TMPDIR)/*.h $(TMPDIR)/*.c $(TMPDIR)/*.cpp
 	cp examples/consul_cli.cpp $(TMPDIR)
-	$(MAKEF) TARGET=$@ SRCDIRS=". base utils event http http/client consul $(TMPDIR)" DEFINES="PRINT_DEBUG"
+	$(MAKEF) TARGET=$@ SRCDIRS=". base utils http http/client consul $(TMPDIR)" DEFINES="$(DEFINES) PRINT_DEBUG"
 
 unittest: prepare
 	$(CC)  -g -Wall -std=c99   -I. -Ibase            -o bin/hmutex     unittest/hmutex_test.c        -pthread

+ 20 - 1
README.md

@@ -10,6 +10,8 @@ but simpler apis and richer protocols.
 
 - cross-platform (Linux, Windows, Mac)
 - event-loop (IO, timer, idle)
+- enable IPv6
+- with OpenSSL
 - http client/server (include https http1/x http2 grpc)
 - protocols
     - dns
@@ -148,7 +150,24 @@ bin/nc -u 127.0.0.1 2222
 - make DEFINES=PRINT_DEBUG
 
 #### compile WITH_OPENSSL
-- make DEFINES=WITH_OPENSSL
+libhv combines OpenSSL perfectly, something almost all asynchronous IO network libraries don't do.
+And enable SSL in libhv is so easy, just only two apis:
+```
+int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file); // init global SSL_CTX, see
+base/ssl_ctx.h
+int hio_enable_ssl(hio_t* io); // enable ssl, see event/hloop.h
+```
+
+https is the best example.
+```
+sudo apt-get install openssl libssl-dev # ubuntu
+make clean
+make libhv httpd curl DEFINES=WITH_OPENSSL
+# editor etc/httpd.conf => ssl = on
+bin/httpd -d
+bin/curl -v https://localhost:8080
+curl -v https://localhost:8080 --insecure
+```
 
 #### compile WITH_CURL
 - make DEFINES="WITH_CURL CURL_STATICLIB"

+ 4 - 4
etc/httpd.conf

@@ -11,10 +11,10 @@ log_filesize = 64M
 worker_processes = 1
 
 # http server
-#ssl = on
-#ssl_certificate = cert/server.crt
-#ssl_privatekey = cert/server.key
-#ssl_ca_certificate = cert/cacert.pem
+ssl = off
+ssl_certificate = cert/server.crt
+ssl_privatekey = cert/server.key
+ssl_ca_certificate = cert/cacert.pem
 port = 8080
 base_url = /v1/api
 document_root = html

+ 11 - 13
examples/httpd.cpp

@@ -145,19 +145,17 @@ int parse_confile(const char* confile) {
     }
     // ssl
     str = ini.GetValue("ssl");
-    if (str.size() != 0) {
-        if (strcmp(str.c_str(), "on") == 0) {
-            g_http_server.ssl = 1;
-            std::string crt_file = ini.GetValue("ssl_certificate");
-            std::string key_file = ini.GetValue("ssl_privatekey");
-            std::string ca_file = ini.GetValue("ssl_ca_certificate");
-            if (ssl_ctx_init(crt_file.c_str(), key_file.c_str(), ca_file.c_str()) != 0) {
-                hlogi("SSL certificate verify failed!");
-                exit(0);
-            }
-            else {
-                hlogi("SSL certificate verify ok!");
-            }
+    if (getboolean(str.c_str())) {
+        g_http_server.ssl = 1;
+        std::string crt_file = ini.GetValue("ssl_certificate");
+        std::string key_file = ini.GetValue("ssl_privatekey");
+        std::string ca_file = ini.GetValue("ssl_ca_certificate");
+        if (ssl_ctx_init(crt_file.c_str(), key_file.c_str(), ca_file.c_str()) != 0) {
+            hloge("SSL certificate verify failed!");
+            exit(0);
+        }
+        else {
+            hlogi("SSL certificate verify ok!");
         }
     }