List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:com.topsec.bdc.platform.api.test.http.snoop.HttpSnoopServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest request = this.request = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx);// www .j a va 2 s. c om } buf.setLength(0); buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n"); buf.append("===================================\r\n"); buf.append("VERSION: ").append(request.getProtocolVersion()).append("\r\n"); buf.append("HOSTNAME: ").append(HttpHeaders.getHost(request, "unknown")).append("\r\n"); buf.append("REQUEST_URI: ").append(request.getUri()).append("\r\n\r\n"); HttpHeaders headers = request.headers(); if (!headers.isEmpty()) { for (Map.Entry<String, String> h : headers) { String key = h.getKey(); String value = h.getValue(); buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n"); } buf.append("\r\n"); } QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); Map<String, List<String>> params = queryStringDecoder.parameters(); if (!params.isEmpty()) { for (Entry<String, List<String>> p : params.entrySet()) { String key = p.getKey(); List<String> vals = p.getValue(); for (String val : vals) { buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n"); } } buf.append("\r\n"); } appendDecoderResult(buf, request); } if (msg instanceof HttpContent) { HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { buf.append("CONTENT: "); buf.append(content.toString(CharsetUtil.UTF_8)); buf.append("\r\n"); appendDecoderResult(buf, request); } if (msg instanceof LastHttpContent) { buf.append("END OF CONTENT\r\n"); LastHttpContent trailer = (LastHttpContent) msg; if (!trailer.trailingHeaders().isEmpty()) { buf.append("\r\n"); for (String name : trailer.trailingHeaders().names()) { for (String value : trailer.trailingHeaders().getAll(name)) { buf.append("TRAILING HEADER: "); buf.append(name).append(" = ").append(value).append("\r\n"); } } buf.append("\r\n"); } if (!writeResponse(trailer, ctx)) { // If keep-alive is off, close the connection once the content is fully written. ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } } } }
From source file:com.touwolf.esl.impl.EslMessageDecoder.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { while (in.isReadable()) { char ch = (char) in.readByte(); if (isBody && ch == '\n') { eslMsg.setBody(decode(sb.toString())); out.add(eslMsg);//from w w w .j ava 2 s.co m isHeader = true; isBody = false; sb = new StringBuilder(); eslMsg = new EslMessageImpl(); } else if (isHeader && ch == '\n' && header == null && !isBody) { if ("api/response".equalsIgnoreCase(eslMsg.get("Content-Type"))) { isHeader = false; isBody = true; } else { if (!eslMsg.getHeaders().isEmpty()) { out.add(eslMsg); } eslMsg = new EslMessageImpl(); } sb = new StringBuilder(); header = null; value = null; } else if (isHeader && ch == ':' && !isBody) { header = sb.toString(); sb = new StringBuilder(); isHeader = false; } else if (ch == '\n' && !isBody) { value = decode(sb.toString()); isHeader = true; sb = new StringBuilder(); if (header != null && value != null) { eslMsg.getHeaders().put(header.trim(), value.trim()); } header = null; value = null; } else { sb.append(ch); } } }
From source file:com.twitter.http2.HttpFrameDecoder.java
License:Apache License
/** * Decode the byte buffer.//from w w w .j a v a 2 s . c o m */ public void decode(ByteBuf buffer) { boolean endStream; boolean endSegment; int minLength; int dependency; int weight; boolean exclusive; int errorCode; while (true) { switch (state) { case READ_CONNECTION_HEADER: while (buffer.isReadable()) { byte b = buffer.readByte(); if (b != CLIENT_CONNECTION_PREFACE[length++]) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Connection Header"); return; } if (length == CLIENT_CONNECTION_PREFACE.length) { state = State.READ_FRAME_HEADER; break; } } if (buffer.isReadable()) { break; } else { return; } case READ_FRAME_HEADER: // Wait until entire header is readable if (buffer.readableBytes() < HTTP_FRAME_HEADER_SIZE) { return; } // Read frame header fields readFrameHeader(buffer); // TODO(jpinner) Sec 4.2 FRAME_SIZE_ERROR if (!isValidFrameHeader(length, type, flags, streamId)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Header"); } else if (frameHasPadding(type, flags)) { state = State.READ_PADDING_LENGTH; } else { paddingLength = 0; state = getNextState(length, type); } break; case READ_PADDING_LENGTH: if (buffer.readableBytes() < 1) { return; } paddingLength = buffer.readUnsignedByte(); --length; if (!isValidPaddingLength(length, type, flags, paddingLength)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Padding Length"); } else { state = getNextState(length, type); } break; case READ_DATA_FRAME: endStream = hasFlag(flags, HTTP_FLAG_END_STREAM); state = State.READ_DATA; if (hasFlag(flags, HTTP_FLAG_PADDED)) { delegate.readDataFramePadding(streamId, endStream, paddingLength + 1); } break; case READ_DATA: // Generate data frames that do not exceed maxChunkSize // maxChunkSize must be > 0 so we cannot infinitely loop int dataLength = Math.min(maxChunkSize, length - paddingLength); // Wait until entire frame is readable if (buffer.readableBytes() < dataLength) { return; } ByteBuf data = buffer.readBytes(dataLength); length -= dataLength; if (length == paddingLength) { if (paddingLength == 0) { state = State.READ_FRAME_HEADER; } else { state = State.SKIP_FRAME_PADDING; } } endStream = length == paddingLength && hasFlag(flags, HTTP_FLAG_END_STREAM); endSegment = length == paddingLength && hasFlag(flags, HTTP_FLAG_END_SEGMENT); delegate.readDataFrame(streamId, endStream, endSegment, data); break; case READ_HEADERS_FRAME: minLength = 0; if (hasFlag(flags, HTTP_FLAG_PRIORITY)) { minLength = 5; } if (buffer.readableBytes() < minLength) { return; } endStream = hasFlag(flags, HTTP_FLAG_END_STREAM); endSegment = hasFlag(flags, HTTP_FLAG_END_SEGMENT); exclusive = false; dependency = 0; weight = 16; if (hasFlag(flags, HTTP_FLAG_PRIORITY)) { dependency = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); weight = buffer.readUnsignedByte() + 1; if (dependency < 0) { dependency = dependency & 0x7FFFFFFF; exclusive = true; } length -= 5; } state = State.READ_HEADER_BLOCK; delegate.readHeadersFrame(streamId, endStream, endSegment, exclusive, dependency, weight); break; case READ_PRIORITY_FRAME: if (buffer.readableBytes() < length) { return; } exclusive = false; dependency = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); weight = buffer.readUnsignedByte() + 1; if (dependency < 0) { dependency = dependency & 0x7FFFFFFF; exclusive = true; } state = State.READ_FRAME_HEADER; delegate.readPriorityFrame(streamId, exclusive, dependency, weight); break; case READ_RST_STREAM_FRAME: if (buffer.readableBytes() < length) { return; } errorCode = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(length); state = State.READ_FRAME_HEADER; delegate.readRstStreamFrame(streamId, errorCode); break; case READ_SETTINGS_FRAME: boolean ack = hasFlag(flags, HTTP_FLAG_ACK); state = State.READ_SETTING; delegate.readSettingsFrame(ack); break; case READ_SETTING: if (length == 0) { state = State.READ_FRAME_HEADER; delegate.readSettingsEnd(); break; } if (buffer.readableBytes() < 6) { return; } int id = getUnsignedShort(buffer, buffer.readerIndex()); int value = getSignedInt(buffer, buffer.readerIndex() + 2); buffer.skipBytes(6); length -= 6; delegate.readSetting(id, value); break; case READ_PUSH_PROMISE_FRAME: if (buffer.readableBytes() < 4) { return; } int promisedStreamId = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); length -= 4; if (promisedStreamId == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Promised-Stream-ID"); } else { state = State.READ_HEADER_BLOCK; delegate.readPushPromiseFrame(streamId, promisedStreamId); } break; case READ_PING_FRAME: if (buffer.readableBytes() < length) { return; } long ping = getSignedLong(buffer, buffer.readerIndex()); buffer.skipBytes(length); boolean pong = hasFlag(flags, HTTP_FLAG_ACK); state = State.READ_FRAME_HEADER; delegate.readPingFrame(ping, pong); break; case READ_GOAWAY_FRAME: if (buffer.readableBytes() < 8) { return; } int lastStreamId = getUnsignedInt(buffer, buffer.readerIndex()); errorCode = getSignedInt(buffer, buffer.readerIndex() + 4); buffer.skipBytes(8); length -= 8; if (length == 0) { state = State.READ_FRAME_HEADER; } else { paddingLength = length; state = State.SKIP_FRAME_PADDING; } delegate.readGoAwayFrame(lastStreamId, errorCode); break; case READ_WINDOW_UPDATE_FRAME: // Wait until entire frame is readable if (buffer.readableBytes() < length) { return; } int windowSizeIncrement = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(length); if (windowSizeIncrement == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Window Size Increment"); } else { state = State.READ_FRAME_HEADER; delegate.readWindowUpdateFrame(streamId, windowSizeIncrement); } break; case READ_CONTINUATION_FRAME_HEADER: // Wait until entire frame header is readable if (buffer.readableBytes() < HTTP_FRAME_HEADER_SIZE) { return; } // Read and validate continuation frame header fields int prevStreamId = streamId; readFrameHeader(buffer); // TODO(jpinner) Sec 4.2 FRAME_SIZE_ERROR // TODO(jpinner) invalid flags if (type != HTTP_CONTINUATION_FRAME || streamId != prevStreamId) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Continuation Frame"); } else { paddingLength = 0; state = State.READ_HEADER_BLOCK; } break; case READ_HEADER_BLOCK: if (length == paddingLength) { boolean endHeaders = hasFlag(flags, HTTP_FLAG_END_HEADERS); if (endHeaders) { state = State.SKIP_FRAME_PADDING; delegate.readHeaderBlockEnd(); } else { state = State.SKIP_FRAME_PADDING_CONTINUATION; } break; } if (!buffer.isReadable()) { return; } int readableBytes = Math.min(buffer.readableBytes(), length - paddingLength); ByteBuf headerBlockFragment = buffer.readBytes(readableBytes); length -= readableBytes; delegate.readHeaderBlock(headerBlockFragment); break; case SKIP_FRAME_PADDING: int numBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numBytes); length -= numBytes; if (length == 0) { state = State.READ_FRAME_HEADER; break; } return; case SKIP_FRAME_PADDING_CONTINUATION: int numPaddingBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numPaddingBytes); length -= numPaddingBytes; if (length == 0) { state = State.READ_CONTINUATION_FRAME_HEADER; break; } return; case FRAME_ERROR: buffer.skipBytes(buffer.readableBytes()); return; default: throw new Error("Shouldn't reach here."); } } }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertDataFrame(ByteBuf frame, int streamId, boolean last, ByteBuf data) { byte type = 0x00; byte flags = 0x00; if (last) {/* www . j a v a2s.c om*/ flags |= 0x01; } int length = assertFrameHeader(frame, type, flags, streamId); assertEquals(data.readableBytes(), length); for (int i = 0; i < length; i++) { assertEquals(data.getByte(i), frame.readByte()); } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertHeadersFrame(ByteBuf frame, int streamId, boolean exclusive, int dependency, int weight, boolean last, ByteBuf headerBlock) { boolean hasPriority = exclusive || dependency != HTTP_DEFAULT_DEPENDENCY || weight != HTTP_DEFAULT_WEIGHT; int maxLength = hasPriority ? HTTP_MAX_LENGTH - 5 : HTTP_MAX_LENGTH; byte type = 0x01; byte flags = 0x00; if (last) {//from ww w.j a v a2s . c o m flags |= 0x01; } if (headerBlock.readableBytes() <= maxLength) { flags |= 0x04; } if (hasPriority) { flags |= 0x20; } int length = assertFrameHeader(frame, type, flags, streamId); if (hasPriority) { assertTrue(length >= 5); if (exclusive) { assertEquals(dependency | 0x80000000, frame.readInt()); } else { assertEquals(dependency, frame.readInt()); } assertEquals(weight - 1, frame.readUnsignedByte()); length -= 5; } assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } while (headerBlock.isReadable()) { type = 0x09; flags = 0x00; if (headerBlock.readableBytes() <= HTTP_MAX_LENGTH) { flags |= 0x04; } length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertPriorityFrame(ByteBuf frame, int streamId, boolean exclusive, int dependency, int weight) { byte type = 0x02; byte flags = 0x00; assertEquals(5, assertFrameHeader(frame, type, flags, streamId)); if (exclusive) { assertEquals(dependency | 0x80000000, frame.readInt()); } else {//w w w .j a v a 2s .c o m assertEquals(dependency, frame.readInt()); } assertEquals(weight - 1, frame.readUnsignedByte()); assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertRstStreamFrame(ByteBuf frame, int streamId, int errorCode) { byte type = 0x03; byte flags = 0x00; assertEquals(4, assertFrameHeader(frame, type, flags, streamId)); assertEquals(errorCode, frame.readInt()); assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertSettingsFrame(ByteBuf frame, boolean ack, int id, int value) { byte type = 0x04; byte flags = ack ? (byte) 0x01 : 0x00; int length = assertFrameHeader(frame, type, flags, 0); if (ack) {/* www .ja v a2 s . c o m*/ assertEquals(0, length); } else { assertEquals(6, length); assertEquals(id, frame.readUnsignedShort()); assertEquals(value, frame.readInt()); } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertPushPromiseFrame(ByteBuf frame, int streamId, int promisedStreamId, ByteBuf headerBlock) { int maxLength = HTTP_MAX_LENGTH - 4; byte type = 0x05; byte flags = 0x00; if (headerBlock.readableBytes() <= maxLength) { flags |= 0x04;/*from ww w . ja v a 2s . co m*/ } int length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length >= 4); assertEquals(promisedStreamId, frame.readInt()); length -= 4; assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } while (headerBlock.isReadable()) { type = 0x09; flags = 0x00; if (headerBlock.readableBytes() <= HTTP_MAX_LENGTH) { flags |= 0x04; } length = assertFrameHeader(frame, type, flags, streamId); assertTrue(length <= headerBlock.readableBytes()); for (int i = 0; i < length; i++) { assertEquals(headerBlock.readByte(), frame.readByte()); } } assertFalse(frame.isReadable()); }
From source file:com.twitter.http2.HttpFrameEncoderTest.java
License:Apache License
private static void assertPingFrame(ByteBuf frame, boolean pong, long data) { byte type = 0x06; byte flags = 0x00; if (pong) {//from w w w . j a v a 2 s. co m flags |= 0x01; } assertEquals(8, assertFrameHeader(frame, type, flags, 0)); assertEquals(data, frame.readLong()); assertFalse(frame.isReadable()); }