X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fj2ee%2FJ2eeRequest.java;h=8eb8b90acf4d96d45cf2d6b0bd2f50e5ca865001;hb=80e2f8488b75b0eddf8df3d22a05f5a0481c2c0e;hp=a5e7518876cab5509554b0a3492205601d9adca2;hpb=78f5d1201f8d3aecb660b7877b08d9bfbe650911;p=jsvc.git diff --git a/src/dolda/jsvc/j2ee/J2eeRequest.java b/src/dolda/jsvc/j2ee/J2eeRequest.java index a5e7518..8eb8b90 100644 --- a/src/dolda/jsvc/j2ee/J2eeRequest.java +++ b/src/dolda/jsvc/j2ee/J2eeRequest.java @@ -13,23 +13,42 @@ public class J2eeRequest extends ResponseBuffer { private HttpServletRequest req; private HttpServletResponse resp; private String method, path; - private URL url; - private Map props = new HashMap(); + private URL url, context; + private MultiMap params = null; public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { this.cfg = cfg; this.req = req; this.resp = resp; - try { - req.setCharacterEncoding("UTF-8"); - resp.setCharacterEncoding("UTF-8"); - } catch(UnsupportedEncodingException e) { - throw(new AssertionError(e)); - } { + /* Ewwww, this is disgusting! */ + String scheme = req.getScheme(); + int port = -1; String host = req.getHeader("Host"); - if((host == null) || (host.length() < 1)) + if((host == null) || (host.length() < 1)) { host = req.getLocalAddr(); + port = req.getLocalPort(); + if((port == 80) && scheme.equals("http")) + port = -1; + else if((port == 443) && scheme.equals("https")) + port = -1; + } else { + int p; + if((host.charAt(0) == '[') && ((p = host.indexOf(']', 1)) > 1)) { + String newhost = host.substring(1, p); + if((p = host.indexOf(':', p + 1)) >= 0) { + try { + port = Integer.parseInt(host.substring(p + 1)); + } catch(NumberFormatException e) {} + } + host = newhost; + } else if((p = host.indexOf(':')) >= 0) { + try { + port = Integer.parseInt(host.substring(p + 1)); + host = host.substring(0, p); + } catch(NumberFormatException e) {} + } + } String pi = req.getPathInfo(); if(pi == null) pi = ""; @@ -39,7 +58,8 @@ public class J2eeRequest extends ResponseBuffer { else q = ""; try { - url = new URL(req.getScheme(), host, req.getServerPort(), req.getContextPath() + req.getServletPath() + pi + q); + url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q); + context = new URL(scheme, host, port, req.getContextPath()); } catch(MalformedURLException e) { throw(new Error(e)); } @@ -50,14 +70,42 @@ public class J2eeRequest extends ResponseBuffer { path = path.substring(1); } - public Map props() { - return(props); + public SocketAddress remoteaddr() { + try { + /* Apparently getRemotePort returns -1 when running on Tomcat over AJP. */ + int port = req.getRemotePort(); + if(port < 0) + port = 0; + return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), port)); + } catch(UnknownHostException e) { + /* req.getRemoteAddr should always be a valid IP address, + * so this should never happen. */ + throw(new Error(e)); + } + } + + public SocketAddress localaddr() { + try { + return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort())); + } catch(UnknownHostException e) { + /* req.getRemoteAddr should always be a valid IP address, + * so this should never happen. */ + throw(new Error(e)); + } } public URL url() { return(url); } + public URL rooturl() { + return(context); + } + + public ServerContext ctx() { + return(ThreadContext.current().server()); + } + public String method() { return(method); } @@ -96,10 +144,13 @@ public class J2eeRequest extends ResponseBuffer { } public MultiMap params() { - return(null); + if(params == null) + params = Params.stdparams(this); + return(params); } protected void backflush() { + resp.setStatus(respcode); for(String key : outheaders().keySet()) { boolean first = true; for(String val : outheaders().values(key)) {