Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special handling for parsing websocket data #73

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion http_server/_server_conn_handler.pony
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class _ServerConnHandler is TCPConnectionNotify
let sconn = _ServerConnection(_handlermaker, _config, conn)
_registry.register_session(sconn)
_session = sconn
_parser = HTTP11RequestParser.create(sconn)
_parser = HTTP11RequestParser.create(sconn, _config.allow_upgrade)

fun ref received(
conn: TCPConnection ref,
Expand Down
16 changes: 13 additions & 3 deletions http_server/request_parser.pony
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ class HTTP11RequestParser
var _persistent_connection: Bool = true
var _transfer_coding: (Chunked | None) = None

new create(handler: HTTP11RequestHandler) =>
let _allow_upgrade: Bool
var _websocket: Bool = false

new create(handler: HTTP11RequestHandler, allow_upgrade: Bool = false) =>
_handler = handler
_allow_upgrade = allow_upgrade

fun ref parse(data: Array[U8] val): ParseReturn =>
_buffer = _buffer + (consume data)
Expand Down Expand Up @@ -272,6 +276,8 @@ class HTTP11RequestParser
end
_current_request.set_content_length(cl)
_expected_body_length = cl
elseif CompareCaseInsensitive(name, "upgrade") and _allow_upgrade then
_websocket = value == "websocket"
elseif CompareCaseInsensitive(name, "transfer-encoding") then
try
value.find("chunked")?
Expand Down Expand Up @@ -356,14 +362,18 @@ class HTTP11RequestParser
_handler._receive_chunk(data, _request_counter)
end
end
if _expected_body_length == 0 then
if _websocket then
_handler._receive_chunk(_buffer.array(), _request_counter)
_buffer = _buffer.drop(_buffer.size())
end
if (_expected_body_length == 0) and (not _websocket) then

_handler._receive_finished(_request_counter)
reset()
if _buffer.size() > 0 then
_parse_request_line()
end
else
elseif not _websocket then
NeedMore
end
end
Expand Down
9 changes: 8 additions & 1 deletion http_server/server_config.pony
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ class val ServerConfig
Default: `(<connection_timeout> * 1000) / 4`
"""

let allow_upgrade: Bool
"""
Allow the connection to be upgraded to a websocket connection.
"""

new val create(
host': String = "localhost",
port': String = "0",
connection_timeout': USize = 0,
max_request_handling_lag': USize = 100,
max_concurrent_connections': USize = 0,
timeout_heartbeat_interval': (U64 | None) = None
timeout_heartbeat_interval': (U64 | None) = None,
allow_upgrade': Bool = false
) =>
host = host'
port = port'
Expand All @@ -78,6 +84,7 @@ class val ServerConfig
((connection_timeout.u64() * 1000) / 4).max(1000)
| let interval: U64 => interval
end
allow_upgrade = allow_upgrade'


fun box has_timeout(): Bool =>
Expand Down
Loading