You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2011/07/21 22:42:41 UTC
svn commit: r1149352 - in /mina/branches/3.0/http: ./ src/ src/main/
src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/mina/ src/test/ src/test/java/ src/test/java/org/
src/test/java/org/apache/ src/test/java/org/apac...
Author: jvermillard
Date: Thu Jul 21 20:42:39 2011
New Revision: 1149352
URL: http://svn.apache.org/viewvc?rev=1149352&view=rev
Log:
simple http filter, made copy/pasting deft code (http://incubator.apache.org/projects/deft.html)
actually it just decode http requests. It's a proof of concept for the new API
Added:
mina/branches/3.0/http/
mina/branches/3.0/http/pom.xml (with props)
mina/branches/3.0/http/src/
mina/branches/3.0/http/src/main/
mina/branches/3.0/http/src/main/java/
mina/branches/3.0/http/src/main/java/org/
mina/branches/3.0/http/src/main/java/org/apache/
mina/branches/3.0/http/src/main/java/org/apache/mina/
mina/branches/3.0/http/src/main/java/org/apache/mina/ArrayUtil.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/HttpException.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/HttpProtocol.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/HttpRequest.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/HttpServerDescriptor.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/HttpVerb.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/MalFormedHttpRequest.java (with props)
mina/branches/3.0/http/src/main/java/org/apache/mina/PartialHttpRequest.java (with props)
mina/branches/3.0/http/src/test/
mina/branches/3.0/http/src/test/java/
mina/branches/3.0/http/src/test/java/org/
mina/branches/3.0/http/src/test/java/org/apache/
mina/branches/3.0/http/src/test/java/org/apache/mina/
mina/branches/3.0/http/src/test/java/org/apache/mina/http/
mina/branches/3.0/http/src/test/java/org/apache/mina/http/HttpTest.java (with props)
mina/branches/3.0/http/src/test/resources/
mina/branches/3.0/http/src/test/resources/log4j.properties (with props)
Added: mina/branches/3.0/http/pom.xml
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/pom.xml?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/pom.xml (added)
+++ mina/branches/3.0/http/pom.xml Thu Jul 21 20:42:39 2011
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<!--
+ 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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.mina</groupId>
+ <artifactId>mina-parent</artifactId>
+ <version>3.0.0-M1-SNAPSHOT</version>
+ </parent>
+ <artifactId>mina-http</artifactId>
+ <name>Apache MINA HTTP</name>
+ <packaging>bundle</packaging>
+ <properties>
+ <symbolicName>${project.groupId}.http</symbolicName>
+ <exportedPackage>${project.groupId}</exportedPackage>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>mina-core</artifactId>
+ <version>${project.version}</version>
+ <type>bundle</type>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>r09</version>
+ </dependency>
+ </dependencies>
+</project>
+
Propchange: mina/branches/3.0/http/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/ArrayUtil.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/ArrayUtil.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/ArrayUtil.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/ArrayUtil.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+public class ArrayUtil {
+
+ public static String[] dropFromEndWhile(String[] array, String regex) {
+ for (int i = array.length - 1; i >= 0; i--) {
+ if (!array[i].trim().equals("")) {
+ String[] trimmedArray = new String[i + 1];
+ System.arraycopy(array, 0, trimmedArray, 0, i + 1);
+ return trimmedArray;
+ }
+ }
+ return null;
+
+ }
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/ArrayUtil.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpException.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/HttpException.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/HttpException.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/HttpException.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+@SuppressWarnings("serial")
+public class HttpException extends RuntimeException {
+
+ private final int statusCode;
+
+ public HttpException(int statusCode) {
+ this(statusCode, "");
+ }
+
+ public HttpException(int statusCode, String message) {
+ super(message);
+ this.statusCode = statusCode;
+ }
+
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpProtocol.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/HttpProtocol.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/HttpProtocol.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/HttpProtocol.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,112 @@
+/*
+ * 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;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.mina.api.IdleStatus;
+import org.apache.mina.api.IoFilter;
+import org.apache.mina.api.IoSession;
+import org.apache.mina.transport.tcp.NioTcpSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HttpProtocol implements IoFilter {
+
+ private final static Logger logger = LoggerFactory.getLogger(HttpProtocol.class);
+
+ // a queue of half-baked (pending/unfinished) HTTP post request
+ private final Map<IoSession, PartialHttpRequest> partials = new HashMap<IoSession, PartialHttpRequest>();
+
+ @Override
+ public String toString() {
+ return "HttpProtocol";
+ }
+
+ @Override
+ public void sessionCreated(IoSession session) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void sessionOpened(IoSession session) {
+ logger.debug("Session {} open", session);
+ }
+
+ @Override
+ public void sessionClosed(IoSession session) {
+ logger.debug("Session {} closed", session);
+ }
+
+ @Override
+ public void sessionIdle(IoSession session, IdleStatus status) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public Object messageReceived(IoSession iosession, Object message) {
+ logger.debug("handle read...");
+
+ NioTcpSession session = (NioTcpSession) iosession;
+ ByteBuffer buffer = (ByteBuffer) message;
+ // do we have any unfinished http post requests for this channel?
+ HttpRequest request = null;
+
+ if (partials.containsKey(session)) {
+ request = HttpRequest.continueParsing(buffer, partials.get(session));
+ if (!(request instanceof PartialHttpRequest)) { // received the entire payload/body
+ partials.remove(session);
+ }
+ } else { // we received a partial request, will keep it until it's totally built
+ request = HttpRequest.of(buffer);
+ if (request instanceof PartialHttpRequest) {
+ partials.put(session, (PartialHttpRequest) request);
+ }
+ }
+ // set extra request info
+ request.setRemoteHost(session.getRemoteAddress().getAddress());
+ request.setRemotePort(session.getRemoteAddress().getPort());
+ request.setServerHost(session.getLocalAddress().getAddress());
+ request.setServerPort(session.getLocalAddress().getPort());
+
+ if (request.isKeepAlive()) {
+ // TODO : handle keep-alive
+ }
+
+ // Only close if not async. In that case its up to RH to close it (+ don't close if it's a partial request).
+ if (!(request instanceof PartialHttpRequest)) {
+ return request;
+ } else {
+ return null;
+ }
+
+ }
+
+ @Override
+ public Object messageWriting(IoSession session, Object message) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpProtocol.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpRequest.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/HttpRequest.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/HttpRequest.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/HttpRequest.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,277 @@
+/*
+ * 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;
+
+import java.net.InetAddress;
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableMultimap;
+
+public class HttpRequest {
+
+ private final String requestLine;
+ private final HttpVerb method;
+ private final String requestedPath; // correct name?
+ private final String version;
+ private Map<String, String> headers;
+ private ImmutableMultimap<String, String> parameters;
+ private String body;
+ private boolean keepAlive;
+ private InetAddress remoteHost;
+ private InetAddress serverHost;
+ private int remotePort;
+ private int serverPort;
+
+ /** Regex to parse HttpRequest Request Line */
+ public static final Pattern REQUEST_LINE_PATTERN = Pattern.compile(" ");
+ /** Regex to parse out QueryString from HttpRequest */
+ public static final Pattern QUERY_STRING_PATTERN = Pattern.compile("\\?");
+ /** Regex to parse out parameters from query string */
+ public static final Pattern PARAM_STRING_PATTERN = Pattern.compile("\\&|;"); // Delimiter is either & or ;
+ /** Regex to parse out key/value pairs */
+ public static final Pattern KEY_VALUE_PATTERN = Pattern.compile("=");
+ /** Regex to parse raw headers and body */
+ public static final Pattern RAW_VALUE_PATTERN = Pattern.compile("\\r\\n\\r\\n"); // TODO fix a better regexp for
+ // this
+ /** Regex to parse raw headers from body */
+ public static final Pattern HEADERS_BODY_PATTERN = Pattern.compile("\\r\\n");
+ /** Regex to parse header name and value */
+ public static final Pattern HEADER_VALUE_PATTERN = Pattern.compile(": ");
+
+ /**
+ * Creates a new HttpRequest
+ *
+ * @param requestLine The Http request text line
+ * @param headers The Http request headers
+ */
+ public HttpRequest(String requestLine, Map<String, String> headers) {
+ this.requestLine = requestLine;
+ String[] elements = REQUEST_LINE_PATTERN.split(requestLine);
+ method = HttpVerb.valueOf(elements[0]);
+ String[] pathFrags = QUERY_STRING_PATTERN.split(elements[1]);
+ requestedPath = pathFrags[0];
+ version = elements[2];
+ this.headers = headers;
+ body = null;
+ initKeepAlive();
+ parameters = parseParameters(elements[1]);
+ }
+
+ /**
+ * Creates a new HttpRequest
+ *
+ * @param requestLine The Http request text line
+ * @param headers The Http request headers
+ * @param body The Http request posted body
+ */
+ public HttpRequest(String requestLine, Map<String, String> headers, String body) {
+ this(requestLine, headers);
+ this.body = body;
+ }
+
+ public static HttpRequest of(ByteBuffer buffer) {
+ try {
+ String raw = new String(buffer.array(), 0, buffer.limit(), Charsets.ISO_8859_1);
+ String[] headersAndBody = RAW_VALUE_PATTERN.split(raw);
+ String[] headerFields = HEADERS_BODY_PATTERN.split(headersAndBody[0]);
+ headerFields = ArrayUtil.dropFromEndWhile(headerFields, "");
+
+ String requestLine = headerFields[0];
+ Map<String, String> generalHeaders = new HashMap<String, String>();
+ for (int i = 1; i < headerFields.length; i++) {
+ String[] header = HEADER_VALUE_PATTERN.split(headerFields[i]);
+ generalHeaders.put(header[0].toLowerCase(), header[1]);
+ }
+
+ String body = "";
+ for (int i = 1; i < headersAndBody.length; ++i) { // First entry contains headers
+ body += headersAndBody[i];
+ }
+
+ if (requestLine.contains("POST")) {
+ int contentLength = Integer.parseInt(generalHeaders.get("content-length"));
+ if (contentLength > body.length()) {
+ return new PartialHttpRequest(requestLine, generalHeaders, body);
+ }
+ }
+ return new HttpRequest(requestLine, generalHeaders, body);
+ } catch (Exception t) {
+ return MalFormedHttpRequest.instance;
+ }
+ }
+
+ public static HttpRequest continueParsing(ByteBuffer buffer, PartialHttpRequest unfinished) {
+ String nextChunk = new String(buffer.array(), 0, buffer.limit(), Charsets.US_ASCII);
+ unfinished.appendBody(nextChunk);
+
+ int contentLength = Integer.parseInt(unfinished.getHeader("Content-Length"));
+ if (contentLength > unfinished.getBody().length()) {
+ return unfinished;
+ } else {
+ return new HttpRequest(unfinished.getRequestLine(), unfinished.getHeaders(), unfinished.getBody());
+ }
+ }
+
+ public String getRequestLine() {
+ return requestLine;
+ }
+
+ public String getRequestedPath() {
+ return requestedPath;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public Map<String, String> getHeaders() {
+ return Collections.unmodifiableMap(headers);
+ }
+
+ public String getHeader(String name) {
+ return headers.get(name.toLowerCase());
+ }
+
+ public HttpVerb getMethod() {
+ return method;
+ }
+
+ /**
+ * Returns the value of a request parameter as a String, or null if the parameter does not exist.
+ *
+ * You should only use this method when you are sure the parameter has only one value. If the parameter might have
+ * more than one value, use getParameterValues(java.lang.String). If you use this method with a multi-valued
+ * parameter, the value returned is equal to the first value in the array returned by getParameterValues.
+ */
+ public String getParameter(String name) {
+ Collection<String> values = parameters.get(name);
+ return values.isEmpty() ? null : values.iterator().next();
+ }
+
+ public Map<String, Collection<String>> getParameters() {
+ return parameters.asMap();
+ }
+
+ public String getBody() {
+ return body;
+ }
+
+ public InetAddress getRemoteHost() {
+ return remoteHost;
+ }
+
+ public InetAddress getServerHost() {
+ return serverHost;
+ }
+
+ public int getRemotePort() {
+ return remotePort;
+ }
+
+ public int getServerPort() {
+ return serverPort;
+ }
+
+ protected void setRemoteHost(InetAddress host) {
+ remoteHost = host;
+ }
+
+ protected void setServerHost(InetAddress host) {
+ serverHost = host;
+ }
+
+ protected void setRemotePort(int port) {
+ remotePort = port;
+ }
+
+ protected void setServerPort(int port) {
+ serverPort = port;
+ }
+
+ /**
+ * Returns a collection of all values associated with the provided parameter. If no values are found and empty
+ * collection is returned.
+ */
+ public Collection<String> getParameterValues(String name) {
+ return parameters.get(name);
+ }
+
+ public boolean isKeepAlive() {
+ return keepAlive;
+ }
+
+ @Override
+ public String toString() {
+ String result = "METHOD: " + method + "\n";
+ result += "VERSION: " + version + "\n";
+ result += "PATH: " + requestedPath + "\n";
+
+ result += "--- HEADER --- \n";
+ for (String key : headers.keySet()) {
+ String value = headers.get(key);
+ result += key + ":" + value + "\n";
+ }
+
+ result += "--- PARAMETERS --- \n";
+ for (String key : parameters.keySet()) {
+ Collection<String> values = parameters.get(key);
+ for (String value : values) {
+ result += key + ":" + value + "\n";
+ }
+ }
+ return result;
+ }
+
+ private ImmutableMultimap<String, String> parseParameters(String requestLine) {
+ ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
+ String[] str = QUERY_STRING_PATTERN.split(requestLine);
+
+ // Parameters exist
+ if (str.length > 1) {
+ String[] paramArray = PARAM_STRING_PATTERN.split(str[1]);
+ for (String keyValue : paramArray) {
+ String[] keyValueArray = KEY_VALUE_PATTERN.split(keyValue);
+ // We need to check if the parameter has a value associated with it.
+ if (keyValueArray.length > 1) {
+ builder.put(keyValueArray[0], keyValueArray[1]); // name, value
+ }
+ }
+ }
+ return builder.build();
+ }
+
+ private void initKeepAlive() {
+ String connection = getHeader("Connection");
+ if ("keep-alive".equalsIgnoreCase(connection)) {
+ keepAlive = true;
+ } else if ("close".equalsIgnoreCase(connection) || requestLine.contains("1.0")) {
+ keepAlive = false;
+ } else {
+ keepAlive = true;
+ }
+ }
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpRequest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpServerDescriptor.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/HttpServerDescriptor.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/HttpServerDescriptor.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/HttpServerDescriptor.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,44 @@
+/*
+ * 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;
+
+/**
+ * This class provides a possiblity to change the tunables used by Deft for the http server configuration.
+ * Do not change the values unless you know what you are doing.
+ */
+public class HttpServerDescriptor {
+
+ /** The number of seconds Deft will wait for subsequent socket activity before closing the connection */
+ public static int KEEP_ALIVE_TIMEOUT = 30 * 1000; // 30s
+
+ /**
+ * Size of the read (receive) buffer.
+ * "Ideally, an HTTP request should not go beyond 1 packet.
+ * The most widely used networks limit packets to approximately 1500 bytes, so if you can constrain each request
+ * to fewer than 1500 bytes, you can reduce the overhead of the request stream." (from: http://bit.ly/bkksUu)
+ */
+ public static int READ_BUFFER_SIZE = 1024; // 1024 bytes
+
+ /**
+ * Size of the write (send) buffer.
+ */
+ public static int WRITE_BUFFER_SIZE = 1024; // 1024 bytes
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpServerDescriptor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpVerb.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/HttpVerb.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/HttpVerb.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/HttpVerb.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+public enum HttpVerb {
+
+ GET,
+ HEAD,
+ POST,
+ PUT,
+ DELETE,
+ OPTIONS,
+ TRACE,
+ CONNECT
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/HttpVerb.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/MalFormedHttpRequest.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/MalFormedHttpRequest.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/MalFormedHttpRequest.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/MalFormedHttpRequest.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+import com.google.common.collect.Maps;
+
+
+public class MalFormedHttpRequest extends HttpRequest {
+
+ public static final MalFormedHttpRequest instance = new MalFormedHttpRequest();
+
+ /* Dummy HttpRequest that represents a malformed client HTTP request */
+ private MalFormedHttpRequest() {
+ super("GET / Mal formed request\r\n", Maps.<String, String>newHashMap());
+ }
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/MalFormedHttpRequest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/main/java/org/apache/mina/PartialHttpRequest.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/main/java/org/apache/mina/PartialHttpRequest.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/main/java/org/apache/mina/PartialHttpRequest.java (added)
+++ mina/branches/3.0/http/src/main/java/org/apache/mina/PartialHttpRequest.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+import java.util.Map;
+
+
+/**
+ * Represents an unfinished "dummy" HTTP request, e.g, an HTTP POST request where the entire payload hasn't been
+ * received.
+ * (E.g. because the size of the underlying (OS) socket's read buffer has a fixed size.)
+ *
+ */
+
+public class PartialHttpRequest extends HttpRequest {
+
+ private final String requestLine;
+ private String unfinishedBody;
+
+ public PartialHttpRequest(String requestLine, Map<String, String> generalHeaders, String body) {
+ super("POST <> Unfinished request\r\n", generalHeaders);
+ this.requestLine = requestLine;
+ this.unfinishedBody = body;
+ }
+
+ public void appendBody(String nextChunk) {
+ unfinishedBody += nextChunk;
+ }
+
+ @Override
+ public String getBody() {
+ return unfinishedBody;
+ }
+
+ @Override
+ public String getRequestLine() {
+ return requestLine;
+ }
+
+}
Propchange: mina/branches/3.0/http/src/main/java/org/apache/mina/PartialHttpRequest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/test/java/org/apache/mina/http/HttpTest.java
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/test/java/org/apache/mina/http/HttpTest.java?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/test/java/org/apache/mina/http/HttpTest.java (added)
+++ mina/branches/3.0/http/src/test/java/org/apache/mina/http/HttpTest.java Thu Jul 21 20:42:39 2011
@@ -0,0 +1,51 @@
+package org.apache.mina.http;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.HttpProtocol;
+import org.apache.mina.api.IoService;
+import org.apache.mina.api.IoServiceListener;
+import org.apache.mina.api.IoSession;
+import org.apache.mina.filter.logging.LoggingFilter;
+import org.apache.mina.service.OneThreadSelectorStrategy;
+import org.apache.mina.service.SelectorFactory;
+import org.apache.mina.transport.tcp.NioSelectorProcessor;
+import org.apache.mina.transport.tcp.nio.NioTcpServer;
+import org.junit.Test;
+
+public class HttpTest {
+
+ @Test
+ public void simpleServer() throws Exception {
+
+ OneThreadSelectorStrategy strategy = new OneThreadSelectorStrategy(new SelectorFactory(
+ NioSelectorProcessor.class));
+ NioTcpServer acceptor = new NioTcpServer(strategy);
+
+ acceptor.addListener(new IoServiceListener() {
+
+ @Override
+ public void sessionDestroyed(IoSession session) {
+ }
+
+ @Override
+ public void sessionCreated(IoSession session) {
+ session.getFilterChain().getChain().add(new HttpProtocol());
+ session.getFilterChain().getChain().add(new LoggingFilter("Logging"));
+ }
+
+ @Override
+ public void serviceInactivated(IoService service) {
+ }
+
+ @Override
+ public void serviceActivated(IoService service) {
+ }
+ });
+
+ acceptor.bind(new InetSocketAddress(8080));
+ Thread.sleep(20000);
+ acceptor.unbindAll();
+
+ }
+}
Propchange: mina/branches/3.0/http/src/test/java/org/apache/mina/http/HttpTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: mina/branches/3.0/http/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/mina/branches/3.0/http/src/test/resources/log4j.properties?rev=1149352&view=auto
==============================================================================
--- mina/branches/3.0/http/src/test/resources/log4j.properties (added)
+++ mina/branches/3.0/http/src/test/resources/log4j.properties Thu Jul 21 20:42:39 2011
@@ -0,0 +1,26 @@
+#############################################################################
+# 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.
+#############################################################################
+# Please don't modify the log level until we reach to acceptable test coverage.
+# It's very useful when I test examples manually.
+log4j.rootCategory=DEBUG, stdout
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %p [%c] - %m%n
+
+# you could use this pattern to test the MDC with the Chat server
+#log4j.appender.stdout.layout.ConversionPattern=[%d{HH:mm:ss}] %t %p %X{name} [%X{user}] [%X{remoteIp}:%X{remotePort}] [%c] - %m%n
Propchange: mina/branches/3.0/http/src/test/resources/log4j.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain