ConnectionParams.cpp 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * ConnectionParams.cpp
 *****************************************************************************
 * Copyright (C) 2016 - VideoLAN and VLC 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.
 *****************************************************************************/
20 21 22 23
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "ConnectionParams.hpp"

#include <vlc_url.h>
#include <sstream>

using namespace adaptive::http;

ConnectionParams::ConnectionParams()
{
}

ConnectionParams::ConnectionParams(const std::string &uri)
{
    this->uri = uri;
    parse();
}

const std::string & ConnectionParams::getUrl() const
{
    return uri;
}

const std::string & ConnectionParams::getScheme() const
{
    return scheme;
}

const std::string & ConnectionParams::getHostname() const
{
    return hostname;
}

const std::string & ConnectionParams::getPath() const
{
    return path;
}

void ConnectionParams::setPath(const std::string &path_)
{
    path = path_;

    std::ostringstream os;
    os.imbue(std::locale("C"));
67 68 69 70 71 72 73 74
    os << scheme << "://";
    if(!hostname.empty())
    {
        os << hostname;
        if( (port != 80 && scheme != "http") ||
            (port != 443 && scheme != "https") )
            os << ":" << port;
    }
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    os << path;
    uri = os.str();
}

uint16_t ConnectionParams::getPort() const
{
    return port;
}

void ConnectionParams::parse()
{
    std::size_t pos = uri.find("://");
    if(pos != std::string::npos)
    {
        scheme = uri.substr(0, pos);
    }

    vlc_url_t url_components;
    vlc_UrlParse(&url_components, uri.c_str());

    if(url_components.psz_path)
        path = url_components.psz_path;
    if(url_components.psz_option)
    {
        path += "?";
        path += url_components.psz_option;
    }
    port = url_components.i_port ? url_components.i_port :
                         ((scheme == "https") ? 443 : 80);
    if(url_components.psz_host)
        hostname = url_components.psz_host;

    vlc_UrlClean(&url_components);
}