Package org.apache.jmeter.protocol.http.util

# Class ConversionUtils

java.lang.Object
org.apache.jmeter.protocol.http.util.ConversionUtils

public class ConversionUtils extends Object
General purpose conversion utilities related to HTTP/HTML

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ConversionUtils()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static String`
  `encodeWithEntities(String value, Charset charset)`
  Encodes non-encodable characters as HTML entities like e.g.
  `static String`
  `escapeIllegalURLCharacters(String url)`
  `static String`
  `getEncodingFromContentType(String contentType)`
  Extract the encoding (charset) from the Content-Type, e.g.
  `static URL`
  `makeRelativeURL(URL baseURL, String location)`
  Generate an absolute URL from a possibly relative location, allowing for extraneous leading "../" segments.
  `static String`
  `percentEncode(String value)`
  Encodes strings for `multipart/form-data` names and values.
  `static String`
  `removeSlashDotDot(String url)`
  collapses absolute or relative URLs containing '/..' converting `http://host/path1/../path2` to `http://host/path2` or `/one/two/../three` to `/one/three`
  `static URI`
  `sanitizeUrl(URL url)`
  Checks a URL and encodes it if necessary, i.e.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ConversionUtils

    public ConversionUtils()

- ## Method Details

  - ### getEncodingFromContentType

    public static String getEncodingFromContentType(String contentType)
    Extract the encoding (charset) from the Content-Type, e.g. "text/html; charset=utf-8".
    Parameters:
      `contentType` - string from which the encoding should be extracted
    Returns:
      the charset encoding - or `null`, if none was found or the charset is not supported
    Throws:
      `IllegalCharsetNameException` - if the found charset is not supported

  - ### percentEncode

    @API(status=MAINTAINED, since="5.6") public static String percentEncode(String value)
    Encodes strings for `multipart/form-data` names and values. The encoding is `"` as `%22`, `CR` as `%0D`, and `LF` as `%0A`. Note: `%` is not encoded, so it creates ambiguity which might be resolved in a later specification version.
    Parameters:
      `value` - input value to convert
    Returns:
      converted value
    Since:
      5.6
    See Also:
      - Multipart form data specification
      - Escaping % in multipart/form-data

  - ### encodeWithEntities

    @API(status=EXPERIMENTAL, since="5.6.1") public static String encodeWithEntities(String value, Charset charset)
    Encodes non-encodable characters as HTML entities like e.g. &#128514; for 😂.
    Parameters:
      `value` - value to encode
      `charset` - charset that will be used for encoding, defaults to UTF-8 if null
    Returns:
      input value with non-encodable characters replaced with HTML entities

  - ### makeRelativeURL

    public static URL makeRelativeURL(URL baseURL, String location) throws MalformedURLException
    Generate an absolute URL from a possibly relative location, allowing for extraneous leading "../" segments. The Java `URL(URL, String)` constructor does not remove these.
    Parameters:
      `baseURL` - the base URL which is used to resolve missing protocol/host in the location
      `location` - the location, possibly with extraneous leading "../"
    Returns:
      URL with extraneous ../ removed
    Throws:
      `MalformedURLException` - when the given `URL` is malformed
    See Also:
      - Bug 46690 - handling of 302 redirects with invalid relative paths

  - ### escapeIllegalURLCharacters

    public static String escapeIllegalURLCharacters(String url) throws Exception
    Parameters:
      `url` - String Url to escape
    Returns:
      String cleaned up url
    Throws:
      `Exception` - when given `url` leads to a malformed URL or URI

  - ### sanitizeUrl

    public static URI sanitizeUrl(URL url) throws URISyntaxException
    Checks a URL and encodes it if necessary, i.e. if it is not currently correctly encoded. Warning: it may not work on all unencoded URLs.
    Parameters:
      `url` - non-encoded URL
    Returns:
      URI which has been encoded as necessary
    Throws:
      `URISyntaxException` - if parts of the url form a non valid URI

  - ### removeSlashDotDot

    public static String removeSlashDotDot(String url)
    collapses absolute or relative URLs containing '/..' converting `http://host/path1/../path2` to `http://host/path2` or `/one/two/../three` to `/one/three`
    Parameters:
      `url` - in which the '/..'s should be removed
    Returns:
      collapsed URL
    See Also:
      - Bug 49083 - collapse /.. in redirect URLs