This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new eb88f0e0c4 Optimise WebSocket client processing of server HTTP 
responses
eb88f0e0c4 is described below

commit eb88f0e0c4d5761819aef99d60d252409b851cf1
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Jun 25 15:43:05 2026 +0100

    Optimise WebSocket client processing of server HTTP responses
---
 .../apache/tomcat/websocket/WsWebSocketContainer.java  | 18 ++++++++----------
 webapps/docs/changelog.xml                             |  4 ++++
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java 
b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
index db774a2111..252e287677 100644
--- a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
+++ b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
@@ -858,6 +858,7 @@ public class WsWebSocketContainer implements 
WebSocketContainer, BackgroundProce
         int status = 0;
         boolean readStatus = false;
         boolean readHeaders = false;
+        StringBuilder lineBuffer = new StringBuilder();
         String line = null;
         while (!readHeaders) {
             // On entering loop buffer will be empty and at the start of a new
@@ -880,14 +881,13 @@ public class WsWebSocketContainer implements 
WebSocketContainer, BackgroundProce
             }
             response.flip();
             while (response.hasRemaining() && !readHeaders) {
-                if (line == null) {
-                    line = readLine(response);
-                } else {
-                    line += readLine(response);
+                if (readLine(response, lineBuffer)) {
+                    line = lineBuffer.toString();
+                    lineBuffer = new StringBuilder();
                 }
                 if ("\r\n".equals(line)) {
                     readHeaders = true;
-                } else if (line.endsWith("\r\n")) {
+                } else if (line != null && line.endsWith("\r\n")) {
                     if (readStatus) {
                         parseHeaders(line, headers);
                     } else {
@@ -937,20 +937,18 @@ public class WsWebSocketContainer implements 
WebSocketContainer, BackgroundProce
         values.add(headerValue);
     }
 
-    private String readLine(ByteBuffer response) {
+    private boolean readLine(ByteBuffer response, StringBuilder sb) {
         // All ISO-8859-1
-        StringBuilder sb = new StringBuilder();
-
         char c;
         while (response.hasRemaining()) {
             c = (char) response.get();
             sb.append(c);
             if (c == 10) {
-                break;
+                return true;
             }
         }
 
-        return sb.toString();
+        return false;
     }
 
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index c62a14355a..180ee240af 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -373,6 +373,10 @@
         available buffer. Fix written by GPT-5.5. Test case written by Hironori
         Ichimiya. (markt)
       </fix>
+      <fix>
+        Optimise WebSocket client processing of server responses during
+        WebSocket HTTP upgrade process. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Web applications">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to