@Override @NotNull public String getBaseAddress() { /* * Computes the Endpoint's address from the request. Use "Host" header * so that it has correct address(IP address or someother hostname) * through which the application reached the endpoint. * */ StringBuilder strBuf = new StringBuilder(); strBuf.append((httpExchange instanceof HttpsExchange) ? "https" : "http"); strBuf.append("://"); String hostHeader = httpExchange.getRequestHeaders().getFirst("Host"); if (hostHeader != null) { strBuf.append(hostHeader); // Uses Host header } else { strBuf.append(httpExchange.getLocalAddress().getHostName()); strBuf.append(":"); strBuf.append(httpExchange.getLocalAddress().getPort()); } //Do not include URL pattern here //strBuf.append(httpExchange.getRequestURI().getPath()); return strBuf.toString(); }
private URI getBaseUri(final HttpExchange exchange, final String decodedBasePath) { final String scheme = (exchange instanceof HttpsExchange) ? "https" : "http"; final URI baseUri; try { final List<String> hostHeader = exchange.getRequestHeaders().get("Host"); if (hostHeader != null) { baseUri = new URI(scheme + "://" + hostHeader.get(0) + decodedBasePath); } else { final InetSocketAddress addr = exchange.getLocalAddress(); baseUri = new URI(scheme, null, addr.getHostName(), addr.getPort(), decodedBasePath, null, null); } } catch (final URISyntaxException ex) { throw new IllegalArgumentException(ex); } return baseUri; }
@NotNull public static Headers withoutEntityHeaders(@NotNull final HttpExchange httpExchange, @NotNull final String cacheControlHeaderValue) { final Headers responseHeaders = httpExchange.getResponseHeaders(); responseHeaders.set(ServerHeaderName, ServerHeaderValue); responseHeaders.set(DateHeaderName, rfc2822DateForNow()); responseHeaders.set(ContentLanguageHeaderName, ContentLanguageHeaderValue); responseHeaders.set(ConnectionHeaderName, "close"); final boolean isHttps = httpExchange instanceof HttpsExchange; if (isHttps) { responseHeaders.set(StrictTransportSecurityHeaderName, StrictTransportSecurityHeaderValueMaximum); } responseHeaders.set(XFrameOptionsHeaderName, "DENY"); responseHeaders.set(XXssProtectionHeaderName, "1; mode=block"); responseHeaders.set(XRimAutoMatchHeaderName, "none"); responseHeaders.set(XRobotsTagHeaderName, "none"); responseHeaders.set(AccessControlAllowOriginHeaderName, "*"); responseHeaders.set(AccessControlAllowMethodsHeaderName, "POST, PUT, DELETE, GET, HEAD, OPTIONS"); responseHeaders.set(AccessControlAllowHeadersHeaderName, "Authorization, Content-Type"); responseHeaders.set(CacheControlHeaderName, cacheControlHeaderValue); responseHeaders.set(AcceptRangesHeaderName, "none"); return responseHeaders; }
@Override public boolean isSecure() { return (httpExchange instanceof HttpsExchange); }
@Override public String getRequestScheme() { return (httpExchange instanceof HttpsExchange) ? "https" : "http"; }
public void handle(HttpExchange httpExchange) throws IOException { HttpsExchange exchange = (HttpsExchange) httpExchange; Boolean hasPassword = false; String myPassword = ""; String myHdrUser = ""; String myHdrMessage = ""; String[] uriQueryList = null; // Get the path and query string from the URI URI uriData = exchange.getRequestURI(); String uriPath = uriData.getPath(); String uriQuery = uriData.getQuery(); if (uriQuery != null) { uriQueryList = uriQuery.split("&"); } // Get the headers Headers headers = exchange.getRequestHeaders(); // Get the Request Method (GET/PUT) String requestMethod = exchange.getRequestMethod(); // Get any data from the body, although, we just discard it, this is required InputStream inputStream = exchange.getRequestBody(); while (inputStream.read() != -1) { inputStream.skip(0x10000); } inputStream.close(); if (headers.containsKey("password")) { myPassword = headers.getFirst("password"); if (myPassword.equals(serverPassword) || myPassword.equals("oauth:" + serverPassword)) { hasPassword = true; } } if (headers.containsKey("webauth")) { myPassword = headers.getFirst("webauth"); if (myPassword.equals(serverWebAuth)) { hasPassword = true; } } if (headers.containsKey("user")) { myHdrUser = headers.getFirst("user"); } if (headers.containsKey("message")) { myHdrMessage = headers.getFirst("message"); } if (requestMethod.equals("GET")) { if (uriPath.contains("..")) { sendHTMLError(403, "Invalid URL", exchange); } else if (uriPath.startsWith("/inistore")) { handleIniStore(uriPath, exchange, hasPassword); } else if (uriPath.startsWith("/dbquery")) { handleDBQuery(uriPath, uriQueryList, exchange, hasPassword); } else if (uriPath.startsWith("/addons") || uriPath.startsWith("/logs")) { handleFile(uriPath, exchange, hasPassword, true); } else if (uriPath.equals("/playlist")) { handleFile("/web/playlist/index.html", exchange, hasPassword, false); } else if (uriPath.equals("/")) { handleFile("/web/index.html", exchange, hasPassword, false); } else { handleFile("/web" + uriPath, exchange, hasPassword, false); } } if (requestMethod.equals("PUT")) { handlePutRequest(myHdrUser, myHdrMessage, exchange, hasPassword); } }
@Override public String getProtocol() { boolean isSecure = this.ex instanceof HttpsExchange; return ex.getProtocol().split("/")[0].toLowerCase() + (isSecure ? "s" : ""); }
public SSLSession getSSLSession() { return ((HttpsExchange) delegate).getSSLSession(); }
public X509Certificate getPeerCertficate() throws SSLPeerUnverifiedException { return ((HttpsExchange) delegate).getSSLSession().getPeerCertificateChain()[0]; }