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

stream_filter: dash: refactor the HTTP thing

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