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

(-)java/org/apache/catalina/connector/Request.java (-6 / +3 lines)
Lines 1516-1530 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
        B2CConverter.getCharset(enc);
1522
1525
        // Save the validated encoding
1523
        // Save the validated encoding
1526
        coyoteRequest.setCharacterEncoding(enc);
1524
        coyoteRequest.setCharacterEncoding(enc);
1527
1528
    }
1525
    }
1529
1526
1530
1527
(-)java/org/apache/tomcat/util/buf/B2CConverter.java (-4 / +36 lines)
Lines 22-27 Link Here
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.io.UnsupportedEncodingException;
25
import java.nio.charset.Charset;
26
import java.util.HashMap;
27
import java.util.Locale;
28
import java.util.Map;
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 Map<String, Charset> encodingToCharsetCache =
48
        new HashMap<String, Charset>();
42
    
49
    
50
    static {
51
        for (Charset charset: Charset.availableCharsets().values()) {
52
            encodingToCharsetCache.put(
53
                    charset.name().toLowerCase(Locale.US), charset);
54
            for (String alias : charset.aliases()) {
55
                encodingToCharsetCache.put(
56
                        alias.toLowerCase(Locale.US), charset);
57
            }
58
        }
59
    }
60
61
    public static Charset getCharset(String enc)
62
            throws UnsupportedEncodingException {
63
64
        // Encoding names should all be ASCII
65
        String lowerCaseEnc = enc.toLowerCase(Locale.US);
66
        
67
        Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
68
69
        if (charset == null) {
70
            // Pre-population of the cache means this must be invalid
71
            throw new UnsupportedEncodingException(enc);
72
        }
73
        return charset;
74
    }
75
43
    private IntermediateInputStream iis;
76
    private IntermediateInputStream iis;
44
    private ReadConvertor conv;
77
    private ReadConvertor conv;
45
    private String encoding;
78
    private String encoding;
Lines 114-120 Link Here
114
    {
147
    {
115
        // destroy the reader/iis
148
        // destroy the reader/iis
116
        iis=new IntermediateInputStream();
149
        iis=new IntermediateInputStream();
117
        conv=new ReadConvertor( iis, encoding );
150
        conv=new ReadConvertor( iis, getCharset(encoding) );
118
    }
151
    }
119
152
120
    private final int debug=0;
153
    private final int debug=0;
Lines 192-201 Link Here
192
    
225
    
193
    /** Create a converter.
226
    /** Create a converter.
194
     */
227
     */
195
    public ReadConvertor( IntermediateInputStream in, String enc )
228
    public ReadConvertor( IntermediateInputStream in, Charset charset )
196
        throws UnsupportedEncodingException
197
    {
229
    {
198
        super( in, enc );
230
        super( in, charset );
199
    }
231
    }
200
    
232
    
201
    /** Overriden - will do nothing but reset internal state.
233
    /** Overriden - will do nothing but reset internal state.
(-)java/org/apache/tomcat/util/buf/ByteChunk.java (-3 / +16 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().

Return to bug 51400