Home | History | Annotate | Download | only in http
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.net.http;
     18 
     19 import java.nio.charset.Charset;
     20 import java.nio.charset.IllegalCharsetNameException;
     21 import java.nio.charset.StandardCharsets;
     22 import java.nio.charset.UnsupportedCharsetException;
     23 import java.util.Collections;
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 /**
     28  * @hide
     29  */
     30 public class ResponseUtils {
     31   /**
     32    * Returns the response charset of a HTTP response based on the {@code Content-Type} of
     33    * the response (see RFC 7230). If the {@code Content-Type} header is missing or invalid,
     34    * the response is assumed to be encoded as {@code UTF-8}. Note that a charset usually
     35    * makes sense only for {@code "text/plain"} and other "text based" responses.
     36    *
     37    * @throws IllegalCharsetNameException if the response specified charset is illegal.
     38    * @throws UnsupportedCharsetException if the response specified charset is unsupported.
     39    */
     40   public static Charset responseCharset(String contentTypeHeader)
     41       throws IllegalCharsetNameException, UnsupportedCharsetException {
     42     Charset responseCharset = StandardCharsets.UTF_8;
     43     if (contentTypeHeader != null) {
     44       Map<String, String> contentTypeParams = parseContentTypeParameters(contentTypeHeader);
     45       String charsetParameter = contentTypeParams.get("charset");
     46       if (charsetParameter != null) {
     47         responseCharset = Charset.forName(charsetParameter);
     48       }
     49     }
     50 
     51     return responseCharset;
     52   }
     53 
     54   /**
     55    * Parse content-type parameters. The format of this header is roughly :
     56    * {@code type/subtype; param1=value1; param2=value2 ...} where each of the
     57    * parameters are optional. Parsing is lenient, malformed parameters are ignored.
     58    *
     59    * Parameter keys & values are trimmed of whitespace and keys are converted to
     60    * lower case.
     61    */
     62   private static Map<String, String> parseContentTypeParameters(String contentTypeHeader) {
     63     Map<String, String> parameters = Collections.EMPTY_MAP;
     64 
     65     String[] fields = contentTypeHeader.split(";");
     66     if (fields.length > 1) {
     67       parameters = new HashMap<>();
     68       // Ignore the first element in the array (the type/subtype).
     69       for (int i = 1; i < fields.length; ++i) {
     70         final String parameter = fields[i];
     71         if (!parameter.isEmpty()) {
     72           final String[] components = parameter.split("=");
     73           if (components.length != 2) {
     74             continue;
     75           }
     76 
     77           final String key = components[0].trim().toLowerCase();
     78           final String value = components[1].trim();
     79           if (key.isEmpty() || value.isEmpty()) {
     80             continue;
     81           }
     82 
     83           parameters.put(key, value);
     84         }
     85       }
     86     }
     87 
     88     return parameters;
     89   }
     90 }
     91