3 #include <lib/base/httpstream.h>
4 #include <lib/base/eerror.h>
6 DEFINE_REF(eHttpStream);
8 eHttpStream::eHttpStream()
13 eHttpStream::~eHttpStream()
18 int eHttpStream::open(const char *url)
22 std::string uri = url;
33 int pathindex = uri.find("/", 7);
36 hostname = uri.substr(7, pathindex - 7);
37 uri = uri.substr(pathindex, uri.length() - pathindex);
41 hostname = uri.substr(7, uri.length() - 7);
44 int customportindex = hostname.find(":");
45 if (customportindex > 0)
47 port = atoi(hostname.substr(customportindex + 1, hostname.length() - customportindex - 1).c_str());
48 hostname = hostname.substr(0, customportindex);
50 else if (customportindex == 0)
52 port = atoi(hostname.substr(1, hostname.length() - 1).c_str());
53 hostname = "localhost";
59 streamSocket = connect(hostname.c_str(), port, 10);
60 if (streamSocket < 0) goto error;
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());
70 linebuf = (char*)malloc(buflen);
72 result = readLine(streamSocket, &linebuf, &buflen);
73 if (result <= 0) goto error;
75 result = sscanf(linebuf, "%99s %d %99s", proto, &statuscode, statusmsg);
76 if (result != 3 || statuscode != 200)
78 eDebug("eHttpStream::open: wrong http response code: %d", statuscode);
83 result = readLine(streamSocket, &linebuf, &buflen);
89 eDebug("eHttpStream::open failed");
95 off_t eHttpStream::lseek(off_t offset, int whence)
100 int eHttpStream::close()
103 if (streamSocket >= 0)
105 retval = ::close(streamSocket);
111 ssize_t eHttpStream::read(off_t offset, void *buf, size_t count)
113 return timedRead(streamSocket, buf, count, 5000, 500);
116 int eHttpStream::valid()
118 return streamSocket >= 0;
121 off_t eHttpStream::length()