summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
diff options
context:
space:
mode:
authorHaavard <havardpe@yahoo-inc.com>2016-12-02 10:20:53 +0000
committerHaavard <havardpe@yahoo-inc.com>2016-12-02 10:20:53 +0000
commit9651729341e8cce9d2c4e529ebc4bf070ac61648 (patch)
treed6cd4a41ba1afe37abdf47fcb92440a75d6471bb /staging_vespalib
parent1845c6c3ab5f870beec72dd85f764bf65adb1e82 (diff)
prepare for url escape
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/vespa/vespalib/net/generic_state_handler.cpp57
1 files changed, 38 insertions, 19 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/net/generic_state_handler.cpp b/staging_vespalib/src/vespa/vespalib/net/generic_state_handler.cpp
index b0310c91c64..5b17b1eb307 100644
--- a/staging_vespalib/src/vespa/vespalib/net/generic_state_handler.cpp
+++ b/staging_vespalib/src/vespa/vespalib/net/generic_state_handler.cpp
@@ -7,6 +7,37 @@ namespace vespalib {
namespace {
+vespalib::string url_escape(const vespalib::string &item) {
+ return item;
+}
+
+class Url {
+private:
+ vespalib::string _url;
+ void append(const vespalib::string &item) {
+ if (*_url.rbegin() != '/') {
+ _url.append('/');
+ }
+ _url.append(url_escape(item));
+ }
+public:
+ Url(const vespalib::string &host, const std::vector<vespalib::string> &items)
+ : _url("http://")
+ {
+ _url.append(host);
+ _url.append('/');
+ for (const auto &item: items) {
+ append(item);
+ }
+ }
+ Url(const Url &parent, const vespalib::string &item)
+ : _url(parent._url)
+ {
+ append(item);
+ }
+ const vespalib::string &get() const { return _url; }
+};
+
std::vector<vespalib::string> split_path(const vespalib::string &path) {
vespalib::string tmp;
std::vector<vespalib::string> items;
@@ -38,43 +69,31 @@ bool is_prefix(const std::vector<vespalib::string> &root, const std::vector<vesp
return true;
}
-vespalib::string make_url(const vespalib::string &host, const std::vector<vespalib::string> &items) {
- vespalib::string url = "http://" + host;
- if (items.empty()) {
- url += "/";
- }
- for (const vespalib::string &item: items) {
- url += "/" + item;
- }
- return url;
-}
-
-void inject_children(const StateExplorer &state, const vespalib::string &url, slime::Cursor &self);
+void inject_children(const StateExplorer &state, const Url &url, slime::Cursor &self);
-Slime child_state(const StateExplorer &state, const vespalib::string &url) {
+Slime child_state(const StateExplorer &state, const Url &url) {
Slime child_state;
state.get_state(slime::SlimeInserter(child_state), false);
if (child_state.get().type().getId() == slime::NIX::ID) {
inject_children(state, url, child_state.setObject());
} else {
- child_state.get().setString("url", url);
+ child_state.get().setString("url", url.get());
}
return child_state;
}
-void inject_children(const StateExplorer &state, const vespalib::string &url, slime::Cursor &self) {
+void inject_children(const StateExplorer &state, const Url &url, slime::Cursor &self) {
std::vector<vespalib::string> children_names = state.get_children_names();
for (const vespalib::string &child_name: children_names) {
std::unique_ptr<StateExplorer> child = state.get_child(child_name);
if (child) {
- vespalib::string child_url = url + "/" + child_name;
- Slime fragment = child_state(*child, child_url);
+ Slime fragment = child_state(*child, Url(url, child_name));
slime::inject(fragment.get(), slime::ObjectInserter(self, child_name));
}
}
}
-vespalib::string render(const StateExplorer &state, const vespalib::string &url) {
+vespalib::string render(const StateExplorer &state, const Url &url) {
Slime top;
state.get_state(slime::SlimeInserter(top), true);
if (top.get().type().getId() == slime::NIX::ID) {
@@ -89,7 +108,7 @@ vespalib::string render(const StateExplorer &state, const vespalib::string &url)
vespalib::string explore(const StateExplorer &state, const vespalib::string &host,
const std::vector<vespalib::string> &items, size_t pos) {
if (pos == items.size()) {
- return render(state, make_url(host, items));
+ return render(state, Url(host, items));
}
std::unique_ptr<StateExplorer> child = state.get_child(items[pos]);
if (!child) {