aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageframework/generic/status/httpurlpath.cpp
blob: bd3ba314028dba9042786f3e20f748802c82b70e (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "httpurlpath.h"
#include <vespa/vespalib/text/stringtokenizer.h>

namespace storage::framework {

HttpUrlPath::HttpUrlPath(const vespalib::string& urlpath)
    : _path(),
      _attributes(),
      _serverSpec()
{
    init(urlpath);
}

HttpUrlPath::HttpUrlPath(const vespalib::string& urlpath,
                         const vespalib::string& serverSpec)
    : _path(),
      _attributes(),
      _serverSpec(serverSpec)
{
    init(urlpath);
}

HttpUrlPath::HttpUrlPath(vespalib::string path,
                         std::map<vespalib::string, vespalib::string> attributes,
                         vespalib::string serverSpec)
    : _path(std::move(path)),
      _attributes(std::move(attributes)),
      _serverSpec(std::move(serverSpec))
{
}

HttpUrlPath::~HttpUrlPath() {}

void
HttpUrlPath::init(const vespalib::string &urlpath)
{
    vespalib::string::size_type pos = urlpath.find('?');
    if (pos == vespalib::string::npos) {
        _path = urlpath;
    } else {
        _path = urlpath.substr(0, pos);
        vespalib::string sub(urlpath.substr(pos+1));
        vespalib::StringTokenizer tokenizer(sub, "&", "");
        for (uint32_t i=0, n=tokenizer.size(); i<n; ++i) {
            const vespalib::string& s(tokenizer[i]);
            pos = s.find('=');
            if (pos == vespalib::string::npos) {
                _attributes[s] = "";
            } else {
                _attributes[s.substr(0,pos)] = s.substr(pos+1);
            }
        }
    }
}

bool
HttpUrlPath::hasAttribute(const vespalib::string& id) const
{
    return (_attributes.find(id) != _attributes.end());
}

vespalib::string
HttpUrlPath::getAttribute(const vespalib::string& id,
                          const vespalib::string& defaultValue) const
{
    std::map<vespalib::string, vespalib::string>::const_iterator it
            = _attributes.find(id);
    return (it == _attributes.end() ? defaultValue : it->second);
}

void
HttpUrlPath::print(std::ostream& out, bool, const std::string&) const
{
    out << _path;
    if (!_attributes.empty()) {
        out << "?";
        size_t cnt = 0;
        for (const auto &attr: _attributes) {
            if (cnt++ > 0) {
                out << "&";
            }
            out << attr.first;
            if (!attr.second.empty()) {
                out << "=";
                out << attr.second;
            }
        }
    }
}

}