Commit a833fd79 authored by Kenton Varda's avatar Kenton Varda Committed by GitHub

Merge pull request #490 from sandstorm-io/url

Add URL parsing library to libkj-http
parents 18e3c9f1 52562bf5
......@@ -471,6 +471,27 @@ TEST(Common, ArrayAsBytes) {
}
}
KJ_TEST("ArrayPtr operator ==") {
KJ_EXPECT(ArrayPtr<const int>({123, 456}) == ArrayPtr<const int>({123, 456}));
KJ_EXPECT(!(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123, 456})));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123, 321}));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123}));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) == ArrayPtr<const short>({123, 456}));
KJ_EXPECT(!(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123, 456})));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123, 321}));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123}));
KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) ==
ArrayPtr<const char* const>({"foo", "bar"})));
KJ_EXPECT(!(ArrayPtr<const StringPtr>({"foo", "bar"}) !=
ArrayPtr<const char* const>({"foo", "bar"})));
KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) !=
ArrayPtr<const char* const>({"foo", "baz"})));
KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) !=
ArrayPtr<const char* const>({"foo"})));
}
KJ_TEST("kj::range()") {
uint expected = 5;
for (uint i: range(5, 10)) {
......
......@@ -1331,6 +1331,17 @@ public:
}
inline bool operator!=(const ArrayPtr& other) const { return !(*this == other); }
template <typename U>
inline bool operator==(const ArrayPtr<U>& other) const {
if (size_ != other.size()) return false;
for (size_t i = 0; i < size_; i++) {
if (ptr[i] != other[i]) return false;
}
return true;
}
template <typename U>
inline bool operator!=(const ArrayPtr<U>& other) const { return !(*this == other); }
private:
T* ptr;
size_t size_;
......
This diff is collapsed.
This diff is collapsed.
// Copyright (c) 2017 Cloudflare, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef KJ_COMPAT_URL_H_
#define KJ_COMPAT_URL_H_
#include <kj/string.h>
#include <inttypes.h>
namespace kj {
struct Url {
String scheme;
// E.g. "http", "https".
struct UserInfo {
String username;
Maybe<String> password;
};
Maybe<UserInfo> userInfo;
// Username / password.
String host;
// Hostname, including port if specified. We choose not to parse out the port because KJ's
// network address parsing functions already accept addresses containing port numbers, and
// because most web standards don't actually want to separate host and port.
Array<String> path;
bool hasTrailingSlash = false;
// Path, split on '/' characters. Note that the individual components of `path` could contain
// '/' characters if they were percent-encoded in the original URL.
struct QueryParam {
String name;
String value;
};
Array<QueryParam> query;
// Query, e.g. from "?key=value&key2=value2". If a component of the query contains no '=' sign,
// it will be parsed as a key with an empty value.
Maybe<String> fragment;
// The stuff after the '#' character (not including the '#' character itself), if present.
// ---------------------------------------------------------------------------
Url() = default;
Url(Url&&) = default;
~Url() noexcept(false);
Url clone() const;
enum Context {
GENERAL,
// The full URL.
HTTP_PROXY_REQUEST,
// The URL to place in the first line of an HTTP proxy request. This includes scheme, host,
// path, and query, but omits userInfo (which should be used to construct the Authorization
// header) and fragment (which should not be transmitted).
HTTP_REQUEST
// The path to place in the first line of a regular HTTP request. This includes only the path
// and query. Scheme, user, host, and fragment are omitted.
};
kj::String toString(Context context = GENERAL) const;
// Convert the URL to a string.
static Url parse(StringPtr text, Context context = GENERAL);
static Maybe<Url> tryParse(StringPtr text, Context context = GENERAL);
// Parse an absolute URL.
Url parseRelative(StringPtr relative) const;
Maybe<Url> tryParseRelative(StringPtr relative) const;
// Parse a relative URL string with this URL as the base.
};
} // namespace kj
#endif // KJ_COMPAT_URL_H_
......@@ -111,6 +111,13 @@ public:
return (bits[c / 64] & (1ll << (c % 64))) != 0;
}
inline bool containsAll(ArrayPtr<const char> text) const {
for (char c: text) {
if (!contains(c)) return false;
}
return true;
}
template <typename Input>
Maybe<char> operator()(Input& input) const {
if (input.atEnd()) return nullptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment