You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_websocket_smacks.patch 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --- mod_websocket.lua
  2. +++ mod_websocket.lua
  3. @@ -163,34 +163,34 @@ function handle_request(event)
  4. return 403;
  5. end
  6. - local function websocket_close(code, message)
  7. + local function websocket_close(conn, code, message)
  8. conn:write(build_close(code, message));
  9. conn:close();
  10. end
  11. local dataBuffer;
  12. - local function handle_frame(frame)
  13. + local function handle_frame(conn, frame)
  14. local opcode = frame.opcode;
  15. local length = frame.length;
  16. module:log("debug", "Websocket received frame: opcode=%0x, %i bytes", frame.opcode, #frame.data);
  17. -- Error cases
  18. if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero
  19. - websocket_close(1002, "Reserved bits not zero");
  20. + websocket_close(conn, 1002, "Reserved bits not zero");
  21. return false;
  22. end
  23. if opcode == 0x8 then -- close frame
  24. if length == 1 then
  25. - websocket_close(1002, "Close frame with payload, but too short for status code");
  26. + websocket_close(conn, 1002, "Close frame with payload, but too short for status code");
  27. return false;
  28. elseif length >= 2 then
  29. local status_code = parse_close(frame.data)
  30. if status_code < 1000 then
  31. - websocket_close(1002, "Closed with invalid status code");
  32. + websocket_close(conn, 1002, "Closed with invalid status code");
  33. return false;
  34. elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then
  35. - websocket_close(1002, "Closed with reserved status code");
  36. + websocket_close(conn, 1002, "Closed with reserved status code");
  37. return false;
  38. end
  39. end
  40. @@ -198,28 +198,28 @@ function handle_request(event)
  41. if opcode >= 0x8 then
  42. if length > 125 then -- Control frame with too much payload
  43. - websocket_close(1002, "Payload too large");
  44. + websocket_close(conn, 1002, "Payload too large");
  45. return false;
  46. end
  47. if not frame.FIN then -- Fragmented control frame
  48. - websocket_close(1002, "Fragmented control frame");
  49. + websocket_close(conn, 1002, "Fragmented control frame");
  50. return false;
  51. end
  52. end
  53. if (opcode > 0x2 and opcode < 0x8) or (opcode > 0xA) then
  54. - websocket_close(1002, "Reserved opcode");
  55. + websocket_close(conn, 1002, "Reserved opcode");
  56. return false;
  57. end
  58. if opcode == 0x0 and not dataBuffer then
  59. - websocket_close(1002, "Unexpected continuation frame");
  60. + websocket_close(conn, 1002, "Unexpected continuation frame");
  61. return false;
  62. end
  63. if (opcode == 0x1 or opcode == 0x2) and dataBuffer then
  64. - websocket_close(1002, "Continuation frame expected");
  65. + websocket_close(conn, 1002, "Continuation frame expected");
  66. return false;
  67. end
  68. @@ -229,11 +229,11 @@ function handle_request(event)
  69. elseif opcode == 0x1 then -- Text frame
  70. dataBuffer = {frame.data};
  71. elseif opcode == 0x2 then -- Binary frame
  72. - websocket_close(1003, "Only text frames are supported");
  73. + websocket_close(conn, 1003, "Only text frames are supported");
  74. return;
  75. elseif opcode == 0x8 then -- Close request
  76. - websocket_close(1000, "Goodbye");
  77. - return;
  78. + websocket_close(conn, 1000, "Goodbye");
  79. + return "";
  80. elseif opcode == 0x9 then -- Ping frame
  81. frame.opcode = 0xA;
  82. conn:write(build_frame(frame));
  83. @@ -276,7 +276,7 @@ function handle_request(event)
  84. while frame do
  85. frameBuffer = frameBuffer:sub(length + 1);
  86. - local result = handle_frame(frame);
  87. + local result = handle_frame(session.conn, frame);
  88. if not result then return; end
  89. cache[#cache+1] = filter_open_close(result);
  90. frame, length = parse_frame(frameBuffer);