一、簡介
Civetweb是一個跨平台的、輕量級的Web伺服器,它以MIT許可證開源,可以在商業和非商業環境中自由使用。Civetweb支持HTTP/1.1、HTTPS、WebSocket和CGI等多種協議,可以運行在Windows、Linux、macOS等各種操作系統上,因此Civetweb被廣泛地應用於各種嵌入式設備和伺服器端應用程序。
Civetweb的特點主要包括:
- 快速輕量:Civetweb採用高度優化的C代碼編寫,可以運行在低端嵌入式設備上。
- 易於集成:Civetweb可以非常容易地集成到其他項目中,支持動態鏈接和靜態鏈接兩種方式。
- 易於擴展:Civetweb支持插件和動態鏈接庫,可以方便地擴展其功能。
- 安全可靠:Civetweb支持HTTPS和WebSocket協議,可以保證數據傳輸的安全性,而且Civetweb有非常完善的日誌記錄機制,可以追蹤問題和安全漏洞。
二、使用方法
使用Civetweb非常簡單,只需要下載源碼編譯即可。以下是一個示例的代碼:
#include "civetweb.h" static int hello_handler(struct mg_connection* conn, void* cbdata) { mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"); mg_printf(conn, "Hello from Civetweb!"); return 1; } int main(int argc, char* argv[]) { struct mg_mgr mgr; struct mg_connection* nc; mg_mgr_init(&mgr, NULL); nc = mg_bind(&mgr, "8080", NULL); mg_set_protocol_http_websocket(nc); mg_register_http_endpoint(nc, "/hello", hello_handler, NULL); printf("Starting Civetweb on port %s\n", mg_get_option(&mgr, "listening_ports")); for (;;) { mg_mgr_poll(&mgr, 1000); } mg_mgr_free(&mgr); return 0; }
上述代碼中,我們定義了一個簡單的HTTP請求處理函數hello_handler,只是簡單地輸出Hello from Civetweb!。然後,我們使用mg_bind函數創建了一個監聽8080埠的連接,並使用mg_register_http_endpoint函數將hello_handler函數註冊為/hello的處理函數。最後,我們使用mg_mgr_poll函數進入事件輪詢循環,等待連接請求。
使用上述代碼編譯並運行,可以通過瀏覽器訪問http://localhost:8080/hello,就可以看到瀏覽器輸出Hello from Civetweb!。
三、配置選項
Civetweb可以通過命令行參數和配置文件進行配置。以下是一些常用的配置選項:
- listening_ports:監聽埠,默認為80。
- ssl_certificate:SSL證書文件路徑。
- ssl_certificate_chain:SSL證書鏈文件路徑(可選)。
- ssl_protocols:SSL協議類型,可選值為TLSv1.1、TLSv1.2、TLSv1.3等。
- access_log_file:訪問日誌文件路徑。
- error_log_file:錯誤日誌文件路徑。
- num_threads:工作線程數量,默認為50。
- document_root:文檔根目錄,默認為當前目錄。
配置選項可以通過以下兩種方式進行設置:
- 命令行參數:Civetweb支持類似於Apache的命令行參數設置方式,例如設置listening_ports選項可以使用-c選項,例如-c listening_ports=8080。
- 配置文件:配置文件可以使用JSON或INI格式,例如以下是一個JSON格式的配置文件示例:
{ "listening_ports": "8080", "ssl_certificate": "/path/to/ssl/certificate.pem", "ssl_certificate_chain": "/path/to/ssl/certificate_chain.pem", "ssl_protocols": "TLSv1.2", "access_log_file": "/path/to/access.log", "error_log_file": "/path/to/error.log", "num_threads": 10, "document_root": "/path/to/document_root" }
通過指定配置文件路徑啟動Civetweb:
./civetweb -c /path/to/config.json
四、上下文和插件
Civetweb支持多個上下文,一個上下文對應一個Web站點,可以使用不同的配置選項和處理程序。以下是一個使用多個上下文的示例:
#include "civetweb.h" static int hello_handler(struct mg_connection* conn, void* cbdata) { mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"); mg_printf(conn, "Hello from Civetweb!"); return 1; } int main(int argc, char* argv[]) { struct mg_context* ctx1, * ctx2; /* 創建兩個上下文 */ ctx1 = mg_start(NULL, NULL, NULL); ctx2 = mg_start(NULL, NULL, NULL); /* 在第一個上下文註冊/hello處理程序 */ mg_register_http_endpoint(ctx1, "/hello", hello_handler, NULL); /* 在第二個上下文註冊/hello/deep處理程序 */ mg_register_http_endpoint(ctx2, "/hello/deep", hello_handler, NULL); /* 等待事件 */ getchar(); /* 停止上下文 */ mg_stop(ctx1); mg_stop(ctx2); return 0; }
通過上述代碼,我們創建了兩個上下文,然後在每個上下文中分別註冊了不同的處理程序。可以用兩個瀏覽器分別訪問http://localhost:8080/hello和http://localhost:8081/hello/deep來驗證這個示例。
除了多個上下文,Civetweb還支持插件。插件可以是動態鏈接庫,可以通過mg_load_plugin函數載入,並且可以在啟動時自動載入。以下是一個使用插件的示例:
#include "civetweb.h" int main(int argc, char* argv[]) { struct mg_context* ctx; const char* plugin_names[] = { "mod_auth_digital_signature.dll", "mod_rewrite.dll", NULL }; /* 創建上下文 */ ctx = mg_start(NULL, NULL, plugin_names); /* 等待事件 */ getchar(); /* 停止上下文 */ mg_stop(ctx); return 0; }
上述代碼會自動載入mod_auth_digital_signature.dll和mod_rewrite.dll這兩個插件。
總結
Civetweb是一個跨平台的、輕量級的Web伺服器,可以應用於各種嵌入式設備和伺服器端應用程序。它支持HTTP/1.1、HTTPS、WebSocket和CGI等多種協議,支持插件和動態鏈接庫,易於集成和擴展。通過命令行參數和配置文件,可以方便地進行配置。同時,Civetweb還支持多個上下文和插件,可以實現更加複雜和靈活的應用場景。
原創文章,作者:XYCV,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/143979.html