]> git.sesse.net Git - vlc/commitdiff
stream_filter: dash: refactor the HTTP thing
authorFrancois Cartegnie <fcvlcdev@free.fr>
Thu, 20 Nov 2014 17:00:15 +0000 (18:00 +0100)
committerFrancois Cartegnie <fcvlcdev@free.fr>
Thu, 18 Dec 2014 20:23:49 +0000 (21:23 +0100)
modules/stream_filter/dash/http/HTTPConnection.cpp
modules/stream_filter/dash/http/HTTPConnection.h
modules/stream_filter/dash/http/IHTTPConnection.cpp
modules/stream_filter/dash/http/IHTTPConnection.h
modules/stream_filter/dash/http/PersistentConnection.cpp
modules/stream_filter/dash/http/PersistentConnection.h

index d545caa9a214811e1caf65961a61e875251ebdd4..0804adadc8179a69435bbc24e362b1fcf647458b 100644 (file)
 #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)
+    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();
@@ -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();
-}
index 316f61282fa52beedcbfaaa295cd0ca0ab9b0fed..97301ac1f316640f7b6ba017289bd37af0f1a0ff 100644 (file)
 #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);
         };
     }
 }
index a72e480b86df711d37e30f5345fbb795d176f557..15623e725cbd83290dce66a7e927ff8b3ba35147 100644 (file)
 
 #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();
 }
index 8c46f5700c8a0a04e7e26bec865480fac1d6b5a0..172f94fc6b16b1922b12fe26a0a0b76a201ca9a1 100644 (file)
 #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;
         };
     }
 }
index 55851d086e3e901e30efa2b370519102d92a5d7d..79ba500ceb3def67a794610b5bc852a4ec2de138 100644 (file)
@@ -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);
+}
index dd5ac70e0481cf81bad108667d4f4056f004fdf3..e0756495c943f8d24d35930b5c0ab0faa2c32f33 100644 (file)
@@ -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 */
         };
     }
 }