Example usage for io.netty.handler.codec.http HttpHeaderNames EXPECT

List of usage examples for io.netty.handler.codec.http HttpHeaderNames EXPECT

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderNames EXPECT.

Prototype

AsciiString EXPECT

To view the source code for io.netty.handler.codec.http HttpHeaderNames EXPECT.

Click Source Link

Document

"expect"

Usage

From source file:com.github.thinker0.mesos.MesosHealthCheckerServer.java

License:Apache License

public boolean is100ContinueExpected(HttpMessage message) {
    if (!(message instanceof HttpRequest)) {
        return false;
    } else if (message.protocolVersion().compareTo(HttpVersion.HTTP_1_1) < 0) {
        return false;
    } else {// www.j  a v  a 2 s  . c  om
        CharSequence value = (CharSequence) message.headers().get(HttpHeaderNames.EXPECT);
        return value == null ? false
                : (HttpHeaderValues.CONTINUE.contentEqualsIgnoreCase(value) ? true
                        : message.headers().contains(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE, true));
    }
}

From source file:io.vertx.core.http.impl.Http2ServerConnection.java

License:Open Source License

@Override
public synchronized void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
        int padding, boolean endOfStream) {
    VertxHttp2Stream stream = streams.get(streamId);
    if (stream == null) {
        if (isMalformedRequest(headers)) {
            handler.writeReset(streamId, Http2Error.PROTOCOL_ERROR.code());
            return;
        }//from www . j  a va2 s  . c o m
        String contentEncoding = options.isCompressionSupported() ? HttpUtils.determineContentEncoding(headers)
                : null;
        Http2Stream s = handler.connection().stream(streamId);
        boolean writable = handler.encoder().flowController().isWritable(s);
        Http2ServerRequestImpl req = new Http2ServerRequestImpl(this, s, metrics, serverOrigin, headers,
                contentEncoding, writable);
        stream = req;
        CharSequence value = headers.get(HttpHeaderNames.EXPECT);
        if (options.isHandle100ContinueAutomatically()
                && ((value != null && HttpHeaderValues.CONTINUE.equals(value))
                        || headers.contains(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE))) {
            req.response().writeContinue();
        }
        streams.put(streamId, req);
        context.executeFromIO(() -> {
            Http2ServerResponseImpl resp = req.response();
            resp.beginRequest();
            requestHandler.handle(req);
            boolean hasPush = resp.endRequest();
            if (hasPush) {
                ctx.flush();
            }
        });
    } else {
        // Http server request trailer - not implemented yet (in api)
    }
    if (endOfStream) {
        context.executeFromIO(stream::onEnd);
    }
}

From source file:org.ballerinalang.test.service.http.sample.ExpectContinueTestCase.java

License:Open Source License

@Test(description = "Test multipart form data request with expect:100-continue header")
public void testMultipartWith100ContinueHeader() throws IOException {
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.EXPECT.toString(), HttpHeaderValues.CONTINUE.toString());

    Map<String, String> formData = new HashMap<>();
    formData.put("person", "engineer");
    formData.put("team", "ballerina");

    HttpResponse response = HttpClientRequest.doMultipartFormData(
            serverInstance.getServiceURLHttp(servicePort, "continue/getFormParam"), headers, formData);
    Assert.assertNotNull(response);/* www  .  j a v  a 2s  .c o  m*/
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertEquals(response.getData(), "Result = Key:person Value: engineer Key:team Value: ballerina");
}

From source file:org.ballerinalang.test.util.client.HttpClient.java

License:Open Source License

public List<FullHttpResponse> sendExpectContinueRequest(DefaultHttpRequest httpRequest,
        DefaultLastHttpContent httpContent) {
    CountDownLatch latch = new CountDownLatch(1);
    this.waitForConnectionClosureLatch = new CountDownLatch(1);
    this.responseHandler.setLatch(latch);
    this.responseHandler.setWaitForConnectionClosureLatch(this.waitForConnectionClosureLatch);

    httpRequest.headers().set(HttpHeaderNames.HOST, host + ":" + port);
    httpRequest.headers().set(HttpHeaderNames.EXPECT, HttpHeaderValues.CONTINUE);
    this.connectedChannel.writeAndFlush(httpRequest);

    try {//  w  w  w  .j  a  va  2s. co  m
        latch.await();
    } catch (InterruptedException e) {
        log.warn("Interrupted before receiving the response.");
    }

    FullHttpResponse response100Continue = this.responseHandler.getHttpFullResponse();

    if (response100Continue.status().equals(HttpResponseStatus.CONTINUE)) {
        latch = new CountDownLatch(1);
        this.responseHandler.setLatch(latch);
        this.connectedChannel.writeAndFlush(httpContent);
        try {
            latch.await();
        } catch (InterruptedException e) {
            log.warn("Interrupted before receiving the response.");
        }
    }

    return responseHandler.getHttpFullResponses();
}

From source file:org.elasticsearch.http.nio.NioHttpServerTransportTests.java

License:Apache License

private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength,
        final HttpResponseStatus expectedStatus) throws InterruptedException {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
        @Override//from   w ww . j  ava2 s  .  com
        public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
            channel.sendResponse(
                    new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
        }

        @Override
        public void dispatchBadRequest(RestRequest request, RestChannel channel, ThreadContext threadContext,
                Throwable cause) {
            throw new AssertionError();
        }
    };
    try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays,
            pageRecycler, threadPool, xContentRegistry(), dispatcher)) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
                    "/");
            request.headers().set(HttpHeaderNames.EXPECT, expectation);
            HttpUtil.setContentLength(request, contentLength);

            final FullHttpResponse response = client.post(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(expectedStatus));
                if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
                    final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                            HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
                    final FullHttpResponse continuationResponse = client.post(remoteAddress.address(),
                            continuationRequest);
                    try {
                        assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
                        assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()),
                                StandardCharsets.UTF_8), is("done"));
                    } finally {
                        continuationResponse.release();
                    }
                }
            } finally {
                response.release();
            }
        }
    }
}