aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/url_builder.cpp
blob: e8a55561e84b6f680c3ab2dfaaa132f06fbaf2a1 (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