Friday, March 22, 2013

Tomcat Tips

Getting your server name can be a tricky question because tomcat may be on box1 behind box2. What you probably care about is how the client addressed you, i.e. box1. That information should be included in the HOST header of the request. Here's the output of various HttpServletRequest functions when the browser asked for www.external.com who was apache and who proxied the request to www.internal.com (which is unresolvable to the browser).

value in browser url box: https://www.external.com/hello/world
actual resource served:   https://www.internal.com/hello/world

HttpServletRequest.getRequestURI()   = /hello/world
HttpServletRequest.getRequestURL()   = https://www.external.com/hello/world
HttpServletRequest.getServerName()   = www.external.com
HttpServletRequest.getHeader("HOST") = www.external.com
HttpServletRequest.getLocalName()    = www.external.com

From HttpUtils.getRequestURL (which I believe is what is actually called by HttpServletRequest.getRequestURL) you can see that host part of getRequestURL is just the result of getServerName. The implementation of getServerName is totally opaque to me but one would hope it was a getHeader("HOST") followed by some system call when absent. According to spec it is the value of the part before ":" in the Host header value, if any, or the resolved server name, or the server IP address.

{ "loggedin": false, "owner": false, "avatar": "", "render": "nothing", "trackingID": "UA-36983794-1", "description": "", "page": { "blogIds": [ 415 ] }, "domain": "holtstrom.com", "base": "\/michael", "url": "https:\/\/holtstrom.com\/michael\/", "frameworkFiles": "https:\/\/holtstrom.com\/michael\/_framework\/_files.4\/", "commonFiles": "https:\/\/holtstrom.com\/michael\/_common\/_files.3\/", "mediaFiles": "https:\/\/holtstrom.com\/michael\/media\/_files.3\/", "tmdbUrl": "http:\/\/www.themoviedb.org\/", "tmdbPoster": "http:\/\/image.tmdb.org\/t\/p\/w342" }