Java Code Examples for io.netty.handler.codec.http.HttpHeaders#entries()

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#entries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrowserMobHttpUtil.java    From CapturePacket with MIT License 5 votes vote down vote up
/**
 * Returns the size of the headers, including the 2 CRLFs at the end of the header block.
 *
 * @param headers headers to size
 * @return length of the headers, in bytes
 */
public static long getHeaderSize(HttpHeaders headers) {
    long headersSize = 0;
    for (Map.Entry<String, String> header : headers.entries()) {
        // +2 for ': ', +2 for new line
        headersSize += header.getKey().length() + header.getValue().length() + 4;
    }
    return headersSize;
}
 
Example 2
Source File: HttpHeaderUtils.java    From timely with Apache License 2.0 5 votes vote down vote up
public static Multimap<String, String> toMultimap(HttpHeaders headers) {
    Multimap<String, String> headerMultimap = HashMultimap.create();
    if (headers != null) {
        for (Map.Entry<String, String> e : headers.entries()) {
            // lower case for HTTP/2 compatibility; even for HTTP/1, these were always
            // case-insensitive
            headerMultimap.put(e.getKey().toLowerCase(), e.getValue());
        }
    }
    return headerMultimap;
}
 
Example 3
Source File: BrowserMobHttpUtil.java    From Dream-Catcher with MIT License 5 votes vote down vote up
/**
 * Returns the size of the headers, including the 2 CRLFs at the end of the header block.
 *
 * @param headers headers to size
 * @return length of the headers, in bytes
 */
public static long getHeaderSize(HttpHeaders headers) {
    long headersSize = 0;
    for (Map.Entry<String, String> header : headers.entries()) {
        // +2 for ': ', +2 for new line
        headersSize += header.getKey().length() + header.getValue().length() + 4;
    }
    return headersSize;
}
 
Example 4
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 5 votes vote down vote up
protected void captureHeaders(HttpHeaders headers) {
    Log.e("InnerHandle", "captureHeaders " + harEntry.getId());
    for (Map.Entry<String, String> header : headers.entries()) {
        harRequest.getRequest().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue()));
        harRequest.addHeader(header.getKey(), header.getValue());
    }
}
 
Example 5
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 5 votes vote down vote up
protected void captureResponseHeaders(HttpResponse httpResponse) {
    Log.e("InnerHandle", "captureResponseHeaders " + harEntry.getId());
    HttpHeaders headers = httpResponse.headers();
    for (Map.Entry<String, String> header : headers.entries()) {
        harResponse.getResponse().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue()));
        harResponse.addHeader(header.getKey(), header.getValue());
    }
}
 
Example 6
Source File: BrowserMobHttpUtil.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
/**
 * Returns the size of the headers, including the 2 CRLFs at the end of the header block.
 *
 * @param headers headers to size
 * @return length of the headers, in bytes
 */
public static long getHeaderSize(HttpHeaders headers) {
    long headersSize = 0;
    for (Map.Entry<String, String> header : headers.entries()) {
        // +2 for ': ', +2 for new line
        headersSize += header.getKey().length() + header.getValue().length() + 4;
    }
    return headersSize;
}
 
Example 7
Source File: NettyRequest.java    From ob1k with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> getHeaders() {
  final HttpHeaders headers = inner.headers();
  final List<Map.Entry<String, String>> entries = headers.entries();
  final Map<String, String> result = new HashMap<>();
  for (final Map.Entry<String, String> entry : entries) {
    result.put(entry.getKey(), entry.getValue());
  }

  return result;
}
 
Example 8
Source File: NettyRequest.java    From ob1k with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, List<String>> getAllHeaders() {
  final HttpHeaders headers = inner.headers();
  final List<Map.Entry<String, String>> entries = headers.entries();
  final Map<String, List<String>> result = new HashMap<>();

  for (final Map.Entry<String, String> entry : entries) {
    final List<String> list = result.computeIfAbsent(entry.getKey(), __ -> new ArrayList<>());
    list.add(entry.getValue());
  }

  return result;
}
 
Example 9
Source File: HttpUploadClient.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 **/
private static List<Entry<String, String>> formget(
        Bootstrap bootstrap, String host, int port, String get, URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();

    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue ���&");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");

    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaderNames.HOST, host);
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);

    headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaderNames.REFERER, uriSimple.toString());
    headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    //connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");

    headers.set(
            HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(
                    new DefaultCookie("my-cookie", "foo"),
                    new DefaultCookie("another-cookie", "bar"))
    );

    // send request
    channel.writeAndFlush(request);

    // Wait for the server to close the connection.
    channel.closeFuture().sync();

    // convert headers to list
    return headers.entries();
}
 
Example 10
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 4 votes vote down vote up
protected void captureHeaders(HttpHeaders headers) {
    for (Map.Entry<String, String> header : headers.entries()) {
        harEntry.getRequest().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue()));
    }
}
 
Example 11
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 4 votes vote down vote up
protected void captureResponseHeaders(HttpResponse httpResponse) {
    HttpHeaders headers = httpResponse.headers();
    for (Map.Entry<String, String> header : headers.entries()) {
        harEntry.getResponse().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue()));
    }
}
 
Example 12
Source File: HttpUploadClient.java    From tools-journey with Apache License 2.0 4 votes vote down vote up
/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 **/
private static List<Entry<String, String>> formget(
        Bootstrap bootstrap, String host, int port, String get, URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();

    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue ���&");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");

    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaderNames.HOST, host);
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);

    headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaderNames.REFERER, uriSimple.toString());
    headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    //connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");

    headers.set(
            HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(
                    new DefaultCookie("my-cookie", "foo"),
                    new DefaultCookie("another-cookie", "bar"))
    );

    // send request
    channel.writeAndFlush(request);

    // Wait for the server to close the connection.
    channel.closeFuture().sync();

    // convert headers to list
    return headers.entries();
}
 
Example 13
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
protected void captureHeaders(HttpHeaders headers) {
    for (Map.Entry<String, String> header : headers.entries()) {
        harEntry.getRequest().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue()));
    }
}
 
Example 14
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
protected void captureResponseHeaders(HttpResponse httpResponse) {
    HttpHeaders headers = httpResponse.headers();
    for (Map.Entry<String, String> header : headers.entries()) {
        harEntry.getResponse().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue()));
    }
}
 
Example 15
Source File: HttpUploadClient.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 **/
private static List<Entry<String, String>> formget(
        Bootstrap bootstrap, String host, int port, String get, URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();

    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue ���&");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");

    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaders.Names.HOST, host);
    headers.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    headers.set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP + ',' + HttpHeaders.Values.DEFLATE);

    headers.set(HttpHeaders.Names.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaders.Names.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaders.Names.REFERER, uriSimple.toString());
    headers.set(HttpHeaders.Names.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaders.Names.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

    //connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");

    headers.set(
            HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(
                    new DefaultCookie("my-cookie", "foo"),
                    new DefaultCookie("another-cookie", "bar"))
    );

    // send request
    List<Entry<String, String>> entries = headers.entries();
    channel.writeAndFlush(request);

    // Wait for the server to close the connection.
    channel.closeFuture().sync();

    return entries;
}