use of io.netty.handler.codec.http.QueryStringDecoder in project netty-socketio by mrniko.
the class PollingTransport method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
List<String> transport = queryDecoder.parameters().get("transport");
if (transport != null && NAME.equals(transport.get(0))) {
List<String> sid = queryDecoder.parameters().get("sid");
List<String> j = queryDecoder.parameters().get("j");
List<String> b64 = queryDecoder.parameters().get("b64");
String origin = req.headers().get(HttpHeaderNames.ORIGIN);
ctx.channel().attr(EncoderHandler.ORIGIN).set(origin);
String userAgent = req.headers().get(HttpHeaderNames.USER_AGENT);
ctx.channel().attr(EncoderHandler.USER_AGENT).set(userAgent);
if (j != null && j.get(0) != null) {
Integer index = Integer.valueOf(j.get(0));
ctx.channel().attr(EncoderHandler.JSONP_INDEX).set(index);
}
if (b64 != null && b64.get(0) != null) {
Integer enable = Integer.valueOf(b64.get(0));
ctx.channel().attr(EncoderHandler.B64).set(enable == 1);
}
try {
if (sid != null && sid.get(0) != null) {
final UUID sessionId = UUID.fromString(sid.get(0));
handleMessage(req, sessionId, queryDecoder, ctx);
} else {
// first connection
ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
handleMessage(req, client.getSessionId(), queryDecoder, ctx);
}
} finally {
req.release();
}
return;
}
}
ctx.fireChannelRead(msg);
}
use of io.netty.handler.codec.http.QueryStringDecoder in project netty-socketio by mrniko.
the class WebSocketTransport method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof CloseWebSocketFrame) {
ctx.channel().close();
ReferenceCountUtil.release(msg);
} else if (msg instanceof BinaryWebSocketFrame || msg instanceof TextWebSocketFrame) {
ByteBufHolder frame = (ByteBufHolder) msg;
ClientHead client = clientsBox.get(ctx.channel());
if (client == null) {
log.debug("Client with was already disconnected. Channel closed!");
ctx.channel().close();
frame.release();
return;
}
ctx.pipeline().fireChannelRead(new PacketsMessage(client, frame.content(), Transport.WEBSOCKET));
frame.release();
} else if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
String path = queryDecoder.path();
List<String> transport = queryDecoder.parameters().get("transport");
List<String> sid = queryDecoder.parameters().get("sid");
if (transport != null && NAME.equals(transport.get(0))) {
try {
if (!configuration.getTransports().contains(Transport.WEBSOCKET)) {
log.debug("{} transport not supported by configuration.", Transport.WEBSOCKET);
ctx.channel().close();
return;
}
if (sid != null && sid.get(0) != null) {
final UUID sessionId = UUID.fromString(sid.get(0));
handshake(ctx, sessionId, path, req);
} else {
ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
// first connection
handshake(ctx, client.getSessionId(), path, req);
}
} finally {
req.release();
}
} else {
ctx.fireChannelRead(msg);
}
} else {
ctx.fireChannelRead(msg);
}
}
use of io.netty.handler.codec.http.QueryStringDecoder in project riposte by Nike-Inc.
the class RequestInfoImplTest method uber_constructor_works_for_valid_values.
@Test
public void uber_constructor_works_for_valid_values() {
// given
String uri = "/some/uri/path/%24foobar%26?notused=blah";
HttpMethod method = HttpMethod.PATCH;
Charset contentCharset = CharsetUtil.US_ASCII;
HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=" + contentCharset.displayName());
QueryStringDecoder queryParams = new QueryStringDecoder(uri + "?foo=bar&secondparam=secondvalue");
Set<Cookie> cookies = new HashSet<>(Arrays.asList(new DefaultCookie("cookie1", "val1"), new DefaultCookie("cookie2", "val2")));
Map<String, String> pathParams = Arrays.stream(new String[][] { { "pathParam1", "val1" }, { "pathParam2", "val2" } }).collect(Collectors.toMap(pair -> pair[0], pair -> pair[1]));
String content = UUID.randomUUID().toString();
byte[] contentBytes = content.getBytes();
LastHttpContent chunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(contentBytes));
chunk.trailingHeaders().add("trailingHeader1", "trailingVal1");
List<HttpContent> contentChunks = Collections.singletonList(chunk);
HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
boolean keepAlive = true;
boolean fullRequest = true;
boolean isMultipart = false;
// when
RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(uri, method, headers, chunk.trailingHeaders(), queryParams, cookies, pathParams, contentChunks, protocolVersion, keepAlive, fullRequest, isMultipart);
// then
assertThat("getUri should return passed in value", requestInfo.getUri(), is(uri));
assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
assertThat(requestInfo.getMethod(), is(method));
assertThat(requestInfo.getHeaders(), is(headers));
assertThat(requestInfo.getTrailingHeaders(), is(chunk.trailingHeaders()));
assertThat(requestInfo.getQueryParams(), is(queryParams));
assertThat(requestInfo.getCookies(), is(cookies));
assertThat(requestInfo.pathTemplate, nullValue());
assertThat(requestInfo.pathParams, is(pathParams));
assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
assertThat(requestInfo.getRawContent(), is(content));
assertThat(requestInfo.content, nullValue());
assertThat(requestInfo.getContentCharset(), is(contentCharset));
assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
assertThat(requestInfo.isKeepAliveRequested(), is(keepAlive));
assertThat(requestInfo.isCompleteRequestWithAllChunks, is(fullRequest));
assertThat(requestInfo.isMultipart, is(isMultipart));
}
use of io.netty.handler.codec.http.QueryStringDecoder in project riposte by Nike-Inc.
the class RequestInfoImpl method dummyInstanceForUnknownRequests.
/**
* Creates a new RequestInfo that represents unknown requests. Usually only needed in error situations. The URI,
* query params, and headers will be tagged with {@link #NONE_OR_UNKNOWN_TAG} to indicate that the request was
* unknown.
*/
public static RequestInfoImpl<?> dummyInstanceForUnknownRequests() {
HttpHeaders headers = new DefaultHttpHeaders().set(NONE_OR_UNKNOWN_TAG, "true");
QueryStringDecoder queryParams = new QueryStringDecoder("/?" + NONE_OR_UNKNOWN_TAG + "=true");
return new RequestInfoImpl(NONE_OR_UNKNOWN_TAG, null, headers, null, queryParams, null, null, null, null, false, true, false);
}
use of io.netty.handler.codec.http.QueryStringDecoder in project riposte by Nike-Inc.
the class RequestInfoTest method getQueryParamSingle_returns_null_if_param_value_list_is_null.
@Test
public void getQueryParamSingle_returns_null_if_param_value_list_is_null() {
// given
QueryStringDecoder queryParamsMock = mock(QueryStringDecoder.class);
Map<String, List<String>> params = new HashMap<>();
doReturn(params).when(queryParamsMock).parameters();
RequestInfo<?> requestInfoSpy = getSpy();
doReturn(queryParamsMock).when(requestInfoSpy).getQueryParams();
// when
String value = requestInfoSpy.getQueryParamSingle("foo");
// then
assertThat(value, nullValue());
}
Aggregations