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

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#getHeader() . 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: HttpObjectUtil.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the entity body from a FullHttpMessage, according to the character set in the message's Content-Type header. If the Content-Type
 * header is not present or does not specify a charset, assumes the ISO-8859-1 character set (see {@link BrowserUpHttpUtil#DEFAULT_HTTP_CHARSET}).
 *
 * @param httpMessage HTTP message to extract entity body from
 * @return String representation of the entity body
 * @throws java.nio.charset.UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static String extractHttpEntityBody(FullHttpMessage httpMessage) {
    Charset charset;
    try {
        charset = getCharsetFromMessage(httpMessage);
    } catch (UnsupportedCharsetException e) {
        // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception
        // to alert the client code.
        java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause();

        String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaderNames.CONTENT_TYPE);
        log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause);

        throw cause;
    }

    return extractHttpEntityBody(httpMessage, charset);
}
 
Example 2
Source File: HttpObjectUtil.java    From Dream-Catcher with MIT License 6 votes vote down vote up
/**
 * Extracts the entity body from a FullHttpMessage, according to the character set in the message's Content-Type header. If the Content-Type
 * header is not present or does not specify a charset, assumes the ISO-8859-1 character set (see {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET}).
 *
 * @param httpMessage HTTP message to extract entity body from
 * @return String representation of the entity body
 * @throws java.nio.charset.UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static String extractHttpEntityBody(FullHttpMessage httpMessage) {
    Charset charset;
    try {
        charset = getCharsetFromMessage(httpMessage);
    } catch (UnsupportedCharsetException e) {
        // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception
        // to alert the client code.
        java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause();

        String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);
        log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause);

        throw cause;
    }

    return extractHttpEntityBody(httpMessage, charset);
}
 
Example 3
Source File: HttpObjectUtil.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
/**
 * Extracts the entity body from a FullHttpMessage, according to the character set in the message's Content-Type header. If the Content-Type
 * header is not present or does not specify a charset, assumes the ISO-8859-1 character set (see {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET}).
 *
 * @param httpMessage HTTP message to extract entity body from
 * @return String representation of the entity body
 * @throws java.nio.charset.UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static String extractHttpEntityBody(FullHttpMessage httpMessage) {
    Charset charset;
    try {
        charset = getCharsetFromMessage(httpMessage);
    } catch (UnsupportedCharsetException e) {
        // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception
        // to alert the client code.
        java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause();

        String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);
        log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause);

        throw cause;
    }

    return extractHttpEntityBody(httpMessage, charset);
}
 
Example 4
Source File: HttpObjectUtil.java    From CapturePacket with MIT License 6 votes vote down vote up
/**
 * Extracts the entity body from a FullHttpMessage, according to the character set in the message's Content-Type header. If the Content-Type
 * header is not present or does not specify a charset, assumes the ISO-8859-1 character set (see {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET}).
 *
 * @param httpMessage HTTP message to extract entity body from
 * @return String representation of the entity body
 * @throws java.nio.charset.UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static String extractHttpEntityBody(FullHttpMessage httpMessage) {
    Charset charset;
    try {
        charset = getCharsetFromMessage(httpMessage);
    } catch (UnsupportedCharsetException e) {
        // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception
        // to alert the client code.
        java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause();

        String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);
        log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause);

        throw cause;
    }

    return extractHttpEntityBody(httpMessage, charset);
}
 
Example 5
Source File: HttpObjectUtil.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Derives the charset from the Content-Type header in the HttpMessage. If the Content-Type header is not present or does not contain
 * a character set, this method returns the ISO-8859-1 character set. See {@link BrowserUpHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return the charset associated with the HTTP message, or the default charset if none is present
 * @throws UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaderNames.CONTENT_TYPE);

    Charset charset = BrowserUpHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserUpHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
Example 6
Source File: HttpObjectUtil.java    From Dream-Catcher with MIT License 5 votes vote down vote up
/**
 * Derives the charset from the Content-Type header in the HttpMessage. If the Content-Type header is not present or does not contain
 * a character set, this method returns the ISO-8859-1 character set. See {@link BrowserMobHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return the charset associated with the HTTP message, or the default charset if none is present
 * @throws UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);

    Charset charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
Example 7
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
protected void captureResponseMimeType(HttpResponse httpResponse) {
    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    // don't set the mimeType to null, since mimeType is a required field
    if (contentType != null) {
        harEntry.getResponse().getContent().setMimeType(contentType);
    }
}
 
Example 8
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
protected void captureResponseContent(HttpResponse httpResponse, byte[] fullMessage) {
    // force binary if the content encoding is not supported
    boolean forceBinary = false;

    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in response from {}. Content will be treated as {}", originalRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    if (responseCaptureFilter.isResponseCompressed() && !responseCaptureFilter.isDecompressionSuccessful()) {
        log.warn("Unable to decompress content with encoding: {}. Contents will be encoded as base64 binary data.", responseCaptureFilter.getContentEncoding());

        forceBinary = true;
    }

    Charset charset;
    try {
        charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP response from {}. Content will not be captured in HAR.", contentType, originalRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents from {}", charset, originalRequest.getUri());
    }

    if (!forceBinary && BrowserMobHttpUtil.hasTextualContent(contentType)) {
        String text = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getResponse().getContent().setText(text);
    } else if (dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        harEntry.getResponse().getContent().setText(BaseEncoding.base64().encode(fullMessage));
        harEntry.getResponse().getContent().setEncoding("base64");
    }

    harEntry.getResponse().getContent().setSize(fullMessage.length);
}
 
Example 9
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 5 votes vote down vote up
protected void captureRedirectUrl(HttpResponse httpResponse) {
    Log.e("InnerHandle", "captureRedirectUrl " + harEntry.getId());
    String locationHeaderValue = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.LOCATION);
    if (locationHeaderValue != null) {
        harResponse.getResponse().setRedirectURL(locationHeaderValue);
    }
}
 
Example 10
Source File: HttpMessageContents.java    From CapturePacket with MIT License 5 votes vote down vote up
/**
 * Retrieves the Content-Type header of this message. If no Content-Type is present, returns the assumed default Content-Type (see
 * {@link BrowserMobHttpUtil#UNKNOWN_CONTENT_TYPE}).
 *
 * @return the message's content type
 */
public String getContentType() {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);
    if (contentTypeHeader == null || contentTypeHeader.isEmpty()) {
        return BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    } else {
        return contentTypeHeader;
    }
}
 
Example 11
Source File: HttpObjectUtil.java    From CapturePacket with MIT License 5 votes vote down vote up
/**
 * Derives the charset from the Content-Type header in the HttpMessage. If the Content-Type header is not present or does not contain
 * a character set, this method returns the ISO-8859-1 character set. See {@link BrowserMobHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return the charset associated with the HTTP message, or the default charset if none is present
 * @throws UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);

    Charset charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
Example 12
Source File: HttpMessageContents.java    From Dream-Catcher with MIT License 5 votes vote down vote up
/**
 * Retrieves the Content-Type header of this message. If no Content-Type is present, returns the assumed default Content-Type (see
 * {@link BrowserMobHttpUtil#UNKNOWN_CONTENT_TYPE}).
 *
 * @return the message's content type
 */
public String getContentType() {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);
    if (contentTypeHeader == null || contentTypeHeader.isEmpty()) {
        return BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    } else {
        return contentTypeHeader;
    }
}
 
Example 13
Source File: HttpObjectUtil.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
/**
 * Derives the charset from the Content-Type header in the HttpMessage. If the Content-Type header is not present or does not contain
 * a character set, this method returns the ISO-8859-1 character set. See {@link BrowserMobHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return the charset associated with the HTTP message, or the default charset if none is present
 * @throws UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);

    Charset charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
Example 14
Source File: HarCaptureFilter.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
protected void captureRedirectUrl(HttpResponse httpResponse) {
    String locationHeaderValue = HttpHeaders.getHeader(httpResponse, HttpHeaderNames.LOCATION);
    if (locationHeaderValue != null) {
        harEntry.getResponse().setRedirectURL(locationHeaderValue);
    }
    else {
        harEntry.getResponse().setRedirectURL("");
    }
}
 
Example 15
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 5 votes vote down vote up
protected void captureResponseMimeType(HttpResponse httpResponse) {
    Log.e("InnerHandle", "captureResponseMimeType " + harEntry.getId());
    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    // don't set the mimeType to null, since mimeType is a required field
    if (contentType != null) {
        harResponse.getResponse().getContent().setMimeType(contentType);
    }
}
 
Example 16
Source File: HarCaptureFilter.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
protected void captureResponseContent(HttpResponse httpResponse, byte[] fullMessage) {
    // force binary if the content encoding is not supported
    boolean forceBinary = false;

    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaderNames.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in response from {}. Content will be treated as {}", originalRequest.getUri(), BrowserUpHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserUpHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    if (responseCaptureFilter.isResponseCompressed() && !responseCaptureFilter.isDecompressionSuccessful()) {
        log.warn("Unable to decompress content with encoding: {}. Contents will be encoded as base64 binary data.", responseCaptureFilter.getContentEncoding());

        forceBinary = true;
    }

    Charset charset;
    try {
        charset = BrowserUpHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP response from {}. Content will not be captured in HAR.", contentType, originalRequest.uri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserUpHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents from {}", charset, originalRequest.getUri());
    }

    if (!forceBinary && BrowserUpHttpUtil.hasTextualContent(contentType)) {
        String text = BrowserUpHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getResponse().getContent().setText(text);
    } else if (dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        harEntry.getResponse().getContent().setText(BaseEncoding.base64().encode(fullMessage));
        harEntry.getResponse().getContent().setEncoding("base64");
    }

    harEntry.getResponse().getContent().setSize((long)fullMessage.length);
}
 
Example 17
Source File: ServerResponseCaptureFilter.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
protected void captureContentEncoding(HttpResponse httpResponse) {
    contentEncoding = HttpHeaders.getHeader(httpResponse, HttpHeaderNames.CONTENT_ENCODING);
}
 
Example 18
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 4 votes vote down vote up
protected void captureRedirectUrl(HttpResponse httpResponse) {
    String locationHeaderValue = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.LOCATION);
    if (locationHeaderValue != null) {
        harEntry.getResponse().setRedirectURL(locationHeaderValue);
    }
}
 
Example 19
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
protected void captureRedirectUrl(HttpResponse httpResponse) {
    String locationHeaderValue = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.LOCATION);
    if (locationHeaderValue != null) {
        harEntry.getResponse().setRedirectURL(locationHeaderValue);
    }
}
 
Example 20
Source File: NettyHttpServletRequest.java    From Jinx with Apache License 2.0 4 votes vote down vote up
@Override
public String getHeader(String name) {
    return HttpHeaders.getHeader(this.request, name);
}