1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00
vdr-plugin-iptv/protocolhttp.c

201 lines
5.2 KiB
C
Raw Normal View History

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>
#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()
2010-09-15 22:41:25 +02:00
: streamAddr(strdup("")),
streamPath(strdup("/"))
2007-09-16 11:38:00 +02:00
{
debug("cIptvProtocolHttp::cIptvProtocolHttp()\n");
2007-09-16 11:38:00 +02:00
}
cIptvProtocolHttp::~cIptvProtocolHttp()
{
debug("cIptvProtocolHttp::~cIptvProtocolHttp()\n");
// Close the socket
2008-01-05 00:36:37 +01:00
cIptvProtocolHttp::Close();
2007-09-16 11:38:00 +02:00
// Free allocated memory
free(streamPath);
2007-09-16 11:38:00 +02:00
free(streamAddr);
}
bool cIptvProtocolHttp::Connect(void)
{
debug("cIptvProtocolHttp::Connect()\n");
// Check that stream address is valid
if (!isActive && !isempty(streamAddr) && !isempty(streamPath)) {
2010-09-15 22:41:25 +02:00
// Ensure that socket is valid and connect
OpenSocket(socketPort, streamAddr);
if (!ConnectSocket()) {
CloseSocket();
2007-09-16 11:38:00 +02:00
return false;
}
// Formulate and send HTTP request
cString buffer = cString::sprintf("GET %s HTTP/1.1\r\n"
"Host: %s\r\n"
"User-Agent: vdr-%s/%s\r\n"
"Range: bytes=0-\r\n"
"Connection: Close\r\n"
"\r\n", streamPath, streamAddr, PLUGIN_NAME_I18N, VERSION);
debug("Sending http request: %s\n", *buffer);
2010-09-15 22:41:25 +02:00
if (!Write(*buffer, (unsigned int)strlen(*buffer))) {
CloseSocket();
return false;
}
2007-09-29 12:55:14 +02:00
// Now process headers
if (!ProcessHeaders()) {
CloseSocket();
return false;
2007-09-29 18:21:04 +02:00
}
// Update active flag
isActive = true;
2007-09-16 11:38:00 +02:00
}
return true;
}
bool cIptvProtocolHttp::Disconnect(void)
{
debug("cIptvProtocolHttp::Disconnect()\n");
if (isActive) {
// Close the socket
CloseSocket();
// Update active flag
isActive = false;
}
2007-09-16 11:38:00 +02:00
return true;
}
2007-09-29 12:55:14 +02:00
bool cIptvProtocolHttp::GetHeaderLine(char* dest, unsigned int destLen,
2009-03-20 16:43:31 +01:00
unsigned int &recvLen)
2007-09-29 12:55:14 +02:00
{
debug("cIptvProtocolHttp::GetHeaderLine()\n");
bool linefeed = false;
bool newline = false;
char *bufptr = dest;
2007-09-29 12:55:14 +02:00
recvLen = 0;
if (!dest)
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;
++recvLen;
}
++bufptr;
// Check that buffer won't be exceeded
if (recvLen >= destLen) {
2007-09-29 12:55:14 +02:00
error("Header wouldn't fit into buffer\n");
recvLen = 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)
{
debug("cIptvProtocolHttp::ProcessHeaders()\n");
unsigned int lineLength = 0;
int response = 0;
bool responseFound = false;
char buf[4096];
2007-09-29 12:55:14 +02:00
2007-09-29 18:21:04 +02:00
while (!responseFound || lineLength != 0) {
memset(buf, '\0', sizeof(buf));
if (!GetHeaderLine(buf, sizeof(buf), lineLength))
return false;
if (!responseFound && sscanf(buf, "HTTP/1.%*i %i ", &response) != 1) {
2007-09-29 18:21:04 +02:00
error("Expected HTTP header not found\n");
continue;
}
else
responseFound = true;
// 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)
{
2007-10-19 23:36:27 +02:00
debug("cIptvProtocolHttp::Open()\n");
2007-09-16 11:38:00 +02:00
// Connect the socket
return Connect();
}
bool cIptvProtocolHttp::Close(void)
{
2007-10-19 23:36:27 +02:00
debug("cIptvProtocolHttp::Close()\n");
2007-09-16 11:38:00 +02:00
// Disconnect the current stream
Disconnect();
return true;
}
2009-02-26 15:04:12 +01:00
int cIptvProtocolHttp::Read(unsigned char* BufferAddr, unsigned int BufferLen)
{
2009-02-26 15:04:12 +01:00
return cIptvTcpSocket::Read(BufferAddr, BufferLen);
}
bool cIptvProtocolHttp::Set(const char* Location, const int Parameter, const int Index)
2007-09-16 11:38:00 +02:00
{
debug("cIptvProtocolHttp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
2007-10-19 23:36:27 +02:00
if (!isempty(Location)) {
2008-01-30 22:57:33 +01:00
// Disconnect the current socket
Disconnect();
// Update stream address, path and port
streamAddr = strcpyrealloc(streamAddr, Location);
char *path = strstr(streamAddr, "/");
if (path) {
streamPath = strcpyrealloc(streamPath, path);
*path = 0;
}
else
streamPath = strcpyrealloc(streamPath, "/");
socketPort = Parameter;
//debug("http://%s:%d%s\n", streamAddr, socketPort, streamPath);
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)
{
//debug("cIptvProtocolHttp::GetInformation()");
return cString::sprintf("http://%s:%d%s", streamAddr, socketPort, streamPath);
2007-10-08 00:54:09 +02:00
}