Url.cpp 3.63 KB
Newer Older
1 2 3
/*
 * Url.cpp
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2014 - VideoLAN and VLC Authors
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 *****************************************************************************/
20 21 22 23
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

24 25 26 27
#include "Url.hpp"
#include "BaseRepresentation.h"
#include "SegmentTemplate.h"

28
using namespace adaptive::playlist;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

Url::Url()
{
}

Url::Url(const Component & comp)
{
    prepend(comp);
}

Url::Url(const std::string &str)
{
    prepend(Component(str));
}

44 45 46 47 48
bool Url::hasScheme() const
{
    if(components.empty())
        return false;

49
    return components[0].b_scheme;
50 51
}

52 53 54 55 56
bool Url::empty() const
{
    return components.empty();
}

57 58 59 60 61 62 63 64
Url & Url::prepend(const Component & comp)
{
    components.insert(components.begin(), comp);
    return *this;
}

Url & Url::append(const Component & comp)
{
65 66
    if(!components.empty() && !components.back().b_dir)
        components.pop_back();
67 68 69 70 71 72 73 74 75 76 77 78
    components.push_back(comp);
    return *this;
}

Url & Url::prepend(const Url &url)
{
    components.insert(components.begin(), url.components.begin(), url.components.end());
    return *this;
}

Url & Url::append(const Url &url)
{
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if(!components.empty() && url.components.front().b_absolute)
    {
        if(components.front().b_scheme)
        {
            while(components.size() > 1)
                components.pop_back();
            std::string scheme(components.front().component);
            std::size_t schemepos = scheme.find_first_of("://");
            if(schemepos != std::string::npos)
            {
                std::size_t pathpos = scheme.find_first_of('/', schemepos + 3);
                if(pathpos != std::string::npos)
                    components.front().component = scheme.substr(0, pathpos);
                /* otherwise should be domain only */
            }
        }
    }

97 98
    if(!components.empty() && !components.back().b_dir)
        components.pop_back();
99 100 101 102 103 104 105 106 107 108 109 110 111
    components.insert(components.end(), url.components.begin(), url.components.end());
    return *this;
}

std::string Url::toString() const
{
    return toString(0, NULL);
}

std::string Url::toString(size_t index, const BaseRepresentation *rep) const
{
    std::string ret;
    std::vector<Component>::const_iterator it;
112
    for(it = components.begin(); it != components.end(); ++it)
113 114 115 116 117 118 119 120 121 122 123
    {
        const Component *comp = & (*it);
        if(rep)
            ret.append(rep->contextualize(index, comp->component, comp->templ));
        else
            ret.append(comp->component);
    }
    return ret;
}

Url::Component::Component(const std::string & str, const MediaSegmentTemplate *templ_)
124
 : component(str), templ(templ_), b_scheme(false), b_dir(false), b_absolute(false)
125
{
126 127
    if(!component.empty())
    {
128
        b_dir = (component[component.length()-1]=='/');
129 130
        b_scheme = (component.find_first_of("://") == (component.find_first_of('/') - 1));
        b_absolute = (component[0] =='/');
131
    }
132
}