ebde72
From 3a561759fcb37af179585adb8478922dc9bc6a85 Mon Sep 17 00:00:00 2001
ebde72
From: Eric Covener <covener@apache.org>
ebde72
Date: Wed, 1 Jun 2022 12:36:39 +0000
ebde72
Subject: [PATCH] Merge r1901502 from trunk:
ebde72
ebde72
use filters consistently
ebde72
ebde72
ebde72
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1901503 13f79535-47bb-0310-9956-ffa450edef68
ebde72
---
ebde72
 modules/lua/lua_request.c | 144 ++++++++++++++------------------------
ebde72
 1 file changed, 53 insertions(+), 91 deletions(-)
ebde72
ebde72
diff --git a/modules/lua/lua_request.c b/modules/lua/lua_request.c
ebde72
index a3e3b613bc9..2ec453e86b4 100644
ebde72
--- a/modules/lua/lua_request.c
ebde72
+++ b/modules/lua/lua_request.c
ebde72
@@ -2227,23 +2227,20 @@ static int lua_websocket_greet(lua_State *L)
ebde72
     return 0;
ebde72
 }
ebde72
 
ebde72
-static apr_status_t lua_websocket_readbytes(conn_rec* c, char* buffer, 
ebde72
-        apr_off_t len) 
ebde72
+static apr_status_t lua_websocket_readbytes(conn_rec* c,
ebde72
+                                            apr_bucket_brigade *brigade,
ebde72
+                                            char* buffer, apr_off_t len)
ebde72
 {
ebde72
-    apr_bucket_brigade *brigade = apr_brigade_create(c->pool, c->bucket_alloc);
ebde72
+    apr_size_t delivered;
ebde72
     apr_status_t rv;
ebde72
+
ebde72
     rv = ap_get_brigade(c->input_filters, brigade, AP_MODE_READBYTES, 
ebde72
             APR_BLOCK_READ, len);
ebde72
     if (rv == APR_SUCCESS) {
ebde72
-        if (!APR_BRIGADE_EMPTY(brigade)) {
ebde72
-            apr_bucket* bucket = APR_BRIGADE_FIRST(brigade);
ebde72
-            const char* data = NULL;
ebde72
-            apr_size_t data_length = 0;
ebde72
-            rv = apr_bucket_read(bucket, &data, &data_length, APR_BLOCK_READ);
ebde72
-            if (rv == APR_SUCCESS) {
ebde72
-                memcpy(buffer, data, len);
ebde72
-            }
ebde72
-            apr_bucket_delete(bucket);
ebde72
+        delivered = len;
ebde72
+        rv = apr_brigade_flatten(brigade, buffer, &delivered);
ebde72
+        if ((rv == APR_SUCCESS) && (delivered < len)) {
ebde72
+            rv = APR_INCOMPLETE;
ebde72
         }
ebde72
     }
ebde72
     apr_brigade_cleanup(brigade);
ebde72
@@ -2273,35 +2270,28 @@ static int lua_websocket_peek(lua_State *L)
ebde72
 
ebde72
 static int lua_websocket_read(lua_State *L) 
ebde72
 {
ebde72
-    apr_socket_t *sock;
ebde72
     apr_status_t rv;
ebde72
     int do_read = 1;
ebde72
     int n = 0;
ebde72
-    apr_size_t len = 1;
ebde72
     apr_size_t plen = 0;
ebde72
     unsigned short payload_short = 0;
ebde72
     apr_uint64_t payload_long = 0;
ebde72
     unsigned char *mask_bytes;
ebde72
     char byte;
ebde72
-    int plaintext;
ebde72
-    
ebde72
-    
ebde72
+    apr_bucket_brigade *brigade;
ebde72
+    conn_rec* c;
ebde72
+
ebde72
     request_rec *r = ap_lua_check_request_rec(L, 1);
ebde72
-    plaintext = ap_lua_ssl_is_https(r->connection) ? 0 : 1;
ebde72
+    c = r->connection;
ebde72
 
ebde72
-    
ebde72
     mask_bytes = apr_pcalloc(r->pool, 4);
ebde72
-    sock = ap_get_conn_socket(r->connection);
ebde72
+
ebde72
+    brigade = apr_brigade_create(r->pool, c->bucket_alloc);
ebde72
 
ebde72
     while (do_read) {
ebde72
         do_read = 0;
ebde72
         /* Get opcode and FIN bit */
ebde72
-        if (plaintext) {
ebde72
-            rv = apr_socket_recv(sock, &byte, &len;;
ebde72
-        }
ebde72
-        else {
ebde72
-            rv = lua_websocket_readbytes(r->connection, &byte, 1);
ebde72
-        }
ebde72
+        rv = lua_websocket_readbytes(c, brigade, &byte, 1);
ebde72
         if (rv == APR_SUCCESS) {
ebde72
             unsigned char ubyte, fin, opcode, mask, payload;
ebde72
             ubyte = (unsigned char)byte;
ebde72
@@ -2311,12 +2301,7 @@ static int lua_websocket_read(lua_State *L)
ebde72
             opcode = ubyte & 0xf;
ebde72
 
ebde72
             /* Get the payload length and mask bit */
ebde72
-            if (plaintext) {
ebde72
-                rv = apr_socket_recv(sock, &byte, &len;;
ebde72
-            }
ebde72
-            else {
ebde72
-                rv = lua_websocket_readbytes(r->connection, &byte, 1);
ebde72
-            }
ebde72
+            rv = lua_websocket_readbytes(c, brigade, &byte, 1);
ebde72
             if (rv == APR_SUCCESS) {
ebde72
                 ubyte = (unsigned char)byte;
ebde72
                 /* Mask is the first bit */
ebde72
@@ -2327,40 +2312,25 @@ static int lua_websocket_read(lua_State *L)
ebde72
 
ebde72
                 /* Extended payload? */
ebde72
                 if (payload == 126) {
ebde72
-                    len = 2;
ebde72
-                    if (plaintext) {
ebde72
-                        /* XXX: apr_socket_recv does not receive len bits, only up to len bits! */
ebde72
-                        rv = apr_socket_recv(sock, (char*) &payload_short, &len;;
ebde72
-                    }
ebde72
-                    else {
ebde72
-                        rv = lua_websocket_readbytes(r->connection, 
ebde72
-                                (char*) &payload_short, 2);
ebde72
-                    }
ebde72
-                    payload_short = ntohs(payload_short);
ebde72
+                    rv = lua_websocket_readbytes(c, brigade,
ebde72
+                                                 (char*) &payload_short, 2);
ebde72
 
ebde72
-                    if (rv == APR_SUCCESS) {
ebde72
-                        plen = payload_short;
ebde72
-                    }
ebde72
-                    else {
ebde72
+                    if (rv != APR_SUCCESS) {
ebde72
                         return 0;
ebde72
                     }
ebde72
+
ebde72
+                    plen = ntohs(payload_short);
ebde72
                 }
ebde72
                 /* Super duper extended payload? */
ebde72
                 if (payload == 127) {
ebde72
-                    len = 8;
ebde72
-                    if (plaintext) {
ebde72
-                        rv = apr_socket_recv(sock, (char*) &payload_long, &len;;
ebde72
-                    }
ebde72
-                    else {
ebde72
-                        rv = lua_websocket_readbytes(r->connection, 
ebde72
-                                (char*) &payload_long, 8);
ebde72
-                    }
ebde72
-                    if (rv == APR_SUCCESS) {
ebde72
-                        plen = ap_ntoh64(&payload_long);
ebde72
-                    }
ebde72
-                    else {
ebde72
+                    rv = lua_websocket_readbytes(c, brigade,
ebde72
+                                                 (char*) &payload_long, 8);
ebde72
+
ebde72
+                    if (rv != APR_SUCCESS) {
ebde72
                         return 0;
ebde72
                     }
ebde72
+
ebde72
+                    plen = ap_ntoh64(&payload_long);
ebde72
                 }
ebde72
                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03210)
ebde72
                               "Websocket: Reading %" APR_SIZE_T_FMT " (%s) bytes, masking is %s. %s", 
ebde72
@@ -2369,46 +2339,27 @@ static int lua_websocket_read(lua_State *L)
ebde72
                               mask ? "on" : "off", 
ebde72
                               fin ? "This is a final frame" : "more to follow");
ebde72
                 if (mask) {
ebde72
-                    len = 4;
ebde72
-                    if (plaintext) {
ebde72
-                        rv = apr_socket_recv(sock, (char*) mask_bytes, &len;;
ebde72
-                    }
ebde72
-                    else {
ebde72
-                        rv = lua_websocket_readbytes(r->connection, 
ebde72
-                                (char*) mask_bytes, 4);
ebde72
-                    }
ebde72
+                    rv = lua_websocket_readbytes(c, brigade,
ebde72
+                                                 (char*) mask_bytes, 4);
ebde72
+
ebde72
                     if (rv != APR_SUCCESS) {
ebde72
                         return 0;
ebde72
                     }
ebde72
                 }
ebde72
                 if (plen < (HUGE_STRING_LEN*1024) && plen > 0) {
ebde72
                     apr_size_t remaining = plen;
ebde72
-                    apr_size_t received;
ebde72
-                    apr_off_t at = 0;
ebde72
                     char *buffer = apr_palloc(r->pool, plen+1);
ebde72
                     buffer[plen] = 0;
ebde72
 
ebde72
-                    if (plaintext) {
ebde72
-                        while (remaining > 0) {
ebde72
-                            received = remaining;
ebde72
-                            rv = apr_socket_recv(sock, buffer+at, &received);
ebde72
-                            if (received > 0 ) {
ebde72
-                                remaining -= received;
ebde72
-                                at += received;
ebde72
-                            }
ebde72
-                        }
ebde72
-                        ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, 
ebde72
-                                "Websocket: Frame contained %" APR_OFF_T_FMT " bytes, pushed to Lua stack", 
ebde72
-                                at);
ebde72
-                    }
ebde72
-                    else {
ebde72
-                        rv = lua_websocket_readbytes(r->connection, buffer, 
ebde72
-                                remaining);
ebde72
-                        ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, 
ebde72
-                                "Websocket: SSL Frame contained %" APR_SIZE_T_FMT " bytes, "\
ebde72
-                                "pushed to Lua stack", 
ebde72
-                                remaining);
ebde72
+                    rv = lua_websocket_readbytes(c, brigade, buffer, remaining);
ebde72
+
ebde72
+                    if (rv != APR_SUCCESS) {
ebde72
+                        return 0;
ebde72
                     }
ebde72
+
ebde72
+                    ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
ebde72
+                                  "Websocket: Frame contained %" APR_SIZE_T_FMT \
ebde72
+                                  " bytes, pushed to Lua stack", remaining);
ebde72
                     if (mask) {
ebde72
                         for (n = 0; n < plen; n++) {
ebde72
                             buffer[n] ^= mask_bytes[n%4];
ebde72
@@ -2420,14 +2371,25 @@ static int lua_websocket_read(lua_State *L)
ebde72
                     return 2;
ebde72
                 }
ebde72
 
ebde72
-
ebde72
                 /* Decide if we need to react to the opcode or not */
ebde72
                 if (opcode == 0x09) { /* ping */
ebde72
                     char frame[2];
ebde72
-                    plen = 2;
ebde72
+                    apr_bucket *b;
ebde72
+
ebde72
                     frame[0] = 0x8A;
ebde72
                     frame[1] = 0;
ebde72
-                    apr_socket_send(sock, frame, &plen); /* Pong! */
ebde72
+
ebde72
+                    /* Pong! */
ebde72
+                    b = apr_bucket_transient_create(frame, 2, c->bucket_alloc);
ebde72
+                    APR_BRIGADE_INSERT_TAIL(brigade, b);
ebde72
+
ebde72
+                    rv = ap_pass_brigade(c->output_filters, brigade);
ebde72
+                    apr_brigade_cleanup(brigade);
ebde72
+
ebde72
+                    if (rv != APR_SUCCESS) {
ebde72
+                        return 0;
ebde72
+                    }
ebde72
+
ebde72
                     do_read = 1;
ebde72
                 }
ebde72
             }