summaryrefslogtreecommitdiffstats
path: root/wsd/ServerURL.hpp
blob: 85dfb27c7c9655dc5d98d10120067e2f80cd3e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <string>
#include "RequestDetails.hpp"
#include "COOLWSD.hpp"

/** This class helps us to build a URL that will reliably point back
 * at our service. It does very simple splitting of proxy URL
 * and handles the proxy prefix feature.
 */
class ServerURL
{
    bool        _ssl;
    bool        _websocket;
    std::string _schemeAuthority;
    std::string _pathPlus;
public:
    ServerURL(const RequestDetails &requestDetails)
    {
        init(requestDetails.getHostUntrusted(),
             requestDetails.getProxyPrefix());
    }

    explicit ServerURL()
    {
        init("nohostname", "");
    }

    void init(const std::string &host, const std::string &proxyPrefix)
    {
        // The user can override the ServerRoot with a new prefix.
        _pathPlus = COOLWSD::ServiceRoot;

        _ssl = (COOLWSD::isSSLEnabled() || COOLWSD::isSSLTermination());
        _websocket = true;
        std::string serverName = COOLWSD::ServerName.empty() ? host : COOLWSD::ServerName;
        _schemeAuthority = serverName;

        // A well formed ProxyPrefix will override it.
        const std::string& url = proxyPrefix;
        if (url.size() <= 0)
            return;

        std::size_t pos = url.find("://");
        if (pos != std::string::npos) {
            pos += 3;
            auto hostEndPos = url.find('/', pos);
            if (hostEndPos != std::string::npos)
            {
                _websocket = false;
                std::string schemeProtocol = url.substr(0, pos);
                _ssl = (schemeProtocol != "http://");
                _schemeAuthority = url.substr(pos, hostEndPos - pos);
                _pathPlus = url.substr(hostEndPos);
                return;
            }
            else
                LOG_ERR("Unusual proxy prefix '" << url << '\'');
        } else
            LOG_ERR("No http[s]:// in unusual proxy prefix '" << url << '\'');
    }

    std::string getResponseRoot() const
    {
        return _pathPlus;
    }

    std::string getWebSocketUrl() const
    {
        std::string schemeProtocol = (_websocket ? "ws" : "http");
        if (_ssl)
            schemeProtocol += 's';
        return schemeProtocol + "://" + _schemeAuthority;
    }

    std::string getSubURLForEndpoint(const std::string &path) const
    {
        return std::string("http") + (_ssl ? "s" : "") + "://" + _schemeAuthority + _pathPlus + path;
    }
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */