You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2012/11/26 15:36:32 UTC
svn commit: r1413650 - in /mina/mina/branches/2.0/mina-http/src:
main/java/org/apache/mina/http/HttpServerDecoder.java
test/java/org/apache/mina/http/HttpServerDecoderTest.java
Author: elecharny
Date: Mon Nov 26 14:36:31 2012
New Revision: 1413650
URL: http://svn.apache.org/viewvc?rev=1413650&view=rev
Log:
Applied DIRMINA-920 patch
Added:
mina/mina/branches/2.0/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java (with props)
Modified:
mina/mina/branches/2.0/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
Modified: mina/mina/branches/2.0/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
URL: http://svn.apache.org/viewvc/mina/mina/branches/2.0/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java?rev=1413650&r1=1413649&r2=1413650&view=diff
==============================================================================
--- mina/mina/branches/2.0/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java (original)
+++ mina/mina/branches/2.0/mina-http/src/main/java/org/apache/mina/http/HttpServerDecoder.java Mon Nov 26 14:36:31 2012
@@ -72,16 +72,16 @@ public class HttpServerDecoder implement
public static final Pattern COOKIE_SEPARATOR_PATTERN = Pattern.compile(";");
public void decode(final IoSession session, final IoBuffer msg, final ProtocolDecoderOutput out) {
- DecoderState state = (DecoderState)session.getAttribute(DECODER_STATE_ATT);
+ DecoderState state = (DecoderState) session.getAttribute(DECODER_STATE_ATT);
if (null == state) {
- session.setAttribute(DECODER_STATE_ATT, DecoderState.NEW);
- state = (DecoderState)session.getAttribute(DECODER_STATE_ATT);
+ session.setAttribute(DECODER_STATE_ATT, DecoderState.NEW);
+ state = (DecoderState) session.getAttribute(DECODER_STATE_ATT);
}
switch (state) {
case HEAD:
LOG.debug("decoding HEAD");
// grab the stored a partial HEAD request
- final ByteBuffer oldBuffer = (ByteBuffer)session.getAttribute(PARTIAL_HEAD_ATT);
+ final ByteBuffer oldBuffer = (ByteBuffer) session.getAttribute(PARTIAL_HEAD_ATT);
// concat the old buffer and the new incoming one
IoBuffer.allocate(oldBuffer.remaining() + msg.remaining()).put(oldBuffer).put(msg).flip();
// now let's decode like it was a new message
@@ -101,18 +101,12 @@ public class HttpServerDecoder implement
} else {
out.write(rq);
// is it a request with some body content ?
- if (rq.getMethod() == HttpMethod.POST || rq.getMethod() == HttpMethod.PUT) {
- LOG.debug("request with content");
- session.setAttribute(DECODER_STATE_ATT, DecoderState.BODY);
-
- final String contentLen = rq.getHeader("content-length");
+ final String contentLen = rq.getHeader("content-length");
- if (contentLen != null) {
- LOG.debug("found content len : {}", contentLen);
- session.setAttribute(BODY_REMAINING_BYTES, Integer.valueOf(contentLen));
- } else {
- throw new HttpException(HttpStatus.CLIENT_ERROR_LENGTH_REQUIRED, "no content length !");
- }
+ if (contentLen != null) {
+ LOG.debug("found content len : {}", contentLen);
+ session.setAttribute(BODY_REMAINING_BYTES, Integer.valueOf(contentLen));
+ session.setAttribute(DECODER_STATE_ATT, DecoderState.BODY);
} else {
LOG.debug("request without content");
session.setAttribute(DECODER_STATE_ATT, DecoderState.NEW);
@@ -127,10 +121,10 @@ public class HttpServerDecoder implement
final int chunkSize = msg.remaining();
// send the chunk of body
if (chunkSize != 0) {
- final IoBuffer wb = IoBuffer.allocate(msg.remaining());
- wb.put(msg);
- wb.flip();
- out.write(wb);
+ final IoBuffer wb = IoBuffer.allocate(msg.remaining());
+ wb.put(msg);
+ wb.flip();
+ out.write(wb);
}
msg.position(msg.limit());
// do we have reach end of body ?
@@ -160,7 +154,7 @@ public class HttpServerDecoder implement
}
private HttpRequestImpl parseHttpRequestHead(final ByteBuffer buffer) {
- // Java 6 >> String raw = new String(buffer.array(), 0, buffer.limit(), Charset.forName("UTF-8"));
+ // Java 6 >> String raw = new String(buffer.array(), 0, buffer.limit(), Charset.forName("UTF-8"));
final String raw = new String(buffer.array(), 0, buffer.limit());
final String[] headersAndBody = RAW_VALUE_PATTERN.split(raw, -1);
Added: mina/mina/branches/2.0/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
URL: http://svn.apache.org/viewvc/mina/mina/branches/2.0/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java?rev=1413650&view=auto
==============================================================================
--- mina/mina/branches/2.0/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java (added)
+++ mina/mina/branches/2.0/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java Mon Nov 26 14:36:31 2012
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.http;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import org.apache.mina.core.buffer.IoBuffer;
+import org.apache.mina.core.filterchain.IoFilter.NextFilter;
+import org.apache.mina.core.session.DummySession;
+import org.apache.mina.core.session.IoSession;
+import org.apache.mina.filter.codec.AbstractProtocolDecoderOutput;
+import org.apache.mina.filter.codec.ProtocolDecoder;
+import org.apache.mina.http.api.HttpEndOfContent;
+import org.apache.mina.http.api.HttpRequest;
+import org.junit.Test;
+
+public class HttpServerDecoderTest {
+ private static final CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder(); //$NON-NLS-1$
+
+ private static final ProtocolDecoder decoder = new HttpServerDecoder();
+
+ /*
+ * Use a single session for all requests in order to test state management better
+ */
+ private static IoSession session = new DummySession();
+
+ /**
+ * Build an IO buffer containing a simple minimal HTTP request.
+ *
+ * @param method the HTTP method
+ * @param body the option body
+ * @return the built IO buffer
+ * @throws CharacterCodingException if encoding fails
+ */
+ protected static IoBuffer getRequestBuffer(String method, String body) throws CharacterCodingException {
+ IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
+ buffer.putString(method + " / HTTP/1.1\r\nHost: dummy\r\n", encoder);
+
+ if (body != null) {
+ buffer.putString("Content-Length: " + body.length() + "\r\n\r\n", encoder);
+ buffer.putString(body, encoder);
+ } else {
+ buffer.putString("\r\n", encoder);
+ }
+
+ buffer.rewind();
+
+ return buffer;
+ }
+
+ protected static IoBuffer getRequestBuffer(String method) throws CharacterCodingException {
+ return getRequestBuffer(method, null);
+ }
+
+ /**
+ * Execute an HTPP request and return the queue of messages.
+ *
+ * @param method the HTTP method
+ * @param body the optional body
+ * @return the protocol output and its queue of messages
+ * @throws Exception if error occurs (encoding,...)
+ */
+ protected static AbstractProtocolDecoderOutput executeRequest(String method, String body) throws Exception {
+ AbstractProtocolDecoderOutput out = new AbstractProtocolDecoderOutput() {
+ public void flush(NextFilter nextFilter, IoSession session) {
+ }
+ };
+
+ IoBuffer buffer = getRequestBuffer(method, body); //$NON-NLS-1$
+
+ while (buffer.hasRemaining()) {
+ decoder.decode(session, buffer, out);
+ }
+
+ return out;
+ }
+
+ @Test
+ public void testGetRequestWithoutBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("GET", null);
+ assertEquals(2, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testGetRequestBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("GET", "body");
+ assertEquals(3, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof IoBuffer);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testPutRequestWithoutBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("PUT", null);
+ assertEquals(2, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testPutRequestBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("PUT", "body");
+ assertEquals(3, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof IoBuffer);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testPostRequestWithoutBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("POST", null);
+ assertEquals(2, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testPostRequestBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("POST", "body");
+ assertEquals(3, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof IoBuffer);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testDeleteRequestWithoutBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("DELETE", null);
+ assertEquals(2, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+
+ @Test
+ public void testDeleteRequestBody() throws Exception {
+ AbstractProtocolDecoderOutput out = executeRequest("DELETE", "body");
+ assertEquals(3, out.getMessageQueue().size());
+ assertTrue(out.getMessageQueue().poll() instanceof HttpRequest);
+ assertTrue(out.getMessageQueue().poll() instanceof IoBuffer);
+ assertTrue(out.getMessageQueue().poll() instanceof HttpEndOfContent);
+ }
+}
Propchange: mina/mina/branches/2.0/mina-http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain