ZLMediaKit/tests/test_httpApi.cpp

137 lines
5.9 KiB
C++
Raw Normal View History

2017-12-08 22:37:17 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2017-12-08 22:37:17 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2017-12-08 22:37:17 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-12-08 22:37:17 +08:00
*/
#include <map>
#include <signal.h>
#include <iostream>
2018-12-20 10:31:31 +08:00
#include "Util/SSLBox.h"
2017-12-08 22:37:17 +08:00
#include "Util/logger.h"
#include "Util/onceToken.h"
2018-12-20 10:31:31 +08:00
#include "Util/NoticeCenter.h"
2017-12-08 22:37:17 +08:00
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
2018-12-20 10:31:31 +08:00
#include "Common/config.h"
#include "Http/WebSocketSession.h"
2017-12-08 22:37:17 +08:00
using namespace std;
2018-10-24 17:17:55 +08:00
using namespace toolkit;
using namespace mediakit;
2017-12-08 22:37:17 +08:00
2018-12-19 17:53:43 +08:00
namespace mediakit {
// //////////HTTP配置/////////// [AUTO-TRANSLATED:a281d694]
// //////////HTTP Configuration///////////
2018-12-19 17:53:43 +08:00
namespace Http {
#define HTTP_FIELD "http."
#define HTTP_PORT 80
const char kPort[] = HTTP_FIELD"port";
#define HTTPS_PORT 443
extern const char kSSLPort[] = HTTP_FIELD"sslport";
onceToken token1([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kPort] = HTTP_PORT;
mINI::Instance()[kSSLPort] = HTTPS_PORT;
2018-12-19 17:53:43 +08:00
},nullptr);
}//namespace Http
} // namespace mediakit
2019-07-20 20:37:52 +08:00
void initEventListener(){
2020-03-20 11:51:24 +08:00
static onceToken s_token([](){
NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastHttpRequest,[](BroadcastHttpRequestArgs){
//const Parser &parser,HttpSession::HttpResponseInvoker &invoker,bool &consumed
2023-06-10 11:04:52 +08:00
if(strstr(parser.url().data(),"/api/") != parser.url().data()){
2020-03-20 11:51:24 +08:00
return;
}
// url以"/api/起始说明是http api" [AUTO-TRANSLATED:8af96c7e]
// URLs starting with "/api/" indicate HTTP API
2020-03-20 11:51:24 +08:00
consumed = true;//该http请求已被消费
_StrPrinter printer;
////////////////method////////////////////
2023-06-10 11:04:52 +08:00
printer << "\r\nmethod:\r\n\t" << parser.method();
2020-03-20 11:51:24 +08:00
////////////////url/////////////////
2023-06-10 11:04:52 +08:00
printer << "\r\nurl:\r\n\t" << parser.url();
2020-03-20 11:51:24 +08:00
////////////////protocol/////////////////
2023-06-10 11:04:52 +08:00
printer << "\r\nprotocol:\r\n\t" << parser.protocol();
2020-03-20 11:51:24 +08:00
///////////////args//////////////////
printer << "\r\nargs:\r\n";
for(auto &pr : parser.getUrlArgs()){
printer << "\t" << pr.first << " : " << pr.second << "\r\n";
}
///////////////header//////////////////
printer << "\r\nheader:\r\n";
2020-04-20 18:13:45 +08:00
for(auto &pr : parser.getHeader()){
2020-03-20 11:51:24 +08:00
printer << "\t" << pr.first << " : " << pr.second << "\r\n";
}
////////////////content/////////////////
2023-06-10 11:04:52 +08:00
printer << "\r\ncontent:\r\n" << parser.content();
2020-03-20 11:51:24 +08:00
auto contentOut = printer << endl;
// //////////////我们测算异步回复,当然你也可以同步回复///////////////// [AUTO-TRANSLATED:5c112e50]
// //////////////We measure asynchronous responses, but you can also respond synchronously/////////////////
2020-03-20 11:51:24 +08:00
EventPollerPool::Instance().getPoller()->async([invoker,contentOut](){
HttpSession::KeyValue headerOut;
// 你可以自定义header,如果跟默认header重名则会覆盖之 [AUTO-TRANSLATED:07b1ecfe]
// You can customize the header; if it has the same name as the default header, it will override it
// 默认header有:Server,Connection,Date,Content-Type,Content-Length [AUTO-TRANSLATED:ca0c35d2]
// Default headers include: Server, Connection, Date, Content-Type, Content-Length
// 请勿覆盖Connection、Content-Length键 [AUTO-TRANSLATED:ef188768]
// Please do not override the Connection and Content-Length keys
// 键名覆盖时不区分大小写 [AUTO-TRANSLATED:32147753]
// Key name overrides are case-insensitive
2020-03-20 11:51:24 +08:00
headerOut["TestHeader"] = "HeaderValue";
invoker(200,headerOut,contentOut);
2020-03-20 11:51:24 +08:00
});
});
}, nullptr);
2019-07-20 20:37:52 +08:00
}
2017-12-08 22:37:17 +08:00
int main(int argc,char *argv[]){
// 设置退出信号处理函数 [AUTO-TRANSLATED:4f047770]
// Set the exit signal processing function
2020-03-20 11:51:24 +08:00
static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
// 设置日志 [AUTO-TRANSLATED:50372045]
// Set the log
2020-03-20 11:51:24 +08:00
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
// 加载配置文件,如果配置文件不存在就创建一个 [AUTO-TRANSLATED:761e7479]
// Load the configuration file; if it does not exist, create one
2020-03-20 11:51:24 +08:00
loadIniConfig();
initEventListener();
2017-12-08 22:37:17 +08:00
// 加载证书,证书包含公钥和私钥 [AUTO-TRANSLATED:fce78641]
// Load the certificate, which includes the public and private keys
2020-03-20 11:51:24 +08:00
SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
// 信任某个自签名证书 [AUTO-TRANSLATED:6815fc55]
// Trust a self-signed certificate
2020-03-20 11:51:24 +08:00
SSL_Initor::Instance().trustCertificate((exeDir() + "ssl.p12").data());
// 不忽略无效证书证书(例如自签名或过期证书) [AUTO-TRANSLATED:ee4a34c4]
// Do not ignore invalid certificates (e.g., self-signed or expired certificates)
2020-03-20 11:51:24 +08:00
SSL_Initor::Instance().ignoreInvalidCertificate(false);
2019-04-16 17:25:19 +08:00
2017-12-08 22:37:17 +08:00
// 开启http服务器 [AUTO-TRANSLATED:ffab30d4]
// Start the HTTP server
2020-03-20 11:51:24 +08:00
TcpServer::Ptr httpSrv(new TcpServer());
httpSrv->start<HttpSession>(mINI::Instance()[Http::kPort]);//默认80
2017-12-08 22:37:17 +08:00
// 如果支持ssl还可以开启https服务器 [AUTO-TRANSLATED:8ef29f9c]
// If SSL is supported, you can also start the HTTPS server
2020-03-20 11:51:24 +08:00
TcpServer::Ptr httpsSrv(new TcpServer());
httpsSrv->start<HttpsSession>(mINI::Instance()[Http::kSSLPort]);//默认443
2017-12-08 22:37:17 +08:00
2023-06-10 11:04:52 +08:00
InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1";
2017-12-08 22:37:17 +08:00
2020-03-20 11:51:24 +08:00
sem.wait();
return 0;
2017-12-08 22:37:17 +08:00
}