You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2016/05/09 13:32:00 UTC

svn commit: r1742949 - in /httpcomponents/httpcore/trunk/httpcore5-h2/src: main/java/org/apache/hc/core5/http2/frame/ main/java/org/apache/hc/core5/http2/setting/ test/java/org/apache/hc/core5/http2/frame/

Author: olegk
Date: Mon May  9 13:32:00 2016
New Revision: 1742949

URL: http://svn.apache.org/viewvc?rev=1742949&view=rev
Log:
RFC 7540: fixed HTTP/2 setting classes

Added:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java   (contents, props changed)
      - copied, changed from r1742948, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Param.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java   (contents, props changed)
      - copied, changed from r1742948, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Setting.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestH2Settings.java
      - copied, changed from r1742948, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java
Removed:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Param.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Setting.java
Modified:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameConsts.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestDefaultFrameFactory.java

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameConsts.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameConsts.java?rev=1742949&r1=1742948&r2=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameConsts.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameConsts.java Mon May  9 13:32:00 2016
@@ -34,5 +34,7 @@ public final class FrameConsts {
 
     public final static int HEAD_LEN = 9;
     public final static int MAX_PADDING = 255;
+    public final static int MIN_FRAME_SIZE = 2 ^ 14;
+    public final static int MAX_FRAME_SIZE = 2 ^ 24 - 1;
 
 };

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java?rev=1742949&r1=1742948&r2=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameFactory.java Mon May  9 13:32:00 2016
@@ -31,16 +31,25 @@ import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 
 import org.apache.hc.core5.http2.H2Error;
-import org.apache.hc.core5.http2.setting.H2Setting;
 import org.apache.hc.core5.util.Args;
 
 public abstract class FrameFactory {
 
+    // PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n
+    private final static byte[] PREFACE = new byte[] {
+            0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54, 0x54, 0x50,
+            0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x53, 0x4d,
+            0x0d, 0x0a, 0x0d, 0x0a};
+
+    public byte[] createConnectionPreface() {
+        return PREFACE;
+    }
+
     public Frame<ByteBuffer> createSettings(final H2Setting... settings) {
         final ByteBuffer payload = ByteBuffer.allocate(settings.length * 12);
         for (H2Setting setting: settings) {
-            payload.putInt(setting.getCode());
-            payload.putLong(setting.getValue());
+            payload.putShort((short) setting.getCode());
+            payload.putInt(setting.getValue());
         }
         payload.flip();
         return new ByteBufferFrame(FrameType.SETTINGS.getValue(), 0, 0, payload);

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java?rev=1742949&r1=1742948&r2=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java Mon May  9 13:32:00 2016
@@ -63,4 +63,11 @@ public enum FrameType {
         return LOOKUP_TABLE[value];
     }
 
+    public static String toString(final int value) {
+        if (value < 0 || value >= LOOKUP_TABLE.length) {
+            return Integer.toString(value);
+        }
+        return LOOKUP_TABLE[value].name();
+    }
+
 };

Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java (from r1742948, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Param.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Param.java&r1=1742948&r2=1742949&rev=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Param.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java Mon May  9 13:32:00 2016
@@ -24,25 +24,22 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.hc.core5.http2.setting;
-
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
+package org.apache.hc.core5.http2.frame;
 
 public enum H2Param {
 
     HEADER_TABLE_SIZE      (0x1,   4096),
     ENABLE_PUSH            (0x2,   1),
-    MAX_CONCURRENT_STREAMS (0x3,   Long.MAX_VALUE),
+    MAX_CONCURRENT_STREAMS (0x3,   Integer.MAX_VALUE),
     INITIAL_WINDOW_SIZE    (0x4,   65535),
     MAX_FRAME_SIZE         (0x5,   16384),
-    MAX_HEADER_LIST_SIZE   (0x6,   Long.MAX_VALUE);
+    MAX_HEADER_LIST_SIZE   (0x6,   Integer.MAX_VALUE);
 
     int code;
 
-    long initialValue;
+    int initialValue;
 
-    H2Param(final int code, final long initialValue) {
+    H2Param(final int code, final int initialValue) {
         this.code = code;
         this.initialValue = initialValue;
     }
@@ -51,20 +48,29 @@ public enum H2Param {
         return code;
     }
 
-    public long getInitialValue() {
+    public int getInitialValue() {
         return initialValue;
     }
 
-    private static final ConcurrentMap<Integer, H2Param> MAP_BY_CODE;
+    private static final H2Param[] LOOKUP_TABLE = new H2Param[6];
     static {
-        MAP_BY_CODE = new ConcurrentHashMap<>();
-        for (H2Param param: values()) {
-            MAP_BY_CODE.putIfAbsent(param.code, param);
+        for (H2Param param: H2Param.values()) {
+            LOOKUP_TABLE[param.code - 1] = param;
         }
     }
 
-    public static H2Param getByCode(final int code) {
-        return MAP_BY_CODE.get(code);
+    public static H2Param valueOf(final int code) {
+        if (code < 1 || code > LOOKUP_TABLE.length) {
+            return null;
+        }
+        return LOOKUP_TABLE[code - 1];
+    }
+
+    public static String toString(final int code) {
+        if (code < 1 || code > LOOKUP_TABLE.length) {
+            return Integer.toString(code);
+        }
+        return LOOKUP_TABLE[code - 1].name();
     }
 
 };

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Param.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java (from r1742948, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Setting.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Setting.java&r1=1742948&r2=1742949&rev=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/setting/H2Setting.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java Mon May  9 13:32:00 2016
@@ -24,17 +24,16 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.hc.core5.http2.setting;
+package org.apache.hc.core5.http2.frame;
 
 import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.LangUtils;
 
 public final class H2Setting {
 
     private final H2Param param;
-    private final long value;
+    private final int value;
 
-    public H2Setting(final H2Param param, final long value) {
+    public H2Setting(final H2Param param, final int value) {
         Args.notNull(param, "Setting parameter");
         Args.notNegative(value, "Setting value must be a non-negative value");
         this.param = param;
@@ -51,34 +50,13 @@ public final class H2Setting {
         return param.code;
     }
 
-    public long getValue() {
+    public int getValue() {
         return value;
     }
 
     @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj instanceof H2Setting) {
-            final H2Setting that = (H2Setting) obj;
-            return this.param.equals(that.param);
-        } else {
-            return false;
-        }
-    }
-
-    @Override
-    public int hashCode() {
-        int hash = LangUtils.HASH_SEED;
-        hash = LangUtils.hashCode(hash, this.param);
-        return hash;
-    }
-
-    @Override
     public String toString() {
-        final StringBuilder sb = new StringBuilder()
-                .append("[").append(param).append(":").append(value).append(']');
+        final StringBuilder sb = new StringBuilder().append(param).append(": ").append(value);
         return sb.toString();
     }
 };

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/H2Setting.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestDefaultFrameFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestDefaultFrameFactory.java?rev=1742949&r1=1742948&r2=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestDefaultFrameFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestDefaultFrameFactory.java Mon May  9 13:32:00 2016
@@ -30,8 +30,6 @@ import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 
 import org.apache.hc.core5.http2.H2Error;
-import org.apache.hc.core5.http2.setting.H2Param;
-import org.apache.hc.core5.http2.setting.H2Setting;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -63,7 +61,7 @@ public class TestDefaultFrameFactory {
         Assert.assertEquals(0, settingsFrame.getFlags());
         final ByteBuffer payload = settingsFrame.getPayload();
         Assert.assertNotNull(payload);
-        Assert.assertEquals(24, payload.remaining());
+        Assert.assertEquals(12, payload.remaining());
     }
 
     @Test

Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestH2Settings.java (from r1742948, httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestH2Settings.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestH2Settings.java&p1=httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java&r1=1742948&r2=1742949&rev=1742949&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/frame/FrameType.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/frame/TestH2Settings.java Mon May  9 13:32:00 2016
@@ -26,41 +26,31 @@
  */
 package org.apache.hc.core5.http2.frame;
 
-public enum FrameType {
+import org.junit.Assert;
+import org.junit.Test;
 
-    DATA          (0x00),
-    HEADERS       (0x01),
-    PRIORITY      (0x02),
-    RST_STREAM    (0x03),
-    SETTINGS      (0x04),
-    PUSH_PROMISE  (0x05),
-    PING          (0x06),
-    GOAWAY        (0x07),
-    WINDOW_UPDATE (0x08),
-    CONTINUATION  (0x09);
+public class TestH2Settings {
 
-    int value;
-
-    FrameType(final int value) {
-        this.value = value;
+    @Test
+    public void testH2ParamBasics() throws Exception {
+        for (H2Param param: H2Param.values()) {
+            Assert.assertEquals(param, H2Param.valueOf(param.getCode()));
+            Assert.assertEquals(param.name(), H2Param.toString(param.getCode()));
+        }
+        Assert.assertEquals(null, H2Param.valueOf(0));
+        Assert.assertEquals(null, H2Param.valueOf(10));
+        Assert.assertEquals("0", H2Param.toString(0));
+        Assert.assertEquals("10", H2Param.toString(10));
     }
 
-    public int getValue() {
-        return value;
-    }
+    @Test
+    public void testH2SettingBasics() throws Exception {
 
-    private static final FrameType[] LOOKUP_TABLE = new FrameType[10];
-    static {
-        for (FrameType frameType: FrameType.values()) {
-            LOOKUP_TABLE[frameType.value] = frameType;
-        }
-    }
+        final H2Setting setting1 = new H2Setting(H2Param.ENABLE_PUSH, 0);
+        final H2Setting setting2 = new H2Setting(H2Param.INITIAL_WINDOW_SIZE, 1024);
 
-    public static FrameType valueOf(final int value) {
-        if (value < 0 || value >= LOOKUP_TABLE.length) {
-            return null;
-        }
-        return LOOKUP_TABLE[value];
+        Assert.assertEquals("ENABLE_PUSH: 0", setting1.toString());
+        Assert.assertEquals("INITIAL_WINDOW_SIZE: 1024", setting2.toString());
     }
 
 };