2007-09-16 11:38:00 +02:00
|
|
|
/*
|
|
|
|
* protocolhttp.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2007-09-19 19:14:03 +02:00
|
|
|
#include <string.h>
|
2007-09-16 11:38:00 +02:00
|
|
|
|
|
|
|
#include <vdr/device.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2007-09-16 15:38:20 +02:00
|
|
|
#include "config.h"
|
2007-09-16 11:38:00 +02:00
|
|
|
#include "protocolhttp.h"
|
|
|
|
|
|
|
|
cIptvProtocolHttp::cIptvProtocolHttp()
|
2013-02-23 14:31:11 +01:00
|
|
|
: streamAddrM(strdup("")),
|
|
|
|
streamPathM(strdup("/")),
|
|
|
|
streamPortM(0)
|
2007-09-16 11:38:00 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-16 11:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvProtocolHttp::~cIptvProtocolHttp()
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-16 11:38:00 +02:00
|
|
|
// Close the socket
|
2008-01-05 00:36:37 +01:00
|
|
|
cIptvProtocolHttp::Close();
|
2007-09-16 11:38:00 +02:00
|
|
|
// Free allocated memory
|
2013-02-23 14:31:11 +01:00
|
|
|
free(streamPathM);
|
|
|
|
free(streamAddrM);
|
2007-09-16 11:38:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::Connect(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-16 11:38:00 +02:00
|
|
|
// Check that stream address is valid
|
2013-02-23 16:12:46 +01:00
|
|
|
if (!isActiveM && !isempty(streamAddrM) && !isempty(streamPathM)) {
|
2010-09-15 22:41:25 +02:00
|
|
|
// Ensure that socket is valid and connect
|
2013-02-23 14:31:11 +01:00
|
|
|
OpenSocket(streamPortM, streamAddrM);
|
2010-09-15 22:41:25 +02:00
|
|
|
if (!ConnectSocket()) {
|
2007-09-29 13:17:57 +02:00
|
|
|
CloseSocket();
|
2007-09-16 11:38:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-19 19:14:03 +02:00
|
|
|
// Formulate and send HTTP request
|
2008-02-17 20:18:47 +01:00
|
|
|
cString buffer = cString::sprintf("GET %s HTTP/1.1\r\n"
|
|
|
|
"Host: %s\r\n"
|
2010-09-10 09:54:06 +02:00
|
|
|
"User-Agent: vdr-%s/%s\r\n"
|
2008-02-17 20:18:47 +01:00
|
|
|
"Range: bytes=0-\r\n"
|
|
|
|
"Connection: Close\r\n"
|
2013-02-23 14:31:11 +01:00
|
|
|
"\r\n", streamPathM, streamAddrM,
|
|
|
|
PLUGIN_NAME_I18N, VERSION);
|
2013-03-01 11:22:41 +01:00
|
|
|
unsigned int len = strlen(*buffer);
|
|
|
|
debug("cIptvProtocolHttp::%s(): requesting %d: %s", __FUNCTION__, len, *buffer);
|
|
|
|
if (!Write(*buffer, len)) {
|
2010-09-15 22:41:25 +02:00
|
|
|
CloseSocket();
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-29 12:55:14 +02:00
|
|
|
// Now process headers
|
2007-09-29 13:17:57 +02:00
|
|
|
if (!ProcessHeaders()) {
|
|
|
|
CloseSocket();
|
|
|
|
return false;
|
2007-09-29 18:21:04 +02:00
|
|
|
}
|
2010-12-02 16:32:02 +01:00
|
|
|
// Update active flag
|
2013-02-23 16:12:46 +01:00
|
|
|
isActiveM = true;
|
2007-09-16 11:38:00 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::Disconnect(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
|
|
|
if (isActiveM) {
|
2010-12-02 16:32:02 +01:00
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
|
|
|
// Update active flag
|
2013-02-23 16:12:46 +01:00
|
|
|
isActiveM = false;
|
2010-12-02 16:32:02 +01:00
|
|
|
}
|
2007-09-16 11:38:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-23 14:31:11 +01:00
|
|
|
bool cIptvProtocolHttp::GetHeaderLine(char* destP, unsigned int destLenP,
|
|
|
|
unsigned int &recvLenP)
|
2007-09-29 12:55:14 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-29 12:55:14 +02:00
|
|
|
bool linefeed = false;
|
|
|
|
bool newline = false;
|
2013-02-23 14:31:11 +01:00
|
|
|
char *bufptr = destP;
|
|
|
|
recvLenP = 0;
|
2007-09-29 12:55:14 +02:00
|
|
|
|
2013-02-23 14:31:11 +01:00
|
|
|
if (!destP)
|
2010-12-02 16:32:02 +01:00
|
|
|
return false;
|
|
|
|
|
2007-09-29 18:21:04 +02:00
|
|
|
while (!newline || !linefeed) {
|
2007-10-21 21:46:03 +02:00
|
|
|
// Wait 500ms for data
|
2010-09-15 22:41:25 +02:00
|
|
|
if (ReadChar(bufptr, 500)) {
|
2007-09-29 12:55:14 +02:00
|
|
|
// Parsing end conditions, if line ends with \r\n
|
2007-09-29 18:21:04 +02:00
|
|
|
if (linefeed && *bufptr == '\n')
|
2007-09-29 12:55:14 +02:00
|
|
|
newline = true;
|
|
|
|
// First occurrence of \r seen
|
2007-09-29 18:21:04 +02:00
|
|
|
else if (*bufptr == '\r')
|
2007-09-29 12:55:14 +02:00
|
|
|
linefeed = true;
|
|
|
|
// Saw just data or \r without \n
|
|
|
|
else {
|
|
|
|
linefeed = false;
|
2013-02-23 14:31:11 +01:00
|
|
|
++recvLenP;
|
2007-09-29 12:55:14 +02:00
|
|
|
}
|
|
|
|
++bufptr;
|
2010-12-02 16:32:02 +01:00
|
|
|
// Check that buffer won't be exceeded
|
2013-02-23 14:31:11 +01:00
|
|
|
if (recvLenP >= destLenP) {
|
2007-09-29 12:55:14 +02:00
|
|
|
error("Header wouldn't fit into buffer\n");
|
2013-02-23 14:31:11 +01:00
|
|
|
recvLenP = 0;
|
2007-09-29 12:55:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-29 18:21:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-09-15 22:41:25 +02:00
|
|
|
error("No HTTP response received in 500ms\n");
|
2007-09-29 12:55:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-29 18:21:04 +02:00
|
|
|
}
|
2007-09-29 12:55:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::ProcessHeaders(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-29 12:55:14 +02:00
|
|
|
unsigned int lineLength = 0;
|
2012-04-01 21:47:50 +02:00
|
|
|
int version = 0, response = 0;
|
2007-09-29 12:55:14 +02:00
|
|
|
bool responseFound = false;
|
2012-04-25 23:27:24 +02:00
|
|
|
char fmt[32];
|
2009-06-17 15:07:58 +02:00
|
|
|
char buf[4096];
|
2007-09-29 12:55:14 +02:00
|
|
|
|
2012-04-25 23:27:24 +02:00
|
|
|
// Generate HTTP response format string with 2 arguments
|
2012-04-26 20:32:20 +02:00
|
|
|
snprintf(fmt, sizeof(fmt), "HTTP/1.%%%zui %%%zui ", sizeof(version) - 1, sizeof(response) - 1);
|
2012-04-25 23:27:24 +02:00
|
|
|
|
2007-09-29 18:21:04 +02:00
|
|
|
while (!responseFound || lineLength != 0) {
|
2013-02-23 16:12:46 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2007-09-29 18:21:04 +02:00
|
|
|
if (!GetHeaderLine(buf, sizeof(buf), lineLength))
|
|
|
|
return false;
|
2012-04-25 23:27:24 +02:00
|
|
|
if (!responseFound && sscanf(buf, fmt, &version, &response) != 2) {
|
2007-09-29 18:21:04 +02:00
|
|
|
error("Expected HTTP header not found\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
responseFound = true;
|
2010-09-10 09:54:06 +02:00
|
|
|
// Allow only 'OK' and 'Partial Content'
|
|
|
|
if ((response != 200) && (response != 206)) {
|
|
|
|
error("Invalid HTTP response (%d): %s\n", response, buf);
|
2007-09-29 18:21:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-09-29 12:55:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-16 11:38:00 +02:00
|
|
|
bool cIptvProtocolHttp::Open(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-16 11:38:00 +02:00
|
|
|
// Connect the socket
|
|
|
|
return Connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolHttp::Close(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2007-09-16 11:38:00 +02:00
|
|
|
// Disconnect the current stream
|
|
|
|
Disconnect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-23 14:31:11 +01:00
|
|
|
int cIptvProtocolHttp::Read(unsigned char* bufferAddrP, unsigned int bufferLenP)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
2013-02-23 14:31:11 +01:00
|
|
|
return cIptvTcpSocket::Read(bufferAddrP, bufferLenP);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 14:31:11 +01:00
|
|
|
bool cIptvProtocolHttp::Set(const char* locationP, const int parameterP, const int indexP)
|
2007-09-16 11:38:00 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolHttp::%s(%s, %d, %d)", __FUNCTION__, locationP, parameterP, indexP);
|
2013-02-23 14:31:11 +01:00
|
|
|
if (!isempty(locationP)) {
|
2008-01-30 22:57:33 +01:00
|
|
|
// Disconnect the current socket
|
|
|
|
Disconnect();
|
|
|
|
// Update stream address, path and port
|
2013-02-23 14:31:11 +01:00
|
|
|
streamAddrM = strcpyrealloc(streamAddrM, locationP);
|
|
|
|
char *path = strstr(streamAddrM, "/");
|
2008-01-30 22:57:33 +01:00
|
|
|
if (path) {
|
2013-02-23 14:31:11 +01:00
|
|
|
streamPathM = strcpyrealloc(streamPathM, path);
|
2008-01-30 22:57:33 +01:00
|
|
|
*path = 0;
|
|
|
|
}
|
|
|
|
else
|
2013-02-23 14:31:11 +01:00
|
|
|
streamPathM = strcpyrealloc(streamPathM, "/");
|
|
|
|
streamPortM = parameterP;
|
2008-01-30 22:57:33 +01:00
|
|
|
// Re-connect the socket
|
|
|
|
Connect();
|
|
|
|
}
|
2007-09-16 11:38:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-10-08 00:54:09 +02:00
|
|
|
|
|
|
|
cString cIptvProtocolHttp::GetInformation(void)
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
//debug("cIptvProtocolHttp::%s()", __FUNCTION__);
|
2013-02-23 14:31:11 +01:00
|
|
|
return cString::sprintf("http://%s:%d%s", streamAddrM, streamPortM, streamPathM);
|
2007-10-08 00:54:09 +02:00
|
|
|
}
|