guohui лет назад: 2
Родитель
Сommit
f8e2c9885e
3 измененных файлов с 53 добавлено и 14 удалено
  1. 15 14
      pro/src/appweb_handle.c
  2. 37 0
      pro/src/file.c
  3. 1 0
      pro/src/file.h

+ 15 - 14
pro/src/appweb_handle.c

@@ -43,6 +43,7 @@ enum {
 
 
 
 
 typedef void (*http_fn_t)(void *arg);
 typedef void (*http_fn_t)(void *arg);
+typedef struct mg_tls_opts mg_tls_t;
 
 
 typedef struct {
 typedef struct {
     struct mg_connection *c;
     struct mg_connection *c;
@@ -57,6 +58,7 @@ typedef struct {
     int        cnt;
     int        cnt;
     mg_fn_t    fn[FN_MAX];
     mg_fn_t    fn[FN_MAX];
     char       rootDir[MG_PATH_MAX];
     char       rootDir[MG_PATH_MAX];
+    mg_tls_t   tls;
 
 
     pthread_t  tid;
     pthread_t  tid;
 }mg_handle_t;
 }mg_handle_t;
@@ -398,14 +400,10 @@ static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
             mg_http_serve_dir(c, ev_data, &opts);
             mg_http_serve_dir(c, ev_data, &opts);
         }
         }
     }
     }
-    else if(ev == MG_EV_CONNECT) {
-        #if 0
-        if (mg_url_is_ssl(url)) {
-            struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
-                                        .name = host};
-            mg_tls_init(c, &opts);
+    else if(ev == MG_EV_ACCEPT) {
+        if (c->is_tls) {
+            mg_tls_init(c, &h->tls);
         }
         }
-        #endif
     }
     }
 }
 }
 void *web_thread(void *arg)
 void *web_thread(void *arg)
@@ -414,8 +412,8 @@ void *web_thread(void *arg)
     struct mg_connection *c;
     struct mg_connection *c;
     mg_handle_t *mh=&webHandle.mg;
     mg_handle_t *mh=&webHandle.mg;
     thread_handle_t *h=(thread_handle_t*)arg;
     thread_handle_t *h=(thread_handle_t*)arg;
-    const char *listen_addr1="http://[::]:80";
-    const char *listen_addr2="http://[::]:443";
+    const char *http_addr="http://[::]:80";
+    const char *https_addr="https://[::]:443";
 
 
     //mg_log_set(MG_LL_DEBUG);
     //mg_log_set(MG_LL_DEBUG);
     mg_mgr_init(&mgr);
     mg_mgr_init(&mgr);
@@ -423,19 +421,22 @@ void *web_thread(void *arg)
     //sys_get_path(h->rootDir, WEB_ROOT);
     //sys_get_path(h->rootDir, WEB_ROOT);
     strcpy(mh->rootDir, "/root/run/web");
     strcpy(mh->rootDir, "/root/run/web");
     
     
-    if ((c = mg_http_listen(&mgr, listen_addr1, http_mg_cb, NULL)) == NULL) {
-        MG_ERROR(("cannot listen on %s. use http://address:port or :port\n", listen_addr1));
+    if ((c = mg_http_listen(&mgr, http_addr, http_mg_cb, NULL)) == NULL) {
+        MG_ERROR(("cannot listen on %s. use http://address:port or :port\n", http_addr));
         return NULL;
         return NULL;
     }
     }
-
-    if ((c = mg_http_listen(&mgr, listen_addr2, http_mg_cb, NULL)) == NULL) {
-        MG_ERROR(("cannot listen on %s. use http://address:port or :port\n", listen_addr2));
+    
+    if ((c = mg_http_listen(&mgr, https_addr, http_mg_cb, NULL)) == NULL) {
+        MG_ERROR(("cannot listen on %s. use https://address:port or :port\n", https_addr));
         return NULL;
         return NULL;
     }
     }
 
 
     webSetCallback();
     webSetCallback();
     printf("_____mgHandle.cnt: %d, rootdir: %s\n", mh->cnt, mh->rootDir);
     printf("_____mgHandle.cnt: %d, rootdir: %s\n", mh->cnt, mh->rootDir);
 
 
+    mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt"));
+    mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key"));
+    mh->tls.name = mg_url_host(https_addr);
     while(h->quit==0) {
     while(h->quit==0) {
         mg_mgr_poll(&mgr, 200);
         mg_mgr_poll(&mgr, 200);
     }
     }

+ 37 - 0
pro/src/file.c

@@ -92,6 +92,43 @@ int file_load(char* path, buf_t *fbuf)
 }
 }
 
 
 
 
+char* file_load2(char* path)
+{
+    FILE* fp=NULL;
+    int r,flen,blen;
+    char *fbuf=NULL;
+
+    flen = file_length(path);
+    if(flen<=0) {
+        LOGE("____ file_load, %s failed\n", path);
+        return NULL;
+    }
+
+    blen = flen;
+    fbuf = (char*)malloc(blen);
+    if(!fbuf) {
+        LOGE("____ file_load, %s malloc %d failed\n", path, flen);
+        return NULL;
+    }
+
+    fp = fopen(path, "r");
+    if (!fp) {
+        LOGE("file_load, fopen %s failed\n", path);;
+        free(fbuf);
+        return NULL;
+    }
+    r = fread(fbuf, 1, flen, fp);
+    if (r != flen) {
+        LOGE("file_load %s failed, %d, %d\n", path, r, flen);
+        fclose(fp); free(fbuf);
+        fbuf = NULL;
+    }
+    fclose(fp);
+
+    return fbuf;
+}
+
+
 int file_copy(char *dst, char *src)
 int file_copy(char *dst, char *src)
 {
 {
     int r;
     int r;

+ 1 - 0
pro/src/file.h

@@ -14,6 +14,7 @@ typedef struct {
 int file_length(char* path);
 int file_length(char* path);
 int file_save(char *path, void *buf, int len);
 int file_save(char *path, void *buf, int len);
 int file_load(char* path, buf_t *buf);
 int file_load(char* path, buf_t *buf);
+char* file_load2(char* path);
 int file_copy(char *dst, char *src);
 int file_copy(char *dst, char *src);
 int file_remove(char *path);
 int file_remove(char *path);
 int file_append(char *path, void *data, int datalen);
 int file_append(char *path, void *data, int datalen);