if (gadgetUrl != null) {
gadgetUri = Uri.parse(gadgetUrl);
}
}
HttpRequest req = new HttpRequest(url).setMethod(getParameter(request, METHOD_PARAM, "GET"))
.setContainer(container).setGadget(gadgetUri);
if ("POST".equals(req.getMethod()) || "PUT".equals(req.getMethod())) {
setPostData(container, request, req);
}
String headerData = getParameter(request, HEADERS_PARAM, "");
if (headerData.length() > 0) {
String[] headerList = StringUtils.split(headerData, '&');
for (String header : headerList) {
String[] parts = StringUtils.splitPreserveAllTokens(header, '=');
if (parts.length != 2) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
"Malformed header param specified:" + header, HttpResponse.SC_BAD_REQUEST);
}
String headerName = Utf8UrlCoder.decode(parts[0]);
if (!HttpRequestHandler.BAD_HEADERS.contains(headerName.toUpperCase())) {
req.addHeader(headerName, Utf8UrlCoder.decode(parts[1]));
}
}
}
// Set the default content type for post requests when a content type is not specified
if ("POST".equals(req.getMethod()) && req.getHeader("Content-Type") == null) {
req.addHeader("Content-Type", "application/x-www-form-urlencoded");
} else if ("1".equals(getParameter(request, MULTI_PART_FORM_POST, null))) {
// We need the entire header from the original request because it comes with a boundary value
// we need to reuse.
req.addHeader("Content-Type", request.getHeader("Content-Type"));
}
req.setIgnoreCache("1".equals(getParameter(request, Param.NO_CACHE.getKey(), null)));
// If the proxy request specifies a refresh param then we want to force the min TTL for
// the retrieved entry in the cache regardless of the headers on the content when it
// is fetched from the original source.
String refresh = getParameter(request, Param.REFRESH.getKey(), null);
if (refresh != null) {
try {
req.setCacheTtl(Integer.parseInt(refresh));
} catch (NumberFormatException ignore) {}
}
// Allow the rewriter to use an externally forced mime type. This is needed
// allows proper rewriting of <script src="x"/> where x is returned with
// a content type like text/html which unfortunately happens all too often
req.setRewriteMimeType(getParameter(request, Param.REWRITE_MIME_TYPE.getKey(), null));
// Figure out whether authentication is required
AuthType auth = AuthType.parse(getParameter(request, AUTHZ_PARAM, null));
req.setAuthType(auth);
if (auth != AuthType.NONE) {
if (auth == AuthType.OAUTH2) {
req.setSecurityToken(extractAndValidateToken(request));
req.setOAuth2Arguments(new OAuth2Arguments(request));
} else {
req.setSecurityToken(extractAndValidateToken(request));
req.setOAuthArguments(new OAuthArguments(auth, request));
}
}
ServletUtil.setXForwardedForHeader(request, req);
return req;