@ SuppressWarnings ( "unchecked" ) private void setProxyRequestHeaders ( HttpServletRequest httpServletRequest , HttpMethod httpMethodProxyRequest ) throws Exception { RequestInformation requestInfo = requestInformation . get ( ) ; String hostName = HttpUtilities . getHostNameFromURL ( httpServletRequest . getRequestURL ( ) . toString ( ) ) ; // Get an Enumeration of all of the header names sent by the client Boolean stripTransferEncoding = false ; Enumeration < String > enumerationOfHeaderNames = httpServletRequest . getHeaderNames ( ) ; while ( enumerationOfHeaderNames . hasMoreElements ( ) ) { String stringHeaderName = enumerationOfHeaderNames . nextElement ( ) ; if ( stringHeaderName . equalsIgnoreCase ( STRING_CONTENT_LENGTH_HEADER_NAME ) ) { // don't add this header continue ; } // The forwarding proxy may supply a POST encoding hint in ODO-POST-TYPE if ( stringHeaderName . equalsIgnoreCase ( "ODO-POST-TYPE" ) && httpServletRequest . getHeader ( "ODO-POST-TYPE" ) . startsWith ( "content-length:" ) ) { stripTransferEncoding = true ; } logger . info ( "Current header: {}" , stringHeaderName ) ; // As per the Java Servlet API 2.5 documentation: // Some headers, such as Accept-Language can be sent by clients // as several headers each with a different value rather than // sending the header as a comma separated list. // Thus, we get an Enumeration of the header values sent by the // client Enumeration < String > enumerationOfHeaderValues = httpServletRequest . getHeaders ( stringHeaderName ) ; while ( enumerationOfHeaderValues . hasMoreElements ( ) ) { String stringHeaderValue = enumerationOfHeaderValues . nextElement ( ) ; // In case the proxy host is running multiple virtual servers, // rewrite the Host header to ensure that we get content from // the correct virtual server if ( stringHeaderName . equalsIgnoreCase ( STRING_HOST_HEADER_NAME ) && requestInfo . handle ) { String hostValue = getHostHeaderForHost ( hostName ) ; if ( hostValue != null ) { stringHeaderValue = hostValue ; } } Header header = new Header ( stringHeaderName , stringHeaderValue ) ; // Set the same header on the proxy request httpMethodProxyRequest . addRequestHeader ( header ) ; } } // this strips transfer encoding headers and adds in the appropriate content-length header // based on the hint provided in the ODO-POST-TYPE header(sent from BrowserMobProxyHandler) if ( stripTransferEncoding ) { httpMethodProxyRequest . removeRequestHeader ( "transfer-encoding" ) ; // add content length back in based on the ODO information String contentLengthHint = httpServletRequest . getHeader ( "ODO-POST-TYPE" ) ; String [ ] contentLengthParts = contentLengthHint . split ( ":" ) ; httpMethodProxyRequest . addRequestHeader ( "content-length" , contentLengthParts [ 1 ] ) ; // remove the odo-post-type header httpMethodProxyRequest . removeRequestHeader ( "ODO-POST-TYPE" ) ; } // bail if we aren't fully handling this request if ( ! requestInfo . handle ) { return ; } // deal with header overrides for the request processRequestHeaderOverrides ( httpMethodProxyRequest ) ; }
Retrieves all of the headers from the servlet request and sets them on the proxy request