aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/application/HttpProxy.java
blob: 22236281a9381e1643ad932996a6b779c293ab82 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.application;

import ai.vespa.http.DomainName;
import ai.vespa.http.HttpURL;
import ai.vespa.http.HttpURL.Path;
import ai.vespa.http.HttpURL.Query;
import ai.vespa.http.HttpURL.Scheme;
import com.yahoo.component.annotation.Inject;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.PortInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.config.provision.security.NodeHostnameVerifier;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.vespa.config.server.http.HttpFetcher;
import com.yahoo.vespa.config.server.http.HttpFetcher.Params;
import com.yahoo.vespa.config.server.http.NotFoundException;
import com.yahoo.vespa.config.server.http.SimpleHttpFetcher;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

public class HttpProxy {

    private final HttpFetcher fetcher;

    @Inject public HttpProxy(NodeHostnameVerifier verifier) { this(new SimpleHttpFetcher(Duration.ofSeconds(30), verifier)); }

    public HttpProxy(HttpFetcher fetcher) { this.fetcher = fetcher; }

    public HttpResponse get(Application application, String hostName, String serviceType, Path path, Query query) {
        return get(application, hostName, serviceType, path, query, null);
    }

    public HttpResponse get(Application application, String hostName, String serviceType, Path path, Query query, HttpURL forwardedUrl) {
        HostInfo host = application.getModel().getHosts().stream()
                .filter(hostInfo -> hostInfo.getHostname().equals(hostName))
                .findFirst()
                .orElseThrow(() -> new NotFoundException("Failed to find host " + hostName));

        ServiceInfo service = host.getServices().stream()
                .filter(serviceInfo -> serviceType.equals(serviceInfo.getServiceType()))
                .findFirst()
                .orElseThrow(() -> new NotFoundException("Failed to find any service of type " + serviceType
                        + " on host " + hostName));

        // "http" and "state" seems to uniquely identify an interesting HTTP port on each service
        PortInfo port = service.getPorts().stream()
                               .filter(portInfo -> portInfo.getTags().containsAll(List.of("http", "state")))
                               .findFirst()
                               .orElseThrow(() -> new NotFoundException("Failed to find HTTP state port"));

        HttpURL url = HttpURL.create(Scheme.http, DomainName.of(host.getHostname()), port.getPort(), path, query);
        HttpResponse response = fetcher.get(new Params(29_000), // 29 sec (30 sec on controller)
                                            url.asURI());
        return forwardedUrl == null ? response : new UrlRewritingProxyResponse(response, url, forwardedUrl);
    }

    static class UrlRewritingProxyResponse extends HttpResponse {

        final HttpResponse wrapped;
        final String patten;
        final String replacement;

        public UrlRewritingProxyResponse(HttpResponse wrapped, HttpURL requestUrl, HttpURL forwardedUrl) {
            super(wrapped.getStatus());
            this.wrapped = wrapped;
            this.patten = requestUrl.withPath(requestUrl.path().withoutTrailingSlash()).withQuery(Query.empty()).asURI().toString();
            this.replacement = forwardedUrl.withPath(forwardedUrl.path().withoutTrailingSlash()).withQuery(Query.empty()).asURI().toString();
        }

        @Override
        public void render(OutputStream outputStream) throws IOException {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            wrapped.render(buffer);
            outputStream.write(buffer.toString(Charset.forName(wrapped.getCharacterEncoding()))
                                     .replace(patten, replacement)
                                     .getBytes(UTF_8));
        }

        @Override
        public String getContentType() {
            return wrapped.getContentType();
        }

    }

}