-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprotocolhttp.c
213 lines (194 loc) · 5.49 KB
/
protocolhttp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* 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>
#include <vdr/device.h>
#include "common.h"
#include "config.h"
#include "log.h"
#include "protocolhttp.h"
cIptvProtocolHttp::cIptvProtocolHttp()
: streamAddrM(strdup("")),
streamPathM(strdup("/")),
streamPortM(0)
{
debug1("%s", __PRETTY_FUNCTION__);
}
cIptvProtocolHttp::~cIptvProtocolHttp()
{
debug1("%s", __PRETTY_FUNCTION__);
// Close the socket
cIptvProtocolHttp::Close();
// Free allocated memory
free(streamPathM);
free(streamAddrM);
}
bool cIptvProtocolHttp::Connect(void)
{
debug1("%s", __PRETTY_FUNCTION__);
// Check that stream address is valid
if (!isActiveM && !isempty(streamAddrM) && !isempty(streamPathM)) {
// Ensure that socket is valid and connect
OpenSocket(streamPortM, streamAddrM);
if (!ConnectSocket()) {
CloseSocket();
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", streamPathM, streamAddrM,
PLUGIN_NAME_I18N, VERSION);
unsigned int len = strlen(*buffer);
debug1("%s Requesting %d: %s", __PRETTY_FUNCTION__, len, *buffer);
if (!Write(*buffer, len)) {
CloseSocket();
return false;
}
// Now process headers
if (!ProcessHeaders()) {
CloseSocket();
return false;
}
// Update active flag
isActiveM = true;
}
return true;
}
bool cIptvProtocolHttp::Disconnect(void)
{
debug1("%s", __PRETTY_FUNCTION__);
if (isActiveM) {
// Close the socket
CloseSocket();
// Update active flag
isActiveM = false;
}
return true;
}
bool cIptvProtocolHttp::GetHeaderLine(char* destP, unsigned int destLenP,
unsigned int &recvLenP)
{
debug1("%s (, %u, )", __PRETTY_FUNCTION__, destLenP);
bool linefeed = false;
bool newline = false;
char *bufptr = destP;
recvLenP = 0;
if (!destP)
return false;
while (!newline || !linefeed) {
// Wait 500ms for data
if (ReadChar(bufptr, 500)) {
// Parsing end conditions, if line ends with \r\n
if (linefeed && *bufptr == '\n')
newline = true;
// First occurrence of \r seen
else if (*bufptr == '\r')
linefeed = true;
// Saw just data or \r without \n
else {
linefeed = false;
++recvLenP;
}
++bufptr;
// Check that buffer won't be exceeded
if (recvLenP >= destLenP) {
error("Header wouldn't fit into buffer");
recvLenP = 0;
return false;
}
}
else {
error("No HTTP response received in 500ms");
return false;
}
}
return true;
}
bool cIptvProtocolHttp::ProcessHeaders(void)
{
debug1("%s", __PRETTY_FUNCTION__);
unsigned int lineLength = 0;
int version = 0, response = 0;
bool responseFound = false;
char fmt[32];
char buf[4096];
// Generate HTTP response format string with 2 arguments
snprintf(fmt, sizeof(fmt), "HTTP/1.%%%zui %%%zui ", sizeof(version) - 1, sizeof(response) - 1);
while (!responseFound || lineLength != 0) {
memset(buf, 0, sizeof(buf));
if (!GetHeaderLine(buf, sizeof(buf), lineLength))
return false;
if (!responseFound && sscanf(buf, fmt, &version, &response) != 2) {
error("Expected HTTP header not found");
continue;
}
else
responseFound = true;
// Allow only 'OK' and 'Partial Content'
if ((response != 200) && (response != 206)) {
error("Invalid HTTP response (%d): %s", response, buf);
return false;
}
}
return true;
}
bool cIptvProtocolHttp::Open(void)
{
debug1("%s", __PRETTY_FUNCTION__);
// Connect the socket
return Connect();
}
bool cIptvProtocolHttp::Close(void)
{
debug1("%s", __PRETTY_FUNCTION__);
// Disconnect the current stream
Disconnect();
return true;
}
int cIptvProtocolHttp::Read(unsigned char* bufferAddrP, unsigned int bufferLenP)
{
return cIptvTcpSocket::Read(bufferAddrP, bufferLenP);
}
bool cIptvProtocolHttp::SetSource(const char* locationP, const int parameterP, const int indexP)
{
debug1("%s (%s, %d, %d)", __PRETTY_FUNCTION__, locationP, parameterP, indexP);
if (!isempty(locationP)) {
// Disconnect the current socket
Disconnect();
// Update stream address, path and port
streamAddrM = strcpyrealloc(streamAddrM, locationP);
char *path = strstr(streamAddrM, "/");
if (path) {
streamPathM = strcpyrealloc(streamPathM, path);
*path = 0;
}
else
streamPathM = strcpyrealloc(streamPathM, "/");
streamPortM = parameterP;
// Re-connect the socket
Connect();
}
return true;
}
bool cIptvProtocolHttp::SetPid(int pidP, int typeP, bool onP)
{
debug16("%s (%d, %d, %d)", __PRETTY_FUNCTION__, pidP, typeP, onP);
return true;
}
cString cIptvProtocolHttp::GetInformation(void)
{
debug16("%s", __PRETTY_FUNCTION__);
return cString::sprintf("http://%s:%d%s", streamAddrM, streamPortM, streamPathM);
}