Java Code Examples for io.netty.buffer.ByteBuf#forEachByteDesc()

The following examples show how to use io.netty.buffer.ByteBuf#forEachByteDesc() . 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: ViewHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
/**
 * Parse out the info portion from the header part of the query response.
 *
 * This includes the total rows, but also debug info if attached.
 */
private void parseViewInfo() {
    int rowsStart = -1;
    for (int i = responseContent.readerIndex(); i < responseContent.writerIndex() - 2; i++) {
        byte curr = responseContent.getByte(i);
        byte f1 = responseContent.getByte(i + 1);
        byte f2 = responseContent.getByte(i + 2);

        if (curr == '"' && f1 == 'r' && f2 == 'o') {
            rowsStart = i;
            break;
        }
    }

    if (rowsStart == -1) {
        return;
    }

    ByteBuf info = responseContent.readBytes(rowsStart - responseContent.readerIndex());
    int closingPointer = info.forEachByteDesc(new ByteBufProcessor() {
        @Override
        public boolean process(byte value) throws Exception {
            return value != ',';
        }
    });

    if (closingPointer > 0) {
        info.setByte(closingPointer, '}');
        viewInfoObservable.onNext(info);
    } else {
        //JVMCBC-360 don't forget to release the now unused info ByteBuf
        info.release();
        viewInfoObservable.onNext(Unpooled.EMPTY_BUFFER);
    }
    viewInfoObservable.onCompleted();
    viewParsingState = QUERY_STATE_ROWS;
}
 
Example 2
Source File: HttpObjectDecoder.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
private void parseHeaderLine(final HttpHeaders headers, final ByteBuf buffer, final int lfIndex) {
    // https://tools.ietf.org/html/rfc7230#section-3.2
    // header-field   = field-name ":" OWS field-value OWS
    //
    //      field-name     = token
    //      field-value    = *( field-content / obs-fold )
    //      field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
    //      field-vchar    = VCHAR / obs-text
    //
    //      obs-fold       = CRLF 1*( SP / HTAB )
    //                     ; obsolete line folding
    //                     ; see Section 3.2.4
    //      OWS            = *( SP / HTAB )
    //                     ; optional whitespace
    // https://tools.ietf.org/html/rfc7230#section-3.2.4
    // No whitespace is allowed between the header field-name and colon.  In
    //    the past, differences in the handling of such whitespace have led to
    //    security vulnerabilities in request routing and response handling.
    final int nonControlIndex = lfIndex - 2;
    final int nameStart = buffer.readerIndex();
    // Other checks will be done by header validator if enabled by users
    final int nameEnd = buffer.forEachByte(nameStart, nonControlIndex - nameStart + 1, FIND_COLON);
    if (nameEnd < 0) {
        throw new IllegalArgumentException("Unable to find end of header name");
    }
    if (nameEnd == nameStart) {
        throw new IllegalArgumentException("Empty header name");
    }

    // We assume the allocator will not leak memory, and so we retain + slice to avoid copying data.
    final CharSequence name = newAsciiString(newBufferFrom(buffer.retainedSlice(nameStart, nameEnd - nameStart)));
    final int valueStart;
    if (nameEnd >= nonControlIndex || (valueStart =
            buffer.forEachByte(nameEnd + 1, nonControlIndex - nameEnd, FIND_FIELD_VALUE)) < 0) {
        headers.add(name, emptyAsciiString());
    } else {
        final int valueEnd = buffer.forEachByteDesc(valueStart, nonControlIndex - valueStart + 1, FIND_FIELD_VALUE);
        // We assume the allocator will not leak memory, and so we retain + slice to avoid copying data.
        headers.add(name, newAsciiString(newBufferFrom(buffer.retainedSlice(valueStart,
                valueEnd - valueStart + 1))));
    }
    // Consume the header line bytes from the buffer.
    consumeCRLF(buffer, lfIndex);
}