2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2010-07-20 14:26:09 +02:00
|
|
|
* $Id: connectionHTTP.c,v 1.13.2.4 2010/07/20 12:26:10 schmirl Exp $
|
2004-12-30 23:43:55 +01:00
|
|
|
*/
|
2005-04-24 18:26:14 +02:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2004-12-30 23:43:55 +01:00
|
|
|
|
|
|
|
#include "server/connectionHTTP.h"
|
2008-03-28 16:11:40 +01:00
|
|
|
#include "server/menuHTTP.h"
|
2008-10-14 13:05:57 +02:00
|
|
|
#include "server/server.h"
|
2004-12-30 23:43:55 +01:00
|
|
|
#include "server/setup.h"
|
|
|
|
|
2005-02-11 17:44:14 +01:00
|
|
|
cConnectionHTTP::cConnectionHTTP(void):
|
|
|
|
cServerConnection("HTTP"),
|
|
|
|
m_Status(hsRequest),
|
|
|
|
m_LiveStreamer(NULL),
|
|
|
|
m_Channel(NULL),
|
|
|
|
m_StreamType((eStreamType)StreamdevServerSetup.HTTPStreamType),
|
2008-03-28 16:11:40 +01:00
|
|
|
m_ChannelList(NULL)
|
2005-02-11 17:44:14 +01:00
|
|
|
{
|
|
|
|
Dprintf("constructor hsRequest\n");
|
2010-07-19 15:50:11 +02:00
|
|
|
m_Apid[0] = m_Apid[1] = 0;
|
|
|
|
m_Dpid[0] = m_Dpid[1] = 0;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-02-11 17:44:14 +01:00
|
|
|
cConnectionHTTP::~cConnectionHTTP()
|
|
|
|
{
|
|
|
|
delete m_LiveStreamer;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2008-10-14 13:05:57 +02:00
|
|
|
bool cConnectionHTTP::CanAuthenticate(void)
|
|
|
|
{
|
|
|
|
return opt_auth != NULL;
|
|
|
|
}
|
|
|
|
|
2005-02-11 17:44:14 +01:00
|
|
|
bool cConnectionHTTP::Command(char *Cmd)
|
|
|
|
{
|
|
|
|
Dprintf("command %s\n", Cmd);
|
2004-12-30 23:43:55 +01:00
|
|
|
switch (m_Status) {
|
|
|
|
case hsRequest:
|
2010-07-19 15:50:11 +02:00
|
|
|
// parse METHOD PATH[?QUERY] VERSION
|
|
|
|
{
|
|
|
|
char *p, *q, *v;
|
|
|
|
p = strchr(Cmd, ' ');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
v = strchr(++p, ' ');
|
|
|
|
if (v) {
|
|
|
|
*v = 0;
|
|
|
|
SetHeader("REQUEST_METHOD", Cmd);
|
|
|
|
q = strchr(p, '?');
|
|
|
|
if (q)
|
|
|
|
*q = 0;
|
|
|
|
SetHeader("QUERY_STRING", q ? ++q : "");
|
|
|
|
SetHeader("PATH_INFO", p);
|
|
|
|
m_Status = hsHeaders;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
|
|
|
case hsHeaders:
|
|
|
|
if (*Cmd == '\0') {
|
2005-02-11 17:44:14 +01:00
|
|
|
m_Status = hsBody;
|
|
|
|
return ProcessRequest();
|
|
|
|
}
|
2010-07-19 15:50:11 +02:00
|
|
|
else if (isspace(*Cmd)) {
|
|
|
|
; //TODO: multi-line header
|
2008-10-14 13:05:57 +02:00
|
|
|
}
|
2010-07-19 15:50:11 +02:00
|
|
|
else {
|
|
|
|
// convert header name to CGI conventions:
|
|
|
|
// uppercase, '-' replaced with '_', prefix "HTTP_"
|
|
|
|
char *p;
|
|
|
|
for (p = Cmd; *p != 0 && *p != ':'; p++) {
|
|
|
|
if (*p == '-')
|
|
|
|
*p = '_';
|
|
|
|
else
|
|
|
|
*p = toupper(*p);
|
|
|
|
}
|
|
|
|
if (*p == ':') {
|
|
|
|
*p = 0;
|
|
|
|
p = skipspace(++p);
|
|
|
|
// don't disclose Authorization header
|
|
|
|
if (strcmp(Cmd, "AUTHORIZATION") == 0) {
|
|
|
|
char *q;
|
|
|
|
for (q = p; *q != 0 && *q != ' '; q++)
|
|
|
|
*q = toupper(*q);
|
|
|
|
if (p != q) {
|
|
|
|
*q = 0;
|
|
|
|
SetHeader("AUTH_TYPE", p);
|
|
|
|
m_Authorization = (std::string) skipspace(++q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetHeader(Cmd, p, "HTTP_");
|
2008-10-14 13:05:57 +02:00
|
|
|
}
|
2008-03-28 16:11:40 +01:00
|
|
|
}
|
2005-02-11 17:44:14 +01:00
|
|
|
return true;
|
2007-04-16 13:01:02 +02:00
|
|
|
default:
|
2009-02-13 08:02:25 +01:00
|
|
|
// skip additional blank lines
|
|
|
|
if (*Cmd == '\0')
|
|
|
|
return true;
|
2007-04-16 13:01:02 +02:00
|
|
|
break;
|
2005-02-11 17:44:14 +01:00
|
|
|
}
|
|
|
|
return false; // ??? shouldn't happen
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
bool cConnectionHTTP::ProcessRequest(void)
|
|
|
|
{
|
2010-07-19 15:50:11 +02:00
|
|
|
// keys for Headers() hash
|
|
|
|
const static std::string AUTH_TYPE("AUTH_TYPE");
|
|
|
|
const static std::string REQUEST_METHOD("REQUEST_METHOD");
|
|
|
|
const static std::string PATH_INFO("PATH_INFO");
|
|
|
|
|
2005-02-11 17:44:14 +01:00
|
|
|
Dprintf("process\n");
|
2010-07-19 15:50:11 +02:00
|
|
|
if (!StreamdevHosts.Acceptable(RemoteIpAddr())) {
|
|
|
|
bool authOk = opt_auth && !m_Authorization.empty();
|
|
|
|
if (authOk) {
|
|
|
|
tStrStrMap::const_iterator it = Headers().find(AUTH_TYPE);
|
|
|
|
|
|
|
|
if (it == Headers().end()) {
|
|
|
|
// no authorization header present
|
|
|
|
authOk = false;
|
|
|
|
}
|
|
|
|
else if (it->second.compare("BASIC") == 0) {
|
|
|
|
// basic auth
|
|
|
|
authOk &= m_Authorization.compare(opt_auth) == 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// unsupported auth type
|
|
|
|
authOk = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!authOk) {
|
2008-10-14 13:05:57 +02:00
|
|
|
isyslog("streamdev-server: HTTP authorization required");
|
|
|
|
DeferClose();
|
|
|
|
return Respond("HTTP/1.0 401 Authorization Required")
|
|
|
|
&& Respond("WWW-authenticate: basic Realm=\"Streamdev-Server\")")
|
|
|
|
&& Respond("");
|
|
|
|
}
|
|
|
|
}
|
2005-02-11 17:44:14 +01:00
|
|
|
|
2010-07-19 15:50:11 +02:00
|
|
|
if (Headers().at(REQUEST_METHOD).compare("GET") == 0 && ProcessURI(Headers().at(PATH_INFO))) {
|
|
|
|
if (m_ChannelList)
|
|
|
|
return Respond("%s", true, m_ChannelList->HttpHeader().c_str());
|
|
|
|
else if (m_Channel != NULL) {
|
2004-12-30 23:43:55 +01:00
|
|
|
cDevice *device = GetDevice(m_Channel, 0);
|
|
|
|
if (device != NULL) {
|
|
|
|
device->SwitchChannel(m_Channel, false);
|
2010-07-19 15:50:11 +02:00
|
|
|
m_LiveStreamer = new cStreamdevLiveStreamer(0, this);
|
|
|
|
if (m_LiveStreamer->SetChannel(m_Channel, m_StreamType, m_Apid[0] ? m_Apid : NULL, m_Dpid[0] ? m_Dpid : NULL)) {
|
2005-02-08 20:54:52 +01:00
|
|
|
m_LiveStreamer->SetDevice(device);
|
2007-05-09 11:12:42 +02:00
|
|
|
if (!SetDSCP())
|
|
|
|
LOG_ERROR_STR("unable to set DSCP sockopt");
|
2010-07-19 15:50:11 +02:00
|
|
|
if (m_StreamType == stEXT) {
|
|
|
|
return Respond("HTTP/1.0 200 OK");
|
|
|
|
} else if (ISRADIO(m_Channel) || (m_StreamType == stES && (m_Apid[0] || m_Dpid[0]))) {
|
2004-12-30 23:43:55 +01:00
|
|
|
return Respond("HTTP/1.0 200 OK")
|
2005-02-11 17:44:14 +01:00
|
|
|
&& Respond("Content-Type: audio/mpeg")
|
2005-05-09 22:22:29 +02:00
|
|
|
&& Respond("icy-name: %s", true, m_Channel->Name())
|
2005-02-11 17:44:14 +01:00
|
|
|
&& Respond("");
|
2004-12-30 23:43:55 +01:00
|
|
|
} else {
|
|
|
|
return Respond("HTTP/1.0 200 OK")
|
2005-02-11 17:44:14 +01:00
|
|
|
&& Respond("Content-Type: video/mpeg")
|
|
|
|
&& Respond("");
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
}
|
2010-07-19 15:50:11 +02:00
|
|
|
DELETENULL(m_LiveStreamer);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
DeferClose();
|
2006-01-26 20:40:18 +01:00
|
|
|
return Respond("HTTP/1.0 409 Channel not available")
|
|
|
|
&& Respond("");
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2010-07-19 15:50:11 +02:00
|
|
|
else {
|
|
|
|
DeferClose();
|
|
|
|
return Respond("HTTP/1.0 404 not found")
|
|
|
|
&& Respond("");
|
|
|
|
}
|
|
|
|
} else if (Headers().at(REQUEST_METHOD).compare("HEAD") == 0 && ProcessURI(Headers().at(PATH_INFO))) {
|
|
|
|
DeferClose();
|
|
|
|
if (m_ChannelList)
|
|
|
|
return Respond("%s", true, m_ChannelList->HttpHeader().c_str());
|
|
|
|
else if (m_Channel != NULL) {
|
|
|
|
cDevice *device = GetDevice(m_Channel, 0);
|
|
|
|
if (device != NULL) {
|
|
|
|
if (m_StreamType == stEXT) {
|
|
|
|
// TODO
|
|
|
|
return Respond("HTTP/1.0 200 OK")
|
|
|
|
&& Respond("");
|
|
|
|
} else if (ISRADIO(m_Channel) || (m_StreamType == stES && (m_Apid[0] || m_Dpid[0]))) {
|
|
|
|
return Respond("HTTP/1.0 200 OK")
|
|
|
|
&& Respond("Content-Type: audio/mpeg")
|
|
|
|
&& Respond("icy-name: %s", true, m_Channel->Name())
|
|
|
|
&& Respond("");
|
|
|
|
} else {
|
|
|
|
return Respond("HTTP/1.0 200 OK")
|
|
|
|
&& Respond("Content-Type: video/mpeg")
|
|
|
|
&& Respond("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Respond("HTTP/1.0 409 Channel not available")
|
|
|
|
&& Respond("");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Respond("HTTP/1.0 404 not found")
|
|
|
|
&& Respond("");
|
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2005-02-11 17:44:14 +01:00
|
|
|
|
|
|
|
DeferClose();
|
2006-01-26 20:40:18 +01:00
|
|
|
return Respond("HTTP/1.0 400 Bad Request")
|
|
|
|
&& Respond("");
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-05-09 22:22:29 +02:00
|
|
|
void cConnectionHTTP::Flushed(void)
|
|
|
|
{
|
2005-02-11 17:44:14 +01:00
|
|
|
std::string line;
|
|
|
|
|
|
|
|
if (m_Status != hsBody)
|
|
|
|
return;
|
|
|
|
|
2010-07-19 15:50:11 +02:00
|
|
|
if (m_ChannelList) {
|
|
|
|
if (m_ChannelList->HasNext()) {
|
|
|
|
if (!Respond("%s", true, m_ChannelList->Next().c_str()))
|
2008-03-28 16:11:40 +01:00
|
|
|
DeferClose();
|
2005-02-11 17:44:14 +01:00
|
|
|
}
|
2010-07-19 15:50:11 +02:00
|
|
|
else {
|
|
|
|
DELETENULL(m_ChannelList);
|
|
|
|
m_Status = hsFinished;
|
|
|
|
DeferClose();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (m_Channel != NULL) {
|
2004-12-30 23:43:55 +01:00
|
|
|
Dprintf("streamer start\n");
|
|
|
|
m_LiveStreamer->Start(this);
|
2005-02-11 17:44:14 +01:00
|
|
|
m_Status = hsFinished;
|
2010-07-19 15:50:11 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// should never be reached
|
|
|
|
esyslog("streamdev-server cConnectionHTTP::Flushed(): no job to do");
|
|
|
|
m_Status = hsFinished;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-19 15:50:11 +02:00
|
|
|
cChannelList* cConnectionHTTP::ChannelListFromString(const std::string& Path, const std::string& Filebase, const std::string& Fileext) const
|
2005-05-09 22:22:29 +02:00
|
|
|
{
|
2010-07-19 15:50:11 +02:00
|
|
|
// keys for Headers() hash
|
|
|
|
const static std::string QUERY_STRING("QUERY_STRING");
|
|
|
|
const static std::string HOST("HTTP_HOST");
|
2008-03-28 16:11:40 +01:00
|
|
|
|
2010-07-19 15:50:11 +02:00
|
|
|
const std::string query = Headers().at(QUERY_STRING);
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2008-03-28 16:11:40 +01:00
|
|
|
std::string groupTarget;
|
|
|
|
cChannelIterator *iterator = NULL;
|
2005-02-11 17:44:14 +01:00
|
|
|
|
2010-07-19 15:50:11 +02:00
|
|
|
if (Filebase.compare("tree") == 0) {
|
2008-03-28 16:11:40 +01:00
|
|
|
const cChannel* c = NULL;
|
|
|
|
size_t groupIndex = query.find("group=");
|
|
|
|
if (groupIndex != std::string::npos)
|
|
|
|
c = cChannelList::GetGroup(atoi(query.c_str() + groupIndex + 6));
|
|
|
|
iterator = new cListTree(c);
|
2010-07-19 15:50:11 +02:00
|
|
|
groupTarget = Filebase + Fileext;
|
|
|
|
} else if (Filebase.compare("groups") == 0) {
|
2008-03-28 16:11:40 +01:00
|
|
|
iterator = new cListGroups();
|
2010-07-19 15:50:11 +02:00
|
|
|
groupTarget = (std::string) "group" + Fileext;
|
|
|
|
} else if (Filebase.compare("group") == 0) {
|
2008-03-28 16:11:40 +01:00
|
|
|
const cChannel* c = NULL;
|
|
|
|
size_t groupIndex = query.find("group=");
|
|
|
|
if (groupIndex != std::string::npos)
|
|
|
|
c = cChannelList::GetGroup(atoi(query.c_str() + groupIndex + 6));
|
|
|
|
iterator = new cListGroup(c);
|
2010-07-19 15:50:11 +02:00
|
|
|
} else if (Filebase.compare("channels") == 0) {
|
2008-03-28 16:11:40 +01:00
|
|
|
iterator = new cListChannels();
|
2010-07-19 15:50:11 +02:00
|
|
|
} else if (Filebase.compare("all") == 0 ||
|
|
|
|
(Filebase.empty() && Fileext.empty())) {
|
2008-03-28 16:11:40 +01:00
|
|
|
iterator = new cListAll();
|
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
|
2008-03-28 16:11:40 +01:00
|
|
|
if (iterator) {
|
2010-07-19 15:50:11 +02:00
|
|
|
if (Filebase.empty() || Fileext.compare(".htm") == 0 || Fileext.compare(".html") == 0) {
|
|
|
|
std::string self = Filebase + Fileext;
|
|
|
|
if (!query.empty())
|
|
|
|
self += '?' + query;
|
|
|
|
return new cHtmlChannelList(iterator, m_StreamType, self.c_str(), groupTarget.c_str());
|
|
|
|
} else if (Fileext.compare(".m3u") == 0) {
|
2008-03-28 16:11:40 +01:00
|
|
|
std::string base;
|
2010-07-19 15:50:11 +02:00
|
|
|
tStrStrMap::const_iterator it = Headers().find(HOST);
|
|
|
|
if (it != Headers().end())
|
|
|
|
base = "http://" + it->second + "/";
|
2008-03-28 16:11:40 +01:00
|
|
|
else
|
|
|
|
base = (std::string) "http://" + LocalIp() + ":" +
|
|
|
|
(const char*) itoa(StreamdevServerSetup.HTTPServerPort) + "/";
|
2010-07-19 15:50:11 +02:00
|
|
|
base += Path;
|
|
|
|
return new cM3uChannelList(iterator, base.c_str());
|
2008-03-28 16:11:40 +01:00
|
|
|
} else {
|
|
|
|
delete iterator;
|
|
|
|
}
|
2010-07-19 15:50:11 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cConnectionHTTP::ProcessURI(const std::string& PathInfo)
|
|
|
|
{
|
|
|
|
std::string filespec, fileext;
|
|
|
|
size_t file_pos = PathInfo.rfind('/');
|
|
|
|
|
|
|
|
if (file_pos != std::string::npos) {
|
|
|
|
size_t ext_pos = PathInfo.rfind('.');
|
|
|
|
// file basename with leading / stripped off
|
|
|
|
filespec = PathInfo.substr(file_pos + 1, ext_pos - file_pos - 1);
|
|
|
|
if (ext_pos != std::string::npos)
|
|
|
|
// file extension including leading .
|
|
|
|
fileext = PathInfo.substr(ext_pos);
|
|
|
|
}
|
|
|
|
if (fileext.length() > 5) {
|
|
|
|
//probably not an extension
|
|
|
|
filespec += fileext;
|
|
|
|
fileext.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Streamtype with leading / stripped off
|
|
|
|
std::string type = PathInfo.substr(1, PathInfo.find_first_of("/;", 1) - 1);
|
|
|
|
const char* pType = type.c_str();
|
|
|
|
if (strcasecmp(pType, "PS") == 0) {
|
|
|
|
m_StreamType = stPS;
|
|
|
|
} else if (strcasecmp(pType, "PES") == 0) {
|
|
|
|
m_StreamType = stPES;
|
|
|
|
} else if (strcasecmp(pType, "TS") == 0) {
|
|
|
|
m_StreamType = stTS;
|
|
|
|
} else if (strcasecmp(pType, "ES") == 0) {
|
|
|
|
m_StreamType = stES;
|
|
|
|
} else if (strcasecmp(pType, "EXT") == 0) {
|
|
|
|
m_StreamType = stEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dprintf("before channelfromstring: type(%s) filespec(%s) fileext(%s)\n", type.c_str(), filespec.c_str(), fileext.c_str());
|
|
|
|
|
2010-07-20 14:26:09 +02:00
|
|
|
if ((m_ChannelList = ChannelListFromString(PathInfo.substr(1, file_pos), filespec.c_str(), fileext.c_str())) != NULL) {
|
2010-07-19 15:50:11 +02:00
|
|
|
Dprintf("Channel list requested\n");
|
|
|
|
return true;
|
|
|
|
} else if ((m_Channel = ChannelFromString(filespec.c_str(), &m_Apid[0], &m_Dpid[0])) != NULL) {
|
|
|
|
Dprintf("Channel found. Apid/Dpid is %d/%d\n", m_Apid[0], m_Dpid[0]);
|
|
|
|
return true;
|
2008-03-28 16:11:40 +01:00
|
|
|
} else
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|