Created
October 2, 2013 16:37
-
-
Save dannyduc/6796580 to your computer and use it in GitHub Desktop.
Snippet from Google JS Test Driver. Forwards requests to another server and feeds the responses back to the client. git clone https://code.google.com/p/js-test-driver/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void handleIt() throws IOException { | |
final HttpMethodBase method = getMethod(request); | |
addRequestHeaders(method, request); | |
method.setQueryString(request.getQueryString()); | |
spoofHostHeader(method); | |
try { | |
final int statusCode = client.executeMethod(method); | |
response.setStatus(statusCode); | |
addResponseHeaders(method, response); | |
if (isRedirect(statusCode)) { | |
spoofLocationHeader(request, (Response) response); | |
} | |
if (isStatusCodeSuppressed(request)) { | |
response.setStatus(HttpServletResponse.SC_OK); | |
response.addIntHeader(X_SUPPRESSED_STATUS_CODE, statusCode); | |
response.addHeader(X_SUPPRESSED_REASON_PHRASE, method.getStatusText()); | |
} | |
// TODO(rdionne): Substitute the JsTD server address for the destination address in any redirects. | |
Streams.copy(method.getResponseBodyAsStream(), response.getOutputStream()); | |
} catch (IOException e) { | |
response.setStatus(HttpServletResponse.SC_BAD_GATEWAY); | |
e.printStackTrace(response.getWriter()); | |
} finally { | |
method.releaseConnection(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment