|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.repositories.url; |
| 20 | + |
| 21 | +import com.sun.net.httpserver.HttpExchange; |
| 22 | +import com.sun.net.httpserver.HttpHandler; |
| 23 | +import com.sun.net.httpserver.HttpServer; |
| 24 | +import org.elasticsearch.common.SuppressForbidden; |
| 25 | +import org.elasticsearch.mocksocket.MockHttpServer; |
| 26 | +import org.elasticsearch.rest.RestStatus; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.lang.management.ManagementFactory; |
| 30 | +import java.net.Inet6Address; |
| 31 | +import java.net.InetAddress; |
| 32 | +import java.net.InetSocketAddress; |
| 33 | +import java.net.SocketAddress; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.nio.file.Paths; |
| 37 | +import java.nio.file.StandardCopyOption; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.Objects; |
| 40 | + |
| 41 | +import static java.util.Collections.emptyMap; |
| 42 | +import static java.util.Collections.singleton; |
| 43 | +import static java.util.Collections.singletonMap; |
| 44 | + |
| 45 | +/** |
| 46 | + * This {@link URLFixture} exposes a filesystem directory over HTTP. It is used in repository-url |
| 47 | + * integration tests to expose a directory populated using a regular FS repository. |
| 48 | + */ |
| 49 | +public class URLFixture { |
| 50 | + |
| 51 | + public static void main(String[] args) throws Exception { |
| 52 | + if (args == null || args.length != 2) { |
| 53 | + throw new IllegalArgumentException("URLFixture <working directory> <repository directory>"); |
| 54 | + } |
| 55 | + |
| 56 | + final InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
| 57 | + final HttpServer httpServer = MockHttpServer.createHttp(socketAddress, 0); |
| 58 | + |
| 59 | + try { |
| 60 | + final Path workingDirectory = dir(args[0]); |
| 61 | + /// Writes the PID of the current Java process in a `pid` file located in the working directory |
| 62 | + writeFile(workingDirectory, "pid", ManagementFactory.getRuntimeMXBean().getName().split("@")[0]); |
| 63 | + |
| 64 | + final String addressAndPort = addressToString(httpServer.getAddress()); |
| 65 | + // Writes the address and port of the http server in a `ports` file located in the working directory |
| 66 | + writeFile(workingDirectory, "ports", addressAndPort); |
| 67 | + |
| 68 | + // Exposes the repository over HTTP |
| 69 | + final String url = "http://" + addressAndPort; |
| 70 | + httpServer.createContext("/", new ResponseHandler(dir(args[1]))); |
| 71 | + httpServer.start(); |
| 72 | + |
| 73 | + // Wait to be killed |
| 74 | + Thread.sleep(Long.MAX_VALUE); |
| 75 | + |
| 76 | + } finally { |
| 77 | + httpServer.stop(0); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @SuppressForbidden(reason = "Paths#get is fine - we don't have environment here") |
| 82 | + private static Path dir(final String dir) { |
| 83 | + return Paths.get(dir); |
| 84 | + } |
| 85 | + |
| 86 | + private static void writeFile(final Path dir, final String fileName, final String content) throws IOException { |
| 87 | + final Path tempPidFile = Files.createTempFile(dir, null, null); |
| 88 | + Files.write(tempPidFile, singleton(content)); |
| 89 | + Files.move(tempPidFile, dir.resolve(fileName), StandardCopyOption.ATOMIC_MOVE); |
| 90 | + } |
| 91 | + |
| 92 | + private static String addressToString(final SocketAddress address) { |
| 93 | + final InetSocketAddress inetSocketAddress = (InetSocketAddress) address; |
| 94 | + if (inetSocketAddress.getAddress() instanceof Inet6Address) { |
| 95 | + return "[" + inetSocketAddress.getHostString() + "]:" + inetSocketAddress.getPort(); |
| 96 | + } else { |
| 97 | + return inetSocketAddress.getHostString() + ":" + inetSocketAddress.getPort(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static class ResponseHandler implements HttpHandler { |
| 102 | + |
| 103 | + private final Path repositoryDir; |
| 104 | + |
| 105 | + ResponseHandler(final Path repositoryDir) { |
| 106 | + this.repositoryDir = repositoryDir; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void handle(HttpExchange exchange) throws IOException { |
| 111 | + Response response; |
| 112 | + if ("GET".equalsIgnoreCase(exchange.getRequestMethod())) { |
| 113 | + String path = exchange.getRequestURI().toString(); |
| 114 | + if (path.length() > 0 && path.charAt(0) == '/') { |
| 115 | + path = path.substring(1); |
| 116 | + } |
| 117 | + |
| 118 | + Path normalizedRepositoryDir = repositoryDir.normalize(); |
| 119 | + Path normalizedPath = normalizedRepositoryDir.resolve(path).normalize(); |
| 120 | + |
| 121 | + if (normalizedPath.startsWith(normalizedRepositoryDir)) { |
| 122 | + if (Files.exists(normalizedPath) && Files.isReadable(normalizedPath) && Files.isRegularFile(normalizedPath)) { |
| 123 | + byte[] content = Files.readAllBytes(normalizedPath); |
| 124 | + Map<String, String> headers = singletonMap("Content-Length", String.valueOf(content.length)); |
| 125 | + response = new Response(RestStatus.OK, headers, "application/octet-stream", content); |
| 126 | + } else { |
| 127 | + response = new Response(RestStatus.NOT_FOUND, emptyMap(), "text/plain", new byte[0]); |
| 128 | + } |
| 129 | + } else { |
| 130 | + response = new Response(RestStatus.FORBIDDEN, emptyMap(), "text/plain", new byte[0]); |
| 131 | + } |
| 132 | + } else { |
| 133 | + response = new Response(RestStatus.INTERNAL_SERVER_ERROR, emptyMap(), "text/plain", new byte[0]); |
| 134 | + } |
| 135 | + exchange.sendResponseHeaders(response.status.getStatus(), response.body.length); |
| 136 | + if (response.body.length > 0) { |
| 137 | + exchange.getResponseBody().write(response.body); |
| 138 | + } |
| 139 | + exchange.close(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Represents a HTTP Response. |
| 145 | + */ |
| 146 | + static class Response { |
| 147 | + |
| 148 | + final RestStatus status; |
| 149 | + final Map<String, String> headers; |
| 150 | + final String contentType; |
| 151 | + final byte[] body; |
| 152 | + |
| 153 | + Response(final RestStatus status, final Map<String, String> headers, final String contentType, final byte[] body) { |
| 154 | + this.status = Objects.requireNonNull(status); |
| 155 | + this.headers = Objects.requireNonNull(headers); |
| 156 | + this.contentType = Objects.requireNonNull(contentType); |
| 157 | + this.body = Objects.requireNonNull(body); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments