Skip to content

Commit 844bba7

Browse files
authored
fix build issue with swift 5.10 (#6)
1 parent 341e8f6 commit 844bba7

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

Sources/LCLWebSocket/WebSocket.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,30 @@ public final class WebSocket: Sendable {
410410
if data.readableBytes == 0 {
411411
return .success("")
412412
}
413+
414+
func getUTF8String(at index: Int, length: Int) throws -> String {
415+
guard let bytes = data.getData(at: data.readerIndex, length: data.readableBytes),
416+
let text = String(data: bytes, encoding: .utf8)
417+
else {
418+
throw LCLWebSocketError.invalidUTF8String
419+
}
420+
421+
return text
422+
}
413423

414424
do {
425+
#if compiler(>=6.0)
415426
if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) {
416427
let text = try data.getUTF8ValidatedString(at: data.readerIndex, length: data.readableBytes) ?? ""
417428
return .success(text)
418429
} else {
419-
guard let bytes = data.getData(at: data.readerIndex, length: data.readableBytes),
420-
let text = String(data: bytes, encoding: .utf8)
421-
else {
422-
throw LCLWebSocketError.invalidUTF8String
423-
}
430+
let text = try getUTF8String(at: data.readerIndex, length: data.readableBytes)
424431
return .success(text)
425432
}
433+
#else
434+
let text = try getUTF8String(at: data.readerIndex, length: data.readableBytes)
435+
return .success(text)
436+
#endif
426437

427438
} catch {
428439
return .failure(error)
@@ -544,7 +555,7 @@ extension WebSocket {
544555
extension WebSocket {
545556

546557
/// A collection of information that the WebSocket uses to make the connection
547-
public struct ConnectionInfo: Sendable {
558+
public struct ConnectionInfo {
548559

549560
/// The URL that the WebSocket client connects to
550561
let url: URLComponents?
@@ -558,3 +569,7 @@ extension WebSocket {
558569
}
559570
}
560571
}
572+
573+
#if compiler(>=6.0)
574+
extension WebSocket.ConnectionInfo: Sendable { }
575+
#endif

Sources/LCLWebSocket/WebSocketHandler.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,20 @@ final class WebSocketHandler: ChannelDuplexHandler {
150150
default:
151151
self.websocket.handleFrame(frame)
152152
}
153-
} catch LCLWebSocketError.invalidUTF8String, is ByteBuffer.ReadUTF8ValidationError {
153+
} catch LCLWebSocketError.invalidUTF8String {
154154
self.websocket.close(code: .dataInconsistentWithMessage, promise: nil)
155155
context.close(mode: .all, promise: nil)
156156
clearBufferedFrames()
157157
} catch {
158+
#if compiler(>=6.0)
159+
if error is ByteBuffer.ReadUTF8ValidationError {
160+
self.websocket.close(code: .dataInconsistentWithMessage, promise: nil)
161+
context.close(mode: .all, promise: nil)
162+
clearBufferedFrames()
163+
return
164+
}
165+
#endif
166+
158167
let reason = (error as? LCLWebSocketError)?.description
159168
self.websocket.close(code: .protocolError, reason: reason, promise: nil)
160169
context.close(mode: .all, promise: nil)

Tests/IntegrationTests/autobahn/scripts/run_client.sh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
77
# Define volumes
88
CONFIG_DIR="$ROOT_DIR/configs"
99
REPORTS_DIR="$ROOT_DIR/reports"
10-
SERVER_PORT=9002
10+
FUZZING_CLIENT_NAME="fuzzingclient"
1111

1212
echo $CONFIG_DIR
1313
echo $REPORTS_DIR
@@ -54,7 +54,23 @@ if [ "${#test_cases[@]}" -ne "${#json_files[@]}" ] || [ "${#test_cases[@]}" -ne
5454
exit 1
5555
fi
5656

57-
# Example usage: loop over the array and print each test case
57+
cleanup() {
58+
echo "Cleaning up..."
59+
60+
# Loop through all the possible server ports and kill processes bound to them
61+
for port in "${server_ports[@]}"; do
62+
# Check if a process is bound to this port
63+
server_pid=$(lsof -n -i :$port -t)
64+
if [ -n "$server_pid" ]; then
65+
echo "Killing process with PID $server_pid on port $port..."
66+
kill -9 "$server_pid" &>/dev/null || true
67+
else
68+
echo "No process found on port $port."
69+
fi
70+
done
71+
}
72+
trap cleanup EXIT
73+
5874
for i in "${!test_cases[@]}"; do
5975
test_case=${test_cases[$i]}
6076
json_file=${json_files[$i]}
@@ -66,7 +82,7 @@ for i in "${!test_cases[@]}"; do
6682

6783

6884
# Run the server in the background
69-
swift test -c release --filter "$test_case" &> "$log_file" &
85+
swift test -c release --filter "$test_case" &> "$log_file" &
7086

7187
# Wait for the server to bind to the port
7288
timeout=30 # Max wait time for the server to start
@@ -95,14 +111,20 @@ for i in "${!test_cases[@]}"; do
95111

96112
sleep 2
97113

98-
docker run -it --rm \
114+
docker run -d --rm \
99115
-v "$CONFIG_DIR:/config" \
100116
-v "$REPORTS_DIR:/reports" \
101117
-p 9001:9001 \
102-
--name fuzzingclient \
103-
crossbario/autobahn-testsuite wstest --mode fuzzingclient --spec /config/$json_file
118+
--name "$FUZZING_CLIENT_NAME" \
119+
crossbario/autobahn-testsuite wstest --mode fuzzingclient --spec /config/$json_file &
120+
121+
sleep 2
122+
docker_id=$(docker ps -q --filter "name=$FUZZING_CLIENT_NAME")
123+
echo "Docker client started with ID: $docker_id"
124+
125+
docker wait "$docker_id"
104126

105-
kill -9 $PID
127+
kill -9 $PID &>/dev/null || true
106128

107129
echo "=========================== Test $test_case Done ============================"
108130
sleep 5

Tests/IntegrationTests/autobahn/scripts/run_server.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
77
# Define volumes
88
CONFIG_DIR="$ROOT_DIR/configs"
99
REPORTS_DIR="$ROOT_DIR/reports"
10+
FUZZING_SERVER_NAME="fuzzingserver"
1011

1112
echo $CONFIG_DIR
1213
echo $REPORTS_DIR
1314

15+
1416
# Run Docker container
15-
docker run -it --rm \
17+
docker run -d --rm \
1618
-v "$CONFIG_DIR:/config" \
1719
-v "$REPORTS_DIR:/reports" \
1820
-p 9001:9001 \
19-
--name fuzzingserver \
20-
crossbario/autobahn-testsuite wstest --mode fuzzingserver --spec /config/fuzzingserver.json
21+
--name "$FUZZING_SERVER_NAME" \
22+
crossbario/autobahn-testsuite wstest --mode fuzzingserver --spec /config/fuzzingserver.json &
23+
24+
echo "waiting for server to be ready"
25+
sleep 5
26+
27+
swift test -c release --filter IntegrationTests.AutobahnClientTest
28+
29+
docker logs $FUZZING_SERVER_NAME > "$FUZZING_SERVER_NAME.log"
30+
31+
FUZZING_SERVER_ID=$(docker ps -f "name=$FUZZING_SERVER_NAME" --format "{{.ID}}")
32+
docker stop $FUZZING_SERVER_ID

0 commit comments

Comments
 (0)