aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/location.cpp
blob: 36c084667363c721a588fbb5c6c2186b6fa2ac22 (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
#include "location.h"

#include <util/string/cast.h>

using namespace NNeh;

TParsedLocation::TParsedLocation(TStringBuf path) {
    path.Split(':', Scheme, path);
    path.Skip(2);

    const size_t pos = path.find_first_of(TStringBuf("?@"));

    if (TStringBuf::npos != pos && '@' == path[pos]) {
        path.SplitAt(pos, UserInfo, path);
        path.Skip(1);
    }

    auto checkRange = [](size_t b, size_t e){
        return b != TStringBuf::npos && e != TStringBuf::npos && b < e;
    };

    size_t oBracket = path.find_first_of('[');
    size_t cBracket = path.find_first_of(']');
    size_t endEndPointPos = path.find_first_of('/');
    if (checkRange(oBracket, cBracket)) {
        endEndPointPos = path.find_first_of('/', cBracket);
    }
    EndPoint = path.SubStr(0, endEndPointPos);
    Host = EndPoint;

    size_t lastColon = EndPoint.find_last_of(':');
    if (checkRange(cBracket, lastColon)
        || (cBracket == TStringBuf::npos && lastColon != TStringBuf::npos))
    {
        Host = EndPoint.SubStr(0, lastColon);
        Port = EndPoint.SubStr(lastColon + 1, EndPoint.size() - lastColon + 1);
    }

    if (endEndPointPos != TStringBuf::npos) {
        Service = path.SubStr(endEndPointPos + 1, path.size() - endEndPointPos + 1);
    }
}

ui16 TParsedLocation::GetPort() const {
    if (!Port) {
        return TStringBuf("https") == Scheme || TStringBuf("fulls") == Scheme || TStringBuf("posts") == Scheme ? 443 : 80;
    }

    return FromString<ui16>(Port);
}