List of usage examples for io.netty.handler.codec.http HttpHeaders get
public String get(CharSequence name)
From source file:br.unifei.edu.eco009.steamlansync.proxy.HttpResponseContent.java
public HttpResponseContent(FullHttpResponse originalResponse) { bytes = new byte[originalResponse.content().capacity()]; originalResponse.content().getBytes(0, bytes); HttpHeaders headers = originalResponse.headers(); length = headers.get("Content-Length"); steam_sid = headers.get("x-steam-sid"); crc = headers.get("x-content-crc"); }
From source file:co.paralleluniverse.comsat.webactors.netty.HttpRequestWrapper.java
License:Open Source License
static Set<io.netty.handler.codec.http.cookie.Cookie> getNettyCookies(FullHttpRequest req) { final HttpHeaders heads = req.headers(); final String head = heads != null ? heads.get(HttpHeaders.Names.COOKIE) : null; if (head != null) return ServerCookieDecoder.LAX.decode(head); else/*from w ww . j ava 2 s . c om*/ return EMPTY_SET; }
From source file:com.basho.riak.client.util.http.HttpMultipart.java
License:Apache License
public static List<HttpMultipart.Part> parse(HttpHeaders headers, byte[] content) { if (!(content.length >= 2 && content[0] == '\r' && content[1] == '\n')) { // In order to parse the multipart efficiently, we want to treat the // first boundary identically to the others, so make sure that the // first boundary is preceded by a '\r\n' like the others byte[] newContent = new byte[content.length + 2]; newContent[0] = '\r'; newContent[1] = '\n'; System.arraycopy(content, 0, newContent, 2, content.length); content = newContent;// w w w. ja v a 2s . c o m } String boundary = "\r\n--" + getBoundary(headers.get(Constants.HDR_CONTENT_TYPE)); byte[] boundaryBytes = CharsetUtils.asBytes(boundary, CharsetUtils.ISO_8859_1); int boundarySize = boundary.length(); if ("\r\n--".equals(boundary)) { return null; } // While this parsing could be more efficiently done in one pass with a // hand written FSM, hopefully this method is more readable/intuitive. List<Part> parts = new ArrayList<Part>(); int pos = indexOf(content, boundaryBytes, 0); if (pos != -1) { while (pos < content.length) { // first char of part int start = pos + boundarySize; // last char of part + 1 int end = indexOf(content, boundaryBytes, start); // end of header section + 1 int headerEnd = indexOf(content, HEADER_DELIM, pos); // start of body section int bodyStart = headerEnd + HEADER_DELIM.length; // check for end boundary, which is (boundary + "--") if (content.length >= (start + 2) && content[start] == '-' && content[start + 1] == '-') { break; } if (end == -1) { end = content.length; } if (headerEnd == -1) { headerEnd = content.length; bodyStart = end; } if (bodyStart > end) { bodyStart = end; } Map<String, String> partHeaders = parseHeaders( CharsetUtils.asASCIIString(Arrays.copyOfRange(content, start, headerEnd))); parts.add(new Part(partHeaders, Arrays.copyOfRange(content, bodyStart, end))); pos = end; } } return parts; }
From source file:com.buildria.mocking.stub.Call.java
License:Open Source License
public static Call fromRequest(HttpRequest req) { Objects.requireNonNull(req);/*w w w .ja 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.chiorichan.http.HttpResponseWrapper.java
License:Mozilla Public License
public void sendMultipart(byte[] bytesToWrite) throws IOException { if (request.method() == HttpMethod.HEAD) throw new IllegalStateException("You can't start MULTIPART mode on a HEAD Request."); if (stage != HttpResponseStage.MULTIPART) { stage = HttpResponseStage.MULTIPART; HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); HttpHeaders h = response.headers(); try {/* w ww. j a v a 2s . c om*/ request.getSession().save(); } catch (SessionException e) { e.printStackTrace(); } for (HttpCookie c : request.getCookies()) if (c.needsUpdating()) h.add("Set-Cookie", c.toHeaderValue()); if (h.get("Server") == null) h.add("Server", Versioning.getProduct() + " Version " + Versioning.getVersion()); h.add("Access-Control-Allow-Origin", request.getLocation().getConfig().getString("site.web-allowed-origin", "*")); h.add("Connection", "close"); h.add("Cache-Control", "no-cache"); h.add("Cache-Control", "private"); h.add("Pragma", "no-cache"); h.set("Content-Type", "multipart/x-mixed-replace; boundary=--cwsframe"); // if ( isKeepAlive( request ) ) { // response.headers().set( CONNECTION, HttpHeaders.Values.KEEP_ALIVE ); } request.getChannel().write(response); } else { StringBuilder sb = new StringBuilder(); sb.append("--cwsframe\r\n"); sb.append("Content-Type: " + httpContentType + "\r\n"); sb.append("Content-Length: " + bytesToWrite.length + "\r\n\r\n"); ByteArrayOutputStream ba = new ByteArrayOutputStream(); ba.write(sb.toString().getBytes(encoding)); ba.write(bytesToWrite); ba.flush(); ChannelFuture sendFuture = request.getChannel().write( new ChunkedStream(new ByteArrayInputStream(ba.toByteArray())), request.getChannel().newProgressivePromise()); ba.close(); sendFuture.addListener(new ChannelProgressiveFutureListener() { @Override public void operationComplete(ChannelProgressiveFuture future) throws Exception { NetworkManager.getLogger().info("Transfer complete."); } @Override public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) { if (total < 0) NetworkManager.getLogger().info("Transfer progress: " + progress); else NetworkManager.getLogger().info("Transfer progress: " + progress + " / " + total); } }); } }
From source file:com.chiorichan.http.HttpResponseWrapper.java
License:Mozilla Public License
/** * Sends the data to the client. Internal Use. * * @throws IOException/*from w w w . ja v a2 s. c om*/ * if there was a problem sending the data, like the connection was unexpectedly closed. */ public void sendResponse() throws IOException { if (stage == HttpResponseStage.CLOSED || stage == HttpResponseStage.WRITTEN) return; FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpStatus, output); HttpHeaders h = response.headers(); if (request.hasSession()) { Session session = request.getSession(); /** * Initiate the Session Persistence Method. * This is usually done with a cookie but we should make a param optional */ session.processSessionCookie(request.getDomain()); for (HttpCookie c : session.getCookies().values()) if (c.needsUpdating()) h.add("Set-Cookie", c.toHeaderValue()); if (session.getSessionCookie().needsUpdating()) h.add("Set-Cookie", session.getSessionCookie().toHeaderValue()); } if (h.get("Server") == null) h.add("Server", Versioning.getProduct() + " Version " + Versioning.getVersion()); // This might be a temporary measure - TODO Properly set the charset for each request. h.set("Content-Type", httpContentType + "; charset=" + encoding.name()); h.add("Access-Control-Allow-Origin", request.getLocation().getConfig().getString("site.web-allowed-origin", "*")); for (Entry<String, String> header : headers.entrySet()) h.add(header.getKey(), header.getValue()); // Expires: Wed, 08 Apr 2015 02:32:24 GMT // DateTimeFormatter formatter = DateTimeFormat.forPattern( "EE, dd-MMM-yyyy HH:mm:ss zz" ); // h.set( HttpHeaders.Names.EXPIRES, formatter.print( DateTime.now( DateTimeZone.UTC ).plusDays( 1 ) ) ); // h.set( HttpHeaders.Names.CACHE_CONTROL, "public, max-age=86400" ); h.setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); h.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); stage = HttpResponseStage.WRITTEN; request.getChannel().writeAndFlush(response); }
From source file:com.github.ambry.admin.AdminIntegrationTest.java
License:Open Source License
/** * Gets the headers of the blob with blob ID {@code blobId} and verifies them against what is expected. * @param blobId the blob ID of the blob to HEAD. * @param expectedHeaders the expected headers in the response. * @throws ExecutionException/*from w ww . j a v a 2 s .c om*/ * @throws InterruptedException */ private void getHeadAndVerify(String blobId, HttpHeaders expectedHeaders) throws ExecutionException, InterruptedException { FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, null, null); Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); HttpResponse response = (HttpResponse) responseParts.poll(); discardContent(responseParts, 1); assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus()); checkCommonGetHeadHeaders(response.headers(), expectedHeaders); assertEquals("Content-Length does not match blob size", Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE)), HttpHeaders.getContentLength(response)); assertEquals("Blob size does not match", expectedHeaders.get(RestUtils.Headers.BLOB_SIZE), HttpHeaders.getHeader(response, RestUtils.Headers.BLOB_SIZE)); assertEquals(RestUtils.Headers.SERVICE_ID + " does not match", expectedHeaders.get(RestUtils.Headers.SERVICE_ID), HttpHeaders.getHeader(response, RestUtils.Headers.SERVICE_ID)); assertEquals(RestUtils.Headers.PRIVATE + " does not match", expectedHeaders.get(RestUtils.Headers.PRIVATE), HttpHeaders.getHeader(response, RestUtils.Headers.PRIVATE)); assertEquals(RestUtils.Headers.AMBRY_CONTENT_TYPE + " does not match", expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE), HttpHeaders.getHeader(response, RestUtils.Headers.AMBRY_CONTENT_TYPE)); assertTrue("No " + RestUtils.Headers.CREATION_TIME, HttpHeaders.getHeader(response, RestUtils.Headers.CREATION_TIME, null) != null); if (Long.parseLong(expectedHeaders.get(RestUtils.Headers.TTL)) != Utils.Infinite_Time) { assertEquals(RestUtils.Headers.TTL + " does not match", expectedHeaders.get(RestUtils.Headers.TTL), HttpHeaders.getHeader(response, RestUtils.Headers.TTL)); } if (expectedHeaders.contains(RestUtils.Headers.OWNER_ID)) { assertEquals(RestUtils.Headers.OWNER_ID + " does not match", expectedHeaders.get(RestUtils.Headers.OWNER_ID), HttpHeaders.getHeader(response, RestUtils.Headers.OWNER_ID)); } }
From source file:com.github.ambry.admin.AdminIntegrationTest.java
License:Open Source License
/** * Checks headers that are common to HEAD and GET. * @param receivedHeaders the {@link HttpHeaders} that were received. * @param expectedHeaders the expected headers. *//*from w ww.j a va 2s . c o m*/ private void checkCommonGetHeadHeaders(HttpHeaders receivedHeaders, HttpHeaders expectedHeaders) { assertEquals("Content-Type does not match", expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE), receivedHeaders.get(HttpHeaders.Names.CONTENT_TYPE)); assertTrue("No Date header", receivedHeaders.get(HttpHeaders.Names.DATE) != null); assertTrue("No Last-Modified header", receivedHeaders.get(HttpHeaders.Names.LAST_MODIFIED) != null); assertEquals(RestUtils.Headers.BLOB_SIZE + " does not match", expectedHeaders.get(RestUtils.Headers.BLOB_SIZE), receivedHeaders.get(RestUtils.Headers.BLOB_SIZE)); }
From source file:com.github.ambry.frontend.FrontendIntegrationTest.java
License:Open Source License
/** * Gets the blob with blob ID {@code blobId} and verifies that the headers and content match with what is expected. * @param blobId the blob ID of the blob to GET. * @param expectedHeaders the expected headers in the response. * @param expectedContent the expected content of the blob. * @throws ExecutionException/*from ww w.j a va 2 s . com*/ * @throws InterruptedException */ private void getBlobAndVerify(String blobId, HttpHeaders expectedHeaders, ByteBuffer expectedContent) throws ExecutionException, InterruptedException { FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, null, null); Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); HttpResponse response = (HttpResponse) responseParts.poll(); assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus()); checkCommonGetHeadHeaders(response.headers()); assertEquals("Content-Type does not match", expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE), response.headers().get(HttpHeaders.Names.CONTENT_TYPE)); assertEquals(RestUtils.Headers.BLOB_SIZE + " does not match", expectedHeaders.get(RestUtils.Headers.BLOB_SIZE), response.headers().get(RestUtils.Headers.BLOB_SIZE)); ByteBuffer responseContent = getContent(response, responseParts); assertArrayEquals("GET content does not match original content", expectedContent.array(), responseContent.array()); assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response)); }
From source file:com.github.ambry.frontend.FrontendIntegrationTest.java
License:Open Source License
/** * Gets the headers of the blob with blob ID {@code blobId} and verifies them against what is expected. * @param blobId the blob ID of the blob to HEAD. * @param expectedHeaders the expected headers in the response. * @throws ExecutionException/*from ww w . ja v a 2 s . c o m*/ * @throws InterruptedException */ private void getHeadAndVerify(String blobId, HttpHeaders expectedHeaders) throws ExecutionException, InterruptedException { FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, null, null); Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); HttpResponse response = (HttpResponse) responseParts.poll(); assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus()); checkCommonGetHeadHeaders(response.headers()); assertEquals(RestUtils.Headers.CONTENT_LENGTH + " does not match " + RestUtils.Headers.BLOB_SIZE, Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE)), HttpHeaders.getContentLength(response)); assertEquals(RestUtils.Headers.CONTENT_TYPE + " does not match " + RestUtils.Headers.AMBRY_CONTENT_TYPE, expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE), HttpHeaders.getHeader(response, HttpHeaders.Names.CONTENT_TYPE)); verifyBlobProperties(expectedHeaders, response); discardContent(responseParts, 1); assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response)); }