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

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

Introduction

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

Prototype

AsciiString ORIGIN

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

Click Source Link

Document

"origin"

Usage

From source file:com.linecorp.armeria.client.http.SimpleHttpClientCodecTest.java

License:Apache License

@Test
public void encodeRequestNoBody() {
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/foo?q=foo&bar=baz")
            .header(HttpHeaderNames.ORIGIN, "localhost").build();
    EncodeResult result = codec.encodeRequest(channel, SCHEME.sessionProtocol(), EXECUTE_METHOD,
            new Object[] { request });
    assertTrue(result.isSuccess());//from  w w  w  .ja  va 2  s .c  om
    assertEquals(SCHEME, result.encodedScheme().get());
    assertEquals("/foo", result.encodedPath().get());
    assertEquals("www.github.com", result.encodedHost().get());
    FullHttpRequest fullHttpRequest = (FullHttpRequest) result.content();
    assertEquals(HttpVersion.HTTP_1_1, fullHttpRequest.protocolVersion());
    assertEquals("/foo?q=foo&bar=baz", fullHttpRequest.uri());
    assertEquals(HttpMethod.GET, fullHttpRequest.method());
    assertEquals("localhost", fullHttpRequest.headers().get(HttpHeaderNames.ORIGIN));
}

From source file:com.linecorp.armeria.client.http.SimpleHttpClientCodecTest.java

License:Apache License

@Test
public void encodeRequestWithBody() {
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/foo?q=foo&bar=baz")
            .content("lorem ipsum foo bar", StandardCharsets.UTF_8).header(HttpHeaderNames.ORIGIN, "localhost")
            .build();/*from  w ww.j a va 2 s .  c  o m*/
    EncodeResult result = codec.encodeRequest(channel, SCHEME.sessionProtocol(), EXECUTE_METHOD,
            new Object[] { request });
    assertTrue(result.isSuccess());
    assertEquals(SCHEME, result.encodedScheme().get());
    assertEquals("/foo", result.encodedPath().get());
    assertEquals("www.github.com", result.encodedHost().get());
    FullHttpRequest fullHttpRequest = (FullHttpRequest) result.content();
    assertEquals(HttpVersion.HTTP_1_1, fullHttpRequest.protocolVersion());
    assertEquals("/foo?q=foo&bar=baz", fullHttpRequest.uri());
    assertEquals(HttpMethod.GET, fullHttpRequest.method());
    assertEquals("localhost", fullHttpRequest.headers().get(HttpHeaderNames.ORIGIN));
    assertEquals("lorem ipsum foo bar",
            new String(ByteBufUtil.getBytes(fullHttpRequest.content()), StandardCharsets.UTF_8));
}

From source file:com.linecorp.armeria.client.http.SimpleHttpRequestBuilderTest.java

License:Apache License

@Test
public void headersObject() {
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.set(HttpHeaderNames.CACHE_CONTROL, "alwayscache");
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, "gzip");
    SimpleHttpRequest request = SimpleHttpRequestBuilder.forGet("/path").header(HttpHeaderNames.ACCEPT, "utf-8")
            .headers(headers).header(HttpHeaderNames.ORIGIN, "localhost").build();
    assertEquals(4, request.headers().size());
    assertEquals("alwayscache", request.headers().get(HttpHeaderNames.CACHE_CONTROL));
    assertEquals("gzip", request.headers().get(HttpHeaderNames.ACCEPT_ENCODING));
    assertEquals("localhost", request.headers().get(HttpHeaderNames.ORIGIN));
    assertEquals("utf-8", request.headers().get(HttpHeaderNames.ACCEPT));
}

From source file:com.tc.websocket.server.handler.WebSocketValidationHandler.java

License:Apache License

/**
 * Checks if is valid request.//  ww w  . j  av a2  s . c om
 *
 * @param ctx the ctx
 * @param req the req
 * @return true, if is valid request
 */
public boolean isValidRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    // Handle a bad request.
    if (!req.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return false;
    }

    // Allow only GET methods.
    if (req.method() != GET) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return false;
    }

    if ("/favicon.ico".equals(req.uri())) {
        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
        sendHttpResponse(ctx, req, res);
        return false;
    }

    if (!req.uri().contains(Const.WEBSOCKET_URI)) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return false;
    }

    if (server.getWebSocketCount() >= cfg.getMaxConnections()) {
        LOG.log(Level.SEVERE, "Maximum number of websockets " + cfg.getMaxConnections() + " created.");
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return false;
    }

    String origin = req.headers().get(HttpHeaderNames.ORIGIN);
    if (origin != null && !Config.getInstance().isAllowedOrigin(origin)) {
        LOG.log(Level.SEVERE, "Invalid origin " + origin + " attempting to make websocket connection");
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return false;
    }

    return true;
}

From source file:org.ballerinalang.net.http.CorsHeaderGenerator.java

License:Open Source License

public static void process(HttpCarbonMessage requestMsg, HttpCarbonMessage responseMsg,
        boolean isSimpleRequest) {

    boolean isCorsResponseHeadersAvailable = false;
    Map<String, String> responseHeaders;
    CorsHeaders resourceCors;//  w  w w .ja v a2 s  . co  m
    if (isSimpleRequest) {
        resourceCors = (CorsHeaders) requestMsg.getProperty(HttpConstants.RESOURCES_CORS);
        String origin = requestMsg.getHeader(HttpHeaderNames.ORIGIN.toString());
        //resourceCors cannot be null here
        if (origin == null || resourceCors == null || !resourceCors.isAvailable()) {
            return;
        }
        if ((responseHeaders = processSimpleRequest(origin, resourceCors)) != null) {
            isCorsResponseHeadersAvailable = true;
        }
    } else {
        String origin = requestMsg.getHeader(HttpHeaderNames.ORIGIN.toString());
        if (origin == null) {
            return;
        }
        if ((responseHeaders = processPreflightRequest(origin, requestMsg)) != null) {
            isCorsResponseHeadersAvailable = true;
        }
    }
    if (isCorsResponseHeadersAvailable) {
        responseHeaders.forEach(responseMsg::setHeader);
        responseMsg.removeHeader(HttpHeaderNames.ALLOW.toString());
    }
}

From source file:org.ballerinalang.stdlib.services.cors.HTTPCorsTest.java

License:Open Source License

@Test(description = "Test for CORS override at two levels for simple requests")
public void testSimpleReqServiceResourceCorsOverride() {
    String path = "/hello1/test1";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", "Hello there");
    cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://www.wso2.com");
    HttpCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);

    Assert.assertNotNull(response);/*w  ww .  j a v  a 2 s . c o m*/
    BValue bJson = JsonParser.parse(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(((BMap<String, BValue>) bJson).get("echo").stringValue(), "resCors");
    Assert.assertEquals("http://www.wso2.com",
            response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString()));
    Assert.assertEquals("true",
            response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString()));
}

From source file:org.ballerinalang.stdlib.services.cors.HTTPCorsTest.java

License:Open Source License

@Test(description = "Test for simple request service CORS")
public void testSimpleReqServiceCors() {
    String path = "/hello1/test2";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "GET");
    cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://www.hello.com");
    HttpCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);

    Assert.assertNotNull(response);//from w w  w .ja v a  2  s . c o  m
    BValue bJson = JsonParser.parse(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(((BMap<String, BValue>) bJson).get("echo").stringValue(), "serCors");
    Assert.assertEquals(response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString()),
            "http://www.hello.com");
    Assert.assertEquals(response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString()),
            "true");
}

From source file:org.ballerinalang.stdlib.services.cors.HTTPCorsTest.java

License:Open Source License

@Test(description = "Test for resource only CORS declaration")
public void testSimpleReqResourceOnlyCors() {
    String path = "/hello2/test1";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", "hello");
    cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://www.hello.com");
    HttpCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);

    Assert.assertNotNull(response);//from   ww  w .j  a  v a 2 s  .co m
    BValue bJson = JsonParser.parse(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(((BMap<String, BValue>) bJson).get("echo").stringValue(), "resOnlyCors");
    Assert.assertEquals("http://www.hello.com",
            response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString()));
    Assert.assertEquals(null, response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString()));
    Assert.assertEquals("X-Content-Type-Options, X-PINGOTHER",
            response.getHeader(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS.toString()));
}

From source file:org.ballerinalang.stdlib.services.cors.HTTPCorsTest.java

License:Open Source License

@Test(description = "Test simple request with multiple origins")
public void testSimpleReqMultipleOrigins() {
    String path = "/hello1/test3";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", "Hello there");
    cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://www.wso2.com http://www.amazon.com");
    HttpCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);

    Assert.assertNotNull(response);//from ww w  . ja v a2s . co  m
    BValue bJson = JsonParser.parse(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(((BMap<String, BValue>) bJson).get("echo").stringValue(), "moreOrigins");
    Assert.assertEquals("http://www.wso2.com http://www.amazon.com",
            response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString()));
    Assert.assertEquals("true",
            response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString()));
}

From source file:org.ballerinalang.stdlib.services.cors.HTTPCorsTest.java

License:Open Source License

@Test(description = "Test simple request for invalid origins")
public void testSimpleReqInvalidOrigin() {
    String path = "/hello1/test1";
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage(path, "POST", "Hello there");
    cMsg.setHeader(HttpHeaderNames.ORIGIN.toString(), "www.wso2.com");
    HttpCarbonMessage response = Services.invokeNew(complieResult, TEST_EP, cMsg);

    Assert.assertNotNull(response);/*from w w w.  j  a  v a2 s. c  om*/
    BValue bJson = JsonParser.parse(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(((BMap<String, BValue>) bJson).get("echo").stringValue(), "resCors");
    Assert.assertEquals(null, response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString()));
    Assert.assertNull(null, response.getHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString()));
}