This commit is contained in:
DDVTech 2021-09-10 23:44:31 +02:00 committed by Thulinma
parent 5b79f296d6
commit fccf66fba2
280 changed files with 56975 additions and 71885 deletions

View file

@ -1,19 +1,17 @@
#include <sys/stat.h>
#include "output_http.h"
#include <mist/stream.h>
#include <mist/checksum.h>
#include <mist/util.h>
#include <mist/langcodes.h>
#include <mist/stream.h>
#include <mist/util.h>
#include <set>
#include <sys/stat.h>
namespace Mist {
HTTPOutput::HTTPOutput(Socket::Connection & conn) : Output(conn) {
namespace Mist{
HTTPOutput::HTTPOutput(Socket::Connection &conn) : Output(conn){
webSock = 0;
idleInterval = 0;
idleLast = 0;
if (config->getString("ip").size()){
myConn.setHost(config->getString("ip"));
}
if (config->getString("ip").size()){myConn.setHost(config->getString("ip"));}
firstRun = true;
if (config->getString("prequest").size()){
myConn.Received().prepend(config->getString("prequest"));
@ -21,14 +19,14 @@ namespace Mist {
config->activate();
}
HTTPOutput::~HTTPOutput() {
HTTPOutput::~HTTPOutput(){
if (webSock){
delete webSock;
webSock = 0;
}
}
void HTTPOutput::init(Util::Config * cfg){
void HTTPOutput::init(Util::Config *cfg){
Output::init(cfg);
capa["deps"] = "HTTP";
capa["forward"]["streamname"]["name"] = "Stream";
@ -40,33 +38,40 @@ namespace Mist {
capa["forward"]["ip"]["type"] = "str";
capa["forward"]["ip"]["option"] = "--ip";
capa["forward"]["ip"]["name"] = "Previous request";
capa["forward"]["ip"]["help"] = "Data to pretend arrived on the socket before parsing the socket.";
capa["forward"]["ip"]["help"] =
"Data to pretend arrived on the socket before parsing the socket.";
capa["forward"]["ip"]["type"] = "str";
capa["forward"]["ip"]["option"] = "--prequest";
cfg->addOption("streamname", JSON::fromString("{\"arg\":\"string\",\"short\":\"s\",\"long\":\"stream\",\"help\":\"The name of the stream that this connector will transmit.\"}"));
cfg->addOption("ip", JSON::fromString("{\"arg\":\"string\",\"short\":\"I\",\"long\":\"ip\",\"help\":\"IP address of connection on stdio.\"}"));
cfg->addOption("prequest", JSON::fromString("{\"arg\":\"string\",\"short\":\"R\",\"long\":\"prequest\",\"help\":\"Data to pretend arrived on the socket before parsing the socket.\"}"));
cfg->addOption("streamname", JSON::fromString("{\"arg\":\"string\",\"short\":\"s\",\"long\":"
"\"stream\",\"help\":\"The name of the stream "
"that this connector will transmit.\"}"));
cfg->addOption("ip", JSON::fromString("{\"arg\":\"string\",\"short\":\"I\",\"long\":\"ip\","
"\"help\":\"IP address of connection on stdio.\"}"));
cfg->addOption("prequest", JSON::fromString("{\"arg\":\"string\",\"short\":\"R\",\"long\":"
"\"prequest\",\"help\":\"Data to pretend arrived "
"on the socket before parsing the socket.\"}"));
cfg->addBasicConnectorOptions(capa);
config = cfg;
}
void HTTPOutput::onFail(const std::string & msg, bool critical){
void HTTPOutput::onFail(const std::string &msg, bool critical){
INFO_MSG("Failing '%s': %s", H.url.c_str(), msg.c_str());
if (!webSock && !isRecording()){
H.Clean(); //make sure no parts of old requests are left in any buffers
H.Clean(); // make sure no parts of old requests are left in any buffers
H.SetHeader("Server", "MistServer/" PACKAGE_VERSION);
H.setCORSHeaders();
H.SetBody("Could not retrieve stream: "+msg);
H.SetBody("Could not retrieve stream: " + msg);
H.SendResponse("404", "Error opening stream", myConn);
}
Output::onFail(msg, critical);
}
bool isMatch(const std::string & url, const std::string & m, std::string & streamname){
bool isMatch(const std::string &url, const std::string &m, std::string &streamname){
size_t found = m.find('$');
if (found != std::string::npos){
if (url.size() < m.size()){return false;}
if (m.substr(0, found) == url.substr(0, found) && m.substr(found+1) == url.substr(url.size() - (m.size() - found) + 1)){
if (m.substr(0, found) == url.substr(0, found) &&
m.substr(found + 1) == url.substr(url.size() - (m.size() - found) + 1)){
if (url.substr(found, url.size() - m.size() + 1).find('/') != std::string::npos){
return false;
}
@ -76,16 +81,14 @@ namespace Mist {
}
return (url == m);
}
bool isPrefix(const std::string & url, const std::string & m, std::string & streamname){
bool isPrefix(const std::string &url, const std::string &m, std::string &streamname){
size_t found = m.find('$');
if (found != std::string::npos){
if (url.size() < m.size()){return false;}
size_t found_suf = url.find(m.substr(found+1), found);
size_t found_suf = url.find(m.substr(found + 1), found);
if (m.substr(0, found) == url.substr(0, found) && found_suf != std::string::npos){
if (url.substr(found, found_suf - found).find('/') != std::string::npos){
return false;
}
if (url.substr(found, found_suf - found).find('/') != std::string::npos){return false;}
streamname = url.substr(found, found_suf - found);
return true;
}
@ -94,18 +97,18 @@ namespace Mist {
}
return false;
}
/// - anything else: The request should be dispatched to a connector on the named socket.
std::string HTTPOutput::getHandler(){
std::string url = H.getUrl();
//check the current output first, the most common case
// check the current output first, the most common case
if (capa.isMember("url_match") || capa.isMember("url_prefix")){
bool match = false;
std::string streamname;
//if there is a matcher, try to match
// if there is a matcher, try to match
if (capa.isMember("url_match")){
if (capa["url_match"].isArray()){
jsonForEach(capa["url_match"], it) {
jsonForEach(capa["url_match"], it){
match |= isMatch(url, it->asStringRef(), streamname);
}
}
@ -113,10 +116,10 @@ namespace Mist {
match |= isMatch(url, capa["url_match"].asStringRef(), streamname);
}
}
//if there is a prefix, try to match
// if there is a prefix, try to match
if (capa.isMember("url_prefix")){
if (capa["url_prefix"].isArray()){
jsonForEach(capa["url_prefix"], it) {
jsonForEach(capa["url_prefix"], it){
match |= isPrefix(url, it->asStringRef(), streamname);
}
}
@ -132,18 +135,19 @@ namespace Mist {
return capa["name"].asStringRef();
}
}
//loop over the connectors
// loop over the connectors
Util::DTSCShmReader rCapa(SHM_CAPA);
DTSC::Scan capa = rCapa.getMember("connectors");
unsigned int capa_ctr = capa.getSize();
for (unsigned int i = 0; i < capa_ctr; ++i){
DTSC::Scan c = capa.getIndice(i);
//if it depends on HTTP and has a match or prefix...
if ((c.getMember("name").asString() == "HTTP" || c.getMember("deps").asString() == "HTTP") && (c.getMember("url_match") || c.getMember("url_prefix"))){
// if it depends on HTTP and has a match or prefix...
if ((c.getMember("name").asString() == "HTTP" || c.getMember("deps").asString() == "HTTP") &&
(c.getMember("url_match") || c.getMember("url_prefix"))){
bool match = false;
std::string streamname;
//if there is a matcher, try to match
// if there is a matcher, try to match
if (c.getMember("url_match")){
if (c.getMember("url_match").getSize()){
for (unsigned int j = 0; j < c.getMember("url_match").getSize(); ++j){
@ -153,7 +157,7 @@ namespace Mist {
match |= isMatch(url, c.getMember("url_match").asString(), streamname);
}
}
//if there is a prefix, try to match
// if there is a prefix, try to match
if (c.getMember("url_prefix")){
if (c.getMember("url_prefix").getSize()){
for (unsigned int j = 0; j < c.getMember("url_prefix").getSize(); ++j){
@ -174,14 +178,14 @@ namespace Mist {
}
return "";
}
void HTTPOutput::requestHandler(){
//Handle onIdle function caller, if needed
// Handle onIdle function caller, if needed
if (idleInterval && (Util::bootMS() > idleLast + idleInterval)){
onIdle();
idleLast = Util::bootMS();
}
//Handle websockets
// Handle websockets
if (webSock){
if (webSock->readFrame()){
onWebsocketFrame();
@ -191,7 +195,7 @@ namespace Mist {
if (!isBlocking && !parseData){Util::sleep(100);}
return;
}
//If we can't read anything more and we're non-blocking, sleep some.
// If we can't read anything more and we're non-blocking, sleep some.
if (!firstRun && !myConn.spool()){
if (!isBlocking && !parseData){Util::sleep(100);}
return;
@ -200,20 +204,24 @@ namespace Mist {
while (H.Read(myConn)){
std::string handler = getHandler();
INFO_MSG("Received request: %s => %s (%s)", H.getUrl().c_str(), handler.c_str(), H.GetVar("stream").c_str());
INFO_MSG("Received request: %s => %s (%s)", H.getUrl().c_str(), handler.c_str(),
H.GetVar("stream").c_str());
if (!handler.size()){
H.Clean();
H.SetHeader("Server", "MistServer/" PACKAGE_VERSION);
H.setCORSHeaders();
H.SetBody("<!DOCTYPE html><html><head><title>Unsupported Media Type</title></head><body><h1>Unsupported Media Type</h1>The server isn't quite sure what you wanted to receive from it.</body></html>");
H.SetBody("<!DOCTYPE html><html><head><title>Unsupported Media "
"Type</title></head><body><h1>Unsupported Media Type</h1>The server isn't quite "
"sure what you wanted to receive from it.</body></html>");
H.SendResponse("415", "Unsupported Media Type", myConn);
myConn.close();
return;
}
std::string connHeader = H.GetHeader("Connection");
Util::stringToLower(connHeader);
std::string connHeader = H.GetHeader("Connection");
Util::stringToLower(connHeader);
if (handler != capa["name"].asStringRef() || H.GetVar("stream") != streamName){
MEDIUM_MSG("Switching from %s (%s) to %s (%s)", capa["name"].asStringRef().c_str(), streamName.c_str(), handler.c_str(), H.GetVar("stream").c_str());
MEDIUM_MSG("Switching from %s (%s) to %s (%s)", capa["name"].asStringRef().c_str(),
streamName.c_str(), handler.c_str(), H.GetVar("stream").c_str());
streamName = H.GetVar("stream");
nProxy.userClient.finish();
statsPage.finish();
@ -225,9 +233,7 @@ namespace Mist {
/*LTS-START*/
reqUrl = H.url + H.allVars();
/*LTS-END*/
if (H.hasHeader("User-Agent")){
UA = H.GetHeader("User-Agent");
}
if (H.hasHeader("User-Agent")){UA = H.GetHeader("User-Agent");}
if (hasSessionIDs()){
if (H.GetVar("sessId").size()){
std::string ua = H.GetVar("sessId");
@ -248,8 +254,8 @@ namespace Mist {
if (H.GetVar("stop") != ""){targetParams["stop"] = H.GetVar("stop");}
if (H.GetVar("startunix") != ""){targetParams["startunix"] = H.GetVar("startunix");}
if (H.GetVar("stopunix") != ""){targetParams["stopunix"] = H.GetVar("stopunix");}
//allow setting of play back rate through buffer variable.
//play back rate is set in MS per second, but the variable is a simple multiplier.
// allow setting of play back rate through buffer variable.
// play back rate is set in MS per second, but the variable is a simple multiplier.
if (H.GetVar("rate") != ""){
long long int multiplier = JSON::Value(H.GetVar("rate")).asInt();
if (multiplier){
@ -266,7 +272,7 @@ namespace Mist {
realTime = 0;
}
}
//Handle upgrade to websocket if the output supports it
// Handle upgrade to websocket if the output supports it
std::string upgradeHeader = H.GetHeader("Upgrade");
Util::stringToLower(upgradeHeader);
if (doesWebsockets() && upgradeHeader == "websocket"){
@ -287,9 +293,7 @@ namespace Mist {
preHTTP();
onHTTP();
idleLast = Util::bootMS();
if (!H.bufferChunks){
H.Clean();
}
if (!H.bufferChunks){H.Clean();}
}
}
@ -298,54 +302,54 @@ namespace Mist {
initialize();
selectDefaultTracks();
}
static inline void builPipedPart(JSON::Value & p, char * argarr[], int & argnum, JSON::Value & argset){
jsonForEach(argset, it) {
static inline void builPipedPart(JSON::Value &p, char *argarr[], int &argnum, JSON::Value &argset){
jsonForEach(argset, it){
if (it->isMember("option") && p.isMember(it.key())){
if (it->isMember("type")){
if ((*it)["type"].asStringRef() == "str" && !p[it.key()].isString()){
p[it.key()] = p[it.key()].asString();
}
if ((*it)["type"].asStringRef() == "uint" || (*it)["type"].asStringRef() == "int" || (*it)["type"].asStringRef() == "debug"){
if ((*it)["type"].asStringRef() == "uint" || (*it)["type"].asStringRef() == "int" ||
(*it)["type"].asStringRef() == "debug"){
p[it.key()] = JSON::Value(p[it.key()].asInt()).asString();
}
if ((*it)["type"].asStringRef() == "inputlist" && p[it.key()].isArray()){
jsonForEach(p[it.key()], iVal){
(*iVal) = iVal->asString();
argarr[argnum++] = (char*)((*it)["option"].c_str());
argarr[argnum++] = (char*)((*iVal).c_str());
argarr[argnum++] = (char *)((*it)["option"].c_str());
argarr[argnum++] = (char *)((*iVal).c_str());
}
continue;
}
}
if (p[it.key()].asStringRef().size() > 0){
argarr[argnum++] = (char*)((*it)["option"].c_str());
argarr[argnum++] = (char*)(p[it.key()].c_str());
argarr[argnum++] = (char *)((*it)["option"].c_str());
argarr[argnum++] = (char *)(p[it.key()].c_str());
}else{
argarr[argnum++] = (char*)((*it)["option"].c_str());
argarr[argnum++] = (char *)((*it)["option"].c_str());
}
}
}
}
///\brief Handles requests by starting a corresponding output process.
///\param connector The type of connector to be invoked.
void HTTPOutput::reConnector(std::string & connector){
//taken from CheckProtocols (controller_connectors.cpp)
char * argarr[32];
for (int i=0; i<32; i++){argarr[i] = 0;}
void HTTPOutput::reConnector(std::string &connector){
// taken from CheckProtocols (controller_connectors.cpp)
char *argarr[32];
for (int i = 0; i < 32; i++){argarr[i] = 0;}
int id = -1;
JSON::Value pipedCapa;
JSON::Value p;//properties of protocol
JSON::Value p; // properties of protocol
{
Util::DTSCShmReader rProto(SHM_PROTO);
DTSC::Scan prots = rProto.getScan();
unsigned int prots_ctr = prots.getSize();
if (connector == "HTTP" || connector == "HTTP.exe"){
//restore from values in the environment, regardless of configged settings
// restore from values in the environment, regardless of configged settings
if (getenv("MIST_HTTP_nostreamtext")){
p["nostreamtext"] = getenv("MIST_HTTP_nostreamtext");
}
@ -354,60 +358,60 @@ namespace Mist {
p["pubaddr"] = JSON::fromString(pubAddrs);
}
}else{
//find connector in config
for (unsigned int i=0; i < prots_ctr; ++i){
if (prots.getIndice(i).getMember("connector").asString() == connector) {
id = i;
break; //pick the first protocol in the list that matches the connector
// find connector in config
for (unsigned int i = 0; i < prots_ctr; ++i){
if (prots.getIndice(i).getMember("connector").asString() == connector){
id = i;
break; // pick the first protocol in the list that matches the connector
}
}
if (id == -1) {
if (id == -1){
connector = connector + ".exe";
for (unsigned int i=0; i < prots_ctr; ++i){
if (prots.getIndice(i).getMember("connector").asString() == connector) {
id = i;
break; //pick the first protocol in the list that matches the connector
for (unsigned int i = 0; i < prots_ctr; ++i){
if (prots.getIndice(i).getMember("connector").asString() == connector){
id = i;
break; // pick the first protocol in the list that matches the connector
}
}
if (id == -1) {
if (id == -1){
connector = connector.substr(0, connector.size() - 4);
ERROR_MSG("No connector found for: %s", connector.c_str());
return;
}
}
//read options from found connector
// read options from found connector
p = prots.getIndice(id).asJSON();
}
HIGH_MSG("Connector found: %s", connector.c_str());
Util::DTSCShmReader rCapa(SHM_CAPA);
DTSC::Scan capa = rCapa.getMember("connectors");
pipedCapa = capa.getMember(connector).asJSON();
}
//build arguments for starting output process
// build arguments for starting output process
std::string tmparg = Util::getMyPath() + std::string("MistOut") + connector;
std::string tmpPrequest;
if (H.url.size()){tmpPrequest = H.BuildRequest();}
int argnum = 0;
argarr[argnum++] = (char*)tmparg.c_str();
std::string temphost=getConnectedHost();
argarr[argnum++] = (char *)tmparg.c_str();
std::string temphost = getConnectedHost();
std::string debuglevel = JSON::Value(Util::Config::printDebugLevel).asString();
argarr[argnum++] = (char*)"--ip";
argarr[argnum++] = (char*)(temphost.c_str());
argarr[argnum++] = (char*)"--stream";
argarr[argnum++] = (char*)(streamName.c_str());
argarr[argnum++] = (char*)"--prequest";
argarr[argnum++] = (char*)(tmpPrequest.c_str());
//set the debug level if non-default
argarr[argnum++] = (char *)"--ip";
argarr[argnum++] = (char *)(temphost.c_str());
argarr[argnum++] = (char *)"--stream";
argarr[argnum++] = (char *)(streamName.c_str());
argarr[argnum++] = (char *)"--prequest";
argarr[argnum++] = (char *)(tmpPrequest.c_str());
// set the debug level if non-default
if (Util::Config::printDebugLevel != DEBUG){
argarr[argnum++] = (char*)"--debug";
argarr[argnum++] = (char*)(debuglevel.c_str());
argarr[argnum++] = (char *)"--debug";
argarr[argnum++] = (char *)(debuglevel.c_str());
}
if (pipedCapa.isMember("required")){builPipedPart(p, argarr, argnum, pipedCapa["required"]);}
if (pipedCapa.isMember("optional")){builPipedPart(p, argarr, argnum, pipedCapa["optional"]);}
///start new/better process
/// start new/better process
execv(argarr[0], argarr);
}
@ -427,7 +431,7 @@ namespace Mist {
return xRealIp;
}
std::string HTTPOutput::getConnectedBinHost(){
//Do first check with connected host because of simplicity
// Do first check with connected host because of simplicity
std::string host = Output::getConnectedHost();
std::string xRealIp = H.GetHeader("X-Real-IP");
@ -439,13 +443,13 @@ namespace Mist {
}
return Output::getConnectedBinHost();
}
Socket::Connection binConn;
binConn.setHost(xRealIp);
return binConn.getBinHost();
}
bool HTTPOutput::isTrustedProxy(const std::string & ip){
bool HTTPOutput::isTrustedProxy(const std::string &ip){
static std::set<std::string> trustedProxies;
if (!trustedProxies.size()){
trustedProxies.insert("localhost");
@ -460,9 +464,7 @@ namespace Mist {
endPos = trustedList.find(" ", pos);
trustedProxies.insert(trustedList.substr(pos, endPos - pos));
pos = endPos;
if (pos != std::string::npos){
pos++;
}
if (pos != std::string::npos){pos++;}
}
}
}
@ -473,11 +475,11 @@ namespace Mist {
return false;
}
/*LTS-END*/
/// Parses a "Range: " header, setting byteStart and byteEnd.
/// Assumes byteStart and byteEnd are initialized to their minimum respectively maximum values when the function is called.
/// On error, byteEnd is set to zero and the function return false.
bool HTTPOutput::parseRange(uint64_t & byteStart, uint64_t & byteEnd){
/// Assumes byteStart and byteEnd are initialized to their minimum respectively maximum values
/// when the function is called. On error, byteEnd is set to zero and the function return false.
bool HTTPOutput::parseRange(uint64_t &byteStart, uint64_t &byteEnd){
std::string header = H.GetHeader("Range");
if (header.size() < 6 || header.substr(0, 6) != "bytes="){
byteEnd = 0;
@ -485,9 +487,9 @@ namespace Mist {
return false;
}
header.erase(0, 6);
//Do parsing of the rest of the header...
// Do parsing of the rest of the header...
if (header.size() && header[0] == '-'){
//negative range = count from end
// negative range = count from end
byteStart = 0;
for (unsigned int i = 1; i < header.size(); ++i){
if (header[i] >= '0' && header[i] <= '9'){
@ -498,17 +500,17 @@ namespace Mist {
break;
}
if (byteStart > byteEnd){
//entire file if starting before byte zero
// entire file if starting before byte zero
byteStart = 0;
}else{
//start byteStart bytes before byteEnd
// start byteStart bytes before byteEnd
byteStart = byteEnd - byteStart;
}
MEDIUM_MSG("Range request: %" PRIu64 "-%" PRIu64 " (%s)", byteStart, byteEnd, header.c_str());
return true;
}
//Positive range
// Positive range
long long size = byteEnd;
byteEnd = 0;
byteStart = 0;
@ -536,9 +538,7 @@ namespace Mist {
}
break;
}
if (byteEnd > size){
byteEnd = size;
}
if (byteEnd > size){byteEnd = size;}
}else{
byteEnd = size;
}
@ -546,5 +546,4 @@ namespace Mist {
return true;
}
}
}// namespace Mist