List of usage examples for io.netty.handler.codec.http HttpRequest getUri
@Deprecated String getUri();
From source file:com.bala.learning.learning.netty.HttpServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest request = this.request = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx);/*from w ww. j ava 2 s . c o m*/ } buf.setLength(0); buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n"); buf.append("===================================\r\n"); buf.append("VERSION: ").append(request.getProtocolVersion()).append("\r\n"); buf.append("HOSTNAME: ").append(HttpHeaders.getHost(request, "unknown")).append("\r\n"); buf.append("REQUEST_URI: ").append(request.getUri()).append("\r\n\r\n"); HttpHeaders headers = request.headers(); if (!headers.isEmpty()) { for (Map.Entry<String, String> h : headers) { String key = h.getKey(); String value = h.getValue(); buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n"); } buf.append("\r\n"); } QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); Map<String, List<String>> params = queryStringDecoder.parameters(); if (!params.isEmpty()) { for (Entry<String, List<String>> p : params.entrySet()) { String key = p.getKey(); List<String> vals = p.getValue(); for (String val : vals) { buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n"); } } buf.append("\r\n"); } appendDecoderResult(buf, request); } if (msg instanceof HttpContent) { HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { buf.append("CONTENT: "); buf.append(content.toString(CharsetUtil.UTF_8)); buf.append("\r\n"); appendDecoderResult(buf, request); } if (msg instanceof LastHttpContent) { buf.append("END OF CONTENT\r\n"); LastHttpContent trailer = (LastHttpContent) msg; if (!trailer.trailingHeaders().isEmpty()) { buf.append("\r\n"); for (String name : trailer.trailingHeaders().names()) { for (String value : trailer.trailingHeaders().getAll(name)) { buf.append("TRAILING HEADER: "); buf.append(name).append(" = ").append(value).append("\r\n"); } } buf.append("\r\n"); } if (!writeResponse(trailer, ctx)) { // If keep-alive is off, close the connection once the content is fully written. ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } } } }
From source file:com.buildria.mocking.stub.Call.java
License:Open Source License
public static Call fromRequest(HttpRequest req) { Objects.requireNonNull(req);/* www. j a v a 2 s. c o m*/ Call call = new Call(); call.method = req.getMethod().name(); QueryStringDecoder decoder = new QueryStringDecoder(req.getUri()); call.path = QueryStringDecoder.decodeComponent(decoder.path()); Map<String, List<String>> params = decoder.parameters(); for (String name : params.keySet()) { List<String> values = params.get(name); for (String value : values) { call.parameters.add(new Pair(name, value)); } } HttpHeaders headers = req.headers(); for (String name : headers.names()) { List<String> values = headers.getAll(name); for (String value : values) { call.headers.add(new Pair(name, value)); } if (CONTENT_TYPE.equalsIgnoreCase(name)) { call.contentType = MediaType.parse(headers.get(CONTENT_TYPE)); } } if (req instanceof ByteBufHolder) { ByteBuf buf = ((ByteBufHolder) req).content(); if (buf != null) { call.body = new byte[buf.readableBytes()]; buf.readBytes(call.body); } } return call; }
From source file:com.buildria.mocking.stub.CallTest.java
License:Open Source License
@Test public void testFromRequestNoContent() throws Exception { HttpRequest req = mock(HttpRequest.class); when(req.getUri()).thenReturn("/api/p?name=%E3%81%82"); when(req.getMethod()).thenReturn(GET); HttpHeaders headers = new DefaultHttpHeaders(); headers.add("key", "value"); when(req.headers()).thenReturn(headers); Call call = Call.fromRequest(req);/*from w w w . j a va2 s .c o m*/ assertThat(call.getBody().length, is(0)); }
From source file:com.chenyang.proxy.http.HttpSchemaHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext uaChannelCtx, final Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) msg; String originalHost = HostNamePortUtil.getHostName(httpRequest); int originalPort = HostNamePortUtil.getPort(httpRequest); HttpRemote apnProxyRemote = new HttpRemote(originalHost, originalPort); if (!HostAuthenticationUtil.isValidAddress(apnProxyRemote.getInetSocketAddress())) { HttpErrorUtil.writeAndFlush(uaChannelCtx.channel(), HttpResponseStatus.FORBIDDEN); return; }// w w w. j av a 2 s.c o m Channel uaChannel = uaChannelCtx.channel(); HttpConnectionAttribute apnProxyConnectionAttribute = HttpConnectionAttribute.build( uaChannel.remoteAddress().toString(), httpRequest.getMethod().name(), httpRequest.getUri(), httpRequest.getProtocolVersion().text(), httpRequest.headers().get(HttpHeaders.Names.USER_AGENT), apnProxyRemote); uaChannelCtx.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute); uaChannel.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute); if (httpRequest.getMethod().equals(HttpMethod.CONNECT)) { if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) != null) { uaChannelCtx.pipeline().remove(HttpUserAgentForwardHandler.HANDLER_NAME); } if (uaChannelCtx.pipeline().get(HttpUserAgentTunnelHandler.HANDLER_NAME) == null) { uaChannelCtx.pipeline().addLast(HttpUserAgentTunnelHandler.HANDLER_NAME, new HttpUserAgentTunnelHandler()); } } else { if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) == null) { uaChannelCtx.pipeline().addLast(HttpUserAgentForwardHandler.HANDLER_NAME, new HttpUserAgentForwardHandler()); } } } uaChannelCtx.fireChannelRead(msg); }
From source file:com.chenyang.proxy.http.HttpUserAgentForwardHandler.java
License:Apache License
private HttpRequest constructRequestForProxy(HttpRequest httpRequest, HttpRemote apnProxyRemote) { String uri = httpRequest.getUri(); uri = this.getPartialUrl(uri); HttpRequest _httpRequest = new DefaultHttpRequest(httpRequest.getProtocolVersion(), httpRequest.getMethod(), uri);//from ww w . jav a2s . co m Set<String> headerNames = httpRequest.headers().names(); for (String headerName : headerNames) { if (StringUtils.equalsIgnoreCase(headerName, "Proxy-Connection")) { _httpRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } else { _httpRequest.headers().add(headerName, httpRequest.headers().getAll(headerName)); } } Iterator<Entry<String, String>> iterator = _httpRequest.headers().iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); logger.info(" heard : {} {}", entry.getKey(), entry.getValue()); } return _httpRequest; }
From source file:com.cisco.oss.foundation.http.netlifx.netty.NettyNetflixHttpClient.java
License:Apache License
private URI buildUri(HttpRequest request, Joiner joiner) { URI requestUri = request.getUri(); Map<String, Collection<String>> queryParams = request.getQueryParams(); if (queryParams != null && !queryParams.isEmpty()) { URIBuilder uriBuilder = new URIBuilder(); StringBuilder queryStringBuilder = new StringBuilder(); boolean hasQuery = !queryParams.isEmpty(); for (Map.Entry<String, Collection<String>> stringCollectionEntry : queryParams.entrySet()) { String key = stringCollectionEntry.getKey(); Collection<String> queryParamsValueList = stringCollectionEntry.getValue(); if (request.isQueryParamsParseAsMultiValue()) { for (String queryParamsValue : queryParamsValueList) { uriBuilder.addParameter(key, queryParamsValue); queryStringBuilder.append(key).append("=").append(queryParamsValue).append("&"); }//from w w w . jav a 2s .c o m } else { String value = joiner.join(queryParamsValueList); uriBuilder.addParameter(key, value); queryStringBuilder.append(key).append("=").append(value).append("&"); } } uriBuilder.setFragment(requestUri.getFragment()); uriBuilder.setHost(requestUri.getHost()); uriBuilder.setPath(requestUri.getPath()); uriBuilder.setPort(requestUri.getPort()); uriBuilder.setScheme(requestUri.getScheme()); uriBuilder.setUserInfo(requestUri.getUserInfo()); try { if (!autoEncodeUri) { String urlPath = ""; if (requestUri.getRawPath() != null && requestUri.getRawPath().startsWith("/")) { urlPath = requestUri.getRawPath(); } else { urlPath = "/" + requestUri.getRawPath(); } if (hasQuery) { String query = queryStringBuilder.substring(0, queryStringBuilder.length() - 1); requestUri = new URI(requestUri.getScheme() + "://" + requestUri.getHost() + ":" + requestUri.getPort() + urlPath + "?" + query); } else { requestUri = new URI(requestUri.getScheme() + "://" + requestUri.getHost() + ":" + requestUri.getPort() + urlPath); } } else { requestUri = uriBuilder.build(); } } catch (URISyntaxException e) { LOGGER.warn("could not update uri: {}", requestUri); } } return requestUri; }
From source file:com.cognifide.qa.bb.proxy.analyzer.predicate.RequestPredicateImpl.java
License:Apache License
@Override public boolean accepts(HttpRequest request) { boolean result = false; URI uri = URI.create(request.getUri()); String path = uri.getPath();/* w w w . j ava2 s .c o m*/ if (path != null && path.startsWith(urlPrefix)) { String query = uri.getQuery(); if (expectedParams.isEmpty() && StringUtils.isEmpty(query)) { result = true; } else if (StringUtils.isNotEmpty(query)) { List<NameValuePair> params = URLEncodedUtils.parse(query, Charsets.UTF_8); result = hasAllExpectedParams(expectedParams, params); } } return result; }
From source file:com.cognifide.qa.bb.proxy.analyzer.predicate.RequestPredicateImplTest.java
License:Apache License
private HttpRequest createMockedHttpRequest(String path, String queryString) { HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS); when(request.getUri()).thenReturn(path + "?" + queryString); return request; }
From source file:com.company.product.test.http.HttpServerHandler.java
License:Apache License
protected void messageReceived(ChannelHandlerContext ctx, Object msg) { FullHttpResponse response = null;//from w ww .j a v a 2s. c om try { if (msg instanceof HttpRequest) { HttpRequest request = (HttpRequest) msg; QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); Map<String, List<String>> params = queryStringDecoder.parameters(); validateQueryString(params); compileAndSendMessage(params); // no need to provide a response, we did what was expected of us. response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); } } catch (IllegalArgumentException iae) { response = new DefaultFullHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR); throw iae; } catch (InvalidTopicException ite) { response = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST); throw ite; } finally { ctx.write(response); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); ReferenceCountUtil.release(msg); } }
From source file:com.corundumstudio.socketio.transport.WebSocketTransport.java
License:Apache License
private String getWebSocketLocation(HttpRequest req) { String protocol = "ws://"; if (isSsl) {/*w ww . ja va 2 s . c om*/ protocol = "wss://"; } return protocol + req.headers().get(HttpHeaders.Names.HOST) + req.getUri(); }