Skip to content

Commit

Permalink
Use URL type internally instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Aug 19, 2024
1 parent 478c281 commit e8d6e9b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Room+Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public enum StartReconnectReason {
// Room+ConnectSequences
extension Room {
// full connect sequence, doesn't update connection state
func fullConnectSequence(_ url: String, _ token: String) async throws {
func fullConnectSequence(_ url: URL, _ token: String) async throws {
let connectResponse = try await signalClient.connect(url,
token,
connectOptions: _state.connectOptions,
Expand Down
11 changes: 8 additions & 3 deletions Sources/LiveKit/Core/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class Room: NSObject, ObservableObject, Loggable {

// expose engine's vars
@objc
public var url: String? { _state.url }
public var url: String? { _state.url?.absoluteString }

@objc
public var token: String? { _state.token }
Expand Down Expand Up @@ -134,7 +134,7 @@ public class Room: NSObject, ObservableObject, Loggable {
var serverInfo: Livekit_ServerInfo?

// Engine
var url: String?
var url: URL?
var token: String?
// preferred reconnect mode which will be used only for next attempt
var nextReconnectMode: ReconnectMode?
Expand Down Expand Up @@ -283,7 +283,12 @@ public class Room: NSObject, ObservableObject, Loggable {
connectOptions: ConnectOptions? = nil,
roomOptions: RoomOptions? = nil) async throws
{
log("connecting to room...", .info)
guard let url = URL(string: url), url.isValidForSocket else {
log("URL parse failed", .error)
throw LiveKitError(.failedToParseUrl)
}

log("Connecting to room...", .info)

var state = _state.copy()

Expand Down
28 changes: 11 additions & 17 deletions Sources/LiveKit/Core/SignalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ actor SignalClient: Loggable {
}

@discardableResult
func connect(_ urlString: String,
func connect(_ url: URL,
_ token: String,
connectOptions: ConnectOptions? = nil,
reconnectMode: ReconnectMode? = nil,
Expand All @@ -122,14 +122,11 @@ actor SignalClient: Loggable {
log("[Connect] mode: \(String(describing: reconnectMode))")
}

guard let url = Utils.buildUrl(urlString,
token,
connectOptions: connectOptions,
reconnectMode: reconnectMode,
adaptiveStream: adaptiveStream)
else {
throw LiveKitError(.failedToParseUrl)
}
let url = try Utils.buildUrl(url,
token,
connectOptions: connectOptions,
reconnectMode: reconnectMode,
adaptiveStream: adaptiveStream)

if reconnectMode != nil {
log("[Connect] with url: \(url)")
Expand Down Expand Up @@ -179,14 +176,11 @@ actor SignalClient: Loggable {
await cleanUp(withError: error)

// Validate...
guard let validateUrl = Utils.buildUrl(urlString,
token,
connectOptions: connectOptions,
adaptiveStream: adaptiveStream,
validate: true)
else {
throw LiveKitError(.failedToParseUrl, message: "Failed to parse validation url")
}
let validateUrl = try Utils.buildUrl(url,
token,
connectOptions: connectOptions,
adaptiveStream: adaptiveStream,
validate: true)

log("Validating with url: \(validateUrl)...")
let validationResponse = try await HTTP.requestString(from: validateUrl)
Expand Down
6 changes: 0 additions & 6 deletions Sources/LiveKit/Extensions/Primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ extension Bool {
}
}

extension URL {
var isSecure: Bool {
scheme == "https" || scheme == "wss"
}
}

public extension Double {
func rounded(to places: Int) -> Double {
let divisor = pow(10.0, Double(places))
Expand Down
27 changes: 27 additions & 0 deletions Sources/LiveKit/Extensions/URL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

extension URL {
var isValidForSocket: Bool {
host != nil && (scheme == "ws" || scheme == "wss")
}

var isSecure: Bool {
scheme == "https" || scheme == "wss"
}
}
24 changes: 14 additions & 10 deletions Sources/LiveKit/Support/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,34 @@ class Utils {
}

static func buildUrl(
_ url: String,
_ url: URL,
_ token: String,
connectOptions: ConnectOptions? = nil,
reconnectMode: ReconnectMode? = nil,
adaptiveStream: Bool,
validate: Bool = false,
forceSecure: Bool = false
) -> URL? {
) throws -> URL {
// use default options if nil
let connectOptions = connectOptions ?? ConnectOptions()

guard let parsedUrl = URL(string: url) else { return nil }
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)

let components = URLComponents(url: parsedUrl, resolvingAgainstBaseURL: false)

guard var builder = components else { return nil }
guard var builder = components else {
throw LiveKitError(.failedToParseUrl)
}

let useSecure = parsedUrl.isSecure || forceSecure
let useSecure = url.isSecure || forceSecure
let httpScheme = useSecure ? "https" : "http"
let wsScheme = useSecure ? "wss" : "ws"

var pathSegments = parsedUrl.pathComponents
var pathSegments = url.pathComponents
// strip empty & slashes
pathSegments.removeAll(where: { $0.isEmpty || $0 == "/" })

// if already ending with `rtc` or `validate`
// and is not a dir, remove it
if !parsedUrl.hasDirectoryPath,
if !url.hasDirectoryPath,
!pathSegments.isEmpty,
["rtc", "validate"].contains(pathSegments.last!)
{
Expand Down Expand Up @@ -196,7 +196,11 @@ class Utils {

builder.queryItems = queryItems

return builder.url
guard let result = builder.url else {
throw LiveKitError(.failedToParseUrl)
}

return result
}

static func computeVideoEncodings(
Expand Down

0 comments on commit e8d6e9b

Please sign in to comment.