src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/common/Utils.java
branchhttp-client-branch
changeset 56009 cf8792f51dee
parent 55973 4d9b002587db
child 56024 de352132c7e8
equal deleted inserted replaced
56008:bbd688c6fbbb 56009:cf8792f51dee
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 package jdk.incubator.http.internal.common;
    26 package jdk.incubator.http.internal.common;
    27 
    27 
    28 import jdk.incubator.http.HttpHeaders;
    28 import jdk.incubator.http.HttpHeaders;
    29 import sun.net.NetProperties;
    29 import sun.net.NetProperties;
    30 import sun.net.util.IPAddressUtil;
    30 import sun.net.util.IPAddressUtil;
       
    31 import sun.net.www.HeaderParser;
    31 
    32 
    32 import javax.net.ssl.SSLParameters;
    33 import javax.net.ssl.SSLParameters;
    33 import java.io.ByteArrayOutputStream;
    34 import java.io.ByteArrayOutputStream;
    34 import java.io.Closeable;
    35 import java.io.Closeable;
    35 import java.io.IOException;
    36 import java.io.IOException;
   474     /**
   475     /**
   475      * Get the Charset from the Content-encoding header. Defaults to
   476      * Get the Charset from the Content-encoding header. Defaults to
   476      * UTF_8
   477      * UTF_8
   477      */
   478      */
   478     public static Charset charsetFrom(HttpHeaders headers) {
   479     public static Charset charsetFrom(HttpHeaders headers) {
   479         String encoding = headers.firstValue("Content-encoding")
   480         String type = headers.firstValue("Content-type")
   480                 .orElse("UTF_8");
   481                 .orElse("text/html; charset=utf-8");
       
   482         int i = type.indexOf(";");
       
   483         if (i >= 0) type = type.substring(i+1);
   481         try {
   484         try {
   482             return Charset.forName(encoding);
   485             HeaderParser parser = new HeaderParser(type);
   483         } catch (IllegalArgumentException e) {
   486             String value = parser.findValue("charset");
       
   487             if (value == null) return StandardCharsets.UTF_8;
       
   488             return Charset.forName(value);
       
   489         } catch (Throwable x) {
       
   490             Log.logTrace("Can't find charset in \"{0}\" ({1})", type, x);
   484             return StandardCharsets.UTF_8;
   491             return StandardCharsets.UTF_8;
   485         }
   492         }
   486     }
   493     }
   487 
   494 
   488     public static UncheckedIOException unchecked(IOException e) {
   495     public static UncheckedIOException unchecked(IOException e) {