aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/URL/protocol.h
blob: c1d831928351c6de3c5c0a5f4b0ef8bc8b43c1b0 (plain) (blame)
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
#pragma once

#include <Common/StringUtils/StringUtils.h>
#include <Functions/StringHelpers.h>


namespace DB
{

/// Extracts scheme from given url.
inline std::string_view getURLScheme(const char * data, size_t size)
{
    // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
    const char * pos = data;
    const char * end = data + size;

    if (isAlphaASCII(*pos))
    {
        for (++pos; pos < end; ++pos)
        {
            if (!(isAlphaNumericASCII(*pos) || *pos == '+' || *pos == '-' || *pos == '.'))
            {
                break;
            }
        }

        return std::string_view(data, pos - data);
    }

    return {};
}

struct ExtractProtocol
{
    static size_t getReserveLengthForElement()
    {
        return strlen("https") + 1;
    }

    static void execute(Pos data, size_t size, Pos & res_data, size_t & res_size)
    {
        res_data = data;
        res_size = 0;

        std::string_view scheme = getURLScheme(data, size);
        Pos pos = data + scheme.size();

        if (scheme.empty() || (data + size) - pos < 4)
            return;

        if (pos[0] == ':')
            res_size = pos - data;
    }
};

}