View | Details | Raw Unified | Return to bug 51400
Collapse All | Expand All

(-)java/org/apache/catalina/connector/Request.java (-5 / +15 lines)
Lines 1516-1527 Link Here
1516
1516
1517
        if (usingReader)
1517
        if (usingReader)
1518
            return;
1518
            return;
1519
        
1520
        // Ensure that the specified encoding is valid
1521
        byte buffer[] = new byte[1];
1522
        buffer[0] = (byte) 'a';
1523
        String dummy = new String(buffer, enc);
1524
1519
1520
        // Confirm that the encoding name is valid
1521
        try {
1522
            B2CConverter.getCharset(enc);
1523
        } catch (java.nio.charset.UnsupportedCharsetException e) {
1524
            UnsupportedEncodingException uee =
1525
                new UnsupportedEncodingException();
1526
            uee.initCause(e);
1527
            throw uee;
1528
        } catch (java.nio.charset.IllegalCharsetNameException e) {
1529
            UnsupportedEncodingException uee =
1530
                new UnsupportedEncodingException();
1531
            uee.initCause(e);
1532
            throw uee;
1533
        }
1534
1525
        // Save the validated encoding
1535
        // Save the validated encoding
1526
        coyoteRequest.setCharacterEncoding(enc);
1536
        coyoteRequest.setCharacterEncoding(enc);
1527
1537
(-)java/org/apache/tomcat/util/buf/B2CConverter.java (-5 / +36 lines)
Lines 21-27 Link Here
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.io.InputStream;
22
import java.io.InputStream;
23
import java.io.InputStreamReader;
23
import java.io.InputStreamReader;
24
import java.io.UnsupportedEncodingException;
24
import java.nio.charset.Charset;
25
import java.nio.charset.UnsupportedCharsetException;
26
import java.util.Locale;
27
import java.util.concurrent.ConcurrentHashMap;
25
28
26
/** Efficient conversion of bytes  to character .
29
/** Efficient conversion of bytes  to character .
27
 *  
30
 *  
Lines 39-45 Link Here
39
    
42
    
40
    private static org.apache.juli.logging.Log log=
43
    private static org.apache.juli.logging.Log log=
41
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
44
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
45
46
    private static final ConcurrentHashMap<String, Charset> encodingToCharsetCache =
47
        new ConcurrentHashMap<String, Charset>();
42
    
48
    
49
    static {
50
        for (Charset charset: Charset.availableCharsets().values()) {
51
            encodingToCharsetCache.put(
52
                    charset.name().toLowerCase(Locale.US), charset);
53
            for (String alias : charset.aliases()) {
54
                encodingToCharsetCache.put(
55
                        alias.toLowerCase(Locale.US), charset);
56
            }
57
        }
58
    }
59
60
    public static Charset getCharset(String enc) {
61
62
        // Encoding names should all be ASCII
63
        String lowerCaseEnc = enc.toLowerCase(Locale.US);
64
        
65
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
66
67
        if (charset == null) {
68
            // Pre-population of the cache means this must be invalid
69
            throw new UnsupportedCharsetException(enc);
70
        }
71
        return charset;
72
    }
73
43
    private IntermediateInputStream iis;
74
    private IntermediateInputStream iis;
44
    private ReadConvertor conv;
75
    private ReadConvertor conv;
45
    private String encoding;
76
    private String encoding;
Lines 114-120 Link Here
114
    {
145
    {
115
        // destroy the reader/iis
146
        // destroy the reader/iis
116
        iis=new IntermediateInputStream();
147
        iis=new IntermediateInputStream();
117
        conv=new ReadConvertor( iis, encoding );
148
        conv=new ReadConvertor( iis, getCharset(encoding) );
118
    }
149
    }
119
150
120
    private final int debug=0;
151
    private final int debug=0;
Lines 192-201 Link Here
192
    
223
    
193
    /** Create a converter.
224
    /** Create a converter.
194
     */
225
     */
195
    public ReadConvertor( IntermediateInputStream in, String enc )
226
    public ReadConvertor( IntermediateInputStream in, Charset charset )
196
        throws UnsupportedEncodingException
197
    {
227
    {
198
        super( in, enc );
228
        super( in, charset );
199
    }
229
    }
200
    
230
    
201
    /** Overriden - will do nothing but reset internal state.
231
    /** Overriden - will do nothing but reset internal state.
Lines 224-229 Link Here
224
        } catch(IOException ioe){
254
        } catch(IOException ioe){
225
        }
255
        }
226
    }
256
    }
257
227
}
258
}
228
259
229
260
(-)java/org/apache/tomcat/util/buf/ByteChunk.java (-4 / +17 lines)
Lines 19-24 Link Here
19
19
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.io.Serializable;
21
import java.io.Serializable;
22
import java.nio.ByteBuffer;
23
import java.nio.charset.Charset;
22
24
23
/*
25
/*
24
 * In a server it is very important to be able to operate on
26
 * In a server it is very important to be able to operate on
Lines 95-101 Link Here
95
        8859_1, and this object is used mostly for servlets. 
97
        8859_1, and this object is used mostly for servlets. 
96
    */
98
    */
97
    public static final String DEFAULT_CHARACTER_ENCODING="ISO-8859-1";
99
    public static final String DEFAULT_CHARACTER_ENCODING="ISO-8859-1";
98
        
100
101
    /** Default Charset to use for interpreting byte[] as as String
102
    */
103
    public static final Charset DEFAULT_CHARSET =
104
        Charset.forName(DEFAULT_CHARACTER_ENCODING);
105
    
99
    // byte[]
106
    // byte[]
100
    private byte[] buff;
107
    private byte[] buff;
101
108
Lines 493-500 Link Here
493
    public String toStringInternal() {
500
    public String toStringInternal() {
494
        String strValue=null;
501
        String strValue=null;
495
        try {
502
        try {
496
            if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING;
503
            Charset charset;
497
            strValue = new String( buff, start, end-start, enc );
504
            if (enc == null) {
505
                charset = DEFAULT_CHARSET;
506
            } else {
507
                charset = B2CConverter.getCharset(enc);
508
            }
509
            strValue = charset.decode(
510
                    ByteBuffer.wrap(buff, start, end-start)).toString();
498
            /*
511
            /*
499
             Does not improve the speed too much on most systems,
512
             Does not improve the speed too much on most systems,
500
             it's safer to use the "clasical" new String().
513
             it's safer to use the "clasical" new String().
Lines 507-513 Link Here
507
             // Method is commented out, in:
520
             // Method is commented out, in:
508
              return B2CConverter.decodeString( enc );
521
              return B2CConverter.decodeString( enc );
509
              */
522
              */
510
        } catch (java.io.UnsupportedEncodingException e) {
523
        } catch (java.nio.charset.UnsupportedCharsetException e) {
511
            // Use the platform encoding in that case; the usage of a bad
524
            // Use the platform encoding in that case; the usage of a bad
512
            // encoding will have been logged elsewhere already
525
            // encoding will have been logged elsewhere already
513
            strValue = new String(buff, start, end-start);
526
            strValue = new String(buff, start, end-start);

Return to bug 51400