Java Code Examples for io.netty.handler.codec.http.HttpHeaders#contains()

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#contains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 6 votes vote down vote up
/**
 * If and only if our proxy is not running in transparent mode, modify the
 * response headers to reflect that it was proxied.
 * 
 * @param httpResponse
 * @return
 */
private void modifyResponseHeadersToReflectProxying(
        HttpResponse httpResponse) {
    if (!proxyServer.isTransparent()) {
        HttpHeaders headers = httpResponse.headers();

        stripConnectionTokens(headers);
        stripHopByHopHeaders(headers);
        ProxyUtils.addVia(httpResponse, proxyServer.getProxyAlias());

        /*
         * RFC2616 Section 14.18
         * 
         * A received message that does not have a Date header field MUST be
         * assigned one by the recipient if the message will be cached by
         * that recipient or gatewayed via a protocol which requires a Date.
         */
        if (!headers.contains(HttpHeaders.Names.DATE)) {
            HttpHeaders.setDate(httpResponse, new Date());
        }
    }
}
 
Example 2
Source File: NettyIpForwardHandler.java    From Launcher with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) {
    if (msg instanceof ReferenceCounted) {
        ((ReferenceCounted) msg).retain();
    }
    if (context.ip != null) {
        out.add(msg);
        return;
    }
    HttpHeaders headers = msg.headers();
    String realIP = null;
    if (headers.contains("X-Forwarded-For")) {
        realIP = headers.get("X-Forwarded-For");
    }
    if (headers.contains("X-Real-IP")) {
        realIP = headers.get("X-Real-IP");
    }
    if (realIP != null) {
        if (LogHelper.isDevEnabled()) {
            LogHelper.dev("Real IP address %s", realIP);
        }
        context.ip = realIP;
    } else LogHelper.error("IpForwarding error. Headers not found");
    out.add(msg);
}
 
Example 3
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies blob properties from output, to that sent in during input
 * @param expectedHeaders the expected headers in the response.
 * @param isPrivate {@code true} if the blob is expected to be private
 * @param response the {@link HttpResponse} that contains the headers.
 */
private void verifyBlobProperties(HttpHeaders expectedHeaders, boolean isPrivate, HttpResponse response) {
  assertEquals("Blob size does not match", Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE)),
      Long.parseLong(response.headers().get(RestUtils.Headers.BLOB_SIZE)));
  assertEquals(RestUtils.Headers.SERVICE_ID + " does not match", expectedHeaders.get(RestUtils.Headers.SERVICE_ID),
      response.headers().get(RestUtils.Headers.SERVICE_ID));
  assertEquals(RestUtils.Headers.PRIVATE + " does not match", isPrivate,
      Boolean.valueOf(response.headers().get(RestUtils.Headers.PRIVATE)));
  assertEquals(RestUtils.Headers.AMBRY_CONTENT_TYPE + " does not match",
      expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
      response.headers().get(RestUtils.Headers.AMBRY_CONTENT_TYPE));
  assertTrue("No " + RestUtils.Headers.CREATION_TIME,
      response.headers().get(RestUtils.Headers.CREATION_TIME, null) != null);
  if (expectedHeaders.get(RestUtils.Headers.TTL) != null
      && Long.parseLong(expectedHeaders.get(RestUtils.Headers.TTL)) != Utils.Infinite_Time) {
    assertEquals(RestUtils.Headers.TTL + " does not match", expectedHeaders.get(RestUtils.Headers.TTL),
        response.headers().get(RestUtils.Headers.TTL));
  } else {
    assertFalse("There should be no TTL in the response", response.headers().contains(RestUtils.Headers.TTL));
  }
  if (expectedHeaders.contains(RestUtils.Headers.OWNER_ID)) {
    assertEquals(RestUtils.Headers.OWNER_ID + " does not match", expectedHeaders.get(RestUtils.Headers.OWNER_ID),
        response.headers().get(RestUtils.Headers.OWNER_ID));
  }
}
 
Example 4
Source File: HttpClientPipelineConfigurator.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (needsToFilterUpgradeResponse && msg instanceof HttpResponse) {
        needsToFilterUpgradeResponse = false;
        final HttpResponse res = (HttpResponse) msg;
        if (res.status().code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) {
            final HttpHeaders headers = res.headers();
            if (!headers.contains(HttpHeaderNames.UPGRADE)) {
                headers.set(HttpHeaderNames.UPGRADE,
                            Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME);
            }
        }

        if (!needsToFilterUpgradeRequest) {
            ctx.pipeline().remove(this);
        }
    }

    ctx.fireChannelRead(msg);
}
 
Example 5
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Switch the de-facto standard "Proxy-Connection" header to "Connection"
 * when we pass it along to the remote host. This is largely undocumented
 * but seems to be what most browsers and servers expect.
 * 
 * @param headers
 *            The headers to modify
 */
private void switchProxyConnectionHeader(HttpHeaders headers) {
    String proxyConnectionKey = "Proxy-Connection";
    if (headers.contains(proxyConnectionKey)) {
        String header = headers.get(proxyConnectionKey);
        headers.remove(proxyConnectionKey);
        headers.set(HttpHeaders.Names.CONNECTION, header);
    }
}
 
Example 6
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/**
 * RFC2616 Section 14.10
 * 
 * HTTP/1.1 proxies MUST parse the Connection header field before a message
 * is forwarded and, for each connection-token in this field, remove any
 * header field(s) from the message with the same name as the
 * connection-token.
 * 
 * @param headers
 *            The headers to modify
 */
private void stripConnectionTokens(HttpHeaders headers) {
    if (headers.contains(HttpHeaders.Names.CONNECTION)) {
        for (String headerValue : headers.getAll(HttpHeaders.Names.CONNECTION)) {
            for (String connectionToken : ProxyUtils.splitCommaSeparatedHeaderValues(headerValue)) {
                // do not strip out the Transfer-Encoding header if it is specified in the Connection header, since LittleProxy does not
                // normally modify the Transfer-Encoding of the message.
                if (!LOWERCASE_TRANSFER_ENCODING_HEADER.equals(connectionToken.toLowerCase(Locale.US))) {
                    headers.remove(connectionToken);
                }
            }
        }
    }
}
 
Example 7
Source File: HttpDownUtil.java    From proxyee-down with Apache License 2.0 5 votes vote down vote up
/**
 * 检测是否支持断点下载
 */
public static TaskInfo getTaskInfo(HttpRequest httpRequest, HttpHeaders resHeaders,
    ProxyConfig proxyConfig,
    SslContext clientSslCtx, NioEventLoopGroup loopGroup)
    throws Exception {
  HttpResponse httpResponse = null;
  if (resHeaders == null) {
    httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
    //处理重定向
    if ((httpResponse.status().code() + "").indexOf("30") == 0) {
      String redirectUrl = httpResponse.headers().get(HttpHeaderNames.LOCATION);
      HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
      requestInfo.headers().remove("Host");
      requestInfo.setUri(redirectUrl);
      RequestProto requestProto = ProtoUtil.getRequestProto(requestInfo);
      requestInfo.headers().set("Host", requestProto.getHost());
      requestInfo.setRequestProto(requestProto);
      return getTaskInfo(httpRequest, null, proxyConfig, clientSslCtx, loopGroup);
    }
    resHeaders = httpResponse.headers();
  }
  TaskInfo taskInfo = new TaskInfo()
      .setId(UUID.randomUUID().toString())
      .setFileName(getDownFileName(httpRequest, resHeaders))
      .setTotalSize(getDownFileSize(resHeaders));
  //chunked编码不支持断点下载
  if (resHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) {
    if (httpResponse == null) {
      httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
    }
    //206表示支持断点下载
    if (httpResponse.status().equals(HttpResponseStatus.PARTIAL_CONTENT)) {
      taskInfo.setSupportRange(true);
    }
  }
  return taskInfo;
}
 
Example 8
Source File: ResponseSender.java    From riposte with Apache License 2.0 5 votes vote down vote up
protected void removeTransferEncodingChunked(HttpHeaders headers) {
    if (headers.contains(TRANSFER_ENCODING, CHUNKED, true)) {
        List<String> transferEncodingsMinusChunked =
            headers.getAll(TRANSFER_ENCODING).stream()
                   .filter(encoding -> !CHUNKED.equalsIgnoreCase(encoding))
                   .collect(Collectors.toList());

        if (transferEncodingsMinusChunked.isEmpty()) {
            headers.remove(TRANSFER_ENCODING);
        }
        else {
            headers.set(TRANSFER_ENCODING, transferEncodingsMinusChunked);
        }
    }
}
 
Example 9
Source File: HttpMessageClientHeaderAdaptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void setHeader(HttpMessage httpMessage, String name, String value) {
    final HttpHeaders headers = httpMessage.headers();
    if (headers != null && !headers.contains(name)) {
        headers.set(name, value);
        if (isDebug) {
            logger.debug("Set header {}={}", name, value);
        }
    }
}
 
Example 10
Source File: HttpClientConnect.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
Publisher<Void> requestWithBody(HttpClientOperations ch) {
	try {
		ch.resourceUrl = toURI.toExternalForm();

		UriEndpoint uri = toURI;
		HttpHeaders headers = ch.getNettyRequest()
		                        .setUri(uri.getPathAndQuery())
		                        .setMethod(method)
		                        .setProtocolVersion(HttpVersion.HTTP_1_1)
		                        .headers();

		ch.path = HttpOperations.resolvePath(ch.uri());

		if (defaultHeaders != null) {
			headers.set(defaultHeaders);
		}

		if (!headers.contains(HttpHeaderNames.USER_AGENT)) {
			headers.set(HttpHeaderNames.USER_AGENT, USER_AGENT);
		}

		SocketAddress remoteAddress = uri.getRemoteAddress();
		if (!headers.contains(HttpHeaderNames.HOST)) {
			headers.set(HttpHeaderNames.HOST, resolveHostHeaderValue(remoteAddress));
		}

		if (!headers.contains(HttpHeaderNames.ACCEPT)) {
			headers.set(HttpHeaderNames.ACCEPT, ALL);
		}

		ch.followRedirectPredicate(followRedirectPredicate);

		if (!Objects.equals(method, HttpMethod.GET) &&
					!Objects.equals(method, HttpMethod.HEAD) &&
					!Objects.equals(method, HttpMethod.DELETE) &&
					!headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
			ch.chunkedTransfer(true);
		}

		ch.listener().onStateChange(ch, HttpClientState.REQUEST_PREPARED);
		if (websocketClientSpec != null) {
			Mono<Void> result =
					Mono.fromRunnable(() -> ch.withWebsocketSupport(websocketClientSpec, compress));
			if (handler != null) {
				result = result.thenEmpty(Mono.fromRunnable(() -> Flux.concat(handler.apply(ch, ch))));
			}
			return result;
		}

		Consumer<HttpClientRequest> consumer = null;
		if (fromURI != null && !toURI.equals(fromURI)) {
			if (handler instanceof RedirectSendHandler) {
				headers.remove(HttpHeaderNames.EXPECT)
				       .remove(HttpHeaderNames.COOKIE)
				       .remove(HttpHeaderNames.AUTHORIZATION)
				       .remove(HttpHeaderNames.PROXY_AUTHORIZATION);
			}
			else {
				consumer = request ->
				        request.requestHeaders()
				               .remove(HttpHeaderNames.EXPECT)
				               .remove(HttpHeaderNames.COOKIE)
				               .remove(HttpHeaderNames.AUTHORIZATION)
				               .remove(HttpHeaderNames.PROXY_AUTHORIZATION);
			}
		}
		if (this.redirectRequestConsumer != null) {
			consumer = consumer != null ? consumer.andThen(this.redirectRequestConsumer) : this.redirectRequestConsumer;
		}
		ch.redirectRequestConsumer(consumer);
		return handler != null ? handler.apply(ch, ch) : ch.send();
	}
	catch (Throwable t) {
		return Mono.error(t);
	}
}
 
Example 11
Source File: Netty4CorsHandler.java    From crate with Apache License 2.0 4 votes vote down vote up
private static boolean isPreflightRequest(final HttpRequest request) {
    final HttpHeaders headers = request.headers();
    return request.method().equals(HttpMethod.OPTIONS) &&
        headers.contains(HttpHeaderNames.ORIGIN) &&
        headers.contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
}
 
Example 12
Source File: PublicAccessLogHandlerTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Sends the provided {@code httpRequest} and verifies that the response is as expected.
 * @param channel the {@link EmbeddedChannel} to send the request over.
 * @param httpRequest the {@link HttpRequest} that has to be sent
 * @param uri, Uri to be used for the request
 * @param headers {@link HttpHeaders} that is set in the request to be used for verification purposes
 * @param testErrorCase true if error case has to be tested, false otherwise
 * @param sslUsed true if SSL was used for this request.
 */
private void sendRequestCheckResponse(EmbeddedChannel channel, HttpRequest httpRequest, String uri,
    HttpHeaders headers, boolean testErrorCase, boolean chunkedResponse, boolean sslUsed) throws Exception {
  channel.writeInbound(httpRequest);
  if (uri.equals(EchoMethodHandler.DISCONNECT_URI)) {
    channel.disconnect();
  } else {
    channel.writeInbound(new DefaultLastHttpContent());
  }
  String lastLogEntry = publicAccessLogger.getLastPublicAccessLogEntry();
  // verify remote host, http method and uri
  String subString = testErrorCase ? "Error" : "Info" + ":embedded" + " " + httpRequest.method() + " " + uri;
  Assert.assertTrue("Public Access log entry doesn't have expected remote host/method/uri ",
      lastLogEntry.startsWith(subString));
  // verify SSL-related info
  subString = "SSL ([used=" + sslUsed + "]";
  if (sslUsed) {
    subString += ", [principal=" + PEER_CERT.getSubjectX500Principal() + "]";
    subString += ", [san=" + PEER_CERT.getSubjectAlternativeNames() + "]";
  }
  subString += ")";
  Assert.assertTrue("Public Access log entry doesn't have SSL info set correctly", lastLogEntry.contains(subString));
  // verify request headers
  verifyPublicAccessLogEntryForRequestHeaders(lastLogEntry, headers, httpRequest.method(), true);

  // verify response
  subString = "Response (";
  for (String responseHeader : RESPONSE_HEADERS.split(",")) {
    if (headers.contains(responseHeader)) {
      subString += "[" + responseHeader + "=" + headers.get(responseHeader) + "] ";
    }
  }
  subString += "[isChunked=" + chunkedResponse + "]), status=" + HttpResponseStatus.OK.code();

  if (!testErrorCase) {
    Assert.assertTrue("Public Access log entry doesn't have response set correctly",
        lastLogEntry.contains(subString));
  } else {
    Assert.assertTrue("Public Access log entry doesn't have error set correctly ",
        lastLogEntry.contains(": Channel closed while request in progress."));
  }
}
 
Example 13
Source File: EtcdResponseHandler.java    From etcd4j with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse response) throws Exception {
  final HttpResponseStatus status =response.status();
  final HttpHeaders headers = response.headers();
  final ByteBuf content = response.content();

  if (logger.isDebugEnabled()) {
    logger.debug("Received {} for {} {}",
      status.code(), this.request.getMethod().name(), this.request.getUri());
  }

  if (status.equals(HttpResponseStatus.MOVED_PERMANENTLY)
    || status.equals(HttpResponseStatus.TEMPORARY_REDIRECT)) {
    if (headers.contains(HttpHeaderNames.LOCATION)) {
      this.request.setUrl(headers.get(HttpHeaderNames.LOCATION));
      this.client.connect(this.request);
      // Closing the connection which handled the previous request.
      ctx.close();
      if (logger.isDebugEnabled()) {
        logger.debug("redirect for {} to {}",
          this.request.getHttpRequest().uri() ,
          headers.get(HttpHeaderNames.LOCATION)
        );
      }
    } else {
      this.promise.setFailure(new Exception("Missing Location header on redirect"));
    }
  } else {
    EtcdResponseDecoder<? extends Throwable> failureDecoder = failureDecoders.get(status);
    if(failureDecoder != null) {
      this.promise.setFailure(failureDecoder.decode(headers, content));
    } else if (!content.isReadable()) {
      // If connection was accepted maybe response has to be waited for
      if (!status.equals(HttpResponseStatus.OK)
        && !status.equals(HttpResponseStatus.ACCEPTED)
        && !status.equals(HttpResponseStatus.CREATED)) {
        this.promise.setFailure(new IOException(
          "Content was not readable. HTTP Status: " + status));
      }
    } else {
      try {
        this.promise.setSuccess(
          request.getResponseDecoder().decode(headers, content));
      } catch (Exception e) {
        if (e instanceof EtcdException) {
          this.promise.setFailure(e);
        } else {
          try {
            // Try to be smart, if an exception is thrown, first try to decode
            // the content and see if it is an EtcdException, i.e. an error code
            // not included in failureDecoders
            this.promise.setFailure(EtcdException.DECODER.decode(headers, content));
          } catch (Exception e1) {
            // if it fails again, set the original exception as failure
            this.promise.setFailure(e);
          }
        }
      }
    }
  }
}
 
Example 14
Source File: CorsHandler.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private static boolean isPreflightRequest(final HttpRequest request) {
    final HttpHeaders headers = request.headers();
    return request.getMethod().equals(OPTIONS) &&
            headers.contains(ORIGIN) &&
            headers.contains(ACCESS_CONTROL_REQUEST_METHOD);
}
 
Example 15
Source File: HttpClient.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
static boolean isCompressing(HttpHeaders h){
	return h.contains(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP, true);
}
 
Example 16
Source File: WebSocketExtensionUtil.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
static boolean isWebsocketUpgrade(HttpHeaders headers) {
    return headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true) &&
            headers.contains(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true);
}
 
Example 17
Source File: ServletOutputStream.java    From spring-boot-protocol with Apache License 2.0 4 votes vote down vote up
/**
     * Set the response header
     * @param isKeepAlive keep alive
     * @param nettyResponse nettyResponse
     * @param servletRequest servletRequest
     * @param servletResponse servletResponse
     * @param sessionCookieConfig sessionCookieConfig
     */
    private static void settingResponseHeader(boolean isKeepAlive, NettyHttpResponse nettyResponse,
                                              ServletHttpServletRequest servletRequest, ServletHttpServletResponse servletResponse,
                                              ServletSessionCookieConfig sessionCookieConfig) {
        HttpHeaderUtil.setKeepAlive(nettyResponse, isKeepAlive);
        HttpHeaders headers = nettyResponse.headers();

        //Content length
        long contentLength = servletResponse.getContentLength();
        if(contentLength >= 0) {
            headers.remove(HttpHeaderConstants.TRANSFER_ENCODING);
            headers.set(HttpHeaderConstants.CONTENT_LENGTH, contentLength);
        }else {
            nettyResponse.enableTransferEncodingChunked();
        }

        // Time and date response header
        if(!headers.contains(HttpHeaderConstants.DATE)) {
            headers.set(HttpHeaderConstants.DATE, ServletUtil.getDateByRfcHttp());
        }

        //Content Type The content of the response header
        String contentType = servletResponse.getContentType();
        if (null != contentType) {
            String characterEncoding = servletResponse.getCharacterEncoding();
            String value = (null == characterEncoding) ? contentType :
                    RecyclableUtil.newStringBuilder()
                            .append(contentType)
                            .append(APPEND_CONTENT_TYPE)
                            .append(characterEncoding).toString();
            headers.set(HttpHeaderConstants.CONTENT_TYPE, value);
        }

        //Server information response header
        String serverHeader = servletRequest.getServletContext().getServerHeader();
        if(serverHeader != null && serverHeader.length() > 0) {
            headers.set(HttpHeaderConstants.SERVER, serverHeader);
        }

        //language
        Locale locale = servletResponse.getLocale();
        if(!headers.contains(HttpHeaderConstants.CONTENT_LANGUAGE)){
            headers.set(HttpHeaderConstants.CONTENT_LANGUAGE,locale.toLanguageTag());
        }

        // Cookies processing
        //Session is handled first. If it is a new Session and the Session id is not the same as the Session id passed by the request, it needs to be written through the Cookie
        List<Cookie> cookies = servletResponse.getCookies();
        ServletHttpSession httpSession = servletRequest.getSession(false);
        if (httpSession != null && httpSession.isNew()
//		        && !httpSession.getId().equals(servletRequest.getRequestedSessionId())
        ) {
            String sessionCookieName = sessionCookieConfig.getName();
            if(sessionCookieName == null || sessionCookieName.isEmpty()){
                sessionCookieName = HttpConstants.JSESSION_ID_COOKIE;
            }
            String sessionCookiePath = sessionCookieConfig.getPath();
            if(sessionCookiePath == null || sessionCookiePath.isEmpty()) {
                sessionCookiePath = HttpConstants.DEFAULT_SESSION_COOKIE_PATH;
            }
            String sessionCookieText = ServletUtil.encodeCookie(sessionCookieName,servletRequest.getRequestedSessionId(), -1,
                    sessionCookiePath,sessionCookieConfig.getDomain(),sessionCookieConfig.isSecure(),Boolean.TRUE);
            headers.add(HttpHeaderConstants.SET_COOKIE, sessionCookieText);

            httpSession.setNewSessionFlag(false);
        }

        //Cookies set by other businesses or frameworks are written to the response header one by one
        int cookieSize = cookies.size();
        if(cookieSize > 0) {
            for (int i=0; i<cookieSize; i++) {
                Cookie cookie = cookies.get(i);
                String cookieText = ServletUtil.encodeCookie(cookie.getName(),cookie.getValue(),cookie.getMaxAge(),cookie.getPath(),cookie.getDomain(),cookie.getSecure(),cookie.isHttpOnly());
                headers.add(HttpHeaderConstants.SET_COOKIE, cookieText);
            }
        }
    }
 
Example 18
Source File: CorsHandler.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static boolean isPreflightRequest(final HttpRequest request) {
    final HttpHeaders headers = request.headers();
    return request.method().equals(OPTIONS) &&
            headers.contains(HttpHeaderNames.ORIGIN) &&
            headers.contains(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
}