Java 类io.netty.handler.codec.http.HttpHeaderValues 实例源码

项目:tasfe-framework    文件:AsyncHttpServletHandler.java   
@Override
public void channelRead(ChannelHandlerContext ctx, Object e) throws Exception {
    if (e instanceof ServletResponse) {
        logger.info("Handler async task...");
        HttpServletResponse response = (HttpServletResponse) e;
        Runnable task = ThreadLocalAsyncExecutor.pollTask(response);
        task.run();

        // write response...
        ChannelFuture future = ctx.channel().writeAndFlush(response);

        String keepAlive = response.getHeader(CONNECTION.toString());
        if (null != keepAlive && HttpHeaderValues.KEEP_ALIVE.toString().equalsIgnoreCase(keepAlive)) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    } else {
        ctx.fireChannelRead(e);
    }
}
项目:restlet-framework    文件:NettyServerCall.java   
@Override
public void writeResponseHead(Response restletResponse) throws IOException {
    setNettyResponse(new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(getStatusCode(), getReasonPhrase())));
    HttpHeaders headers = getNettyResponse().headers();

    // this.response.clear();
    for (Header header : getResponseHeaders()) {
        headers.add(header.getName(), header.getValue());
    }

    // Decide whether to close the connection or not.
    if (isKeepAlive()) {
        headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        getNettyChannel().write(getNettyResponse());
    } else {
        getNettyChannel().writeAndFlush(getNettyResponse()).addListener(ChannelFutureListener.CLOSE);
    }
}
项目:lannister    文件:HttpClient.java   
protected static void setDefaultHeaders(HttpRequest httpRequest) {
    if (!httpRequest.headers().contains(HttpHeaderNames.HOST)) {
        httpRequest.headers().set(HttpHeaderNames.HOST, httpRequest.uriObject().getHost());
    }
    if (!httpRequest.headers().contains(HttpHeaderNames.CONNECTION)) {
        httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    if (!httpRequest.headers().contains(HttpHeaderNames.ACCEPT_ENCODING)) {
        httpRequest.headers().set(HttpHeaderNames.ACCEPT_ENCODING,
                HttpHeaderValues.GZIP + ", " + HttpHeaderValues.DEFLATE);
    }
    if (!httpRequest.headers().contains(HttpHeaderNames.ACCEPT_CHARSET)) {
        httpRequest.headers().set(HttpHeaderNames.ACCEPT_CHARSET, "utf-8");
    }
    if (!httpRequest.headers().contains(HttpHeaderNames.CONTENT_TYPE)) {
        httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
    }
}
项目:lannister    文件:HttpRequestRouter.java   
protected static void setDefaultHeaders(FullHttpRequest request, HttpResponse response) {
    response.headers().add(HttpHeaderNames.SERVER,
            "lannister " + net.anyflow.lannister.Settings.INSTANCE.version());

    boolean keepAlive = HttpHeaderValues.KEEP_ALIVE.toString()
            .equals(request.headers().get(HttpHeaderNames.CONNECTION));
    if (keepAlive) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    if (Settings.INSTANCE.getProperty("webserver.allowCrossDomain", "false").equalsIgnoreCase("true")) {
        response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
        response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, DELETE");
        response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-PINGARUNER");
        response.headers().add(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, "1728000");
    }

    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
}
项目:lannister    文件:HttpRequest.java   
public Map<String, List<String>> parameters() {

        if (parameters != null) { return parameters; }

        Map<String, List<String>> ret = Maps.newHashMap();

        if (HttpMethod.GET.equals(method()) || HttpMethod.DELETE.equals(method())) {
            ret.putAll(new QueryStringDecoder(uri()).parameters());
            return ret;
        }
        else if (headers().contains(HttpHeaderNames.CONTENT_TYPE)
                && headers().get(HttpHeaderNames.CONTENT_TYPE)
                        .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())
                && (HttpMethod.POST.equals(method()) || HttpMethod.PUT.equals(method()))) {

            ret.putAll(new QueryStringDecoder("/dummy?" + content().toString(CharsetUtil.UTF_8)).parameters());
        }

        return ret;
    }
项目:lannister    文件:HttpRequest.java   
private void normalizeParameters() {
    String address = new StringBuilder().append(uriObject().getScheme()).append("://")
            .append(uriObject().getAuthority()).append(uriObject().getPath()).toString();

    if (HttpMethod.GET.equals(method()) || HttpMethod.DELETE.equals(method())) {
        String parameters = convertParametersToString();
        address += Strings.isNullOrEmpty(parameters) ? "" : "?" + parameters;
    }
    else if ((HttpMethod.POST.equals(method()) || HttpMethod.PUT.equals(method()))
            && (!headers().contains(HttpHeaderNames.CONTENT_TYPE) || headers().get(HttpHeaderNames.CONTENT_TYPE)
                    .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString()))) {
        ByteBuf content = Unpooled.copiedBuffer(convertParametersToString(), CharsetUtil.UTF_8);

        headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
        content().clear();
        content().writeBytes(content);
    }

    setUri(address);
}
项目:reactor-netty    文件:HttpClient.java   
static Function<? super HttpClientRequest, ? extends Publisher<Void>> handler(Function<? super HttpClientRequest, ? extends Publisher<Void>> h,
        HttpClientOptions opts) {
    if (opts.acceptGzip()) {
        if (h != null) {
            return req -> h.apply(req.header(HttpHeaderNames.ACCEPT_ENCODING,
                    HttpHeaderValues.GZIP));
        }
        else {
            return req -> req.header(HttpHeaderNames.ACCEPT_ENCODING,
                    HttpHeaderValues.GZIP);
        }
    }
    else {
        return h;
    }
}
项目:reactor-netty    文件:HttpServerRoutes.java   
/**
 * Listen for WebSocket on the passed path to be used as a routing condition. Incoming
 * connections will query the internal registry to invoke the matching handlers. <p>
 * Additional regex matching is available e.g. "/test/{param}".
 * Params are resolved using {@link HttpServerRequest#param(CharSequence)}
 * They are not accessible in the handler provided as parameter.
 *
 * @param path The websocket path used by clients
 * @param handler an handler to invoke for the given condition
 * @param protocols sub-protocol to use in WS handshake signature
 *
 * @return a new handler
 */
@SuppressWarnings("unchecked")
default HttpServerRoutes ws(String path,
        BiFunction<? super WebsocketInbound, ? super WebsocketOutbound, ? extends
                Publisher<Void>> handler,
        String protocols) {
    Predicate<HttpServerRequest> condition = HttpPredicate.get(path);

    return route(condition, (req, resp) -> {
        if (req.requestHeaders()
               .contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {

            HttpServerOperations ops = (HttpServerOperations) req;
            return ops.withWebsocketSupport(req.uri(), protocols,
                    handler);
        }
        return resp.sendNotFound();
    });
}
项目:reactor-netty    文件:HttpServerOperations.java   
@Override
protected void onOutboundError(Throwable err) {

    if (!channel().isActive()) {
        super.onOutboundError(err);
        return;
    }

    discreteRemoteClose(err);
    if (markSentHeaders()) {
        log.error("Error starting response. Replying error status", err);

        HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.INTERNAL_SERVER_ERROR);
        response.headers()
                .setInt(HttpHeaderNames.CONTENT_LENGTH, 0)
                .set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        channel().writeAndFlush(response)
                 .addListener(ChannelFutureListener.CLOSE);
        return;
    }

    markSentBody();
    channel().writeAndFlush(EMPTY_BUFFER)
             .addListener(ChannelFutureListener.CLOSE);
}
项目:dqueue    文件:HttpHandler.java   
private void writeResponse(Channel channel, Response response, HttpRequest httpRequest) {
    ByteBuf buf = Unpooled.copiedBuffer(JsonCodec.encodeResponse(response), CharsetUtil.UTF_8);
    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);

    String contentType = "text/html; charset=UTF-8";
    httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);

    boolean close = httpRequest.headers().contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true)
            || httpRequest.protocolVersion().equals(HttpVersion.HTTP_1_0)
            || !httpRequest.headers().contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true);

    if (!close) {
        httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
    }

    ChannelFuture future = channel.writeAndFlush(response);
    future.addListener(ChannelFutureListener.CLOSE);
}
项目:JavaAyo    文件:HelloWorldHttp1Handler.java   
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    if (HttpUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }
    boolean keepAlive = HttpUtil.isKeepAlive(req);

    ByteBuf content = ctx.alloc().buffer();
    content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());
    ByteBufUtil.writeAscii(content, " - via " + req.protocolVersion() + " (" + establishApproach + ")");

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

    if (!keepAlive) {
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        ctx.writeAndFlush(response);
    }
}
项目:JavaAyo    文件:Http1RequestHandler.java   
@Override
protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency,
        final FullHttpResponse response, final FullHttpRequest request) {
    HttpUtil.setContentLength(response, response.content().readableBytes());
    ctx.executor().schedule(new Runnable() {
        @Override
        public void run() {
            if (isKeepAlive(request)) {
                response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
                ctx.writeAndFlush(response);
            } else {
                ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
            }
        }
    }, latency, TimeUnit.MILLISECONDS);
}
项目:JavaAyo    文件:SpdyServerHandler.java   
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;

        if (is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        boolean keepAlive = isKeepAlive(req);

        ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8);

        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
        response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());

        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {
            response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
            ctx.write(response);
        }
    }
}
项目:netty-cookbook    文件:HelloWorldHttp1Handler.java   
@Override
public void messageReceived(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
    if (HttpHeaderUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }
    boolean keepAlive = HttpHeaderUtil.isKeepAlive(req);

    ByteBuf content = ctx.alloc().buffer();
    content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

    if (!keepAlive) {
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        ctx.writeAndFlush(response);
    }
}
项目:armeria    文件:Http1RequestDecoder.java   
private void fail(ChannelHandlerContext ctx, HttpResponseStatus status) {
    discarding = true;
    req = null;

    final ChannelFuture future;
    if (receivedRequests <= sentResponses) {
        // Just close the connection if sending an error response will make the number of the sent
        // responses exceed the number of the received requests, which doesn't make sense.
        future = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
    } else {
        final ByteBuf content = Unpooled.copiedBuffer(status.toString(), StandardCharsets.UTF_8);
        final FullHttpResponse res = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1, status, content);

        final HttpHeaders headers = res.headers();
        headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        headers.set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8);
        headers.setInt(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());

        future = ctx.writeAndFlush(res);
    }

    future.addListener(ChannelFutureListener.CLOSE);
}
项目:armeria    文件:ArmeriaHttpUtil.java   
/**
 * Filter the {@link HttpHeaderNames#TE} header according to the
 * <a href="https://tools.ietf.org/html/rfc7540#section-8.1.2.2">special rules in the HTTP/2 RFC</a>.
 * @param entry An entry whose name is {@link HttpHeaderNames#TE}.
 * @param out the resulting HTTP/2 headers.
 */
private static void toHttp2HeadersFilterTE(Entry<CharSequence, CharSequence> entry,
                                           HttpHeaders out) {
    if (AsciiString.indexOf(entry.getValue(), ',', 0) == -1) {
        if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(entry.getValue()),
                                                HttpHeaderValues.TRAILERS)) {
            out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
        }
    } else {
        List<CharSequence> teValues = StringUtil.unescapeCsvFields(entry.getValue());
        for (CharSequence teValue : teValues) {
            if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(teValue),
                                                    HttpHeaderValues.TRAILERS)) {
                out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
                break;
            }
        }
    }
}
项目:NettyStages    文件:HttpStaticFileServerHandler.java   
public static void beginHTTPResponse(ChannelHandlerContext ctx, FullHttpRequest request, long lastModified,
        String path, long fileLength) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    HttpUtil.setContentLength(response, fileLength);
    setContentTypeHeader(response, path);

    response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);

    setDateAndCacheHeaders(response, lastModified);
    if (HttpUtil.isKeepAlive(request)) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // Write the initial line and the header.
    ctx.write(response);
}
项目:xio    文件:HttpsUpgradeHandler.java   
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
  List<ByteBuf> payload;

  HttpHeaders headers = new CombinedHttpHeaders(true);
  headers.add(HttpHeaderNames.UPGRADE, "TLS/1.2");
  headers.add(HttpHeaderNames.UPGRADE, HTTP_1_1);
  headers.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
  headers.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
  headers.add(HttpHeaderNames.CONTENT_LENGTH, "0");
  DefaultFullHttpResponse response =
      new DefaultFullHttpResponse(
          HTTP_1_1, UPGRADE_REQUIRED, Unpooled.EMPTY_BUFFER, headers, EmptyHttpHeaders.INSTANCE);
  payload = Recipes.encodeResponse(response);

  for (ByteBuf buffer : payload) {
    ctx.write(buffer.copy());
  }
  ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
项目:sinetja    文件:Request.java   
public Request(Server server, Channel channel, FullHttpRequest request, RouteResult<Action> routeResult) {
    this.server = server;
    this.channel = channel;
    this.request = request;
    this.routeResult = routeResult;

    // Get client IP while the client is still connected; Netty may not allow
    // us to get this info later when the connection may be closed
    clientIp = getClientIpFromChannel();
    remoteIp = getRemoteIpFromClientIpOrReverseProxy();

    // Parse body params
    String contentTye = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
    if (HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.contentEqualsIgnoreCase(contentTye)) {
        String content = request.content().toString(server.charset());
        QueryStringDecoder qsd = new QueryStringDecoder("?" + content);
        bodyParams = qsd.parameters();
    } else {
        bodyParams = Collections.<String, List<String>>emptyMap();
    }
}
项目:bazel    文件:HttpUploadHandlerTest.java   
private void uploadsShouldWork(boolean casUpload, EmbeddedChannel ch, HttpResponseStatus status)
    throws Exception {
  ByteArrayInputStream data = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(new UploadCommand(CACHE_URI, casUpload, "abcdef", data, 5), writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request.method()).isEqualTo(HttpMethod.PUT);
  assertThat(request.headers().get(HttpHeaders.CONNECTION))
      .isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());

  HttpChunkedInput content = ch.readOutbound();
  assertThat(content.readChunk(ByteBufAllocator.DEFAULT).content().readableBytes()).isEqualTo(5);

  FullHttpResponse response =
      new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
  response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

  ch.writeInbound(response);

  assertThat(writePromise.isDone()).isTrue();
  assertThat(ch.isOpen()).isTrue();
}
项目:bazel    文件:HttpUploadHandlerTest.java   
/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test
public void httpErrorsAreSupported() throws Exception {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpUploadHandler(null));
  ByteArrayInputStream data = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(new UploadCommand(CACHE_URI, true, "abcdef", data, 5), writePromise);

  HttpRequest request = ch.readOutbound();
  assertThat(request).isInstanceOf(HttpRequest.class);
  HttpChunkedInput content = ch.readOutbound();
  assertThat(content).isInstanceOf(HttpChunkedInput.class);

  FullHttpResponse response =
      new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);

  ch.writeInbound(response);

  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).status())
      .isEqualTo(HttpResponseStatus.FORBIDDEN);
  assertThat(ch.isOpen()).isFalse();
}
项目:bazel    文件:HttpDownloadHandlerTest.java   
/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test
public void httpErrorsAreSupported() throws IOException {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null));
  ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
  DownloadCommand cmd = new DownloadCommand(CACHE_URI, true, "abcdef", out);
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpResponse response =
      new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.CLOSE);
  ch.writeInbound(response);
  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).status())
      .isEqualTo(HttpResponseStatus.NOT_FOUND);
  // No data should have been written to the OutputStream and it should have been closed.
  assertThat(out.size()).isEqualTo(0);
  verify(out).close();
  assertThat(ch.isOpen()).isFalse();
}
项目:laputa    文件:AbstractResponseTranslator.java   
protected void writeResponse(ChannelHandlerContext ctx,
                             FullHttpRequest req,
                             FullHttpResponse res,
                             HttpResponseStatus status) {
  setDateHeader(req, res, status);
  if (!HttpUtil.isContentLengthSet(res)) {
    HttpUtil.setContentLength(res, res.content().readableBytes());
  }

  boolean keepAlive = HttpUtil.isKeepAlive(req);
  if (keepAlive) {
    res.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    ctx.write(res);
  } else {
    ctx.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
  }
}
项目:docker-java    文件:NettyInvocationBuilder.java   
private void postChunkedStreamRequest(HttpRequestProvider requestProvider, Channel channel, InputStream body) {
    HttpRequest request = requestProvider.getHttpRequest(resource);

    // don't accept FullHttpRequest here
    if (request instanceof FullHttpRequest) {
        throw new DockerClientException("fatal: request is instance of FullHttpRequest");
    }

    request.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    request.headers().remove(HttpHeaderNames.CONTENT_LENGTH);

    channel.write(request);

    channel.write(new ChunkedStream(new BufferedInputStream(body, 1024 * 1024), 1024 * 1024));
    channel.write(LastHttpContent.EMPTY_LAST_CONTENT);
    channel.flush();
}
项目:socketio    文件:PipelineUtils.java   
public static HttpResponse createHttpResponse(final String origin, ByteBuf content, boolean json) {
  FullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
  if (json) {
    res.headers().add(HttpHeaderNames.CONTENT_TYPE, "text/javascript; charset=UTF-8");
  } else {
    res.headers().add(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
  }
  res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  if (origin != null) {
    res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
    res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
  }
  HttpUtil.setContentLength(res, content.readableBytes());

  return res;
}
项目:incubator-servicecomb-java-chassis    文件:RestBodyHandler.java   
BHandler(RoutingContext context) {
  this.context = context;
  Set<FileUpload> fileUploads = context.fileUploads();

  final String contentType = context.request().getHeader(HttpHeaders.CONTENT_TYPE);
  if (contentType == null) {
    isMultipart = false;
    isUrlEncoded = false;
  } else {
    final String lowerCaseContentType = contentType.toLowerCase();
    isMultipart = lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString());
    isUrlEncoded = lowerCaseContentType.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString());
  }

  if (isMultipart || isUrlEncoded) {
    makeUploadDir(context.vertx().fileSystem());
    context.request().setExpectMultipart(true);
    context.request().uploadHandler(upload -> {
      // *** cse begin ***
      if (uploadsDir == null) {
        failed = true;
        CommonExceptionData data = new CommonExceptionData("not support file upload.");
        throw new ErrorDataDecoderException(ExceptionFactory.createConsumerException(data));
      }
      // *** cse end ***

      // we actually upload to a file with a generated filename
      uploadCount.incrementAndGet();
      String uploadedFileName = new File(uploadsDir, UUID.randomUUID().toString()).getPath();
      upload.streamToFileSystem(uploadedFileName);
      FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName, upload);
      fileUploads.add(fileUpload);
      upload.exceptionHandler(context::fail);
      upload.endHandler(v -> uploadEnded());
    });
  }
  context.request().exceptionHandler(context::fail);
}
项目:elasticsearch_my    文件:Netty4HttpServerTransportTests.java   
/**
 * Test that {@link Netty4HttpServerTransport} responds to a
 * 100-continue expectation with too large a content-length
 * with a 413 status.
 * @throws InterruptedException if the client communication with the server is interrupted
 */
public void testExpectContinueHeaderContentLengthTooLong() throws InterruptedException {
    final String key = HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.getKey();
    final int maxContentLength = randomIntBetween(1, 104857600);
    final Settings settings = Settings.builder().put(key, maxContentLength + "b").build();
    final int contentLength = randomIntBetween(maxContentLength + 1, Integer.MAX_VALUE);
    runExpectHeaderTest(
            settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE);
}
项目:libcwfincore    文件:GdaxExchangeSession.java   
public void sendRequest(Channel channel, HttpRequest request) throws GeneralSecurityException {
    addAuthHeaders(request);
    request.headers().set(HttpHeaders.HOST, conn.getHost());
    request.headers().set(HttpHeaders.ACCEPT, "*/*");
    request.headers().set(HttpHeaders.USER_AGENT, "Cloudwall/1.0");
    request.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    channel.writeAndFlush(request);
}
项目:proxyee-down    文件:HttpDownSniffIntercept.java   
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse,
    HttpProxyInterceptPipeline pipeline) throws Exception {
  boolean downFlag = false;
  if ((httpResponse.status().code() + "").indexOf("20") == 0) { //响应码为20x
    HttpHeaders httpResHeaders = httpResponse.headers();
    String accept = pipeline.getHttpRequest().headers().get(HttpHeaderNames.ACCEPT);
    String contentType = httpResHeaders.get(HttpHeaderNames.CONTENT_TYPE);
    if (accept != null
        && accept.matches("^.*text/html.*$")  //直接url的方式访问不是以HTML标签加载的(a标签除外)
        && contentType != null
        && !contentType.matches("^.*text/.*$")) { //响应体不是text/html报文
      //有两种情况进行下载 1.url后缀为.xxx  2.带有CONTENT_DISPOSITION:ATTACHMENT响应头
      String disposition = httpResHeaders.get(HttpHeaderNames.CONTENT_DISPOSITION);
      if (pipeline.getHttpRequest().uri().matches("^.*\\.[^./]{1,5}(\\?[^?]*)?$")
          || (disposition != null && disposition.contains(HttpHeaderValues.ATTACHMENT))) {
        downFlag = true;
      }
    }
    HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest();
    if (downFlag) {   //如果是下载
      proxyChannel.close();//关闭嗅探下载连接
      HttpDownServer.LOGGER.debug("=====================下载===========================\n" +
          pipeline.getHttpRequest().toString() + "\n" +
          httpResponse.toString() + "\n" +
          "================================================");
      //原始的请求协议
      httpRequestInfo.setRequestProto(pipeline.getRequestProto());
      pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
    } else {
      if (httpRequestInfo.content() != null) {
        httpRequestInfo.setContent(null);
      }
    }
  }
  pipeline.getDefault().afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
}
项目:proxyee-down    文件:ResponseTextIntercept.java   
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse,
    HttpProxyInterceptPipeline pipeline) throws Exception {
  if (match(httpResponse, pipeline)) {
    isMatch = true;
    //解压gzip响应
    if ("gzip".equalsIgnoreCase(httpResponse.headers().get(HttpHeaderNames.CONTENT_ENCODING))) {
      isGzip = true;
      pipeline.reset3();
      proxyChannel.pipeline().addAfter("httpCodec", "decompress", new HttpContentDecompressor());
      proxyChannel.pipeline().fireChannelRead(httpResponse);
    } else {
      if (isGzip) {
        httpResponse.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
      }
      contentBuf = PooledByteBufAllocator.DEFAULT.buffer();
      /*contentBuf.writeBytes(hookResponse().getBytes());
      for (HttpProxyIntercept intercept : pipeline) {
        if (intercept != this && intercept instanceof ResponseTextIntercept) {
          ResponseTextIntercept textIntercept = (ResponseTextIntercept) intercept;
          if (textIntercept.match(httpResponse, pipeline)) {
            contentBuf.writeBytes(textIntercept.hookResponse().getBytes());
          }
        }
      }*/
    }
    //直接调用默认拦截器,跳过下载拦截器
    pipeline.getDefault()
        .afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
  } else {
    isMatch = false;
    pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
  }
}
项目:aliyun-oss-hadoop-fs    文件:SimpleHttpProxyHandler.java   
@Override
public void channelRead0
  (final ChannelHandlerContext ctx, final HttpRequest req) {
  uri = req.uri();
  final Channel client = ctx.channel();
  Bootstrap proxiedServer = new Bootstrap()
    .group(client.eventLoop())
    .channel(NioSocketChannel.class)
    .handler(new ChannelInitializer<SocketChannel>() {
      @Override
      protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        p.addLast(new HttpRequestEncoder(), new Forwarder(uri, client));
      }
    });
  ChannelFuture f = proxiedServer.connect(host);
  proxiedChannel = f.channel();
  f.addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
      if (future.isSuccess()) {
        ctx.channel().pipeline().remove(HttpResponseEncoder.class);
        HttpRequest newReq = new DefaultFullHttpRequest(HTTP_1_1,
          req.method(), req.uri());
        newReq.headers().add(req.headers());
        newReq.headers().set(CONNECTION, HttpHeaderValues.CLOSE);
        future.channel().writeAndFlush(newReq);
      } else {
        DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1,
          INTERNAL_SERVER_ERROR);
        resp.headers().set(CONNECTION, HttpHeaderValues.CLOSE);
        LOG.info("Proxy " + uri + " failed. Cause: ", future.cause());
        ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
        client.close();
      }
    }
  });
}
项目:parser-excel-elasticsearch    文件:ElasticWriter.java   
private void addHeaders(HttpClientRequest client) {

        // comply with ElasticSearch 6.0 - strict content type.
        client.putHeader(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);

        // support basic authentication.
        Configuration.getBasicAuth().ifPresent(auth -> {
            client.putHeader(HttpHeaderNames.AUTHORIZATION, "Basic " + auth);
        });
    }
项目:nettythrift    文件:HttpThriftBufDecoder.java   
private boolean directHandleMethod(ChannelHandlerContext ctx, FullHttpRequest request, HttpMethod method) {
    if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST)) {
        return false;
    }
    // 处理 OPTIONS 请求
    HttpResponseStatus status = HttpResponseStatus.OK;
    boolean invalid = false;
    if (!method.equals(HttpMethod.OPTIONS)) {
        invalid = true;
        status = HttpResponseStatus.METHOD_NOT_ALLOWED;
    }
    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.EMPTY_BUFFER);
    HttpHeaders headers = response.headers();
    // headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS,
    // "X-Requested-With, accept, origin, content-type");
    headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-Requested-With, content-type");
    headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,OPTIONS");
    headers.set(HttpHeaderNames.SERVER, "Netty5");
    if (invalid) {
        headers.set("Client-Warning", "Invalid Method");
    }
    boolean keepAlive = HttpHeaderUtil.isKeepAlive(request);
    if (keepAlive) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    ctx.write(response);
    ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
    return true;
}
项目:restnext    文件:CustomHttpContentCompressor.java   
@Override
protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out)
    throws Exception {
  if (msg instanceof HttpResponse) {
    HttpResponse res = (HttpResponse) msg;

    skipCompression = false;

    // if an "content-encoding: identity" header was set, we do not compress
    if (skipCompression = res.headers().containsValue(
        HttpHeaderNames.CONTENT_ENCODING,
        HttpHeaderValues.IDENTITY,
        true)) {
      // remove header as one should not send Identity as content encoding
      res.headers().remove(HttpHeaderNames.CONTENT_ENCODING);
    } else {
      CharSequence mimeType = HttpUtil.getMimeType(res);
      // skip compression if the media type is not compressible by the server
      skipCompression = mimeType != null && !isCompressable(MediaType.parse(mimeType.toString()));

      // skip compression if the content length is less than expected by the server
      int contentLength = res.headers().getInt(HttpHeaderNames.CONTENT_LENGTH, 0);
      skipCompression = contentLength > 0 && contentLength < compressionContentLength;
    }
  }

  super.encode(ctx, msg, out);
}
项目:shortcircuit-proxy    文件:EvaluationServerConnectHandler.java   
private HttpResponse connectedResponse() {
    HttpResponseStatus status = new HttpResponseStatus(200, "Connection established");
    HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
    response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    response.headers().set("Proxy-Connection", HttpHeaderValues.KEEP_ALIVE);
    // TODO add via header
    return response;
}
项目:DAVe    文件:AuthTest.java   
private HttpServer createOpenIdMockServer(String jwksCerts) {
    return AuthTest.vertx.createHttpServer().requestHandler(request -> {
        HttpServerResponse response = request.response();
        response.putHeader(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
        JsonObject content = new JsonObject();
        content.put("issuer", "https://auth.dave.dbg-devops.com/auth/realms/DAVe");
        content.put("jwks_uri", JWKSAuthProviderImpl.class.getResource(jwksCerts).toString());
        response.end(content.toBuffer());
    });
}
项目:DAVe    文件:AuthTest.java   
private HttpServer createOpenIdMockServerInvalidJwks() {
    return AuthTest.vertx.createHttpServer().requestHandler(request -> {
        HttpServerResponse response = request.response();
        response.putHeader(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
        JsonObject content = new JsonObject();
        content.put("issuer", "https://auth.dave.dbg-devops.com/auth/realms/DAVe");
        content.put("jwks_uri", "invalid");
        response.end(content.toBuffer());
    });
}
项目:HeliosStreams    文件:HubManager.java   
protected HttpRequest buildHttpRequest(final ByteBuf jsonRequest) {
        final String[] endpoints = tsdbEndpoint.getUpServers();
        final URL postUrl = URLHelper.toURL(endpoints[0] + "/query/");
        log.debug("Http Post to [{}]", postUrl);
        final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, postUrl.getPath(), jsonRequest);
        request.headers().set(HttpHeaderNames.HOST, postUrl.getHost());
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
//      request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, jsonRequest.readableBytes());
        request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
        return request;
    }
项目:JavaAyo    文件:HttpSnoopServerHandler.java   
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpUtil.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(
            HTTP_1_1, currentObj.decoderResult().isSuccess()? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // Encode the cookie.
    String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie: cookies) {
                response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key1", "value1"));
        response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key2", "value2"));
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;
}
项目:yarpc-java    文件:TransportRequestEncoderConfiguration.java   
@Override
public HttpRequest buildStartMessage(TransportRequest request, AttributeMap channelAttrs) {
  DefaultHttpRequest httpRequest =
      new DefaultHttpRequest(HttpTransport.HTTP_VERSION, HttpMethod.POST, url.getPath());
  HttpHeaders httpHeaders = httpRequest.headers();
  setCommonHeaders(httpHeaders, request, channelAttrs);
  httpHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
  return httpRequest;
}