netty5/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageDecoder.java

305 lines
10 KiB
Java
Raw Normal View History

2008-11-19 08:22:15 +01:00
/*
* JBoss, Home of Professional Open Source
* Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.handler.codec.http;
import static org.jboss.netty.buffer.ChannelBuffers.*;
2008-11-19 08:22:15 +01:00
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2008-11-19 08:22:15 +01:00
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
/**
* Decodes an Http type message.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Andy Taylor (andy.taylor@jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
2008-11-19 08:22:15 +01:00
*/
public abstract class HttpMessageDecoder extends ReplayingDecoder<HttpMessageDecoder.State> {
2008-11-19 08:22:15 +01:00
private static final Pattern INITIAL_PATTERN = Pattern.compile(
"^\\s*(\\S+)\\s+(\\S+)\\s+(.*)\\s*$");
private static final Pattern HEADER_PATTERN = Pattern.compile(
"^\\s*(\\S+)\\s*:\\s*(.*)\\s*$");
2008-11-19 08:22:15 +01:00
private final boolean mergeChunks;
protected volatile HttpMessage message;
private volatile ChannelBuffer content;
private volatile int chunkSize;
2008-11-19 08:22:15 +01:00
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*
* @apiviz.exclude
*/
protected enum State {
2009-02-12 06:02:22 +01:00
SKIP_CONTROL_CHARS,
2008-11-19 08:22:15 +01:00
READ_INITIAL,
READ_HEADER,
READ_VARIABLE_LENGTH_CONTENT,
2008-11-19 08:22:15 +01:00
READ_FIXED_LENGTH_CONTENT,
READ_CHUNK_SIZE,
READ_CHUNKED_CONTENT,
READ_CHUNK_DELIMITER,
READ_CHUNK_FOOTER;
2008-11-19 08:22:15 +01:00
}
protected HttpMessageDecoder() {
this(true);
}
protected HttpMessageDecoder(boolean mergeChunks) {
2009-02-12 06:02:22 +01:00
super(State.SKIP_CONTROL_CHARS);
this.mergeChunks = mergeChunks;
2008-11-19 08:22:15 +01:00
}
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, State state) throws Exception {
2008-11-19 08:22:15 +01:00
switch (state) {
2009-02-12 06:02:22 +01:00
case SKIP_CONTROL_CHARS: {
try {
skipControlCharacters(buffer);
checkpoint(State.READ_INITIAL);
} finally {
checkpoint();
}
}
2008-11-19 08:22:15 +01:00
case READ_INITIAL: {
readInitial(buffer);
}
case READ_HEADER: {
readHeaders(buffer);
if (message.isChunked()) {
checkpoint(State.READ_CHUNK_SIZE);
if (!mergeChunks) {
return message;
}
} else {
int contentLength = message.getContentLength(-1);
if (contentLength == 0 || contentLength == -1 && isDecodingRequest()) {
content = ChannelBuffers.EMPTY_BUFFER;
return reset();
}
}
2008-11-19 08:22:15 +01:00
//we return null here, this forces decode to be called again where we will decode the content
return null;
}
case READ_VARIABLE_LENGTH_CONTENT: {
2008-11-19 08:22:15 +01:00
if (content == null) {
content = ChannelBuffers.dynamicBuffer(channel.getConfig().getBufferFactory());
2008-11-19 08:22:15 +01:00
}
//this will cause a replay error until the channel is closed where this will read what's left in the buffer
2008-11-19 08:22:15 +01:00
content.writeBytes(buffer.readBytes(buffer.readableBytes()));
return reset();
}
case READ_FIXED_LENGTH_CONTENT: {
//we have a content-length so we just read the correct number of bytes
readFixedLengthContent(buffer);
return reset();
}
/**
* 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: {
String line = readIntoCurrentLine(buffer);
chunkSize = getChunkSize(line);
if (chunkSize == 0) {
checkpoint(State.READ_CHUNK_FOOTER);
return null;
} else {
checkpoint(State.READ_CHUNKED_CONTENT);
2008-11-19 08:22:15 +01:00
}
}
case READ_CHUNKED_CONTENT: {
if (mergeChunks) {
if (content == null) {
content = ChannelBuffers.dynamicBuffer(
chunkSize, channel.getConfig().getBufferFactory());
}
content.writeBytes(buffer, chunkSize);
checkpoint(State.READ_CHUNK_DELIMITER);
} else {
HttpChunk chunk = new DefaultHttpChunk(buffer.readBytes(chunkSize));
checkpoint(State.READ_CHUNK_DELIMITER);
return chunk;
}
2008-11-19 08:22:15 +01:00
}
case READ_CHUNK_DELIMITER: {
for (;;) {
byte next = buffer.readByte();
if (next == HttpCodecUtil.CR) {
if (buffer.readByte() == HttpCodecUtil.LF) {
checkpoint(State.READ_CHUNK_SIZE);
return null;
}
} else if (next == HttpCodecUtil.LF) {
checkpoint(State.READ_CHUNK_SIZE);
return null;
}
2008-11-19 08:22:15 +01:00
}
}
case READ_CHUNK_FOOTER: {
String line = readIntoCurrentLine(buffer);
if (line.trim().length() == 0) {
if (mergeChunks) {
return reset();
} else {
reset();
// The last chunk, which is empty
return new DefaultHttpChunk(EMPTY_BUFFER);
}
} else {
checkpoint(State.READ_CHUNK_FOOTER);
}
2009-02-12 07:26:05 +01:00
return null;
}
2008-11-19 08:22:15 +01:00
default: {
throw new Error("Shouldn't reach here.");
}
}
}
private Object reset() {
// TODO: Do we need to set message to null here?
2008-11-19 08:22:15 +01:00
message.setContent(content);
content = null;
2009-02-12 06:02:22 +01:00
checkpoint(State.SKIP_CONTROL_CHARS);
2008-11-19 08:22:15 +01:00
return message;
}
2009-02-12 06:02:22 +01:00
private void skipControlCharacters(ChannelBuffer buffer) {
for (;;) {
char c = (char) buffer.readUnsignedByte();
if (!Character.isISOControl(c) &&
!Character.isWhitespace(c)) {
buffer.readerIndex(buffer.readerIndex() - 1);
break;
}
}
}
2008-11-19 08:22:15 +01:00
private void readFixedLengthContent(ChannelBuffer buffer) {
int length = message.getContentLength(-1);
2008-11-19 08:22:15 +01:00
if (content == null) {
content = buffer.readBytes(length);
} else {
content.writeBytes(buffer.readBytes(length));
2008-11-19 08:22:15 +01:00
}
}
private void readHeaders(ChannelBuffer buffer) {
message.clearHeaders();
String line = readIntoCurrentLine(buffer);
String lastHeader = null;
while (line.length() != 0) {
2008-11-19 08:22:15 +01:00
if (line.startsWith(" ") || line.startsWith("\t")) {
List<String> current = message.getHeaders(lastHeader);
int lastPos = current.size() - 1;
String newString = current.get(lastPos) + line.trim();
current.remove(lastPos);
current.add(newString);
}
else {
String[] header = splitHeader(line);
message.addHeader(header[0], header[1]);
lastHeader = header[0];
}
line = readIntoCurrentLine(buffer);
}
State nextState;
if (message.isChunked()) {
nextState = State.READ_CHUNK_SIZE;
} else if (message.getContentLength(-1) >= 0) {
nextState = State.READ_FIXED_LENGTH_CONTENT;
} else {
nextState = State.READ_VARIABLE_LENGTH_CONTENT;
2008-11-19 08:22:15 +01:00
}
checkpoint(nextState);
}
protected abstract boolean isDecodingRequest();
2008-11-19 08:22:15 +01:00
protected abstract void readInitial(ChannelBuffer buffer) throws Exception;
private int getChunkSize(String hex) {
2009-02-12 06:23:39 +01:00
hex = hex.trim();
for (int i = 0; i < hex.length(); i ++) {
char c = hex.charAt(i);
if (c == ';' || Character.isWhitespace(c) || Character.isISOControl(c)) {
hex = hex.substring(0, i);
break;
}
2009-02-12 06:10:25 +01:00
}
2009-02-12 06:23:39 +01:00
2009-02-12 06:10:25 +01:00
return Integer.parseInt(hex, 16);
2008-11-19 08:22:15 +01:00
}
protected String readIntoCurrentLine(ChannelBuffer buffer) {
StringBuilder sb = new StringBuilder(64);
2008-11-19 08:22:15 +01:00
while (true) {
byte nextByte = buffer.readByte();
2008-11-19 08:22:15 +01:00
if (nextByte == HttpCodecUtil.CR) {
nextByte = buffer.readByte();
if (nextByte == HttpCodecUtil.LF) {
return sb.toString();
}
2008-11-19 08:22:15 +01:00
}
else if (nextByte == HttpCodecUtil.LF) {
return sb.toString();
}
else {
sb.append((char) nextByte);
}
}
}
protected String[] splitInitial(String sb) {
Matcher m = INITIAL_PATTERN.matcher(sb);
if (m.matches()) {
return new String[] { m.group(1), m.group(2), m.group(3) };
} else {
throw new IllegalArgumentException("Invalid initial line: " + sb);
2008-11-19 08:22:15 +01:00
}
}
private String[] splitHeader(String sb) {
Matcher m = HEADER_PATTERN.matcher(sb);
if (m.matches()) {
return new String[] { m.group(1), m.group(2) };
} else {
throw new IllegalArgumentException("Invalid header syntax: " + sb);
2008-11-19 08:22:15 +01:00
}
}
}