use of org.apache.tomcat.util.buf.B2CConverter in project tomcat by apache.
the class CoyoteAdapter method convertURI.
/**
* Character conversion of the URI.
*
* @param uri MessageBytes object containing the URI
* @param request The Servlet request obejct
* @throws IOException if a IO exception occurs sending an error to the client
*/
protected void convertURI(MessageBytes uri, Request request) throws IOException {
ByteChunk bc = uri.getByteChunk();
int length = bc.getLength();
CharChunk cc = uri.getCharChunk();
cc.allocate(length, -1);
String enc = connector.getURIEncoding();
if (enc != null) {
B2CConverter conv = request.getURIConverter();
try {
if (conv == null) {
conv = new B2CConverter(B2CConverter.getCharset(enc), true);
request.setURIConverter(conv);
} else {
conv.recycle();
}
} catch (IOException e) {
log.error(sm.getString("coyoteAdapter.invalidEncoding"));
connector.setURIEncoding(null);
}
if (conv != null) {
try {
conv.convert(bc, cc, true);
uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength());
return;
} catch (IOException ioe) {
// Should never happen as B2CConverter should replace
// problematic characters
request.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
}
// Default encoding: fast conversion for ISO-8859-1
byte[] bbuf = bc.getBuffer();
char[] cbuf = cc.getBuffer();
int start = bc.getStart();
for (int i = 0; i < length; i++) {
cbuf[i] = (char) (bbuf[i + start] & 0xff);
}
uri.setChars(cbuf, 0, length);
}
use of org.apache.tomcat.util.buf.B2CConverter in project tomcat by apache.
the class InputBuffer method setConverter.
private void setConverter() throws IOException {
if (coyoteRequest != null) {
enc = coyoteRequest.getCharacterEncoding();
}
if (enc == null) {
enc = org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING;
}
Charset charset = B2CConverter.getCharset(enc);
SynchronizedStack<B2CConverter> stack = encoders.get(charset);
if (stack == null) {
stack = new SynchronizedStack<>();
encoders.putIfAbsent(charset, stack);
stack = encoders.get(charset);
}
conv = stack.pop();
if (conv == null) {
conv = createConverter(charset);
}
}
Aggregations