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 / +37 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.Map.Entry;
28
import java.util.concurrent.ConcurrentHashMap;
25
29
26
/** Efficient conversion of bytes  to character .
30
/** Efficient conversion of bytes  to character .
27
 *  
31
 *  
Lines 39-45 Link Here
39
    
43
    
40
    private static org.apache.juli.logging.Log log=
44
    private static org.apache.juli.logging.Log log=
41
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
45
        org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
46
47
    private static final ConcurrentHashMap<String, Charset> encodingToCharsetCache =
48
        new ConcurrentHashMap<String, Charset>();
42
    
49
    
50
    static {
51
        for (Entry<String,Charset> entry :
52
                Charset.availableCharsets().entrySet()) {
53
            Charset charset = entry.getValue(); 
54
            encodingToCharsetCache.put(entry.getKey().toLowerCase(), charset);
55
            for (String alias : charset.aliases()) {
56
                encodingToCharsetCache.put(alias, charset);
57
            }
58
        }
59
    }
60
61
    public static Charset getCharset(String enc) {
62
63
        // Encoding names should all be ASCII
64
        String lowerCaseEnc = enc.toLowerCase(Locale.US);
65
        
66
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
67
68
        if (charset == null) {
69
            // Pre-population of the cache means this must be invalid
70
            throw new UnsupportedCharsetException(enc);
71
        }
72
        return charset;
73
    }
74
43
    private IntermediateInputStream iis;
75
    private IntermediateInputStream iis;
44
    private ReadConvertor conv;
76
    private ReadConvertor conv;
45
    private String encoding;
77
    private String encoding;
Lines 114-120 Link Here
114
    {
146
    {
115
        // destroy the reader/iis
147
        // destroy the reader/iis
116
        iis=new IntermediateInputStream();
148
        iis=new IntermediateInputStream();
117
        conv=new ReadConvertor( iis, encoding );
149
        conv=new ReadConvertor( iis, getCharset(encoding) );
118
    }
150
    }
119
151
120
    private final int debug=0;
152
    private final int debug=0;
Lines 192-201 Link Here
192
    
224
    
193
    /** Create a converter.
225
    /** Create a converter.
194
     */
226
     */
195
    public ReadConvertor( IntermediateInputStream in, String enc )
227
    public ReadConvertor( IntermediateInputStream in, Charset charset )
196
        throws UnsupportedEncodingException
197
    {
228
    {
198
        super( in, enc );
229
        super( in, charset );
199
    }
230
    }
200
    
231
    
201
    /** Overriden - will do nothing but reset internal state.
232
    /** Overriden - will do nothing but reset internal state.
Lines 224-229 Link Here
224
        } catch(IOException ioe){
255
        } catch(IOException ioe){
225
        }
256
        }
226
    }
257
    }
258
227
}
259
}
228
260
229
261
(-)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