#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)
{
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)
+ if (IHTTPConnection::init(chunk))
+ return parseHeader();
+ else
return false;
-
- if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
- return this->parseHeader();
-
- return false;
}
+
bool HTTPConnection::parseHeader ()
{
std::string line = this->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)
}
if ((size_t)size != data.length())
{
- this->sendData(data.substr(size, data.size()));
+ this->send(data.substr(size, data.size()));
}
return true;
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();
-}
#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"
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);
};
}
}
#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
- {
- 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;
+ 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";
+
+ 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();
}
#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
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;
};
}
}
#include "PersistentConnection.h"
+#include <vlc_network.h>
+
using namespace dash::http;
const int PersistentConnection::RETRY = 5;
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)
{
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;
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)
{
}
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);
+}
bool initChunk (Chunk *chunk);
bool reconnect (Chunk *chunk);
bool resendAllRequests ();
+ virtual std::string getRequestHeader (const Chunk *chunk) const; /* reimpl */
};
}
}