[vlc-devel] [PATCH 02/12] dash: refactored HTTPConnection

Christopher at mailsrv.uni-klu.ac.at Christopher at mailsrv.uni-klu.ac.at
Tue Mar 13 15:18:15 CET 2012


From: Christopher Mueller <christopher.mueller at itec.aau.at>

---
 modules/stream_filter/dash/http/HTTPConnection.cpp |   74 +++++++++++---------
 modules/stream_filter/dash/http/HTTPConnection.h   |   34 ++++-----
 .../dash/http/HTTPConnectionManager.cpp            |    4 +-
 3 files changed, 57 insertions(+), 55 deletions(-)

diff --git a/modules/stream_filter/dash/http/HTTPConnection.cpp b/modules/stream_filter/dash/http/HTTPConnection.cpp
index 13f572a..8fbda43 100644
--- a/modules/stream_filter/dash/http/HTTPConnection.cpp
+++ b/modules/stream_filter/dash/http/HTTPConnection.cpp
@@ -26,16 +26,14 @@
 #endif
 
 #include "HTTPConnection.h"
-#include <vlc_url.h>
 
 using namespace dash::http;
 
-HTTPConnection::HTTPConnection  (Chunk *chunk, stream_t *stream) :
+HTTPConnection::HTTPConnection  (stream_t *stream) :
                 stream          (stream),
-                chunk           (chunk),
-                peekBufferLen   (0)
+                peekBufferLen   (0),
+                contentLength   (0)
 {
-    this->url        = chunk->getUrl();
     this->peekBuffer = new uint8_t[PEEKBUFFER];
 }
 HTTPConnection::~HTTPConnection ()
@@ -73,48 +71,41 @@ int             HTTPConnection::peek            (const uint8_t **pp_peek, size_t
     *pp_peek = peek;
     return size;
 }
-void            HTTPConnection::parseURL        ()
+std::string     HTTPConnection::prepareRequest  (Chunk *chunk)
 {
-    vlc_url_t url_components;
-    vlc_UrlParse(&url_components, this->url.c_str(), 0);
-    this->path = url_components.psz_path;
-    this->port = url_components.i_port ? url_components.i_port : 80;
+    std::string request;
 
-    if(this->url.compare(0, 4, "http"))
-        this->hostname = Helper::combinePaths(Helper::getDirectoryPath(stream->psz_path), this->url);
-    else
-        this->hostname = url_components.psz_host;
-
-    this->request = "GET " + this->path + " HTTP/1.1\r\n" +
-                    "Host: " + this->hostname + "\r\nConnection: close\r\n\r\n";
-}
-void            HTTPConnection::prepareRequest  ()
-{
     if(!chunk->useByteRange())
     {
-        this->request = "GET "          + this->path     + " HTTP/1.1" + "\r\n" +
-                        "Host: "        + this->hostname + "\r\n" +
-                        "Connection: close\r\n\r\n";
+        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 " << this->path << " HTTP/1.1\r\n" <<
-               "Host: " << this->hostname << "\r\n" <<
-               "Range: bytes=" << this->chunk->getStartByte() << "-" << this->chunk->getEndByte() << "\r\n" <<
+        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";
 
-        this->request = req.str();
+        request = req.str();
     }
+
+    return request;
 }
-bool            HTTPConnection::init            ()
+bool            HTTPConnection::init            (Chunk *chunk)
 {
-    this->parseURL();
-    this->prepareRequest();
+    if(!chunk->hasHostname())
+        if(!this->setUrlRelative(chunk))
+            return false;
+
+    this->httpSocket = net_ConnectTCP(this->stream, chunk->getHostname().c_str(), chunk->getPort());
 
-    this->httpSocket = net_ConnectTCP(this->stream, this->hostname.c_str(), this->port);
+    if(this->httpSocket == -1)
+        return false;
 
-    if(this->sendData(this->request))
+    if(this->sendData(this->prepareRequest(chunk)))
         return this->parseHeader();
 
     return false;
@@ -123,9 +114,18 @@ bool            HTTPConnection::parseHeader     ()
 {
     std::string line = this->readLine();
 
+    if(line.size() == 0)
+        return false;
+
     while(line.compare("\r\n"))
     {
+        if(!line.compare(0, 14, "Content-Length"))
+            this->contentLength = atoi(line.substr(15,line.size()).c_str());
+
         line = this->readLine();
+
+        if(line.size() == 0)
+            return false;
     }
 
     return true;
@@ -148,7 +148,7 @@ std::string     HTTPConnection::readLine        ()
     if(size > 0)
         return ss.str();
 
-    return "\r\n";
+    return "";
 }
 bool            HTTPConnection::sendData        (const std::string& data)
 {
@@ -168,3 +168,11 @@ void            HTTPConnection::closeSocket     ()
 {
     net_Close(this->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();
+}
diff --git a/modules/stream_filter/dash/http/HTTPConnection.h b/modules/stream_filter/dash/http/HTTPConnection.h
index f27b152..297f993 100644
--- a/modules/stream_filter/dash/http/HTTPConnection.h
+++ b/modules/stream_filter/dash/http/HTTPConnection.h
@@ -48,32 +48,26 @@ namespace dash
         class HTTPConnection : public IHTTPConnection
         {
             public:
-                HTTPConnection          ( Chunk *chunk, stream_t *stream );
+                HTTPConnection          (stream_t *stream);
                 virtual ~HTTPConnection ();
 
-                bool        init            ();
-                void        closeSocket     ();
-
+                virtual bool    init        (Chunk *chunk);
+                void            closeSocket ();
                 virtual int     read        (void *p_buffer, size_t len);
                 virtual int     peek        (const uint8_t **pp_peek, size_t i_peek);
 
-            private:
-                int                     httpSocket;
-                std::string             url;
-                std::string             hostname;
-                std::string             path;
-                int                     port;
-                std::string             request;
-                stream_t                *stream;
-                Chunk                   *chunk;
-                uint8_t                 *peekBuffer;
-                size_t                  peekBufferLen;
+            protected:
+                int         httpSocket;
+                stream_t    *stream;
+                uint8_t     *peekBuffer;
+                size_t      peekBufferLen;
+                int         contentLength;
 
-                void            parseURL        ();
-                bool            sendData        (const std::string& data);
-                bool            parseHeader     ();
-                std::string     readLine        ();
-                void            prepareRequest  ();
+                bool                sendData        (const std::string& data);
+                bool                parseHeader     ();
+                std::string         readLine        ();
+                virtual std::string prepareRequest  (Chunk *chunk);
+                bool                setUrlRelative  (Chunk *chunk);
         };
     }
 }
diff --git a/modules/stream_filter/dash/http/HTTPConnectionManager.cpp b/modules/stream_filter/dash/http/HTTPConnectionManager.cpp
index dd4a57e..c0e18b2 100644
--- a/modules/stream_filter/dash/http/HTTPConnectionManager.cpp
+++ b/modules/stream_filter/dash/http/HTTPConnectionManager.cpp
@@ -137,8 +137,8 @@ int                 HTTPConnectionManager::peek                     (Chunk *chun
 
 IHTTPConnection*     HTTPConnectionManager::initConnection(Chunk *chunk)
 {
-    HTTPConnection *con = new HTTPConnection(chunk, this->stream);
-    if ( con->init() == false )
+    HTTPConnection *con = new HTTPConnection(this->stream);
+    if ( con->init(chunk) == false )
         return NULL;
     this->chunkMap[chunk] = con;
     this->chunkCount++;
-- 
1.7.0.4




More information about the vlc-devel mailing list