}
}
}
public static Request parseRequest(GrizzlyRequest grizzlyRequest) throws Exception {
Request request = new Http.Request();
Request.current.set(request);
URI uri = new URI(grizzlyRequest.getRequestURI());
request.method = grizzlyRequest.getMethod().intern();
request.path = uri.getPath();
request.querystring = grizzlyRequest.getQueryString() == null ? "" : grizzlyRequest.getQueryString();
Router.routeOnlyStatic(request);
if (grizzlyRequest.getHeader("Content-Type") != null) {
request.contentType = grizzlyRequest.getHeader("Content-Type").split(";")[0].trim().toLowerCase().intern();
} else {
request.contentType = "text/html".intern();
}
if (grizzlyRequest.getHeader("X-HTTP-Method-Override") != null) {
request.method = grizzlyRequest.getHeader("X-HTTP-Method-Override").intern();
}
request.body = grizzlyRequest.getInputStream();
request.secure = grizzlyRequest.isSecure();
request.url = uri.toString() + (grizzlyRequest.getQueryString() == null ? "" : "?" + grizzlyRequest.getQueryString());
request.host = grizzlyRequest.getHeader("host");
if (request.host.contains(":")) {
request.port = Integer.parseInt(request.host.split(":")[1]);
request.domain = request.host.split(":")[0];
} else {
request.port = 80;
request.domain = request.host;
}
request.remoteAddress = grizzlyRequest.getRemoteAddr();
if (Yalp.configuration.containsKey("XForwardedSupport") && grizzlyRequest.getHeader("X-Forwarded-For") != null) {
if (!Arrays.asList(Yalp.configuration.getProperty("XForwardedSupport", "127.0.0.1").split(",")).contains(request.remoteAddress)) {
throw new RuntimeException("This proxy request is not authorized");
} else {
request.secure = ("https".equals(Yalp.configuration.get("XForwardedProto")) || "https".equals(grizzlyRequest.getHeader("X-Forwarded-Proto")) || "on".equals(grizzlyRequest.getHeader("X-Forwarded-Ssl")));
if (Yalp.configuration.containsKey("XForwardedHost")) {
request.host = (String) Yalp.configuration.get("XForwardedHost");
} else if (grizzlyRequest.getHeader("X-Forwarded-Host") != null) {
request.host = grizzlyRequest.getHeader("X-Forwarded-Host");
}
if (grizzlyRequest.getHeader("X-Forwarded-For") != null) {
request.remoteAddress = grizzlyRequest.getHeader("X-Forwarded-For");
}
}
}
Enumeration headersNames = grizzlyRequest.getHeaderNames();
while (headersNames.hasMoreElements()) {
Http.Header hd = new Http.Header();
hd.name = (String) headersNames.nextElement();
hd.values = new ArrayList<String>();
Enumeration enumValues = grizzlyRequest.getHeaders(hd.name);
while (enumValues.hasMoreElements()) {
String value = (String) enumValues.nextElement();
hd.values.add(value);
}
request.headers.put(hd.name.toLowerCase(), hd);
}
request.resolveFormat();
Cookie[] cookies = grizzlyRequest.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
Http.Cookie yalpCookie = new Http.Cookie();
yalpCookie.name = cookie.getName();
yalpCookie.path = cookie.getPath();
yalpCookie.domain = cookie.getDomain();
yalpCookie.secure = cookie.getSecure();
yalpCookie.value = cookie.getValue();
yalpCookie.maxAge = cookie.getMaxAge();
request.cookies.put(yalpCookie.name, yalpCookie);
}
}
request._init();
return request;
}