summaryrefslogtreecommitdiff
path: root/src/net/Url.cpp
blob: 95e31c73a56be19d3878e4179ad55d1f540811d5 (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
#include "tgbot/net/Url.h"

#include <cstddef>

using namespace std;

namespace TgBot {

Url::Url(const string& url) {
    bool isProtocolParsed = false;
    bool isHostParsed = false;
    bool isPathParsed = false;
    bool isQueryParsed = false;

    for (std::size_t i = 0, count = url.length(); i < count; ++i) {
        char c = url[i];

        if (!isProtocolParsed) {
            if (c == ':') {
                isProtocolParsed = true;
                i += 2;
            } else {
                protocol += c;
            }
        } else if (!isHostParsed) {
            if (c == '/') {
                isHostParsed = true;
                path += '/';
            } else if (c == '?') {
                isHostParsed = isPathParsed = true;
                path += '/';
            } else if (c == '#') {
                isHostParsed = isPathParsed = isQueryParsed = true;
                path += '/';
            } else {
                host += c;
            }
        } else if (!isPathParsed) {
            if (c == '?') {
                isPathParsed = true;
            } else if (c == '#') {
                isPathParsed = isQueryParsed = true;
            } else {
                path += c;
            }
        } else if (!isQueryParsed) {
            if (c == '#') {
                isQueryParsed = true;
            } else {
                query += c;
            }
        } else {
            fragment += c;
        }
    }
}

}