]> git.sesse.net Git - vlc/commitdiff
stream_filter: dash: factorize http header stuff
authorFrancois Cartegnie <fcvlcdev@free.fr>
Thu, 20 Nov 2014 13:52:56 +0000 (14:52 +0100)
committerFrancois Cartegnie <fcvlcdev@free.fr>
Thu, 18 Dec 2014 20:23:49 +0000 (21:23 +0100)
modules/stream_filter/Makefile.am
modules/stream_filter/dash/http/HTTPConnection.cpp
modules/stream_filter/dash/http/HTTPConnection.h
modules/stream_filter/dash/http/IHTTPConnection.cpp [new file with mode: 0644]
modules/stream_filter/dash/http/IHTTPConnection.h
modules/stream_filter/dash/http/PersistentConnection.cpp
modules/stream_filter/dash/http/PersistentConnection.h

index bb8b61e4772baaefd4ade1d247ee17153e938305..b0a8aaaec9fb364f073716ea54a7c37b9a96daeb 100644 (file)
@@ -30,6 +30,7 @@ libdash_plugin_la_SOURCES = \
     stream_filter/dash/http/HTTPConnection.h \
     stream_filter/dash/http/HTTPConnectionManager.cpp \
     stream_filter/dash/http/HTTPConnectionManager.h \
+    stream_filter/dash/http/IHTTPConnection.cpp \
     stream_filter/dash/http/IHTTPConnection.h \
     stream_filter/dash/http/PersistentConnection.cpp \
     stream_filter/dash/http/PersistentConnection.h \
index 30a6427028691dd3774f1da8c4f0120b9c5f6f94..d545caa9a214811e1caf65961a61e875251ebdd4 100644 (file)
@@ -72,29 +72,13 @@ int             HTTPConnection::peek            (const uint8_t **pp_peek, size_t
     *pp_peek = peek;
     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();
-    }
-
-    return request;
+std::string     HTTPConnection::getRequestHeader  (const Chunk *chunk) const
+{
+    return IHTTPConnection::getRequestHeader(chunk)
+            .append("Connection: close\r\n");
 }
+
 bool            HTTPConnection::init            (Chunk *chunk)
 {
     if(!chunk->hasHostname())
@@ -106,7 +90,7 @@ bool            HTTPConnection::init            (Chunk *chunk)
     if(this->httpSocket == -1)
         return false;
 
-    if(this->sendData(this->prepareRequest(chunk)))
+    if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
         return this->parseHeader();
 
     return false;
index 81a3dd5ae483dcb4e3fb6d2c876a12972dcb4407..316f61282fa52beedcbfaaa295cd0ca0ab9b0fed 100644 (file)
@@ -66,7 +66,7 @@ namespace dash
                 bool                sendData        (const std::string& data);
                 bool                parseHeader     ();
                 std::string         readLine        ();
-                virtual std::string prepareRequest  (Chunk *chunk);
+                virtual std::string getRequestHeader(const Chunk *chunk) const; /* reimpl */
                 bool                setUrlRelative  (Chunk *chunk);
         };
     }
diff --git a/modules/stream_filter/dash/http/IHTTPConnection.cpp b/modules/stream_filter/dash/http/IHTTPConnection.cpp
new file mode 100644 (file)
index 0000000..a72e480
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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;
+}
index cc69e900d98c2470b91bb06c41a55e82decbd5a0..8c46f5700c8a0a04e7e26bec865480fac1d6b5a0 100644 (file)
 
 #include <stdint.h>
 #include <unistd.h>
+#include <string>
 
 namespace dash
 {
     namespace http
     {
+        class Chunk;
         class IHTTPConnection
         {
             public:
                 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;
         };
     }
 }
index 689986e8614e194d851be84f6ce4cbac2400f58e..55851d086e3e901e30efa2b370519102d92a5d7d 100644 (file)
@@ -85,25 +85,7 @@ int                 PersistentConnection::read              (void *p_buffer, siz
 
     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)
 {
     if(this->isInit)
@@ -121,7 +103,7 @@ bool                PersistentConnection::init              (Chunk *chunk)
     if(this->httpSocket == -1)
         return false;
 
-    if(this->sendData(this->prepareRequest(chunk)))
+    if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
         this->isInit = true;
 
     this->chunkQueue.push_back(chunk);
@@ -144,7 +126,7 @@ bool                PersistentConnection::addChunk          (Chunk *chunk)
     if(chunk->getHostname().compare(this->hostname))
         return false;
 
-    if(this->sendData(this->prepareRequest(chunk)))
+    if(this->sendData(this->getRequestHeader(chunk).append("\r\n")))
     {
         this->chunkQueue.push_back(chunk);
         return true;
@@ -174,7 +156,7 @@ bool                PersistentConnection::initChunk         (Chunk *chunk)
 bool                PersistentConnection::reconnect         (Chunk *chunk)
 {
     int         count   = 0;
-    std::string request = this->prepareRequest(chunk);
+    std::string request = this->getRequestHeader(chunk).append("\r\n");
 
     while(count < this->RETRY)
     {
@@ -199,7 +181,7 @@ bool                PersistentConnection::isConnected       () const
 bool                PersistentConnection::resendAllRequests ()
 {
     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 true;
index c1fe9364701641a0c98d9ed5afe2a07e0b9ea1d1..dd5ac70e0481cf81bad108667d4f4056f004fdf3 100644 (file)
@@ -52,7 +52,6 @@ namespace dash
                 static const int RETRY;
 
             protected:
-                virtual std::string prepareRequest      (Chunk *chunk);
                 bool                initChunk           (Chunk *chunk);
                 bool                reconnect           (Chunk *chunk);
                 bool                resendAllRequests   ();