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

Fix: respond with correct reason code #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
23 changes: 13 additions & 10 deletions Sources/KituraWebSocket/WebSocketConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public class WebSocketConnection {
return
}
} else if frame.payload.length == 0 {
reasonCode = .noReasonCodeSent
connectionClosed(reason: .noReasonCodeSent, reasonToSendBack: .normal)
return
} else {
connectionClosed(reason: .protocolError, description: "Close frames, which contain a payload, must be between 2 and 125 octets inclusive")
return
Expand Down Expand Up @@ -286,15 +287,17 @@ public class WebSocketConnection {
}

case .ping:
guard frame.payload.length < 126 else {
connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets")
return
}
guard frame.finalFrame else {
connectionClosed(reason: .protocolError, description: "Control frames must not be fragmented")
return
if active {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would guard active else { break } be better, to avoid the indentation?

guard frame.payload.length < 126 else {
connectionClosed(reason: .protocolError, description: "Control frames are only allowed to have payload up to and including 125 octets")
return
}
guard frame.finalFrame else {
connectionClosed(reason: .protocolError, description: "Control frames must not be fragmented")
return
}
sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length)
}
sendMessage(withOpCode: .pong, payload: frame.payload.bytes, payloadLength: frame.payload.length)

case .pong:
break
Expand Down Expand Up @@ -380,7 +383,7 @@ public class WebSocketConnection {
}
if abs(lastFrameReceivedAt.timeIntervalSinceNow) > (Double(connectionTimeout) * 0.4) {
if abs(lastFrameReceivedAt.timeIntervalSinceNow) > (Double(connectionTimeout)) {
strongSelf.connectionClosed(reason: .closedAbnormally)
strongSelf.connectionClosed(reason: .closedAbnormally, description: "Client failed to respond to a heartbeat ping with a pong", reasonToSendBack: .protocolError)
return
}
strongSelf.ping()
Expand Down