You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2017/05/03 20:59:53 UTC

svn commit: r1793716 - in /tomcat/trunk/java/org/apache/coyote/http2: Http2AsyncUpgradeHandler.java Http2UpgradeHandler.java Stream.java

Author: markt
Date: Wed May  3 20:59:53 2017
New Revision: 1793716

URL: http://svn.apache.org/viewvc?rev=1793716&view=rev
Log:
Refactor towards a single method for writing headers.

Modified:
    tomcat/trunk/java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java
    tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java
    tomcat/trunk/java/org/apache/coyote/http2/Stream.java

Modified: tomcat/trunk/java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java?rev=1793716&r1=1793715&r2=1793716&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java Wed May  3 20:59:53 2017
@@ -133,7 +133,7 @@ public class Http2AsyncUpgradeHandler ex
 
 
     @Override
-    void writeHeaders(Stream stream, Response coyoteResponse, int payloadSize)
+    void writeHeaders(Stream stream, Response coyoteResponse, boolean endOfStream, int payloadSize)
             throws IOException {
         if (log.isDebugEnabled()) {
             log.debug(sm.getString("upgradeHandler.writeHeaders", connectionId,
@@ -144,8 +144,6 @@ public class Http2AsyncUpgradeHandler ex
             return;
         }
 
-        prepareHeaders(coyoteResponse);
-
         boolean first = true;
         State state = null;
         ArrayList<ByteBuffer> bufs = new ArrayList<>();
@@ -160,7 +158,7 @@ public class Http2AsyncUpgradeHandler ex
                 if (first) {
                     first = false;
                     header[3] = FrameType.HEADERS.getIdByte();
-                    if (stream.getOutputBuffer().hasNoBody()) {
+                    if (endOfStream) {
                         header[4] = FLAG_END_OF_STREAM;
                     }
                 } else {
@@ -194,6 +192,10 @@ public class Http2AsyncUpgradeHandler ex
                     stream.getIdentifier(), Integer.toString(pushedStreamId)));
         }
 
+        if (!stream.canWrite()) {
+            return;
+        }
+
         boolean first = true;
         State state = null;
         ArrayList<ByteBuffer> bufs = new ArrayList<>();

Modified: tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java?rev=1793716&r1=1793715&r2=1793716&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Http2UpgradeHandler.java Wed May  3 20:59:53 2017
@@ -49,8 +49,6 @@ import org.apache.coyote.http2.Http2Pars
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.codec.binary.Base64;
-import org.apache.tomcat.util.http.FastHttpDateFormat;
-import org.apache.tomcat.util.http.MimeHeaders;
 import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
 import org.apache.tomcat.util.net.SSLSupport;
 import org.apache.tomcat.util.net.SocketEvent;
@@ -524,7 +522,7 @@ class Http2UpgradeHandler extends Abstra
         }
     }
 
-    void writeHeaders(Stream stream, Response coyoteResponse, int payloadSize)
+    void writeHeaders(Stream stream, Response coyoteResponse, boolean endOfStream, int payloadSize)
             throws IOException {
         if (log.isDebugEnabled()) {
             log.debug(sm.getString("upgradeHandler.writeHeaders", connectionId,
@@ -535,8 +533,6 @@ class Http2UpgradeHandler extends Abstra
             return;
         }
 
-        prepareHeaders(coyoteResponse);
-
         byte[] header = new byte[9];
         ByteBuffer target = ByteBuffer.allocate(payloadSize);
         boolean first = true;
@@ -551,7 +547,7 @@ class Http2UpgradeHandler extends Abstra
                     if (first) {
                         first = false;
                         header[3] = FrameType.HEADERS.getIdByte();
-                        if (stream.getOutputBuffer().hasNoBody()) {
+                        if (endOfStream) {
                             header[4] = FLAG_END_OF_STREAM;
                         }
                     } else {
@@ -582,34 +578,6 @@ class Http2UpgradeHandler extends Abstra
     }
 
 
-    protected void prepareHeaders(Response coyoteResponse) {
-        MimeHeaders headers = coyoteResponse.getMimeHeaders();
-        int statusCode = coyoteResponse.getStatus();
-
-        // Add the pseudo header for status
-        headers.addValue(":status").setString(Integer.toString(statusCode));
-
-        // Check to see if a response body is present
-        if (!(statusCode < 200 || statusCode == 205 || statusCode == 304)) {
-            String contentType = coyoteResponse.getContentType();
-            if (contentType != null) {
-                headers.setValue("content-type").setString(contentType);
-            }
-            String contentLanguage = coyoteResponse.getContentLanguage();
-            if (contentLanguage != null) {
-                headers.setValue("content-language").setString(contentLanguage);
-            }
-        }
-
-
-        // Add date header unless it is an informational response or the
-        // application has already set one
-        if (statusCode >= 200 && headers.getValue("date") == null) {
-            headers.addValue("date").setString(FastHttpDateFormat.getCurrentDate());
-        }
-    }
-
-
     protected void writePushHeaders(Stream stream, int pushedStreamId, Request coyoteRequest, int payloadSize)
             throws IOException {
         if (log.isDebugEnabled()) {
@@ -617,6 +585,10 @@ class Http2UpgradeHandler extends Abstra
                     stream.getIdentifier(), Integer.toString(pushedStreamId)));
         }
 
+        if (!stream.canWrite()) {
+            return;
+        }
+
         byte[] header = new byte[9];
         ByteBuffer target = ByteBuffer.allocate(payloadSize);
         boolean first = true;

Modified: tomcat/trunk/java/org/apache/coyote/http2/Stream.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Stream.java?rev=1793716&r1=1793715&r2=1793716&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Stream.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Stream.java Wed May  3 20:59:53 2017
@@ -34,6 +34,8 @@ import org.apache.coyote.http2.HpackDeco
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.http.FastHttpDateFormat;
+import org.apache.tomcat.util.http.MimeHeaders;
 import org.apache.tomcat.util.net.ApplicationBufferHandler;
 import org.apache.tomcat.util.res.StringManager;
 
@@ -51,6 +53,7 @@ class Stream extends AbstractStream impl
 
     static {
         ACK_RESPONSE.setStatus(100);
+        prepareHeaders(ACK_RESPONSE);
     }
 
     private volatile int weight = Constants.DEFAULT_WEIGHT;
@@ -385,13 +388,15 @@ class Stream extends AbstractStream impl
 
 
     final void writeHeaders() throws IOException {
+        prepareHeaders(coyoteResponse);
+        boolean endOfStream = getOutputBuffer().hasNoBody();
         // TODO: Is 1k the optimal value?
-        handler.writeHeaders(this, coyoteResponse, 1024);
+        handler.writeHeaders(this, coyoteResponse, endOfStream, 1024);
     }
 
     final void writeAck() throws IOException {
         // TODO: Is 64 too big? Just the status header with compression
-        handler.writeHeaders(this, ACK_RESPONSE, 64);
+        handler.writeHeaders(this, ACK_RESPONSE, false, 64);
     }
 
 
@@ -597,6 +602,33 @@ class Stream extends AbstractStream impl
         }
     }
 
+
+    private static void prepareHeaders(Response coyoteResponse) {
+        MimeHeaders headers = coyoteResponse.getMimeHeaders();
+        int statusCode = coyoteResponse.getStatus();
+
+        // Add the pseudo header for status
+        headers.addValue(":status").setString(Integer.toString(statusCode));
+
+        // Check to see if a response body is present
+        if (!(statusCode < 200 || statusCode == 205 || statusCode == 304)) {
+            String contentType = coyoteResponse.getContentType();
+            if (contentType != null) {
+                headers.setValue("content-type").setString(contentType);
+            }
+            String contentLanguage = coyoteResponse.getContentLanguage();
+            if (contentLanguage != null) {
+                headers.setValue("content-language").setString(contentLanguage);
+            }
+        }
+
+        // Add date header unless it is an informational response or the
+        // application has already set one
+        if (statusCode >= 200 && headers.getValue("date") == null) {
+            headers.addValue("date").setString(FastHttpDateFormat.getCurrentDate());
+        }
+    }
+
 
     private static class PrivilegedPush implements PrivilegedExceptionAction<Void> {
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org