Skip to content

Commit

Permalink
Initial HTTP2 work (single HTTP2 PDU)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmaury committed Mar 17, 2015
1 parent 6022738 commit 9979693
Show file tree
Hide file tree
Showing 31 changed files with 1,645 additions and 6 deletions.
12 changes: 12 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ public void sessionClosed(IoSession session) {
// ----------- Helper methods ---------------------------------------------

@SuppressWarnings("unchecked")
private DECODING_STATE getDecodingState(IoSession session) {
protected DECODING_STATE getDecodingState(IoSession session) {
return (DECODING_STATE) session.getAttribute(DECODER);
}

@SuppressWarnings("unchecked")
private ENCODING_STATE getEncodingState(IoSession session) {
protected ENCODING_STATE getEncodingState(IoSession session) {
return (ENCODING_STATE) session.getAttribute(ENCODER);
}

Expand Down
16 changes: 12 additions & 4 deletions http/src/main/java/org/apache/mina/http/HttpRequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class HttpRequestImpl implements HttpRequest {

private final HttpMethod method;

private final String requestedPath;
private final String targetURI;

private final Map<String, String> headers;

public HttpRequestImpl(HttpVersion version, HttpMethod method, String requestedPath, Map<String, String> headers) {
public HttpRequestImpl(HttpVersion version, HttpMethod method, String targetURI, Map<String, String> headers) {
this.version = version;
this.method = method;
this.requestedPath = requestedPath;
this.targetURI = targetURI;
this.headers = Collections.unmodifiableMap(headers);
}

Expand All @@ -58,6 +58,14 @@ public HttpVersion getProtocolVersion() {
return version;
}

/**
* {@inheritDoc}
*/
@Override
public String getTargetURI() {
return targetURI;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -148,7 +156,7 @@ public String toString() {

sb.append("HTTP REQUEST METHOD: ").append(method).append('\n');
sb.append("VERSION: ").append(version).append('\n');
sb.append("PATH: ").append(requestedPath).append('\n');
sb.append("PATH: ").append(targetURI).append('\n');

sb.append("--- HEADER --- \n");

Expand Down
14 changes: 14 additions & 0 deletions http/src/main/java/org/apache/mina/http/api/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ public interface HttpRequest extends HttpMessage {
* @return the method
*/
HttpMethod getMethod();

/**
* Return the HTTP protocol version of the request {@link HttpVersion}.
*
* @return the HTTP version
*/
HttpVersion getProtocolVersion();

/**
* Return the target URI of the request.
*
* @return the target URI
*/
String getTargetURI();
}
Binary file added http2/Http2Frames.ods
Binary file not shown.
56 changes: 56 additions & 0 deletions http2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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-M3-SNAPSHOT</version>
</parent>

<artifactId>mina-http2</artifactId>
<name>Apache MINA HTTP2 ${project.version}</name>
<packaging>jar</packaging>
<description>Low level HTTP2 codec for building simple &amp; fast HTTP2 server and clients</description>

<properties>
<symbolicName>${project.groupId}.http2</symbolicName>
<exportedPackage>${project.groupId}.http2.api</exportedPackage>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mina-core</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mina-http</artifactId>
</dependency>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>hpack</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
*
*/
package org.apache.mina.http2.api;

import java.nio.ByteBuffer;

/**
* @author jeffmaury
*
*/
public class BytePartialDecoder implements PartialDecoder<byte[]> {
private int offset;
private byte[] value;

/**
* Decode an byte array.
*
* @param size the size of the byte array to decode
*/
public BytePartialDecoder(int size) {
this.offset = 0;
this.value = new byte[size];
}

public boolean consume(ByteBuffer buffer) {
if (value.length - offset == 0) {
throw new IllegalStateException();
}
int length = Math.min(buffer.remaining(), value.length - offset);
buffer.get(value, offset, length);
offset += length;
return value.length - offset == 0;
}

public byte[] getValue() {
if (value.length - offset > 0) {
throw new IllegalStateException();
}
return value;
}

/**
* {@inheritDoc}
*/
@Override
public void reset() {
offset = 0;
}
}
193 changes: 193 additions & 0 deletions http2/src/main/java/org/apache/mina/http2/api/Http2Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
*
*/
package org.apache.mina.http2.api;

import java.nio.charset.Charset;

/**
* @author jeffmaury
*
*/
public final class Http2Constants {
/**
* Mask used when decoding on a 4 byte boundary, masking the reserved
* bit
*/
public static final int HTTP2_31BITS_MASK = 0x7FFFFFFF;

/**
* Mask used when decoding on a 4 byte boundary, retrieving
* the exclusive bit
*/
public static final int HTTP2_EXCLUSIVE_MASK = 0x80000000;

/*
* Frame types
*/
/**
* DATA frame
*/
public static final int FRAME_TYPE_DATA = 0x00;

/**
* HEADERS frame
*/
public static final int FRAME_TYPE_HEADERS = 0x01;

/**
* PRIORITY frame
*/
public static final int FRAME_TYPE_PRIORITY = 0x02;

/**
* RST_STREAM frame
*/
public static final int FRAME_TYPE_RST_STREAM = 0x03;

/**
* SETTINGS stream
*/
public static final int FRAME_TYPE_SETTINGS = 0x04;

/**
* PUSH_PROMISE frame
*/
public static final int FRAME_TYPE_PUSH_PROMISE = 0x05;

/**
* PING frame
*/
public static final int FRAME_TYPE_PING = 0x06;

/**
* GOAWAY frame
*/
public static final int FRAME_TYPE_GOAWAY = 0x07;

/**
* WINDOW_UPDATE frame
*/
public static final int FRAME_TYPE_WINDOW_UPDATE = 0x08;

/**
* CONTINUATION frame
*/
public static final int FRAME_TYPE_CONTINUATION = 0x09;

/*
* Flags
*/
public static final byte FLAGS_END_STREAM = 0x01;

public static final byte FLAGS_ACK = 0x01;

public static final byte FLAGS_END_HEADERS = 0x04;

public static final byte FLAGS_PADDING = 0x08;

public static final byte FLAGS_PRIORITY = 0x20;

/*
* Error codes
*/
/**
* The associated condition is not as a result of an error. For example, a GOAWAY might include this code to indicate graceful shutdown of a connection.
*/
public static final int NO_ERROR = 0x0;

/**
* The endpoint detected an unspecific protocol error. This error is for use when a more specific error code is not available.
*/
public static final int PROTOCOL_ERROR = 0x1;

/**
* The endpoint encountered an unexpected internal error.
*/
public static final int INTERNAL_ERROR = 0x2;

/**
* The endpoint detected that its peer violated the flow control protocol.
*/
public static final int FLOW_CONTROL_ERROR = 0x3;

/**
* The endpoint sent a SETTINGS frame, but did not receive a response in a timely manner. See Settings Synchronization (Section 6.5.3).
*/
public static final int SETTINGS_TIMEOUT = 0x4;

/**
* The endpoint received a frame after a stream was half closed.
*/
public static final int STREAM_CLOSED = 0x5;

/**
* The endpoint received a frame with an invalid size.
*/
public static final int FRAME_SIZE_ERROR = 0x6;

/**
* The endpoint refuses the stream prior to performing any application processing, see Section 8.1.4 for details.
*/
public static final int REFUSED_STREAM = 0x7;

/**
* Used by the endpoint to indicate that the stream is no longer needed.
*/
public static final int CANCEL = 0x8;

/**
* The endpoint is unable to maintain the header compression context for the connection.
*/
public static final int COMPRESSION_ERROR = 0x9;

/**
* The connection established in response to a CONNECT request (Section 8.3) was reset or abnormally closed.
*/
public static final int CONNECT_ERROR = 0xa;

/**
* The endpoint detected that its peer is exhibiting a behavior that might be generating excessive load.
*/
public static final int ENHANCE_YOUR_CALM = 0xb;

/**
* The underlying transport has properties that do not meet minimum security requirements (see Section 9.2).
*/
public static final int INADEQUATE_SECURITY = 0xc;

/**
* The endpoint requires that HTTP/1.1 be used instead of HTTP/2.
*/
public static final int HTTP_1_1_REQUIRED = 0xd;

/*
* Settings related stuff
*/
public static final int SETTINGS_HEADER_TABLE_SIZE = 0x01;

public static final int SETTINGS_HEADER_TABLE_SIZE_DEFAULT = 4096;

public static final int SETTINGS_ENABLE_PUSH = 0x02;

public static final int SETTINGS_ENABLE_PUSH_DEFAULT = 1;

public static final int SETTINGS_MAX_CONCURRENT_STREAMS = 0x03;

public static final int SETTINGS_MAX_CONCURRENT_STREAMS_DEFAULT = Integer.MAX_VALUE;

public static final int SETTINGS_INITIAL_WINDOW_SIZE = 0x04;

public static final int SETTINGS_INITIAL_WINDOW_SIZE_DEFAULT = 65535;

public static final int SETTINGS_MAX_FRAME_SIZE = 0x05;

public static final int SETTINGS_MAX_FRAME_SIZE_DEFAULT = 16384;

public static final int SETTINGS_MAX_HEADER_LIST_SIZE = 0x06;

public static final int SETTINGS_MAX_HEADER_LIST_SIZE_DEFAULT = Integer.MAX_VALUE;

public static final Charset US_ASCII_CHARSET = Charset.forName("US-ASCII");

}
Loading

0 comments on commit 9979693

Please sign in to comment.