Skip to content

Commit

Permalink
Copy the original CORS support from the DriverServlet to the WebDrive…
Browse files Browse the repository at this point in the history
…rServlet
  • Loading branch information
shs96c committed May 21, 2017
1 parent d8b259b commit 2ab61e1
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 50 deletions.
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/remote/server/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ java_library(name = 'server',
'DriverProvider.java',
'DriverServlet.java',
'FirefoxDriverProvider.java',
'InputStreamWrappingServletInputStream.java',
'JsonHttpCommandHandler.java',
'JsonParametersAware.java',
'SessionCleaner.java',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.common.base.Strings.nullToEmpty;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
Expand All @@ -45,7 +44,6 @@
import java.util.logging.Handler;
import java.util.logging.Logger;

import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
Expand Down Expand Up @@ -303,51 +301,4 @@ private byte[] getResourceData(URL url) throws IOException {
}
}

private static class InputStreamWrappingServletInputStream extends ServletInputStream {

private final InputStream delegate;
private int lastRead;

public InputStreamWrappingServletInputStream(InputStream delegate) {
this.delegate = Preconditions.checkNotNull(delegate);

}

@Override
public int available() throws IOException {
return delegate.available();
}

@Override
public void close() throws IOException {
delegate.close();
lastRead = -1;
}

@Override
public boolean isFinished() {
return lastRead != -1;
}

@Override
public boolean isReady() {
return !isFinished();
}

@Override
public void setReadListener(ReadListener readListener) {
throw new UnsupportedOperationException("setReadListener");
}

@Override
public int read() throws IOException {
lastRead = delegate.read();
return lastRead;
}

@Override
public boolean markSupported() {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote.server;

import com.google.common.base.Preconditions;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;


class InputStreamWrappingServletInputStream extends ServletInputStream {

private final InputStream delegate;
private int lastRead;

public InputStreamWrappingServletInputStream(InputStream delegate) {
this.delegate = Preconditions.checkNotNull(delegate);

}

@Override
public int available() throws IOException {
return delegate.available();
}

@Override
public void close() throws IOException {
delegate.close();
lastRead = -1;
}

@Override
public boolean isFinished() {
return lastRead != -1;
}

@Override
public boolean isReady() {
return !isFinished();
}

@Override
public void setReadListener(ReadListener readListener) {
throw new UnsupportedOperationException("setReadListener");
}

@Override
public int read() throws IOException {
lastRead = delegate.read();
return lastRead;
}

@Override
public boolean markSupported() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;

import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.server.xdrpc.CrossDomainRpc;
import org.openqa.selenium.remote.server.xdrpc.CrossDomainRpcLoader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand All @@ -35,15 +40,19 @@
import java.util.concurrent.TimeoutException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class WebDriverServlet extends HttpServlet {

public static final String SESSIONS_KEY = DriverServlet.class.getName() + ".sessions";
public static final String ACTIVE_SESSIONS_KEY = WebDriverServlet.class.getName() + ".sessions";

private static final String CROSS_DOMAIN_RPC_PATH = "/xdrpc";

private final ExecutorService executor = Executors.newCachedThreadPool();
private Cache<SessionId, ActiveSession> allSessions;
private DriverSessions legacyDriverSessions;
Expand Down Expand Up @@ -98,7 +107,47 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
handle(req, resp);
if (CROSS_DOMAIN_RPC_PATH.equalsIgnoreCase(req.getPathInfo())) {
handleCrossDomainRpc(req, resp);
} else {
handle(req, resp);
}
}

private void handleCrossDomainRpc(
HttpServletRequest servletRequest, HttpServletResponse servletResponse)
throws ServletException, IOException {
CrossDomainRpc rpc;

try {
rpc = new CrossDomainRpcLoader().loadRpc(servletRequest);
} catch (IllegalArgumentException e) {
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
servletResponse.getOutputStream().println(e.getMessage());
servletResponse.getOutputStream().flush();
return;
}

servletRequest.setAttribute(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(servletRequest) {
@Override
public String getMethod() {
return rpc.getMethod();
}

@Override
public String getPathInfo() {
return rpc.getPath();
}

@Override
public ServletInputStream getInputStream() throws IOException {
return new InputStreamWrappingServletInputStream(
new ByteArrayInputStream(rpc.getContent()));
}
};

handle(wrapper, servletResponse);
}

private void handle(HttpServletRequest req, HttpServletResponse resp) {
Expand Down

0 comments on commit 2ab61e1

Please sign in to comment.