summaryrefslogtreecommitdiff
path: root/tools/http.c
blob: 4a5295505085b2b5a1d4301b9cb4841b01b4fabe (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * http.c: HTTP (/RTSP) helper classes
 *
 * See the main source file 'xineliboutput.c' for copyright information and
 * how to reach the author.
 *
 * $Id: http.c,v 1.6 2007-06-21 09:12:52 phintuka Exp $
 *
 */

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#include <string.h>

#include <vdr/config.h>
#include <vdr/tools.h>

#include "../logdefs.h"

#include "http.h"

//
// cHttpReq
//

bool cHttpReq::SetCommand(const char *Command) 
{
  char *tmp = strdup(Command);
  char *pt = strchr(tmp, ' '), *uri;

  m_Valid = false;
  if(pt) {
    *pt++ = 0;
    m_Name = tmp;

    while(*pt && *pt == ' ') pt++;

    uri = pt;
    pt = strrchr(uri, ' ');
    if(pt) {
      m_Version = pt+1;
      while(*pt && *pt == ' ') *pt-- = 0;
      m_Uri = uri;
      m_Valid = true;
    }
  }

  free(tmp);
  return m_Valid;
}

cHeader *cHttpReq::Header(const char *Name) 
{
  for(cHeader *i = m_Headers.First(); i; i = m_Headers.Next(i))
    if(!strcmp(Name, i->Name()))
      return i;
  return NULL;
}

void cHttpReq::AddHeader(const char *Header, bool Duplicate) 
{
  if(strlen(Header) < 4096) {
    char *name = strdup(Header);
    char *val = strchr(name, ':');
    if(val) {
      *val++ = 0;
      while(*val == ' ') val++;
      AddHeader(name, val, Duplicate);
    }
    free(name);
  } else {
    LOGMSG("cConnState::AddHeader: header length exceeds 4096 !");
  }
}

void cHttpReq::AddHeader(const char *Name, const char *Value, bool Duplicate) 
{
  if(strlen(Name) > 64 || strlen(Value) > 4096) {
    LOGMSG("cConnState::AddHeader: header length exceeds limit !");
  } else {
    cHeader *h = Header(Name);
    if(!Duplicate && h)
      h->SetValue(Value);
    else {
      if(m_Headers.Count() < 50)
	m_Headers.Add(new cHeader(Name, Value));
      else
	LOGMSG("cConnState::AddHeader: header count exceeds 50 !");
    }
  }
}    

void cHttpReq::Reset(void) 
{
  m_Name = NULL;
  m_Uri = NULL;
  m_Version = NULL;
  m_Valid = false;
  m_Headers.Clear();
}

//
// Map file extensions to mime types
//

static const char *mimetype(const char *ext)
{
  static const struct {
    const char *ext;
    const char *mime;
  } ext2mime[] = {
    {"avi",  "video/avi"},
    {"vob",  "video/mpeg"},
    {"mpg",  "video/mpeg"},
    {"mpeg", "video/mpeg"},
    {"vdr",  "video/mp2p"},
    
    {"mp3",  "audio/mp3"},
    {"flac", "audio/flac"},
    
    {"jpg",  "image/jpeg"},
    {"jpeg", "image/jpeg"},
    {"gif",  "image/gif"},
    
    {NULL, NULL}
  };

  int i = -1;
  while(ext2mime[++i].ext)
    if(!strcmp(ext, ext2mime[i].ext))
      return ext2mime[i].mime;
  return NULL;
}

static char *unescape_uri(const char *uri)
{
  char *d = strdup(uri), *s = d, *result = d;
  while(*s) {
    if(s[0] == '%' && s[1] && s[2]) {
      unsigned int c;
      if (sscanf(s+1, "%02x", &c) == 1) {
	*d++ = (char)c;
	s += 3;
	continue;
      }
    }
    *d++ = *s++;
  }
  *d = 0;
  return result;
}

//
// cHttpStreamer
//

cList<cHttpStreamer> cHttpStreamer::m_Streamers;

void cHttpStreamer::CloseAll(bool OnlyFinished)
{
  if(!OnlyFinished) {
    while(m_Streamers.First())
      m_Streamers.Del(m_Streamers.First());

  } else {
    /*  purge finished streamers from list */
    cHttpStreamer *it = m_Streamers.First();
    while(it) {
      if(it->Active()) {
	it = (cHttpStreamer*)it->Next();
      } else {
	m_Streamers.Del(it);
	it = m_Streamers.First();
      }
    }
  }
}

cHttpStreamer::cHttpStreamer(int fd_http, const char *filename, 
			     cConnState *Request) :
    m_Filename(unescape_uri(filename), true)
{
  m_fds.set_handle(fd_http);
  m_fds.set_cork(true);
  m_fdf = -1;

  //m_Filename = filename;
  m_FileSize = -1;
  m_Start = 0;
  m_End = -1;
  m_KeepOpen = true;

  m_ConnState = Request;

  m_Finished = false;

  CloseAll(true);

  m_Streamers.Add(this);

  if(m_Streamers.Count() > 5) {
    LOGMSG("WARNING: There are %d running HTTP streamers !", m_Streamers.Count());
    if(m_Streamers.Count() > 20) {
      errno = 0;
      LOGERR("ERROR: There are %d running HTTP streamers, cancelling first", 
	     m_Streamers.Count());
      m_Streamers.Del(m_Streamers.First());
    }
  }

  Start();
}

cHttpStreamer::~cHttpStreamer()
{
  Cancel(3);
  if(m_ConnState)
    delete m_ConnState;
  if(m_fdf >= 0) 
    close(m_fdf);
  m_fdf = -1;
}

void cHttpStreamer::ParseRange(const char *Range)
{
  m_Start = 0;
  m_End = -1;
  if(Range) {
    LOGDBG("cHttpStreamer: Request range is \'%s\'", Range);
    switch(sscanf(Range, "bytes=%" PRId64 "-%" PRId64, &m_Start, &m_End)) {
    case 2: LOGMSG("  Range: %s (%" PRId64 " - %" PRId64 ")", Range, m_Start, m_End);
            break;
    case 1: m_End = -1;
            LOGMSG("  Range start: %s (%" PRId64 " - )", Range, m_Start);
	    break;
    default:
    case 0: m_Start = 0;
            m_End = -1;
	    break;
    }
  }
}

bool cHttpStreamer::ParseRequest(void)
{
  cHeader *h;

  if((h = m_ConnState->Header("Range")) != NULL)
    ParseRange(h->Value());

  m_KeepOpen = false;
  if((h = m_ConnState->Header("Connection")) != NULL) {
    m_KeepOpen = !strcasecmp(h->Value(), "keep-alive");
    if(m_KeepOpen) 
      LOGDBG("cHttpStreamer: client wants to keep connection open");
  }

  return true;
}

bool cHttpStreamer::Seek(void)
{
  if(m_fdf < 0) {
    m_fdf = open(m_Filename, O_RDONLY);
    if(m_fdf < 0) {
      LOGERR("cHttpStreamer: error opening %s", *m_Filename);
      m_fds.write_cmd(HTTP_REPLY_401); // 401 Not Found
      return false;
    }

    m_FileSize = lseek(m_fdf, 0, SEEK_END);
    if(m_FileSize <= 0) {
      LOGERR("cHttpStreamer: error seeking %s to end", *m_Filename);
      m_fds.write_cmd(HTTP_REPLY_401); // 401 Not Found
      return false;
    }
  }

  if(m_Start >= m_FileSize) {
    LOGERR("cHttpStreamer: Requested range not available "
	   "(%s:%" PRId64 "-%" PRId64 " ; len=%" PRIu64 ")", 
	   *m_Filename, m_Start, m_End, (uint64_t)m_FileSize);
    m_fds.write_cmd(HTTP_REPLY_416); // 416 Requested Range Not Satisfiable
    return false;
  }
  
  if(m_Start > 0) {
    if(m_End >= m_FileSize || m_End < 0)
      m_End = m_FileSize-1;

    m_fds.write_cmd("HTTP/1.1 206 Partial Content\r\n");
    m_fds.printf("Content-Range: bytes %" PRId64 "-%" PRId64 "/%" PRIu64 "\r\n", 
		 m_Start, m_End, (uint64_t)m_FileSize);
  } else {
    m_fds.write_cmd("HTTP/1.1 200 OK\r\n");
  }

  /* content type */
  char *ext = strrchr(m_Filename, '.');
  if(ext) {
    const char *mime = mimetype(ext+1);
    if(mime)
      m_fds.printf("Content-Type: %s\r\n", mime);
  }

  /* Content-Length */
  if(m_FileSize >= 0) {
    int64_t len = m_FileSize;
    if(m_End >= 0)
      len = m_End + 1;
    if(m_Start >= 0)
      len -= m_Start;
    m_fds.printf("Content-Length: %" PRId64 "\r\n", len);
  }

  /* Connection and end of reply */
  if(m_KeepOpen)
    m_fds.write_cmd("Connection: Keep-Alive\r\n"
		    "\r\n");
  else
    m_fds.write_cmd("Connection: Close\r\n"
		    "\r\n");

  if(m_Start)
    lseek(m_fdf, (off_t)m_Start, SEEK_SET);
  else
    lseek(m_fdf, 0, SEEK_SET);

  return true;
}

bool cHttpStreamer::ReadPipelined(void)
{
  char buf[2048];
  int r;

  if(m_ConnState)
    delete m_ConnState;
  m_ConnState = new cConnState;

  do {
    r = m_fds.readline(buf, sizeof(buf), 1000);
    if(r < 0 || errno == EAGAIN || r >= (int)sizeof(buf)) {
      LOGMSG("cHttpStreamer: disconnected");
      return false;
    }

    LOGMSG("cHttpStreamer: pipelined request: %s", buf);

    if(!*m_ConnState->Name()) {
      if(!m_ConnState->SetCommand(buf) ||
	 strcmp(m_ConnState->Name(), "GET") ||
	 strncmp(m_ConnState->Uri(), "/PLAYFILE", 9) ||
	 strncmp(m_ConnState->Version(), "HTTP/1.", 7)) {
	LOGMSG("Incorrect HTTP request: %s", buf);
	return false;
      }
    }
    else if(r > 0)
      m_ConnState->AddHeader(buf);
  } while(r>0);

  return true;
}

void cHttpStreamer::Action(void)
{
  int      n = 0;
  cxPoller p(m_fds);
  bool     Disc = !(ParseRequest() && Seek());
  uint64_t pos  = m_Start;
  off_t    start = (off_t)m_Start;

  while(Running() && !Disc) {

    n = m_End>0 ? (m_End-start+1) : m_FileSize - start;
    if(n > 0) {
      errno = 0;
      pthread_testcancel();
      n = m_fds.sendfile(m_fdf, &start, n);
      pthread_testcancel();
      if(n <= 0) {
	if(errno == EAGAIN || errno == EINTR) {
	  p.Poll(100);
	  pthread_testcancel();
	} else {
	  LOGERR("cHttpStreamer: sendfile() failed");
	  Disc=true;
	}
      } else if(n == 0) {
	LOGMSG("cHttpStreamer: disconnected at %" PRId64, (int64_t)start);
	Disc = true;
      }
      continue;
    }

    LOGDBG("cHttpStreamer: Hit to EOF or end of requested range");

    m_fds.flush_cork();

    if(!m_KeepOpen) {
      LOGMSG("cHttpStreamer: disconnecting (request complete)");
      Disc = true;
      continue;
    }

    // keep connection open for new range for max. 30 sec
    n = 30; 
    do {
      pthread_testcancel();
      //cxPoller p(m_fds);
      LOGDBG("cHttpStreamer: Request complete, waiting...");
      if(p.Poll(1000)) {
	LOGDBG("cHttpStreamer: Reading pipelined request");
	pthread_testcancel();
	Disc = !(ReadPipelined() && ParseRequest() && Seek());
	pos = m_Start;
      }
    } while(--n && Running() && !Disc);

    if(n <= 0) {
      LOGMSG("cHttpStreamer: Disconnecting (timeout)");
      Disc = true;
    }
  }

  close(m_fdf);
  m_fdf = -1;

  m_fds.close();

  m_Finished = true;
}