Commit 5b85664e authored by Francois Cartegnie's avatar Francois Cartegnie

stream_filter: dash: factorize http header stuff

parent e50f11bb
...@@ -30,6 +30,7 @@ libdash_plugin_la_SOURCES = \ ...@@ -30,6 +30,7 @@ libdash_plugin_la_SOURCES = \
stream_filter/dash/http/HTTPConnection.h \ stream_filter/dash/http/HTTPConnection.h \
stream_filter/dash/http/HTTPConnectionManager.cpp \ stream_filter/dash/http/HTTPConnectionManager.cpp \
stream_filter/dash/http/HTTPConnectionManager.h \ stream_filter/dash/http/HTTPConnectionManager.h \
stream_filter/dash/http/IHTTPConnection.cpp \
stream_filter/dash/http/IHTTPConnection.h \ stream_filter/dash/http/IHTTPConnection.h \
stream_filter/dash/http/PersistentConnection.cpp \ stream_filter/dash/http/PersistentConnection.cpp \
stream_filter/dash/http/PersistentConnection.h \ stream_filter/dash/http/PersistentConnection.h \
......
...@@ -72,29 +72,13 @@ int HTTPConnection::peek (const uint8_t **pp_peek, size_t ...@@ -72,29 +72,13 @@ int HTTPConnection::peek (const uint8_t **pp_peek, size_t
*pp_peek = peek; *pp_peek = peek;
return size; return size;
} }
std::string HTTPConnection::prepareRequest (Chunk *chunk)
{
std::string request;
if(!chunk->usesByteRange())
{
request = "GET " + chunk->getPath() + " HTTP/1.1" + "\r\n" +
"Host: " + chunk->getHostname() + "\r\n" +
"Connection: close\r\n\r\n";
}
else
{
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" <<
"Connection: close\r\n\r\n";
request = req.str(); std::string HTTPConnection::getRequestHeader (const Chunk *chunk) const
} {
return IHTTPConnection::getRequestHeader(chunk)
return request; .append("Connection: close\r\n");
} }
bool HTTPConnection::init (Chunk *chunk) bool HTTPConnection::init (Chunk *chunk)
{ {
if(!chunk->hasHostname()) if(!chunk->hasHostname())
...@@ -106,7 +90,7 @@ bool HTTPConnection::init (Chunk *chunk) ...@@ -106,7 +90,7 @@ bool HTTPConnection::init (Chunk *chunk)
if(this->httpSocket == -1) if(this->httpSocket == -1)
return false; return false;
if(this->sendData(this->prepareRequest(chunk))) if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
return this->parseHeader(); return this->parseHeader();
return false; return false;
......
...@@ -66,7 +66,7 @@ namespace dash ...@@ -66,7 +66,7 @@ namespace dash
bool sendData (const std::string& data); bool sendData (const std::string& data);
bool parseHeader (); bool parseHeader ();
std::string readLine (); std::string readLine ();
virtual std::string prepareRequest (Chunk *chunk); virtual std::string getRequestHeader(const Chunk *chunk) const; /* reimpl */
bool setUrlRelative (Chunk *chunk); bool setUrlRelative (Chunk *chunk);
}; };
} }
......
/*
* IHTTPConnection.cpp
*****************************************************************************
* Copyright (C) 2014 - VideoLAN Authors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "IHTTPConnection.h"
#include "Chunk.h"
#include <sstream>
using namespace dash::http;
std::string IHTTPConnection::getRequestHeader(const Chunk *chunk) const
{
std::string request;
if(!chunk->usesByteRange())
{
request = "GET " + chunk->getPath() + " HTTP/1.1" + "\r\n" +
"Host: " + chunk->getHostname() + "\r\n";
}
else
{
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";
request = req.str();
}
return request;
}
...@@ -27,17 +27,21 @@ ...@@ -27,17 +27,21 @@
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
#include <string>
namespace dash namespace dash
{ {
namespace http namespace http
{ {
class Chunk;
class IHTTPConnection class IHTTPConnection
{ {
public: public:
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() {} virtual ~IHTTPConnection() {}
protected:
virtual std::string getRequestHeader(const Chunk *chunk) const;
}; };
} }
} }
......
...@@ -85,25 +85,7 @@ int PersistentConnection::read (void *p_buffer, siz ...@@ -85,25 +85,7 @@ int PersistentConnection::read (void *p_buffer, siz
return ret; return ret;
} }
std::string PersistentConnection::prepareRequest (Chunk *chunk)
{
std::string request;
if(!chunk->usesByteRange())
{
request = "GET " + chunk->getPath() + " HTTP/1.1" + "\r\n" +
"Host: " + chunk->getHostname() + "\r\n\r\n";
}
else
{
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\r\n";
request = req.str();
}
return request;
}
bool PersistentConnection::init (Chunk *chunk) bool PersistentConnection::init (Chunk *chunk)
{ {
if(this->isInit) if(this->isInit)
...@@ -121,7 +103,7 @@ bool PersistentConnection::init (Chunk *chunk) ...@@ -121,7 +103,7 @@ bool PersistentConnection::init (Chunk *chunk)
if(this->httpSocket == -1) if(this->httpSocket == -1)
return false; return false;
if(this->sendData(this->prepareRequest(chunk))) if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
this->isInit = true; this->isInit = true;
this->chunkQueue.push_back(chunk); this->chunkQueue.push_back(chunk);
...@@ -144,7 +126,7 @@ bool PersistentConnection::addChunk (Chunk *chunk) ...@@ -144,7 +126,7 @@ bool PersistentConnection::addChunk (Chunk *chunk)
if(chunk->getHostname().compare(this->hostname)) if(chunk->getHostname().compare(this->hostname))
return false; return false;
if(this->sendData(this->prepareRequest(chunk))) if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
{ {
this->chunkQueue.push_back(chunk); this->chunkQueue.push_back(chunk);
return true; return true;
...@@ -174,7 +156,7 @@ bool PersistentConnection::initChunk (Chunk *chunk) ...@@ -174,7 +156,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->prepareRequest(chunk); std::string request = this->getRequestHeader(chunk).append("\r\n");
while(count < this->RETRY) while(count < this->RETRY)
{ {
...@@ -199,7 +181,7 @@ bool PersistentConnection::isConnected () const ...@@ -199,7 +181,7 @@ 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 < this->chunkQueue.size(); i++)
if(!this->sendData((this->prepareRequest(this->chunkQueue.at(i))))) if(!this->sendData(this->getRequestHeader(this->chunkQueue.at(i)).append("\r\n")))
return false; return false;
return true; return true;
......
...@@ -52,7 +52,6 @@ namespace dash ...@@ -52,7 +52,6 @@ namespace dash
static const int RETRY; static const int RETRY;
protected: protected:
virtual std::string prepareRequest (Chunk *chunk);
bool initChunk (Chunk *chunk); bool initChunk (Chunk *chunk);
bool reconnect (Chunk *chunk); bool reconnect (Chunk *chunk);
bool resendAllRequests (); bool resendAllRequests ();
......
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