summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/libcore/net/http/ResponseUtils.java
blob: c892b53a333dec5d70b137b3bf4941233c326fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.net.http;

import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @hide
 */
public class ResponseUtils {
  /**
   * Returns the response charset of a HTTP response based on the {@code Content-Type} of
   * the response (see RFC 7230). If the {@code Content-Type} header is missing or invalid,
   * the response is assumed to be encoded as {@code UTF-8}. Note that a charset usually
   * makes sense only for {@code "text/plain"} and other "text based" responses.
   *
   * @throws IllegalCharsetNameException if the response specified charset is illegal.
   * @throws UnsupportedCharsetException if the response specified charset is unsupported.
   */
  public static Charset responseCharset(String contentTypeHeader)
      throws IllegalCharsetNameException, UnsupportedCharsetException {
    Charset responseCharset = StandardCharsets.UTF_8;
    if (contentTypeHeader != null) {
      Map<String, String> contentTypeParams = parseContentTypeParameters(contentTypeHeader);
      String charsetParameter = contentTypeParams.get("charset");
      if (charsetParameter != null) {
        responseCharset = Charset.forName(charsetParameter);
      }
    }

    return responseCharset;
  }

  /**
   * Parse content-type parameters. The format of this header is roughly :
   * {@code type/subtype; param1=value1; param2=value2 ...} where each of the
   * parameters are optional. Parsing is lenient, malformed parameters are ignored.
   *
   * Parameter keys & values are trimmed of whitespace and keys are converted to
   * lower case.
   */
  private static Map<String, String> parseContentTypeParameters(String contentTypeHeader) {
    Map<String, String> parameters = Collections.EMPTY_MAP;

    String[] fields = contentTypeHeader.split(";");
    if (fields.length > 1) {
      parameters = new HashMap<>();
      // Ignore the first element in the array (the type/subtype).
      for (int i = 1; i < fields.length; ++i) {
        final String parameter = fields[i];
        if (!parameter.isEmpty()) {
          final String[] components = parameter.split("=");
          if (components.length != 2) {
            continue;
          }

          final String key = components[0].trim().toLowerCase();
          final String value = components[1].trim();
          if (key.isEmpty() || value.isEmpty()) {
            continue;
          }

          parameters.put(key, value);
        }
      }
    }

    return parameters;
  }
}