public static String getClientSessionId(HttpRequest req) { Set<Cookie> cookies; String value = req.headers().get(HttpHeaders.Names.COOKIE); if (value == null) { return null; } else { cookies = CookieDecoder.decode(value); } for (Cookie cookie : cookies) { if (cookie.getName().contains("JSESSIONID")) { return cookie.getValue(); } } return null; }
@Override public Cookie[] getCookies() { String cookieString = this.request.headers().get(COOKIE); if (cookieString != null) { Set<io.netty.handler.codec.http.Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { Cookie[] cookiesArray = new Cookie[cookies.size()]; int index = 0; for (io.netty.handler.codec.http.Cookie c : cookies) { Cookie cookie = new Cookie(c.getName(), c.getValue()); cookie.setComment(c.getComment()); if(c.getDomain()!=null)cookie.setDomain(c.getDomain()); cookie.setMaxAge((int) c.getMaxAge()); cookie.setPath(c.getPath()); cookie.setSecure(c.isSecure()); cookie.setVersion(c.getVersion()); cookiesArray[index] = cookie; index++; } return cookiesArray; } } return new Cookie[0]; }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (!request.getDecoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST); return; } String uri = request.getUri(); QueryStringDecoder decoder = new QueryStringDecoder(uri); SimpleHttpRequest req = new SimpleHttpRequest(decoder.path(), decoder.parameters()); String cookieString = request.headers().get(HttpHeaders.Names.COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); req.setCookies(cookies); } else { req.setCookies(Collections.emptySet()); } req.setHeaders(request.headers()); copyHttpBodyData(request, req); SimpleHttpResponse resp = eventHandler.apply(req); ctx.write( HttpEventHandler.buildHttpResponse(resp.toString(), resp.getStatus(), resp.getContentType()) ); ctx.flush().close(); }
void copyToServletCookie(FullHttpRequest fullHttpReq, MockHttpServletRequest servletRequest){ String cookieString = fullHttpReq.headers().get(HttpHeaders.Names.COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. javax.servlet.http.Cookie[] sCookies = new javax.servlet.http.Cookie[cookies.size()]; int i = 0; for (Cookie cookie: cookies) { javax.servlet.http.Cookie sCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue()); sCookie.setPath(cookie.getPath()); sCookie.setMaxAge((int) cookie.getMaxAge()); sCookies[i++] = sCookie; } servletRequest.setCookies(sCookies); } } else { servletRequest.setCookies( new javax.servlet.http.Cookie[0]); } }
@Override public Cookie[] getCookies() { String cookieString = getHeader(HttpHeaders.Names.COOKIE); if (cookieString != null) { Set<io.netty.handler.codec.http.Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { Cookie[] cookiecopy = new Cookie[cookies.size()]; int i = 0; for (io.netty.handler.codec.http.Cookie reqcookie : cookies) { cookiecopy[i] = new Cookie(reqcookie.getName(), reqcookie.getValue()); cookiecopy[i].setDomain(reqcookie.getDomain()); cookiecopy[i].setPath(reqcookie.getPath()); cookiecopy[i].setMaxAge((int) reqcookie.getMaxAge()); cookiecopy[i].setVersion(reqcookie.getVersion()); } return cookiecopy; } } return null; }
@Override public void addCookie(Cookie arg0) { String cookieString = m_nettyhttpresp.headers().get(HttpHeaders.Names.COOKIE); Set<io.netty.handler.codec.http.Cookie> cookies; if (cookieString != null) { cookies = CookieDecoder.decode(cookieString); cookies.add(new io.netty.handler.codec.http.DefaultCookie(arg0.getName(), arg0.getValue())); } else { cookies = new HashSet<io.netty.handler.codec.http.Cookie>(); cookies.add(new io.netty.handler.codec.http.DefaultCookie(arg0.getName(), arg0.getValue())); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. HttpHeaders.setHeader(m_nettyhttpresp, HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.encode(cookies)); } }
private synchronized Map<String, Set<Cookie>> _parseIfNeededAndGet() { if (cookiesParsed) { // This method is synchronized, so creates a memory barrier for this variable to be refreshed. return allCookies; } List<String> allCookieHeaders = nettyHeaders.getAll(cookiesHeaderName); Map<String, Set<Cookie>> cookies = new HashMap<String, Set<Cookie>>(); for (String aCookieHeader : allCookieHeaders) { Set<Cookie> decode = CookieDecoder.decode(aCookieHeader); for (Cookie cookie : decode) { Set<Cookie> existingCookiesOfName = cookies.get(cookie.getName()); if (null == existingCookiesOfName) { existingCookiesOfName = new HashSet<Cookie>(); cookies.put(cookie.getName(), existingCookiesOfName); } existingCookiesOfName.add(cookie); } } allCookies = Collections.unmodifiableMap(cookies); cookiesParsed = true; return allCookies; }
@Test public void testSetCookie() throws Exception { DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, ""); String cookie1Name = "PREF"; String cookie1Value = "ID=a95756377b78e75e:FF=0:TM=1392709628:LM=1392709628:S=a5mOVvTB7DBkexgi"; String cookie1Domain = ".google.com"; String cookie1Path = "/"; Cookie cookie = new DefaultCookie(cookie1Name, cookie1Value); cookie.setPath(cookie1Path); cookie.setDomain(cookie1Domain); new HttpClientRequest<ByteBuf>(nettyRequest).withCookie(cookie); String cookieHeader = nettyRequest.headers().get(HttpHeaders.Names.COOKIE); Assert.assertNotNull("No cookie header found.", cookieHeader); Set<Cookie> decodeCookies = CookieDecoder.decode(cookieHeader); Assert.assertNotNull("No cookie found with name.", decodeCookies); Assert.assertEquals("Unexpected number of cookies.", 1, decodeCookies.size()); Cookie decodedCookie = decodeCookies.iterator().next(); Assert.assertEquals("Unexpected cookie name.", cookie1Name, decodedCookie.getName()); Assert.assertEquals("Unexpected cookie path.", cookie1Path, decodedCookie.getPath()); Assert.assertEquals("Unexpected cookie domain.", cookie1Domain, decodedCookie.getDomain()); }
@Test public void testSetCookie() throws Exception { DefaultHttpResponse nettyResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); HttpServerResponse<ByteBuf> response = new HttpServerResponse<ByteBuf>(new NoOpChannelHandlerContext(), nettyResponse); String cookieName = "name"; String cookieValue = "value"; response.addCookie(new DefaultCookie(cookieName, cookieValue)); String cookieHeader = nettyResponse.headers().get(HttpHeaders.Names.SET_COOKIE); Assert.assertNotNull("Cookie header not found.", cookieHeader); Set<Cookie> decode = CookieDecoder.decode(cookieHeader); Assert.assertNotNull("Decoded cookie not found.", decode); Assert.assertEquals("Unexpected number of decoded cookie not found.", 1, decode.size()); Cookie cookie = decode.iterator().next(); Assert.assertEquals("Unexpected cookie name.", cookieName, cookie.getName()); Assert.assertEquals("Unexpected cookie value.", cookieValue, cookie.getValue()); }
void setCookie(HttpRequest request, FullHttpResponse response) { // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { if("visit-count".equals(cookie.getName())) { int count = Integer.parseInt(cookie.getValue()) ; cookie.setValue(Integer.toString(count + 1)); //System.out.println("Visit: " + count); } response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("id", UUID.randomUUID().toString())); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("visit-count", "1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("first-visit-time", new Date().toString())); } }
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.getDecoderResult().isSuccess()? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(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(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.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(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(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(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
/** * 填充头部信息和Cookie信息 * * @param headers * HttpHeaders */ private void buildHeadersAndCookies(HttpHeaders headers) { for (Entry<String, String> entry : headers) { this.headers.put(entry.getKey(), entry.getValue()); } final String cookieString = this.headers.get(Names.COOKIE.toString()); if (StringUtils.isNotBlank(cookieString)) { final Set<Cookie> cookies = CookieDecoder.decode(cookieString); for (Cookie cookie : cookies) { this.cookies.put(cookie.getName(), cookie); } } }
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.getDecoderResult().isSuccess()? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(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(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(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(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.headers().set(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(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
private void populateCookies() { cookies = Maps.newHashMap(); final String cookieHeaderValue = inner.headers().get("Cookie"); if (cookieHeaderValue == null) return; final Set<Cookie> cookiesSet = CookieDecoder.decode(cookieHeaderValue, false); for (final Cookie cookie : cookiesSet) { cookies.put(cookie.getName(), cookie); } }
public static String get(String name, HttpServerRequest request) { if (request.headers().get("Cookie") != null) { Set<Cookie> cookies = CookieDecoder.decode(request.headers().get("Cookie")); for (Cookie c : cookies) { if (c.getName().equals(name)) { return c.getValue(); } } } return null; }
public String getSigned(String name, String path, HttpServerRequest request) { if (request.headers().get("Cookie") != null) { Set<Cookie> cookies = CookieDecoder.decode(request.headers().get("Cookie")); return getSignedCookie(name, path, cookies); } return null; }
public String getSigned(String name, String path, ServerWebSocket ws) { if (ws.headers().get("Cookie") != null) { Set<Cookie> cookies = CookieDecoder.decode(ws.headers().get("Cookie")); return getSignedCookie(name, path, cookies); } return null; }
private String readAuthentication(HttpRequest request) { String requestCookie = request.headers().get("Cookie"); if (requestCookie == null) { return null; } Set<Cookie> cookies = CookieDecoder.decode(requestCookie); for (Cookie cookie : cookies) { if ("who-am-i".equals(cookie.getName())) { return cookie.getValue(); } } return null; }
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0); // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION)); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().set(CONTENT_LENGTH, buf.readableBytes()); } Set<Cookie> cookies; String value = request.headers().get(COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = CookieDecoder.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } // Write the response. ChannelFuture future = channel.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0); // Decide whether to close the connection or not. boolean close = request.headers().contains(CONNECTION, HttpHeaders.Values.CLOSE, true) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !request.headers().contains(CONNECTION, HttpHeaders.Values.KEEP_ALIVE, true); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().set(CONTENT_LENGTH, buf.readableBytes()); } Set<Cookie> cookies; String value = request.headers().get(COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = CookieDecoder.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } // Write the response. ChannelFuture future = channel.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }