aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/ai/vespa/http/HttpURL.java
blob: 94d99fa7aab89db6c184820286f021791d67e240 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.http;

import ai.vespa.validation.StringWrapper;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import static ai.vespa.validation.Validation.require;
import static ai.vespa.validation.Validation.requireInRange;
import static java.net.URLDecoder.decode;
import static java.net.URLEncoder.encode;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

/**
 * This is the best class for creating, manipulating and inspecting HTTP URLs, because:
 * <ul>
 *     <li>It is more restrictive than {@link URI}, but with a richer construction API, reducing risk of blunder.
 *         <ul>
 *             <li>Scheme must be HTTP or HTTPS.</li>
 *             <li>Authority must be a {@link DomainName}, with an optional port.</li>
 *             <li>{@link Path} must be normalized at all times.</li>
 *             <li>Only {@link Query} is allowed, in addition to the above.</li>
 *         </ul>
 *     </li>
 *     <li>
 *         It contains all those helpful builder methods that {@link URI} has none of.
 *         <ul>
 *             <li>{@link Path} can be parsed, have segments or other paths appended, and cut.</li>
 *             <li>{@link Query} can be parsed, and keys and key-value pairs can be inserted or removed.</li>
 *         </ul>
 *         All these (except the parse methods) operate on <em>decoded</em> values.
 *     </li>
 *     <li>It makes it super-easy to use a {@link StringWrapper} for validation of path and query segments.</li>
 * </ul>
 *
 * @author jonmv
 */
public class HttpURL {

    private final Scheme scheme;
    private final DomainName domain;
    private final int port;
    private final Path path;
    private final Query query;

    private HttpURL(Scheme scheme, DomainName domain, int port, Path path, Query query) {
        this.scheme = requireNonNull(scheme);
        this.domain = requireNonNull(domain);
        this.port = requireInRange(port, "port number", -1, (1 << 16) - 1);
        this.path = requireNonNull(path);
        this.query = requireNonNull(query);
    }

    public static HttpURL create(Scheme scheme, DomainName domain, int port, Path path, Query query) {
        return new HttpURL(scheme, domain, port, path, query);
    }

    public static HttpURL create(Scheme scheme, DomainName domain, int port, Path path) {
        return create(scheme, domain, port, path, Query.empty());
    }

    public static HttpURL create(Scheme scheme, DomainName domain, int port) {
        return create(scheme, domain, port, Path.empty(), Query.empty());
    }

    public static HttpURL create(Scheme scheme, DomainName domain) {
        return create(scheme, domain, -1);
    }

    public static HttpURL from(URI uri) {
        return from(uri, HttpURL::requirePathSegment, HttpURL::requireNothing);
    }

    public static HttpURL from(URI uri, Consumer<String> pathValidator, Consumer<String> queryValidator) {
        if ( ! uri.normalize().equals(uri))
            throw new IllegalArgumentException("uri should be normalized, but got: " + uri);

        return create(Scheme.of(uri.getScheme()),
                      DomainName.of(requireNonNull(uri.getHost(), "URI must specify a host")),
                      uri.getPort(),
                      Path.parse(uri.getRawPath(), pathValidator),
                      Query.parse(uri.getRawQuery(), queryValidator));
    }

    /** Returns a copy of this with the given scheme. */
    public HttpURL withScheme(Scheme scheme) {
        return create(scheme, domain, port, path, query);
    }

    /** Returns a copy of this with the given domain. */
    public HttpURL withDomain(DomainName domain) {
        return create(scheme, domain, port, path, query);
    }

    /** Returns a copy of this with the given non-negative port. */
    public HttpURL withPort(int port) {
        return create(scheme, domain, port, path, query);
    }

    /** Returns a copy of this with no port specified. */
    public HttpURL withoutPort() {
        return create(scheme, domain, -1, path, query);
    }

    /** Returns a copy of this with only the given path. */
    public HttpURL withPath(Path path) {
        return create(scheme, domain, port, path, query);
    }

    /** Returns a copy of this with the given path appended. */
    public HttpURL appendPath(Path path) {
        return create(scheme, domain, port, this.path.append(path), query);
    }

    /** Returns a copy of this with only the given query. */
    public HttpURL withQuery(Query query) {
        return create(scheme, domain, port, path, query);
    }

    /** Returns a copy of this with all entries of the query appended. */
    public HttpURL appendQuery(Query query) {
        return create(scheme, domain, port, path, this.query.add(query.entries()));
    }

    public Scheme scheme() {
        return scheme;
    }

    public DomainName domain() {
        return domain;
    }

    public OptionalInt port() {
        return port == -1 ? OptionalInt.empty() : OptionalInt.of(port);
    }

    public Path path() {
        return path;
    }

    public Query query() {
        return query;
    }

    /** Returns an absolute, hierarchical URI representing this HTTP URL. */
    public URI asURI() {
        try {
            return new URI(scheme.name() + "://" + domain.value() + (port == -1 ? "" : ":" + port) + path.raw() + query.raw());
        }
        catch (URISyntaxException e) {
            throw new IllegalStateException("invalid URI, this should not happen", e);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        HttpURL httpURL = (HttpURL) o;
        return port == httpURL.port && scheme == httpURL.scheme && domain.equals(httpURL.domain) && path.equals(httpURL.path) && query.equals(httpURL.query);
    }

    @Override
    public int hashCode() {
        return Objects.hash(scheme, domain, port, path, query);
    }

    @Override
    public String toString() {
        return asURI().toString();
    }

    /** Require that the given string (possibly decoded multiple times) contains none of {@code '/', '?', '#'}, and isn't either of {@code "", ".", ".."}. */
    public static String requirePathSegment(String value) {
        while ( ! value.equals(value = decode(value, UTF_8)));
        require( ! value.contains("/"), value, "path segment decoded cannot contain '/'");
        require( ! value.contains("?"), value, "path segment decoded cannot contain '?'");
        require( ! value.contains("#"), value, "path segment decoded cannot contain '#'");
        return Path.requireNonNormalizable(value);
    }

    private static void requireNothing(String value) { }

    public static class Path {

        private static class Node {

            final Node next;
            final String value;

            Node(Node next, String value) {
                this.next = next;
                this.value = value;
            }

        }

        private static final Path empty = empty(HttpURL::requirePathSegment);

        private final Node head;
        private final int length;
        private final boolean trailingSlash;
        private final UnaryOperator<String> validator;

        private Path(Node head, int length, boolean trailingSlash, UnaryOperator<String> validator) {
            this.head = head;
            this.length = length;
            this.trailingSlash = trailingSlash;
            this.validator = requireNonNull(validator);
        }

        /** Creates a new, empty path, with a trailing slash, using {@link HttpURL#requirePathSegment} for segment validation. */
        public static Path empty() {
            return empty;
        }

        /** Creates a new, empty path, with a trailing slash, using the indicated validator for segments. */
        public static Path empty(Consumer<String> validator) {
            return new Path(null, 0, true, segmentValidator(validator));
        }

        /** Parses the given raw, normalized path string; this ignores whether the path is absolute or relative. */
        public static Path parse(String raw) {
            return parse(raw, HttpURL::requirePathSegment);
        }

        /** Parses the given raw, normalized path string; this ignores whether the path is absolute or relative. */
        public static Path parse(String raw, Consumer<String> validator) {
            Path path = new Path(null, 0, raw.endsWith("/"), segmentValidator(validator));
            if (raw.startsWith("/")) raw = raw.substring(1);
            if (raw.isEmpty()) return path;
            for (String segment : raw.split("/")) path = path.append(decode(segment, UTF_8));
            if (path.length == 0) requireNonNormalizable(""); // Raw path was only slashes.
            return path;
        }

        private static UnaryOperator<String> segmentValidator(Consumer<String> validator) {
            requireNonNull(validator, "segment validator cannot be null");
            return value -> {
                requireNonNormalizable(value);
                validator.accept(value);
                return value;
            };
        }

        private static String requireNonNormalizable(String segment) {
            return require( ! (segment.isEmpty() || segment.equals(".") || segment.equals("..")),
                           segment, "path segments cannot be \"\", \".\", or \"..\"");
        }

        /** Returns a copy of this where only the first segments are retained, and with a trailing slash. */
        public Path head(int count) {
            requireInRange(count, "head count", 0, length);
            Node node = head;
            for (int i = count; i < length; i++)
                node = node.next;

            return new Path(node, count, true, validator);
        }

        /** Returns a copy of this where only the last segments are retained. */
        public Path tail(int count) {
            requireInRange(count, "tail count", 0, length);
            return count == length ? this : new Path(head, count, trailingSlash, validator);
        }

        /** Returns a copy of this where the first segments are skipped. */
        public Path skip(int count) {
            requireInRange(count, "skip count", 0, length);
            return count == 0 ? this : new Path(head, length - count, trailingSlash, validator);
        }

        /** Returns a copy of this where the last segments are cut off, and with a trailing slash. */
        public Path cut(int count) {
            requireInRange(count, "cut count", 0, length);
            Node node = head;
            for (int i = 0; i < count; i++)
                node = node.next;

            return new Path(node, length - count, true, validator);
        }

        /** Returns a copy of this with the <em>decoded</em> segment appended at the end; it may not be either of {@code ""}, {@code "."} or {@code ".."}. */
        public Path append(String segment) {
            return append(List.of(segment), trailingSlash);
        }

        /** Returns a copy of this all segments of the other path appended, with a trailing slash as per the appendage. */
        public Path append(Path other) {
            return append(other.segments(), other.trailingSlash);
        }

        /** Returns a copy of this all given segments appended, with a trailing slash as per this path. */
        public Path append(List<String> segments) {
            return append(segments, trailingSlash);
        }

        private Path append(Iterable<String> segments, boolean trailingSlash) {
            Node node = head;
            int count = 0;
            for (String segment : segments) {
                node = new Node(node, validator.apply(segment));
                count++;
            }
            return new Path(node, length + count, trailingSlash, validator);
        }

        /** Whether this path has a trailing slash. */
        public boolean hasTrailingSlash() {
            return trailingSlash;
        }

        /** Returns a copy of this which encodes a trailing slash. */
        public Path withTrailingSlash() {
            return new Path(head, length, true, validator);
        }

        /** Returns a copy of this which does not encode a trailing slash. */
        public Path withoutTrailingSlash() {
            return new Path(head, length, false, validator);
        }

        /** A mutable copy of the <em>URL decoded</em> segments that make up this path; never {@code null}, {@code ""}, {@code "."} or {@code ".."}. */
        public List<String> segments() {
            ArrayList<String> list = new ArrayList<>(length);
            for (int i = 0; i < length; i++) list.add(null);
            Node node = head;
            for (int i = length; i-- > 0; node = node.next)
                list.set(i, node.value);

            return list;
        }

        /** The number of segments in this path. */
        public int length() {
            return length;
        }

        /** A raw path string which parses to this, by splitting on {@code "/"}, and then URL decoding. */
        private String raw() {
            StringJoiner joiner = new StringJoiner("/", "/", trailingSlash ? "/" : "").setEmptyValue(trailingSlash ? "/" : "");
            for (String segment : segments()) joiner.add(encode(segment, UTF_8));
            return joiner.toString();
        }

        /** Intentionally not usable for constructing new URIs. Use {@link HttpURL} for that instead. */
        @Override
        public String toString() {
            return "path '" + raw() + "'";
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Path path = (Path) o;
            return trailingSlash == path.trailingSlash && segments().equals(path.segments());
        }

        @Override
        public int hashCode() {
            return Objects.hash(segments(), trailingSlash);
        }

    }


    public static class Query {

        private static final Query empty = empty(__ -> { });

        private static class Node {

            final Node next;
            final String key;
            final String value;

            public Node(Node next, String key, String value) {
                this.next = next;
                this.key = key;
                this.value = value;
            }

        }

        private final Node head;
        private final UnaryOperator<String> validator;

        private Query(Node head, UnaryOperator<String> validator) {
            this.head = head;
            this.validator = requireNonNull(validator);
        }

        /** Creates a new, empty query part. */
        public static Query empty() {
            return empty;
        }

        /** Creates a new, empty query part, using the indicated string wrapper for keys and non-null values. */
        public static Query empty(Consumer<String> validator) {
            return new Query(null, entryValidator(validator));
        }

        /** Parses the given raw query string. */
        public static Query parse(String raw) {
            if (raw == null) return empty();
            return parse(raw, __-> { });
        }

        /** Parses the given raw query string, using the validator on all keys and non-null values. */
        public static Query parse(String raw, Consumer<String> validator) {
            if (raw == null) return empty(validator);
            Query query = empty(validator);
            for (String pair : raw.split("&")) {
                int split = pair.indexOf("=");
                if (split == -1) query = query.add(decode(pair, UTF_8));
                else query = query.add(decode(pair.substring(0, split), UTF_8),
                                       decode(pair.substring(split + 1), UTF_8)); // any additional '=' become part of the value
            }
            return query;
        }

        private static UnaryOperator<String> entryValidator(Consumer<String> validator) {
            requireNonNull(validator);
            return value -> {
                validator.accept(value);
                return value;
            };
        }

        /** Returns a copy of this with the <em>decoded</em> non-null key pointing to the <em>decoded</em> non-null value. */
        public Query add(String key, String value) {
            return new Query(new Node(head, validator.apply(requireNonNull(key)), validator.apply(requireNonNull(value))), validator);
        }

        /** Returns a copy of this with the <em>decoded</em> non-null key pointing to "nothing". */
        public Query add(String key) {
            return new Query(new Node(head, validator.apply(requireNonNull(key)), null), validator);
        }

        /** Returns a copy of this with the <em>decoded</em> non-null key pointing <em>only</em> to the <em>decoded</em> non-null value. */
        public Query set(String key, String value) {
            return remove(key).add(key, value);
        }

        /** Returns a copy of this with the <em>decoded</em> non-null key <em>only</em> pointing to "nothing". */
        public Query set(String key) {
            return remove(key).add(key);
        }

        /** Returns a copy of this without any key-value pair with the <em>decoded</em> key. */
        public Query remove(String key) {
            Node node = without(key::equals);
            return node == head ? this : new Query(node, validator);
        }

        private Node without(Predicate<String> filter) {
            Node head = null;
            boolean changed = false;
            for (Node node : nodes()) {
                if (filter.test(node.key)) changed = true;                  // skip node if filter applies
                else head = changed ? new Node(head, node.key, node.value)  // if our tail has changed, so must we
                                    : node;                                 // otherwise, return us unchanged
            }
            return head;
        }

        /** Returns a copy of this with all given mappings appended to this. {@code null} values, but not lists of values, are allowed. */
        public Query add(Map<String, ? extends Iterable<String>> values) {
            Query query = this;
            for (Map.Entry<String, ? extends Iterable<String>> entry : values.entrySet())
                for (String value : entry.getValue())
                    query = value == null ? query.add(entry.getKey())
                                          : query.add(entry.getKey(), value);

            return query;
        }

        /** Returns a copy of this with all given mappings added to this, possibly replacing existing mappings. */
        public Query set(Map<String, String> values) {
            Query query = remove(values.keySet());
            for (Map.Entry<String, String> entry : values.entrySet())
                query = entry.getValue() == null ? query.add(entry.getKey())
                                                 : query.add(entry.getKey(), entry.getValue());

            return query;
        }

        /** Returns a copy of this with all given keys removed. */
        public Query remove(Collection<String> keys) {
            Node node = without(keys::contains);
            return node == head ? this : new Query(node, validator);
        }

        /**
         * A mutable copy of the <em>URL decoded</em> key-value pairs that make up this query.
         * Keys and values may be {@code ""}, and values are {@code null} when only key was specified.
         * When a key was used multiple times, this map contains only the last value associated with the key.
         */
        public Map<String, String> lastEntries() {
            Map<String, String> entries = new LinkedHashMap<>();
            for (Node node : nodes())
                entries.put(node.key, node.value);

            return entries;
        }

        /**
         * A mutable copy of the <em>URL decoded</em> key-value pairs that make up this query.
         * Keys and values may be {@code ""}, and values (not lists of values) are {@code null} when only key was specified.
         * When a key was used multiple times, this map lists the values in the same order as they were given.
         */
        public Map<String, List<String>> entries() {
            Map<String, List<String>> entries = new LinkedHashMap<>();
            for (Node node : nodes())
                entries.computeIfAbsent(node.key, __ -> new ArrayList<>(2)).add(node.value);

            return entries;
        }

        /** A raw query string, with {@code '?'} prepended, that parses to this, by splitting on {@code "&"}, then on {@code "="}, and then URL decoding; or the empty string if this is empty. */
        private String raw() {
            StringJoiner joiner = new StringJoiner("&", "?", "").setEmptyValue("");
            for (Node node : nodes())
                joiner.add(encode(node.key, UTF_8) +
                           (node.value == null ? "" : "=" + encode(node.value, UTF_8)));

            return joiner.toString();
        }

        /** Nodes in insertion order. */
        private Iterable<Node> nodes() {
            Deque<Node> nodes = new ArrayDeque<>();
            for (Node node = head; node != null; node = node.next)
                nodes.push(node);

            return nodes;
        }

        /** Intentionally not usable for constructing new URIs. Use {@link HttpURL} for that instead. */
        @Override
        public String toString() {
            return head == null ? "no query" : "query '" + raw().substring(1) + "'";
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Query query = (Query) o;
            return entries().equals(query.entries());
        }

        @Override
        public int hashCode() {
            return Objects.hash(entries());
        }

    }


    public enum Scheme {
        http,
        https;
        public static Scheme of(String scheme) {
            if (scheme.equalsIgnoreCase(http.name())) return http;
            if (scheme.equalsIgnoreCase(https.name())) return https;
            throw new IllegalArgumentException("scheme must be HTTP or HTTPS");
        }
    }

}