List of usage examples for io.netty.util.internal AppendableCharSequence toString
@Override
public String toString()
From source file:openbns.commons.net.codec.sts.HttpObjectDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { switch (state()) { case SKIP_CONTROL_CHARS: { try {//from ww w .j a v a2 s . c o m skipControlCharacters(buffer); checkpoint(State.READ_INITIAL); } finally { checkpoint(); } } case READ_INITIAL: try { String[] initialLine = splitInitialLine(readLine(buffer, maxInitialLineLength)); if (initialLine.length < 3) { // Invalid initial line - ignore. checkpoint(State.SKIP_CONTROL_CHARS); return; } message = createMessage(initialLine); checkpoint(State.READ_HEADER); } catch (Exception e) { out.add(invalidMessage(e)); return; } case READ_HEADER: try { State nextState = readHeaders(buffer); checkpoint(nextState); if (nextState == State.READ_CHUNK_SIZE) { if (!chunkedSupported) { throw new IllegalArgumentException("Chunked messages not supported"); } // Chunked encoding - generate StsMessage first. HttpChunks will follow. out.add(message); return; } if (nextState == State.SKIP_CONTROL_CHARS) { // No content is expected. out.add(message); out.add(LastStsContent.EMPTY_LAST_CONTENT); reset(); return; } long contentLength = contentLength(); if (contentLength == 0 || contentLength == -1 && isDecodingRequest()) { out.add(message); out.add(LastStsContent.EMPTY_LAST_CONTENT); reset(); return; } assert nextState == State.READ_FIXED_LENGTH_CONTENT || nextState == State.READ_VARIABLE_LENGTH_CONTENT; out.add(message); if (nextState == State.READ_FIXED_LENGTH_CONTENT) { // chunkSize will be decreased as the READ_FIXED_LENGTH_CONTENT state reads data chunk by chunk. chunkSize = contentLength; } // We return here, this forces decode to be called again where we will decode the content return; } catch (Exception e) { out.add(invalidMessage(e)); return; } case READ_VARIABLE_LENGTH_CONTENT: { // Keep reading data as a chunk until the end of connection is reached. int toRead = Math.min(actualReadableBytes(), maxChunkSize); if (toRead > 0) { ByteBuf content = readBytes(ctx.alloc(), buffer, toRead); if (buffer.isReadable()) { out.add(new DefaultStsContent(content)); } else { // End of connection. out.add(new DefaultLastStsContent(content, validateHeaders)); reset(); } } else if (!buffer.isReadable()) { // End of connection. out.add(LastStsContent.EMPTY_LAST_CONTENT); reset(); } return; } case READ_FIXED_LENGTH_CONTENT: { int readLimit = actualReadableBytes(); // Check if the buffer is readable first as we use the readable byte count // to create the HttpChunk. This is needed as otherwise we may end up with // create a HttpChunk instance that contains an empty buffer and so is // handled like it is the last HttpChunk. // // See https://github.com/commons/commons/issues/433 if (readLimit == 0) { return; } int toRead = Math.min(readLimit, maxChunkSize); if (toRead > chunkSize) { toRead = (int) chunkSize; } ByteBuf content = readBytes(ctx.alloc(), buffer, toRead); chunkSize -= toRead; if (chunkSize == 0) { // Read all content. out.add(new DefaultLastStsContent(content, validateHeaders)); reset(); } else { out.add(new DefaultStsContent(content)); } return; } /** * everything else after this point takes care of reading chunked content. basically, read chunk size, * read chunk, read and ignore the CRLF and repeat until 0 */ case READ_CHUNK_SIZE: try { AppendableCharSequence line = readLine(buffer, maxInitialLineLength); int chunkSize = getChunkSize(line.toString()); this.chunkSize = chunkSize; if (chunkSize == 0) { checkpoint(State.READ_CHUNK_FOOTER); return; } else { checkpoint(State.READ_CHUNKED_CONTENT); } } catch (Exception e) { out.add(invalidChunk(e)); return; } case READ_CHUNKED_CONTENT: { assert chunkSize <= Integer.MAX_VALUE; int toRead = Math.min((int) chunkSize, maxChunkSize); StsContent chunk = new DefaultStsContent(readBytes(ctx.alloc(), buffer, toRead)); chunkSize -= toRead; out.add(chunk); if (chunkSize == 0) { // Read all content. checkpoint(State.READ_CHUNK_DELIMITER); } else { return; } } case READ_CHUNK_DELIMITER: { for (;;) { byte next = buffer.readByte(); if (next == StsConstants.CR) { if (buffer.readByte() == StsConstants.LF) { checkpoint(State.READ_CHUNK_SIZE); return; } } else if (next == StsConstants.LF) { checkpoint(State.READ_CHUNK_SIZE); return; } else { checkpoint(); } } } case READ_CHUNK_FOOTER: try { LastStsContent trailer = readTrailingHeaders(buffer); out.add(trailer); reset(); return; } catch (Exception e) { out.add(invalidChunk(e)); return; } case BAD_MESSAGE: { // Keep discarding until disconnection. buffer.skipBytes(actualReadableBytes()); break; } case UPGRADED: { // Do not touch anything read - other handler will replace this codec with the upgraded protocol codec to // take the trafic over. break; } } }
From source file:openbns.commons.net.codec.sts.HttpObjectDecoder.java
License:Apache License
private State readHeaders(ByteBuf buffer) { headerSize = 0;// ww w . j a va 2 s .c om final StsMessage message = this.message; final StsHeaders headers = message.headers(); AppendableCharSequence line = readHeader(buffer); String name = null; String value = null; if (line.length() > 0) { headers.clear(); do { char firstChar = line.charAt(0); if (name != null && (firstChar == ' ' || firstChar == '\t')) { value = value + ' ' + line.toString().trim(); } else { if (name != null) { headers.add(name, value); } String[] header = splitHeader(line); name = header[0]; value = header[1]; } line = readHeader(buffer); } while (line.length() > 0); // Add the last header. if (name != null) { headers.add(name, value); } } State nextState; if (isContentAlwaysEmpty(message)) { nextState = State.SKIP_CONTROL_CHARS; } else if (contentLength() >= 0) { nextState = State.READ_FIXED_LENGTH_CONTENT; } else { nextState = State.READ_VARIABLE_LENGTH_CONTENT; } return nextState; }
From source file:openbns.commons.net.codec.sts.HttpObjectDecoder.java
License:Apache License
private LastStsContent readTrailingHeaders(ByteBuf buffer) { headerSize = 0;/*from w ww. j ava 2s .co m*/ AppendableCharSequence line = readHeader(buffer); String lastHeader = null; if (line.length() > 0) { LastStsContent trailer = new DefaultLastStsContent(Unpooled.EMPTY_BUFFER, validateHeaders); do { char firstChar = line.charAt(0); if (lastHeader != null && (firstChar == ' ' || firstChar == '\t')) { List<String> current = trailer.trailingHeaders().getAll(lastHeader); if (!current.isEmpty()) { int lastPos = current.size() - 1; String newString = current.get(lastPos) + line.toString().trim(); current.set(lastPos, newString); } else { // Content-Length, Transfer-Encoding, or Trailer } } else { String[] header = splitHeader(line); String name = header[0]; if (!StsHeaders.equalsIgnoreCase(name, StsHeaders.Names.CONTENT_LENGTH)) { trailer.trailingHeaders().add(name, header[1]); } lastHeader = name; } line = readHeader(buffer); } while (line.length() > 0); return trailer; } return LastStsContent.EMPTY_LAST_CONTENT; }