Skip to content

Commit

Permalink
Use User-Agent "LemMinX" when downloading schemas
Browse files Browse the repository at this point in the history
Closes redhat-developer/vscode-xml#429

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Feb 24, 2021
1 parent a54317a commit 36ba49b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ private CompletableFuture<Path> downloadResource(final String resourceURI, Path
String actualURI = resourceURI;
URL url = new URL(actualURI);
conn = url.openConnection();
conn.setRequestProperty("User-Agent", "LemMinX");
/* XXX: This should really be implemented using HttpClient or similar */
int allowedRedirects = 5;
while (conn.getHeaderField("Location") != null && allowedRedirects > 0) //$NON-NLS-1$
{
allowedRedirects--;
url = new URL(actualURI = conn.getHeaderField("Location")); //$NON-NLS-1$
conn = url.openConnection();
conn.setRequestProperty("User-Agent", "LemMinX");
}

// Download resource in a temporary file
Expand Down Expand Up @@ -228,9 +230,9 @@ public static Path getResourceCachePath(URI uri) throws IOException {
* Try to get the cached {@link ResourceToDeploy#resourceCachePath} in cache
* file system and if it is not found, create the file with the given content of
* {@link ResourceToDeploy#resourceFromClasspath} stored in classpath.
*
*
* @param resource the resource to deploy if needed.
*
*
* @return the cached {@link ResourceToDeploy#resourceCachePath} in cache file
* system.
* @throws IOException
Expand All @@ -249,7 +251,7 @@ public static Path getResourceCachePath(ResourceToDeploy resource) throws IOExce
/**
* Returns <code>true</code> if cache is enabled and url comes from "http(s)" or
* "ftp" and <code>false</code> otherwise.
*
*
* @param url
* @return <code>true</code> if cache is enabled and url comes from "http(s)" or
* "ftp" and <code>false</code> otherwise.
Expand All @@ -260,7 +262,7 @@ public boolean canUseCache(String url) {

/**
* Set <code>true</code> if cache must be used, <code>false</code> otherwise.
*
*
* @param useCache <code>true</code> if cache must be used, <code>false</code>
* otherwise.
*/
Expand All @@ -271,7 +273,7 @@ public void setUseCache(boolean useCache) {
/**
* Returns <code>true</code> if cache must be used, <code>false</code>
* otherwise.
*
*
* @return <code>true</code> if cache must be used, <code>false</code>
* otherwise.
*/
Expand All @@ -281,7 +283,7 @@ public boolean isUseCache() {

/**
* Remove the cache directory (.lemminx/cache) if it exists.
*
*
* @throws IOException if the delete of directory (.lemminx/cache) cannot be
* done.
*/
Expand All @@ -296,7 +298,7 @@ public void evictCache() throws IOException {

/**
* Add protocol for using cache when url will start with the given protocol.
*
*
* @param protocol the protocol to add.
*/
public void addProtocolForCahe(String protocol) {
Expand All @@ -306,7 +308,7 @@ public void addProtocolForCahe(String protocol) {
/**
* Remove protocol to avoid using cache when url will start with the given
* protocol.
*
*
* @param protocol the protocol to remove.
*/
public void removeProtocolForCahe(String protocol) {
Expand All @@ -315,9 +317,9 @@ public void removeProtocolForCahe(String protocol) {

/**
* Add ':' separator if the given protocol doesn't contain it.
*
*
* @param protocol the protocol to format.
*
*
* @return the protocol concat with ':'.
*/
private static String formatProtocol(String protocol) {
Expand All @@ -329,9 +331,9 @@ private static String formatProtocol(String protocol) {

/**
* Returns true if the cache must be used for the given url and false otherwise.
*
*
* @param url the url.
*
*
* @return true if the cache must be used for the given url and false otherwise.
*/
private boolean isUseCacheFor(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class FileServer {
/**
* Creates an http server on a random port, serving the
* <code>src/test/resources</code> directory.
*
*
* @throws IOException
*/
public FileServer() throws IOException {
Expand All @@ -46,14 +46,14 @@ public FileServer() throws IOException {
/**
* Creates an http server on a random port, serving the <code>dir</code>
* directory (relative to the current project).
*
*
* @param baseDir the base directory.
* @throws IOException
*/
public FileServer(Path baseDir) throws IOException {
Files.createDirectories(baseDir);
server = new Server(0);
ResourceHandler resourceHandler = new ResourceHandler();
ResourceHandler resourceHandler = new ModifiedResourceHandler();
resourceHandler.setResourceBase(baseDir.toUri().toString());
resourceHandler.setDirectoriesListed(true);
HandlerList handlers = new HandlerList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.uriresolver;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;

public class ModifiedResourceHandler extends ResourceHandler {

public ModifiedResourceHandler() {
super();
}

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

// 403 if user agent starts with Java/1.
if (request.getHeader("User-Agent").indexOf("Java/1.") == 0) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Java 8 is not allowed");
return;
}

super.handle(target, baseRequest, request, response);
}

}

0 comments on commit 36ba49b

Please sign in to comment.