Java Code Examples for org.apache.tomcat.util.buf.MessageBytes#T_BYTES

The following examples show how to use org.apache.tomcat.util.buf.MessageBytes#T_BYTES . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoyoteAdapter.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Character conversion of the a US-ASCII MessageBytes.
 *
 * @param mb The MessageBytes instance containing the bytes that should be converted to chars
 */
protected void convertMB(MessageBytes mb) {

    // This is of course only meaningful for bytes
    if (mb.getType() != MessageBytes.T_BYTES) {
        return;
    }

    ByteChunk bc = mb.getByteChunk();
    CharChunk cc = mb.getCharChunk();
    int length = bc.getLength();
    cc.allocate(length, -1);

    // Default encoding: fast conversion
    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);
    }
    mb.setChars(cbuf, 0, length);

}
 
Example 2
Source File: Http11OutputBuffer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * This method will write the contents of the specified message bytes
 * buffer to the output stream, without filtering. This method is meant to
 * be used to write the response header.
 *
 * @param mb data to be written
 */
private void write(MessageBytes mb) {
    if (mb.getType() != MessageBytes.T_BYTES) {
        mb.toBytes();
        ByteChunk bc = mb.getByteChunk();
        // Need to filter out CTLs excluding TAB. ISO-8859-1 and UTF-8
        // values will be OK. Strings using other encodings may be
        // corrupted.
        byte[] buffer = bc.getBuffer();
        for (int i = bc.getOffset(); i < bc.getLength(); i++) {
            // byte values are signed i.e. -128 to 127
            // The values are used unsigned. 0 to 31 are CTLs so they are
            // filtered (apart from TAB which is 9). 127 is a control (DEL).
            // The values 128 to 255 are all OK. Converting those to signed
            // gives -128 to -1.
            if ((buffer[i] > -1 && buffer[i] <= 31 && buffer[i] != 9) ||
                    buffer[i] == 127) {
                buffer[i] = ' ';
            }
        }
    }
    write(mb.getByteChunk());
}
 
Example 3
Source File: CoyoteAdapter.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Character conversion of the a US-ASCII MessageBytes.
 */
protected void convertMB(MessageBytes mb) {

    // This is of course only meaningful for bytes
    if (mb.getType() != MessageBytes.T_BYTES) {
        return;
    }

    ByteChunk bc = mb.getByteChunk();
    CharChunk cc = mb.getCharChunk();
    int length = bc.getLength();
    cc.allocate(length, -1);

    // Default encoding: fast conversion
    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);
    }
    mb.setChars(cbuf, 0, length);

}
 
Example 4
Source File: AjpMessage.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Write a MessageBytes out at the current write position.
 * A null MessageBytes is encoded as a string with length 0.  
 */
public void appendBytes(MessageBytes mb) {
    if (mb == null) {
        log.error(sm.getString("ajpmessage.null"), 
                new NullPointerException());
        appendInt(0);
        appendByte(0);
        return;
    }
    if (mb.getType() == MessageBytes.T_BYTES) {
        ByteChunk bc = mb.getByteChunk();
        appendByteChunk(bc);
    } else if (mb.getType() == MessageBytes.T_CHARS) {
        CharChunk cc = mb.getCharChunk();
        appendCharChunk(cc);
    } else {
        appendString(mb.toString());
    }
}
 
Example 5
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Character conversion of the a US-ASCII MessageBytes.
 */
protected void convertMB(MessageBytes mb) {

    // This is of course only meaningful for bytes
    if (mb.getType() != MessageBytes.T_BYTES) {
        return;
    }

    ByteChunk bc = mb.getByteChunk();
    CharChunk cc = mb.getCharChunk();
    int length = bc.getLength();
    cc.allocate(length, -1);

    // Default encoding: fast conversion
    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);
    }
    mb.setChars(cbuf, 0, length);

}
 
Example 6
Source File: AjpMessage.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Write a MessageBytes out at the current write position.
 * A null MessageBytes is encoded as a string with length 0.  
 */
public void appendBytes(MessageBytes mb) {
    if (mb == null) {
        log.error(sm.getString("ajpmessage.null"), 
                new NullPointerException());
        appendInt(0);
        appendByte(0);
        return;
    }
    if (mb.getType() == MessageBytes.T_BYTES) {
        ByteChunk bc = mb.getByteChunk();
        appendByteChunk(bc);
    } else if (mb.getType() == MessageBytes.T_CHARS) {
        CharChunk cc = mb.getCharChunk();
        appendCharChunk(cc);
    } else {
        appendString(mb.toString());
    }
}
 
Example 7
Source File: AjpMessage.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Write a MessageBytes out at the current write position. A null
 * MessageBytes is encoded as a string with length 0.
 *
 * @param mb The data to write
 */
public void appendBytes(MessageBytes mb) {
    if (mb == null) {
        log.error(sm.getString("ajpmessage.null"),
                new NullPointerException());
        appendInt(0);
        appendByte(0);
        return;
    }
    if (mb.getType() != MessageBytes.T_BYTES) {
        mb.toBytes();
        ByteChunk bc = mb.getByteChunk();
        // Need to filter out CTLs excluding TAB. ISO-8859-1 and UTF-8
        // values will be OK. Strings using other encodings may be
        // corrupted.
        byte[] buffer = bc.getBuffer();
        for (int i = bc.getOffset(); i < bc.getLength(); i++) {
            // byte values are signed i.e. -128 to 127
            // The values are used unsigned. 0 to 31 are CTLs so they are
            // filtered (apart from TAB which is 9). 127 is a control (DEL).
            // The values 128 to 255 are all OK. Converting those to signed
            // gives -128 to -1.
            if ((buffer[i] > -1 && buffer[i] <= 31 && buffer[i] != 9) ||
                    buffer[i] == 127) {
                buffer[i] = ' ';
            }
        }
    }
    appendByteChunk(mb.getByteChunk());
}
 
Example 8
Source File: Parameters.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void processParameters(MessageBytes data, Charset charset) {
    if( data==null || data.isNull() || data.getLength() <= 0 ) {
        return;
    }

    if( data.getType() != MessageBytes.T_BYTES ) {
        data.toBytes();
    }
    ByteChunk bc=data.getByteChunk();
    processParameters(bc.getBytes(), bc.getOffset(), bc.getLength(), charset);
}
 
Example 9
Source File: LegacyCookieProcessor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void parseCookieHeader(MimeHeaders headers, ServerCookies serverCookies) {

    if (headers == null) {
        // nothing to process
        return;
    }
    // process each "cookie" header
    int pos = headers.findHeader("Cookie", 0);
    while (pos >= 0) {
        MessageBytes cookieValue = headers.getValue(pos);

        if (cookieValue != null && !cookieValue.isNull() ) {
            if (cookieValue.getType() != MessageBytes.T_BYTES ) {
                Exception e = new Exception();
                // TODO: Review this in light of HTTP/2
                log.debug("Cookies: Parsing cookie as String. Expected bytes.", e);
                cookieValue.toBytes();
            }
            if (log.isDebugEnabled()) {
                log.debug("Cookies: Parsing b[]: " + cookieValue.toString());
            }
            ByteChunk bc = cookieValue.getByteChunk();
            processCookieHeader(bc.getBytes(), bc.getOffset(), bc.getLength(), serverCookies);
        }

        // search from the next position
        pos = headers.findHeader("Cookie", ++pos);
    }
}
 
Example 10
Source File: Rfc6265CookieProcessor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void parseCookieHeader(MimeHeaders headers,
        ServerCookies serverCookies) {

    if (headers == null) {
        // nothing to process
        return;
    }

    // process each "cookie" header
    int pos = headers.findHeader("Cookie", 0);
    while (pos >= 0) {
        MessageBytes cookieValue = headers.getValue(pos);

        if (cookieValue != null && !cookieValue.isNull() ) {
            if (cookieValue.getType() != MessageBytes.T_BYTES ) {
                if (log.isDebugEnabled()) {
                    Exception e = new Exception();
                    // TODO: Review this in light of HTTP/2
                    log.debug("Cookies: Parsing cookie as String. Expected bytes.", e);
                }
                cookieValue.toBytes();
            }
            if (log.isDebugEnabled()) {
                log.debug("Cookies: Parsing b[]: " + cookieValue.toString());
            }
            ByteChunk bc = cookieValue.getByteChunk();

            Cookie.parseCookie(bc.getBytes(), bc.getOffset(), bc.getLength(),
                    serverCookies);
        }

        // search from the next position
        pos = headers.findHeader("Cookie", ++pos);
    }
}
 
Example 11
Source File: AbstractOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * This method will write the contents of the specified message bytes 
 * buffer to the output stream, without filtering. This method is meant to
 * be used to write the response header.
 * 
 * @param mb data to be written
 */
protected void write(MessageBytes mb) {

    if (mb.getType() == MessageBytes.T_BYTES) {
        ByteChunk bc = mb.getByteChunk();
        write(bc);
    } else if (mb.getType() == MessageBytes.T_CHARS) {
        CharChunk cc = mb.getCharChunk();
        write(cc);
    } else {
        write(mb.toString());
    }

}
 
Example 12
Source File: Parameters.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void processParameters( MessageBytes data, String encoding ) {
    if( data==null || data.isNull() || data.getLength() <= 0 ) {
        return;
    }

    if( data.getType() != MessageBytes.T_BYTES ) {
        data.toBytes();
    }
    ByteChunk bc=data.getByteChunk();
    processParameters( bc.getBytes(), bc.getOffset(),
                       bc.getLength(), getCharset(encoding));
}
 
Example 13
Source File: AbstractOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * This method will write the contents of the specified message bytes 
 * buffer to the output stream, without filtering. This method is meant to
 * be used to write the response header.
 * 
 * @param mb data to be written
 */
protected void write(MessageBytes mb) {

    if (mb.getType() == MessageBytes.T_BYTES) {
        ByteChunk bc = mb.getByteChunk();
        write(bc);
    } else if (mb.getType() == MessageBytes.T_CHARS) {
        CharChunk cc = mb.getCharChunk();
        write(cc);
    } else {
        write(mb.toString());
    }

}
 
Example 14
Source File: Parameters.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void processParameters( MessageBytes data, String encoding ) {
    if( data==null || data.isNull() || data.getLength() <= 0 ) {
        return;
    }

    if( data.getType() != MessageBytes.T_BYTES ) {
        data.toBytes();
    }
    ByteChunk bc=data.getByteChunk();
    processParameters( bc.getBytes(), bc.getOffset(),
                       bc.getLength(), getCharset(encoding));
}
 
Example 15
Source File: Cookies.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/** Add all Cookie found in the headers of a request.
 */
public  void processCookies( MimeHeaders headers ) {
    if( headers==null ) {
        return;// nothing to process
    }
    // process each "cookie" header
    int pos=0;
    while( pos>=0 ) {
        // Cookie2: version ? not needed
        pos=headers.findHeader( "Cookie", pos );
        // no more cookie headers headers
        if( pos<0 ) {
            break;
        }

        MessageBytes cookieValue=headers.getValue( pos );
        if( cookieValue==null || cookieValue.isNull() ) {
            pos++;
            continue;
        }

        if( cookieValue.getType() != MessageBytes.T_BYTES ) {
            Exception e = new Exception();
            log.warn("Cookies: Parsing cookie as String. Expected bytes.",
                    e);
            cookieValue.toBytes();
        }
        if(log.isDebugEnabled()) {
            log.debug("Cookies: Parsing b[]: " + cookieValue.toString());
        }
        ByteChunk bc=cookieValue.getByteChunk();
        if (CookieSupport.PRESERVE_COOKIE_HEADER) {
            int len = bc.getLength();
            if (len > 0) {
                byte[] buf = new byte[len];
                System.arraycopy(bc.getBytes(), bc.getOffset(), buf, 0, len);
                processCookieHeader(buf, 0, len);
            }
        } else {
            processCookieHeader( bc.getBytes(),
                    bc.getOffset(),
                    bc.getLength());
        }
        pos++;// search from the next position
    }
}
 
Example 16
Source File: Cookies.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/** Add all Cookie found in the headers of a request.
 */
public  void processCookies( MimeHeaders headers ) {
    if( headers==null ) {
        return;// nothing to process
    }
    // process each "cookie" header
    int pos=0;
    while( pos>=0 ) {
        // Cookie2: version ? not needed
        pos=headers.findHeader( "Cookie", pos );
        // no more cookie headers headers
        if( pos<0 ) {
            break;
        }

        MessageBytes cookieValue=headers.getValue( pos );
        if( cookieValue==null || cookieValue.isNull() ) {
            pos++;
            continue;
        }

        if( cookieValue.getType() != MessageBytes.T_BYTES ) {
            Exception e = new Exception();
            log.warn("Cookies: Parsing cookie as String. Expected bytes.",
                    e);
            cookieValue.toBytes();
        }
        if(log.isDebugEnabled()) {
            log.debug("Cookies: Parsing b[]: " + cookieValue.toString());
        }
        ByteChunk bc=cookieValue.getByteChunk();
        if (CookieSupport.PRESERVE_COOKIE_HEADER) {
            int len = bc.getLength();
            if (len > 0) {
                byte[] buf = new byte[len];
                System.arraycopy(bc.getBytes(), bc.getOffset(), buf, 0, len);
                processCookieHeader(buf, 0, len);
            }
        } else {
            processCookieHeader( bc.getBytes(),
                    bc.getOffset(),
                    bc.getLength());
        }
        pos++;// search from the next position
    }
}