TranscodingSetup : add framerate settings (25k, 50k)
[vuplus_dvbapp] / lib / base / httpstream.cpp
1 #include <cstdio>
2
3 #include <lib/base/httpstream.h>
4 #include <lib/base/eerror.h>
5
6 DEFINE_REF(eHttpStream);
7
8 eHttpStream::eHttpStream()
9 {
10         streamSocket = -1;
11 }
12
13 eHttpStream::~eHttpStream()
14 {
15         close();
16 }
17
18 int eHttpStream::open(const char *url)
19 {
20         int port;
21         std::string hostname;
22         std::string uri = url;
23         std::string request;
24         size_t buflen = 1024;
25         char *linebuf = NULL;
26         int result;
27         char proto[100];
28         int statuscode = 0;
29         char statusmsg[100];
30
31         close();
32
33         int pathindex = uri.find("/", 7);
34         if (pathindex > 0) 
35         {
36                 hostname = uri.substr(7, pathindex - 7);
37                 uri = uri.substr(pathindex, uri.length() - pathindex);
38         } 
39         else 
40         {
41                 hostname = uri.substr(7, uri.length() - 7);
42                 uri = "";
43         }
44         int customportindex = hostname.find(":");
45         if (customportindex > 0) 
46         {
47                 port = atoi(hostname.substr(customportindex + 1, hostname.length() - customportindex - 1).c_str());
48                 hostname = hostname.substr(0, customportindex);
49         } 
50         else if (customportindex == 0) 
51         {
52                 port = atoi(hostname.substr(1, hostname.length() - 1).c_str());
53                 hostname = "localhost";
54         }
55         else
56         {
57                 port = 80;
58         }
59         streamSocket = connect(hostname.c_str(), port, 10);
60         if (streamSocket < 0) goto error;
61
62         request = "GET ";
63         request.append(uri).append(" HTTP/1.1\r\n");
64         request.append("Host: ").append(hostname).append("\r\n");
65         request.append("Accept: */*\r\n");
66         request.append("Connection: close\r\n");
67         request.append("\r\n");
68         writeAll(streamSocket, request.c_str(), request.length());
69
70         linebuf = (char*)malloc(buflen);
71
72         result = readLine(streamSocket, &linebuf, &buflen);
73         if (result <= 0) goto error;
74
75         result = sscanf(linebuf, "%99s %d %99s", proto, &statuscode, statusmsg);
76         if (result != 3 || statuscode != 200) 
77         {
78                 eDebug("eHttpStream::open: wrong http response code: %d", statuscode);
79                 goto error;
80         }
81         while (result > 0)
82         {
83                 result = readLine(streamSocket, &linebuf, &buflen);
84         }
85
86         free(linebuf);
87         return 0;
88 error:
89         eDebug("eHttpStream::open failed");
90         free(linebuf);
91         close();
92         return -1;
93 }
94
95 off_t eHttpStream::lseek(off_t offset, int whence)
96 {
97         return (off_t)-1;
98 }
99
100 int eHttpStream::close()
101 {
102         int retval = -1;
103         if (streamSocket >= 0)
104         {
105                 retval = ::close(streamSocket);
106                 streamSocket = -1;
107         }
108         return retval;
109 }
110
111 ssize_t eHttpStream::read(off_t offset, void *buf, size_t count)
112 {
113         return timedRead(streamSocket, buf, count, 5000, 500);
114 }
115
116 int eHttpStream::valid()
117 {
118         return streamSocket >= 0;
119 }
120
121 off_t eHttpStream::length()
122 {
123         return (off_t)-1;
124 }