Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
225 andreas 1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <algorithm>
20
#include <cctype>
21
#include <functional>
22
 
23
#include "turl.h"
24
#include "terror.h"
25
 
26
using std::string;
27
using std::transform;
28
using std::distance;
29
 
30
TUrl::TUrl()
31
{
32
    DECL_TRACER("TUrl::TUrl()");
33
}
34
 
35
TUrl::TUrl(const string &url)
36
{
37
    DECL_TRACER("TUrl::TUrl(const string &url)");
38
 
39
    if (!url.empty())
40
    {
41
        mUrl = url;
42
        parse();
43
    }
44
}
45
 
46
TUrl::~TUrl()
47
{
48
    DECL_TRACER("TUrl::~TUrl()");
49
}
50
 
51
bool TUrl::setUrl(const string &url)
52
{
53
    DECL_TRACER("TUrl::setUrl(const string &url)");
54
 
55
    mUrl = url;
56
    return parse();
57
}
58
 
59
bool TUrl::parse()
60
{
61
    DECL_TRACER("TUrl::parse()");
62
 
63
    string x, port;
64
    size_t offset = 0;
65
    size_t pos1, pos2, pos3, pos4, pos5;
66
    x = _trim(mUrl);
67
 
68
    offset = mUrl.find_first_of("://");
69
 
70
    if (offset == string::npos)
71
        return false;
72
 
73
    offset += 3;
74
 
75
    pos1 = x.find_first_of('/', offset + 1);
76
    mPath = pos1 == string::npos ? "" : x.substr(pos1);
77
    mDomain = string(x.begin() + offset, pos1 != string::npos ? x.begin() + pos1 : x.end());
78
 
79
    if ((pos5 = mDomain.find_first_of('@')) != string::npos)
80
    {
81
        mUser = mDomain.substr(0, pos5);
82
 
83
        if ((pos5 = mUser.find(':')) != string::npos)
84
        {
85
            mPassword = mUser.substr(pos5 + 1);
86
            mUser = mUser.substr(0, pos5);
87
        }
88
    }
89
 
90
    mPath = (pos2 = mPath.find("#")) != string::npos ? mPath.substr(0, pos2) : mPath;
91
    port = (pos3 = mDomain.find(":"))!=string::npos ? mDomain.substr(pos3+1) : "";
92
    mDomain = mDomain.substr(0, pos3!=string::npos ? pos3 : mDomain.length());
93
    mProtocol = offset > 0 ? x.substr(0,offset-3) : "";
94
    mQuery = (pos4 = mPath.find("?")) != string::npos ? mPath.substr(pos4+1) : "";
95
    mPath = pos4 != string::npos ? mPath.substr(0,pos4) : mPath;
96
 
97
    if (!port.empty())
98
        mPort = atoi(port.c_str());
99
 
100
    if (mPort == 0)
101
    {
102
        if (mProtocol == "https")           mPort = 443;
103
        else if (mProtocol == "http")       mPort = 80;
104
        else if (mProtocol == "ssh" ||
105
                 mProtocol == "sftp")       mPort = 22;
106
        else if (mProtocol == "ftp")        mPort = 21;
107
        else if (mProtocol == "mysql")      mPort = 3306;
108
        else if (mProtocol == "mongo")      mPort = 27017;
109
        else if (mProtocol == "mongo+srv")  mPort = 27017;
110
        else if (mProtocol == "kafka")      mPort = 9092;
111
        else if (mProtocol == "postgres")   mPort = 5432;
112
        else if (mProtocol == "postgresql") mPort = 5432;
113
        else if (mProtocol == "redis")      mPort = 6379;
114
        else if (mProtocol == "zookeeper")  mPort = 2181;
115
        else if (mProtocol == "ldap")       mPort = 389;
116
        else if (mProtocol == "ldaps")      mPort = 636;
117
    }
118
 
119
    return true;
120
}
121
 
122
string TUrl::_trim(const string& str)
123
{
124
    DECL_TRACER("TUrl::_trim(const string& str)");
125
 
126
    size_t start = str.find_first_not_of(" \n\r\t");
127
    size_t until = str.find_last_not_of(" \n\r\t");
128
    string::const_iterator i = start==string::npos ? str.begin() : str.begin() + start;
129
    string::const_iterator x = until==string::npos ? str.end()   : str.begin() + until+1;
130
    return string(i,x);
131
}