Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-35315] Give browser relative URL for redirect to annotator.js
The code previously used the sendRedirect method, which resolves the relative
URL to an absolute URL within the servlet container and so gives the wrong
result when running behind a proxy.

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String)
  • Loading branch information
StevenGBrown committed Jun 12, 2016
1 parent 699796a commit 6ed19b1
Showing 1 changed file with 14 additions and 1 deletion.
Expand Up @@ -34,15 +34,19 @@
import java.io.IOException;

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

import jenkins.YesNoMaybe;
import jenkins.model.Jenkins;

import org.kohsuke.stapler.ResponseImpl;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;

import com.google.common.net.HttpHeaders;

/**
* Provides the initial {@link TimestampAnnotator} for an annotated console
* output.
Expand Down Expand Up @@ -125,12 +129,21 @@ public boolean hasScript() {
@WebMethod(name = "script.js")
public void doScriptJs(StaplerRequest req, StaplerResponse rsp)
throws IOException, ServletException {

// This URL is cached for one day. Redirect to a URL which includes the
// plug-in version and is cached for 1 year. The script will be downloaded
// again when the plug-in version changes.
String url = req.getContextPath() + getResourcePath()
+ "/plugin/timestamper/annotator.js";
rsp.sendRedirect2(url);

// Send the redirect manually and allow the relative URL to be handled by
// the browser. Do not use the sendRedirect method, which resolves the
// relative URL to an absolute URL within the servlet container and so
// gives the wrong result when running behind a proxy. Redirects to
// relative URLs are allowed by RFC 7231 and the most popular web browsers.
// https://en.wikipedia.org/wiki/HTTP_location
rsp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
rsp.setHeader(HttpHeaders.LOCATION, ResponseImpl.encode(url));
}

private String getResourcePath() {
Expand Down

0 comments on commit 6ed19b1

Please sign in to comment.