aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/url_builder.cpp
blob: 31344fbb22148a61b85457d0864498edaae8e971 (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 "url_builder.h"
#include <library/cpp/string_utils/quote/quote.h>
#include <util/generic/yexception.h>

namespace NYql {

TUrlBuilder::TUrlBuilder(const TString& uri)
    : MainUri(uri)
{
}

TUrlBuilder& TUrlBuilder::AddUrlParam(const TString& name, const TString& value) {
    Params.emplace_back(TParam {name, value});
    return *this;
}

TUrlBuilder& TUrlBuilder::AddPathComponent(const TString& value) {
    if (!value) {
        throw yexception() << "Empty path component is not allowed";
    }
    TStringBuilder res;
    res << MainUri;
    if (!MainUri.EndsWith('/')) {
        res << '/';
    }
    res << UrlEscapeRet(value, true);

    MainUri = std::move(res);
    return *this;
}

TString TUrlBuilder::Build() const {
    if (Params.empty()) {
        return MainUri;
    }

    TStringBuilder res;
    res << MainUri << "?";
    TStringBuf separator = ""sv;
    for (const auto& p : Params) {
        res << separator << p.Name;
        if (p.Value) {
            res << "=" << CGIEscapeRet(p.Value);
        }
        separator = "&"sv;
    }
    return std::move(res);
}

} // NYql