Commit 2801bd5a authored by Francois Cartegnie's avatar Francois Cartegnie

stream_filter: dash: refactor the HTTP thing

parent 06a57693
...@@ -26,12 +26,14 @@ ...@@ -26,12 +26,14 @@
#endif #endif
#include "HTTPConnection.h" #include "HTTPConnection.h"
#include <vlc_network.h>
#include <sstream>
using namespace dash::http; using namespace dash::http;
HTTPConnection::HTTPConnection (stream_t *stream) : HTTPConnection::HTTPConnection (stream_t *stream) :
httpSocket (-1), IHTTPConnection (stream),
stream (stream),
peekBufferLen (0), peekBufferLen (0),
contentLength (0) contentLength (0)
{ {
...@@ -81,20 +83,12 @@ std::string HTTPConnection::getRequestHeader (const Chunk *chunk) const ...@@ -81,20 +83,12 @@ std::string HTTPConnection::getRequestHeader (const Chunk *chunk) const
bool HTTPConnection::init (Chunk *chunk) bool HTTPConnection::init (Chunk *chunk)
{ {
if(!chunk->hasHostname()) if (IHTTPConnection::init(chunk))
if(!this->setUrlRelative(chunk)) return parseHeader();
return false; else
this->httpSocket = net_ConnectTCP(this->stream, chunk->getHostname().c_str(), chunk->getPort());
if(this->httpSocket == -1)
return false;
if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
return this->parseHeader();
return false; return false;
} }
bool HTTPConnection::parseHeader () bool HTTPConnection::parseHeader ()
{ {
std::string line = this->readLine(); std::string line = this->readLine();
...@@ -135,7 +129,7 @@ std::string HTTPConnection::readLine () ...@@ -135,7 +129,7 @@ std::string HTTPConnection::readLine ()
return ""; return "";
} }
bool HTTPConnection::sendData (const std::string& data) bool HTTPConnection::send (const std::string& data)
{ {
ssize_t size = net_Write(this->stream, this->httpSocket, NULL, data.c_str(), data.size()); ssize_t size = net_Write(this->stream, this->httpSocket, NULL, data.c_str(), data.size());
if (size == -1) if (size == -1)
...@@ -144,7 +138,7 @@ bool HTTPConnection::sendData (const std::string& data) ...@@ -144,7 +138,7 @@ bool HTTPConnection::sendData (const std::string& data)
} }
if ((size_t)size != data.length()) if ((size_t)size != data.length())
{ {
this->sendData(data.substr(size, data.size())); this->send(data.substr(size, data.size()));
} }
return true; return true;
...@@ -154,11 +148,4 @@ void HTTPConnection::closeSocket () ...@@ -154,11 +148,4 @@ void HTTPConnection::closeSocket ()
if (httpSocket >= 0) if (httpSocket >= 0)
net_Close(httpSocket); net_Close(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();
}
...@@ -25,15 +25,7 @@ ...@@ -25,15 +25,7 @@
#ifndef HTTPCONNECTION_H_ #ifndef HTTPCONNECTION_H_
#define HTTPCONNECTION_H_ #define HTTPCONNECTION_H_
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_stream.h>
#include <vlc_network.h>
#include <string> #include <string>
#include <stdint.h>
#include <iostream>
#include <sstream>
#include "http/IHTTPConnection.h" #include "http/IHTTPConnection.h"
#include "http/Chunk.h" #include "http/Chunk.h"
...@@ -57,17 +49,14 @@ namespace dash ...@@ -57,17 +49,14 @@ namespace dash
virtual int peek (const uint8_t **pp_peek, size_t i_peek); virtual int peek (const uint8_t **pp_peek, size_t i_peek);
protected: protected:
int httpSocket;
stream_t *stream;
uint8_t *peekBuffer; uint8_t *peekBuffer;
size_t peekBufferLen; size_t peekBufferLen;
int contentLength; int contentLength;
bool sendData (const std::string& data); virtual bool send (const std::string& data);
bool parseHeader (); bool parseHeader ();
std::string readLine (); std::string readLine ();
virtual std::string getRequestHeader(const Chunk *chunk) const; /* reimpl */ virtual std::string getRequestHeader(const Chunk *chunk) const; /* reimpl */
bool setUrlRelative (Chunk *chunk);
}; };
} }
} }
......
...@@ -20,27 +20,60 @@ ...@@ -20,27 +20,60 @@
#include "IHTTPConnection.h" #include "IHTTPConnection.h"
#include "Chunk.h" #include "Chunk.h"
#include "Helper.h"
#include <vlc_network.h>
#include <sstream> #include <sstream>
using namespace dash::http; using namespace dash::http;
std::string IHTTPConnection::getRequestHeader(const Chunk *chunk) const IHTTPConnection::IHTTPConnection(stream_t *stream_)
{
stream = stream_;
httpSocket = -1;
}
IHTTPConnection::~IHTTPConnection()
{
}
bool IHTTPConnection::init(Chunk *chunk)
{ {
std::string request; if(chunk == NULL)
if(!chunk->usesByteRange()) return false;
if(!chunk->hasHostname())
{ {
request = "GET " + chunk->getPath() + " HTTP/1.1" + "\r\n" + chunk->setUrl(getUrlRelative(chunk));
"Host: " + chunk->getHostname() + "\r\n"; if(!chunk->hasHostname())
return false;
} }
else
{ httpSocket = net_ConnectTCP(stream, chunk->getHostname().c_str(), chunk->getPort());
if(httpSocket == -1)
return false;
return send(getRequestHeader(chunk).append("\r\n"));
}
std::string IHTTPConnection::getRequestHeader(const Chunk *chunk) const
{
std::stringstream req; std::stringstream req;
req << "GET " << chunk->getPath() << " HTTP/1.1\r\n" << req << "GET " << chunk->getPath() << " HTTP/1.1\r\n" <<
"Host: " << chunk->getHostname() << "\r\n" << "Host: " << chunk->getHostname() << "\r\n";
"Range: bytes=" << chunk->getStartByte() << "-" << chunk->getEndByte() << "\r\n";
request = req.str(); if(chunk->usesByteRange())
} req << "Range: bytes=" << chunk->getStartByte() << "-" << chunk->getEndByte() << "\r\n";
return request;
return req.str();
}
std::string IHTTPConnection::getUrlRelative(const Chunk *chunk) const
{
std::stringstream ss;
ss << stream->psz_access << "://" << Helper::combinePaths(Helper::getDirectoryPath(stream->psz_path), chunk->getUrl());
return ss.str();
} }
...@@ -25,8 +25,12 @@ ...@@ -25,8 +25,12 @@
#ifndef IHTTPCONNECTION_H_ #ifndef IHTTPCONNECTION_H_
#define IHTTPCONNECTION_H_ #define IHTTPCONNECTION_H_
#include <stdint.h> #ifdef HAVE_CONFIG_H
#include <unistd.h> # include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_stream.h>
#include <string> #include <string>
namespace dash namespace dash
...@@ -37,11 +41,17 @@ namespace dash ...@@ -37,11 +41,17 @@ namespace dash
class IHTTPConnection class IHTTPConnection
{ {
public: public:
IHTTPConnection(stream_t *stream);
virtual ~IHTTPConnection();
virtual bool init (Chunk *chunk);
virtual bool send (const std::string& data) = 0;
virtual int read (void *p_buffer, size_t len) = 0; virtual int read (void *p_buffer, size_t len) = 0;
virtual int peek (const uint8_t **pp_peek, size_t i_peek) = 0; virtual int peek (const uint8_t **pp_peek, size_t i_peek) = 0;
virtual ~IHTTPConnection() {}
protected: protected:
virtual std::string getRequestHeader(const Chunk *chunk) const; virtual std::string getRequestHeader(const Chunk *chunk) const;
virtual std::string getUrlRelative (const Chunk *chunk) const;
stream_t *stream;
int httpSocket;
}; };
} }
} }
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include "PersistentConnection.h" #include "PersistentConnection.h"
#include <vlc_network.h>
using namespace dash::http; using namespace dash::http;
const int PersistentConnection::RETRY = 5; const int PersistentConnection::RETRY = 5;
...@@ -88,28 +90,17 @@ int PersistentConnection::read (void *p_buffer, siz ...@@ -88,28 +90,17 @@ int PersistentConnection::read (void *p_buffer, siz
bool PersistentConnection::init (Chunk *chunk) bool PersistentConnection::init (Chunk *chunk)
{ {
if(this->isInit) if(isInit)
return true; return true;
if(chunk == NULL) if (IHTTPConnection::init(chunk))
return false; {
isInit = true;
if(!chunk->hasHostname()) chunkQueue.push_back(chunk);
if(!this->setUrlRelative(chunk)) hostname = chunk->getHostname();
return false; }
this->httpSocket = net_ConnectTCP(this->stream, chunk->getHostname().c_str(), chunk->getPort());
if(this->httpSocket == -1)
return false;
if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
this->isInit = true;
this->chunkQueue.push_back(chunk);
this->hostname = chunk->getHostname();
return this->isInit; return isInit;
} }
bool PersistentConnection::addChunk (Chunk *chunk) bool PersistentConnection::addChunk (Chunk *chunk)
{ {
...@@ -120,13 +111,16 @@ bool PersistentConnection::addChunk (Chunk *chunk) ...@@ -120,13 +111,16 @@ bool PersistentConnection::addChunk (Chunk *chunk)
return this->init(chunk); return this->init(chunk);
if(!chunk->hasHostname()) if(!chunk->hasHostname())
if(!this->setUrlRelative(chunk)) {
chunk->setUrl(getUrlRelative(chunk));
if(!chunk->hasHostname())
return false; return false;
}
if(chunk->getHostname().compare(this->hostname)) if(chunk->getHostname().compare(this->hostname))
return false; return false;
if(this->sendData(this->getRequestHeader(chunk).append("\r\n"))) if(send(getRequestHeader(chunk).append("\r\n")))
{ {
this->chunkQueue.push_back(chunk); this->chunkQueue.push_back(chunk);
return true; return true;
...@@ -156,7 +150,7 @@ bool PersistentConnection::initChunk (Chunk *chunk) ...@@ -156,7 +150,7 @@ bool PersistentConnection::initChunk (Chunk *chunk)
bool PersistentConnection::reconnect (Chunk *chunk) bool PersistentConnection::reconnect (Chunk *chunk)
{ {
int count = 0; int count = 0;
std::string request = this->getRequestHeader(chunk).append("\r\n"); std::string request = getRequestHeader(chunk).append("\r\n");
while(count < this->RETRY) while(count < this->RETRY)
{ {
...@@ -180,9 +174,15 @@ bool PersistentConnection::isConnected () const ...@@ -180,9 +174,15 @@ bool PersistentConnection::isConnected () const
} }
bool PersistentConnection::resendAllRequests () bool PersistentConnection::resendAllRequests ()
{ {
for(size_t i = 0; i < this->chunkQueue.size(); i++) for(size_t i = 0; i < chunkQueue.size(); i++)
if(!this->sendData(this->getRequestHeader(this->chunkQueue.at(i)).append("\r\n"))) if(!send(getRequestHeader(chunkQueue.at(i)).append("\r\n")))
return false; return false;
return true; return true;
} }
std::string PersistentConnection::getRequestHeader(const Chunk *chunk) const
{
/* can clearly see here that inheritance is reversed :/ */
return IHTTPConnection::getRequestHeader(chunk);
}
...@@ -55,6 +55,7 @@ namespace dash ...@@ -55,6 +55,7 @@ namespace dash
bool initChunk (Chunk *chunk); bool initChunk (Chunk *chunk);
bool reconnect (Chunk *chunk); bool reconnect (Chunk *chunk);
bool resendAllRequests (); bool resendAllRequests ();
virtual std::string getRequestHeader (const Chunk *chunk) const; /* reimpl */
}; };
} }
} }
......
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