1.url 结构
2.http和 http-restful区别在于对于mg_tls_opts的赋值
2.1 http和https 区分
a) port地址
static const char *s_http_addr = "http://0.0.0.0:8000"; // HTTP port
static const char *s_https_addr = "https://0.0.0.0:8443"; // HTTPS port
b)https时 对mg_tls_opts赋值
struct mg_tls_opts opts = {
#ifdef TLS_TWOWAY.ca = mg_str(s_tls_ca),
#endif.cert = mg_str(s_tls_cert),.key = mg_str(s_tls_key)};mg_tls_init(c, &opts);}
3个重要变量:
static const char* s_tls_ca ;
static const char* s_tls_cert;
static const char* s_tls_key;
#ifdef TLS_TWOWAY
static const char* s_tls_ca =
"-----BEGIN CERTIFICATE-----\n"
"MIIBqjCCAU+gAwIBAgIUESoOPGqMhf9uarzblVFwzrQweMcwCgYIKoZIzj0EAwIw\n"
"RDELMAkGA1UEBhMCSUUxDzANBgNVBAcMBkR1YmxpbjEQMA4GA1UECgwHQ2VzYW50\n"
"YTESMBAGA1UEAwwJVGVzdCBSb290MCAXDTIwMDUwOTIxNTE0NFoYDzIwNTAwNTA5\n"
"MjE1MTQ0WjBEMQswCQYDVQQGEwJJRTEPMA0GA1UEBwwGRHVibGluMRAwDgYDVQQK\n"
"DAdDZXNhbnRhMRIwEAYDVQQDDAlUZXN0IFJvb3QwWTATBgcqhkjOPQIBBggqhkjO\n"
"PQMBBwNCAAQsq9ECZiSW1xI+CVBP8VDuUehVA166sR2YsnJ5J6gbMQ1dUCH/QvLa\n"
"dBdeU7JlQcH8hN5KEbmM9BnZxMor6ussox0wGzAMBgNVHRMEBTADAQH/MAsGA1Ud\n"
"DwQEAwIBrjAKBggqhkjOPQQDAgNJADBGAiEAnHFsAIwGQQyRL81B04dH6d86Iq0l\n"
"fL8OKzndegxOaB0CIQCPwSIwEGFdURDqCC0CY2dnMrUGY5ZXu3hHCojZGS7zvg==\n"
"-----END CERTIFICATE-----\n";
#endif
static const char* s_tls_cert =
"-----BEGIN CERTIFICATE-----\n"
"MIIBhzCCASygAwIBAgIUbnMoVd8TtWH1T09dANkK2LU6IUswCgYIKoZIzj0EAwIw\n"
"RDELMAkGA1UEBhMCSUUxDzANBgNVBAcMBkR1YmxpbjEQMA4GA1UECgwHQ2VzYW50\n"
"YTESMBAGA1UEAwwJVGVzdCBSb290MB4XDTIwMDUwOTIxNTE0OVoXDTMwMDUwOTIx\n"
"NTE0OVowETEPMA0GA1UEAwwGc2VydmVyMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD\n"
"QgAEkuBGnInDN6l06zVVQ1VcrOvH5FDu9MC6FwJc2e201P8hEpq0Q/SJS2nkbSuW\n"
"H/wBTTBaeXN2uhlBzMUWK790KKMvMC0wCQYDVR0TBAIwADALBgNVHQ8EBAMCA6gw\n"
"EwYDVR0lBAwwCgYIKwYBBQUHAwEwCgYIKoZIzj0EAwIDSQAwRgIhAPo6xx7LjCdZ\n"
"QY133XvLjAgVFrlucOZHONFVQuDXZsjwAiEAzHBNligA08c5U3SySYcnkhurGg50\n"
"BllCI0eYQ9ggp/o=\n"
"-----END CERTIFICATE-----\n";static const char* s_tls_key =
"-----BEGIN PRIVATE KEY-----\n"
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQglNni0t9Dg9icgG8w\n"
"kbfxWSS+TuNgbtNybIQXcm3NHpmhRANCAASS4EacicM3qXTrNVVDVVys68fkUO70\n"
"wLoXAlzZ7bTU/yESmrRD9IlLaeRtK5Yf/AFNMFp5c3a6GUHMxRYrv3Qo\n"
"-----END PRIVATE KEY-----\n";
2.2code
restfulserver_fn函数
static void restfulserver_fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
{if (ev == MG_EV_ACCEPT && fn_data != NULL) //1.如果收到消息 MG_EV_ACCEPT{struct mg_tls_opts opts = //1.1.结构赋值{#ifdef TLS_TWOWAY.ca = mg_str(s_tls_ca),#endif.cert = mg_str(s_tls_cert),.key = mg_str(s_tls_key)}; mg_tls_init(c, &opts); //1.2放入上下文中}if (ev == MG_EV_HTTP_MSG) //2.如果收到消息 MG_EV_HTTP_MSG{struct mg_http_message *hm = (struct mg_http_message *) ev_data;//1.get请求 ,显示if (mg_http_match_uri(hm, "/api/stats")) {// Print some statistics about currently established connectionsmg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");mg_http_printf_chunk(c, "ID PROTO TYPE LOCAL REMOTE\n");for (struct mg_connection *t = c->mgr->conns; t != NULL; t = t->next) {mg_http_printf_chunk(c, "%-3lu %4s %s %M %M\n", t->id,t->is_udp ? "UDP" : "TCP",t->is_listening ? "LISTENING": t->is_accepted ? "ACCEPTED ": "CONNECTED",mg_print_ip, &t->loc, mg_print_ip, &t->rem);}mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk} //2.post请求 回resultelse if (mg_http_match_uri(hm, "/api/f2/*")) {mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len,hm->uri.ptr);} //3.显示当前目录文件列表else {struct mg_http_serve_opts opts = {.root_dir = s_root_dir};mg_http_serve_dir(c, ev_data, &opts);}}(void) fn_data;
}
main 函数
void main()
{struct mg_mgr mgr; // Event managermg_log_set(MG_LL_DEBUG); // Set log levelmg_mgr_init(&mgr); // Initialise event managermg_http_listen(&mgr, s_http_addr, restfulserver_fn, NULL); // Create HTTP listenermg_http_listen(&mgr, s_https_addr, restfulserver_fn, (void*)1); // HTTPS listenerfor (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loopmg_mgr_free(&mgr);
}
2.3测试
启动httpserver 程序
1.2.1 浏览器访问:http://127.0.0.1:8000:
将显示exe程序根目录文件列表
1.2.2 浏览器访问: http://127.0.0.1:8000/api/stats:
将显示:
1.2.3 使用postman: 发送post请求:
http://127.0.0.1:8000/api/f2/*,