aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/status/statuspage/StatusPageServer.java
blob: 7070d75424868fd91b2e53018384cf50f68b822e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.status.statuspage;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Shows status pages with debug information through a very simple HTTP interface.
 */
public class StatusPageServer {

    public static Logger log = Logger.getLogger(StatusPageServer.class.getName());

    /**
     * Very simple HTTP request class. This should be replaced the second
     * the fleetcontroller e.g. moves into the container.
     */
    public static class HttpRequest {
        private final String request;
        private final String path;

        static Pattern pathPattern;
        static {
            // NOTE: allow [=.] in path to be backwards-compatible with legacy node
            // status pages.
            // If you stare at it for long enough, this sorta looks like one of those
            // magic eye pictures.
            pathPattern = Pattern.compile("^(/([\\w=./]+)?)(?:\\?((?:&?\\w+(?:=[\\w.]*)?)*))?$");
        }

        public HttpRequest(String request) {
            this.request = request;
            Matcher m = pathPattern.matcher(request);
            if (!m.matches()) {
                throw new IllegalArgumentException("Illegal HTTP request path: " + request);
            }
            path = m.group(1);
        }

        public String toString() {
            return "HttpRequest(" + request + ")";
        }

        public String getRequest() {
            return request;
        }

        public String getPath() {
            return path;
        }

    }

    public interface RequestHandler {
        StatusPageResponse handle(HttpRequest request);
        String pattern();
    }

    public interface RequestRouter {
        /**
         * Resolve a request's handler based on its path.
         * @param request HTTP request to resolve for.
         * @return the request handler, or null if none matched.
         */
        RequestHandler resolveHandler(HttpRequest request);
    }

    /**
     * Request router inspired by the Django framework's regular expression
     * based approach. Patterns are matched in the same order as they were
     * added to the router and the first matching one is used as the handler.
     */
    public static class PatternRequestRouter implements RequestRouter {
        private static class PatternRouting {
            public Pattern pattern;
            public RequestHandler handler;

            private PatternRouting(Pattern pattern, RequestHandler handler) {
                this.pattern = pattern;
                this.handler = handler;
            }
        }

        private final List<PatternRouting> patterns = new ArrayList<>();

        public void addHandler(RequestHandler handler) {
            patterns.add(new PatternRouting(Pattern.compile(handler.pattern()), handler));
        }

        @Override
        public RequestHandler resolveHandler(HttpRequest request) {
            for (PatternRouting routing : patterns) {
                Matcher m = routing.pattern.matcher(request.getPath());
                if (m.matches()) {
                    return routing.handler;
                }
            }
            return null;
        }
    }

}