hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joan Balaguero" <joan.balagu...@grupoventus.com>
Subject RE: Headers
Date Wed, 11 Jul 2012 08:24:52 GMT
Hello Jose,

Thanks a lot.

I suppose your app is a proxy. Then you should also manage the request
headers, and only send to server those headers that are not hop-by-hop.

In addition, I think you should remove those headers whose name is present
in the "Connection" header. This is my implementation (with request
headers):

private void setHeaders(HttpServletRequest request)
{
  String connectionHeaderValue  = null;	 
  Enumeration<?> headerNames 	= request.getHeaderNames();
  
  while (headerNames.hasMoreElements()) 
  {
   String headerName  = (String)headerNames.nextElement();
   
   // Obviamos las 'cookies', porque ya las hemos cogido antes.
   if (!headerName.equalsIgnoreCase(GenericConstants.HEADER_COOKIE))
   {
    // Si es el 'Connection' header, no lo añado pero me quedo con su valor.

    if (headerName.equalsIgnoreCase(GenericConstants.HEADER_CONNECTION))
connectionHeaderValue = request.getHeader(headerName);   
    else this.addHeader(headerName, request.getHeader(headerName));
   }
  }
  
  // Al final de recoger todos los 'header', hay que eliminar aquellos cuyo
nombre esté incluído en 'connectionHeaderValue' (tiene los valores separados
por ",").
  if (connectionHeaderValue != null)
  {
   String[] connectionHeaderNames =
connectionHeaderValue.split(GenericConstants.COMA_SEPARATOR);
   for (String headerName : connectionHeaderNames)
this.headers.remove(headerName.trim().toLowerCase());
  }
 }


It's not tested yet. I'm working on it.

Regards,
Joan.


-----Mensaje original-----
De: Jose Escobar [mailto:eb.jose@gmail.com] 
Enviado el: miércoles, 11 de julio de 2012 9:06
Para: HttpClient User Discussion
Asunto: Re: Headers

Hello Joan,

I didn't find any method to do this. This is my implementation:

public static final String[] SET_HOP_BY_HOP_VALUES = new String[] {
"connection", "keep-alive", "proxy-authenticate", "proxy-authorization",
"te", "trailers", "transfer-encoding", "upgrade", "content-length"}; public
static final Set<String> HOP_BY_HOP_HEADERS_SET = new
HashSet<String>(Arrays.asList(SET_HOP_BY_HOP_VALUES));


....


httppost.setEntity(bufferedHttpEntity);
						
			HttpResponse response =
httpClient.execute(httppost);
			HttpEntity entity = response.getEntity();
			
			try{
				Header[] headers=response.getAllHeaders();
				
				for(int i=0;i<headers.length;i++){
	
if(!HOP_BY_HOP_HEADERS_SET.contains(headers[i].getName().toLowerCase()..trim
())){
	
currentResponse.addHeader(headers[i].getName(), headers[i].getValue());
						logger.info("Añadiendo linea
de nombre {} y valor {} para la respuesta a traves de proxy",
headers[i].getName(), headers[i].getValue());
					}
				}
				
				InputStream is=entity.getContent();
				IOUtils.write(IOUtils.toByteArray(is),
currentResponse.getOutputStream());
			}catch(Exception ex){
				if(response!=null){
					try {
	
EntityUtils.consume(response.getEntity());
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
				currentResponse.setStatus(500);
				IOUtils.write("Error formateando el response
en el proxy: "+ ex.getMessage(), currentResponse.getOutputStream());
				logger.error("Error formateando el response
en el proxy: {}", ex.getMessage());
			}


Hope this help you.

PD: I had to include "content-length" header as a hop-by-hop header for some
problems, but it's not declared as a hop-by-hop header in RFC.

2012/7/10 Joan Balaguero <joan.balaguero@grupoventus.com>:
> Hello,
>
>
>
> Is there any way in HC4 to find out if  a header is a hop by hop 
> header (and , therefore, it should not be transmitted),  or must the 
> Connection header be parsed manually?
>
>
>
> Thanks,
>
> Joan.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


-----
No se encontraron virus en este mensaje.
Comprobado por AVG - www.avg.com
Versión: 2012.0.2195 / Base de datos de virus: 2437/5124 - Fecha de
publicación: 07/10/12


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message