Commit 60ceaa11 authored by Christopher Mueller's avatar Christopher Mueller Committed by Hugo Beauzée-Luyssen

dash: refactored HTTPConnection

Signed-off-by: default avatarHugo Beauzée-Luyssen <beauze.h@gmail.com>
parent 1cad10dc
...@@ -26,16 +26,14 @@ ...@@ -26,16 +26,14 @@
#endif #endif
#include "HTTPConnection.h" #include "HTTPConnection.h"
#include <vlc_url.h>
using namespace dash::http; using namespace dash::http;
HTTPConnection::HTTPConnection (Chunk *chunk, stream_t *stream) : HTTPConnection::HTTPConnection (stream_t *stream) :
stream (stream), stream (stream),
chunk (chunk), peekBufferLen (0),
peekBufferLen (0) contentLength (0)
{ {
this->url = chunk->getUrl();
this->peekBuffer = new uint8_t[PEEKBUFFER]; this->peekBuffer = new uint8_t[PEEKBUFFER];
} }
HTTPConnection::~HTTPConnection () HTTPConnection::~HTTPConnection ()
...@@ -73,48 +71,41 @@ int HTTPConnection::peek (const uint8_t **pp_peek, size_t ...@@ -73,48 +71,41 @@ int HTTPConnection::peek (const uint8_t **pp_peek, size_t
*pp_peek = peek; *pp_peek = peek;
return size; return size;
} }
void HTTPConnection::parseURL () std::string HTTPConnection::prepareRequest (Chunk *chunk)
{ {
vlc_url_t url_components; std::string request;
vlc_UrlParse(&url_components, this->url.c_str(), 0);
this->path = url_components.psz_path;
this->port = url_components.i_port ? url_components.i_port : 80;
if(this->url.compare(0, 4, "http"))
this->hostname = Helper::combinePaths(Helper::getDirectoryPath(stream->psz_path), this->url);
else
this->hostname = url_components.psz_host;
this->request = "GET " + this->path + " HTTP/1.1\r\n" +
"Host: " + this->hostname + "\r\nConnection: close\r\n\r\n";
}
void HTTPConnection::prepareRequest ()
{
if(!chunk->useByteRange()) if(!chunk->useByteRange())
{ {
this->request = "GET " + this->path + " HTTP/1.1" + "\r\n" + request = "GET " + chunk->getPath() + " HTTP/1.1" + "\r\n" +
"Host: " + this->hostname + "\r\n" + "Host: " + chunk->getHostname() + "\r\n" +
"Connection: close\r\n\r\n"; "Connection: close\r\n\r\n";
} }
else else
{ {
std::stringstream req; std::stringstream req;
req << "GET " << this->path << " HTTP/1.1\r\n" << req << "GET " << chunk->getPath() << " HTTP/1.1\r\n" <<
"Host: " << this->hostname << "\r\n" << "Host: " << chunk->getHostname() << "\r\n" <<
"Range: bytes=" << this->chunk->getStartByte() << "-" << this->chunk->getEndByte() << "\r\n" << "Range: bytes=" << chunk->getStartByte() << "-" << chunk->getEndByte() << "\r\n" <<
"Connection: close\r\n\r\n"; "Connection: close\r\n\r\n";
this->request = req.str(); request = req.str();
} }
return request;
} }
bool HTTPConnection::init () bool HTTPConnection::init (Chunk *chunk)
{ {
this->parseURL(); if(!chunk->hasHostname())
this->prepareRequest(); if(!this->setUrlRelative(chunk))
return false;
this->httpSocket = net_ConnectTCP(this->stream, chunk->getHostname().c_str(), chunk->getPort());
this->httpSocket = net_ConnectTCP(this->stream, this->hostname.c_str(), this->port); if(this->httpSocket == -1)
return false;
if(this->sendData(this->request)) if(this->sendData(this->prepareRequest(chunk)))
return this->parseHeader(); return this->parseHeader();
return false; return false;
...@@ -123,9 +114,18 @@ bool HTTPConnection::parseHeader () ...@@ -123,9 +114,18 @@ bool HTTPConnection::parseHeader ()
{ {
std::string line = this->readLine(); std::string line = this->readLine();
if(line.size() == 0)
return false;
while(line.compare("\r\n")) while(line.compare("\r\n"))
{ {
if(!line.compare(0, 14, "Content-Length"))
this->contentLength = atoi(line.substr(15,line.size()).c_str());
line = this->readLine(); line = this->readLine();
if(line.size() == 0)
return false;
} }
return true; return true;
...@@ -148,7 +148,7 @@ std::string HTTPConnection::readLine () ...@@ -148,7 +148,7 @@ std::string HTTPConnection::readLine ()
if(size > 0) if(size > 0)
return ss.str(); return ss.str();
return "\r\n"; return "";
} }
bool HTTPConnection::sendData (const std::string& data) bool HTTPConnection::sendData (const std::string& data)
{ {
...@@ -168,3 +168,11 @@ void HTTPConnection::closeSocket () ...@@ -168,3 +168,11 @@ void HTTPConnection::closeSocket ()
{ {
net_Close(this->httpSocket); net_Close(this->httpSocket);
} }
bool HTTPConnection::setUrlRelative (Chunk *chunk)
{
std::stringstream ss;
ss << stream->psz_access << "://" << Helper::combinePaths(Helper::getDirectoryPath(stream->psz_path), chunk->getUrl());
chunk->setUrl(ss.str());
return chunk->hasHostname();
}
...@@ -48,32 +48,26 @@ namespace dash ...@@ -48,32 +48,26 @@ namespace dash
class HTTPConnection : public IHTTPConnection class HTTPConnection : public IHTTPConnection
{ {
public: public:
HTTPConnection ( Chunk *chunk, stream_t *stream ); HTTPConnection (stream_t *stream);
virtual ~HTTPConnection (); virtual ~HTTPConnection ();
bool init (); virtual bool init (Chunk *chunk);
void closeSocket (); void closeSocket ();
virtual int read (void *p_buffer, size_t len); virtual int read (void *p_buffer, size_t len);
virtual int peek (const uint8_t **pp_peek, size_t i_peek); virtual int peek (const uint8_t **pp_peek, size_t i_peek);
private: protected:
int httpSocket; int httpSocket;
std::string url;
std::string hostname;
std::string path;
int port;
std::string request;
stream_t *stream; stream_t *stream;
Chunk *chunk;
uint8_t *peekBuffer; uint8_t *peekBuffer;
size_t peekBufferLen; size_t peekBufferLen;
int contentLength;
void parseURL ();
bool sendData (const std::string& data); bool sendData (const std::string& data);
bool parseHeader (); bool parseHeader ();
std::string readLine (); std::string readLine ();
void prepareRequest (); virtual std::string prepareRequest (Chunk *chunk);
bool setUrlRelative (Chunk *chunk);
}; };
} }
} }
......
...@@ -137,8 +137,8 @@ int HTTPConnectionManager::peek (Chunk *chun ...@@ -137,8 +137,8 @@ int HTTPConnectionManager::peek (Chunk *chun
IHTTPConnection* HTTPConnectionManager::initConnection(Chunk *chunk) IHTTPConnection* HTTPConnectionManager::initConnection(Chunk *chunk)
{ {
HTTPConnection *con = new HTTPConnection(chunk, this->stream); HTTPConnection *con = new HTTPConnection(this->stream);
if ( con->init() == false ) if ( con->init(chunk) == false )
return NULL; return NULL;
this->chunkMap[chunk] = con; this->chunkMap[chunk] = con;
this->chunkCount++; this->chunkCount++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment