From 0c5f9b02e31445879ae1c9ea507b28a5fb359a88 Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Sun, 17 Oct 2021 10:17:01 -0700 Subject: [PATCH] Do not encode serverURL that includes protocol. Entered hostname by user is expected to be ok Closes #531 --- OctoPod/OctoPrint/HTTPClient.swift | 9 +++++++-- OctoPodTests/OctoPodTests.swift | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/OctoPod/OctoPrint/HTTPClient.swift b/OctoPod/OctoPrint/HTTPClient.swift index 97fc8804..cd484d8f 100644 --- a/OctoPod/OctoPrint/HTTPClient.swift +++ b/OctoPod/OctoPrint/HTTPClient.swift @@ -337,9 +337,14 @@ class HTTPClient: NSObject, URLSessionTaskDelegate { } func buildURL(_ service: String) -> URL? { + var serviceToAnalyze = service let urlFragment: String + // No need to encode serverURL + if service.starts(with: serverURL) { + serviceToAnalyze = String(service.dropFirst(serverURL.count)) + } // Split into path and query params - let parts = service.split(separator: "?", maxSplits: 1, omittingEmptySubsequences: true).map(String.init) + let parts = serviceToAnalyze.split(separator: "?", maxSplits: 1, omittingEmptySubsequences: true).map(String.init) // Escape path and query params let path = parts[0].addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? parts[0] if parts.count > 1 { @@ -349,7 +354,7 @@ class HTTPClient: NSObject, URLSessionTaskDelegate { } else { urlFragment = path } - return service.starts(with: serverURL) ? URL(string: urlFragment) : URL(string: serverURL + urlFragment) + return URL(string: serverURL + urlFragment) } // MARK: URLSessionDelegate diff --git a/OctoPodTests/OctoPodTests.swift b/OctoPodTests/OctoPodTests.swift index de2ac8a3..9ebb1e03 100644 --- a/OctoPodTests/OctoPodTests.swift +++ b/OctoPodTests/OctoPodTests.swift @@ -56,13 +56,21 @@ class OctoPodTests: XCTestCase { XCTAssertNotNil(httpClient.buildURL("/plugin/prusaslicerthumbnails/thumbnail/CE5_cube ?(1).gcode?20210812220714")) var url = httpClient.buildURL("/plugin/prusaslicerthumbnails/thumbnail/CE5_cube >(1).gcode?20210812220714") + XCTAssertEqual(url?.absoluteString, "http://octopi.local/plugin/prusaslicerthumbnails/thumbnail/CE5_cube%20%3E(1).gcode?20210812220714") XCTAssertEqual(url?.path, "/plugin/prusaslicerthumbnails/thumbnail/CE5_cube >(1).gcode") XCTAssertEqual(url?.query, "20210812220714") // Filenames with ? are trouble. It will generate some URL but will not work url = httpClient.buildURL("/plugin/prusaslicerthumbnails/thumbnail/CE5_cube ?(1).gcode?20210812220714") + XCTAssertEqual(url?.absoluteString, "http://octopi.local/plugin/prusaslicerthumbnails/thumbnail/CE5_cube%20?(1).gcode?20210812220714") XCTAssertEqual(url?.path, "/plugin/prusaslicerthumbnails/thumbnail/CE5_cube ") XCTAssertEqual(url?.query, "(1).gcode?20210812220714") + + // Test that absolute path still works. Protocol is not encoded but path and query params do + url = httpClient.buildURL("http://octopi.local/plugin/prusaslicerthumbnails/thumbnail/CE5_cube >(1).gcode?20210812220714") + XCTAssertEqual(url?.absoluteString, "http://octopi.local/plugin/prusaslicerthumbnails/thumbnail/CE5_cube%20%3E(1).gcode?20210812220714") + XCTAssertEqual(url?.path, "/plugin/prusaslicerthumbnails/thumbnail/CE5_cube >(1).gcode") + XCTAssertEqual(url?.query, "20210812220714") } // func testPerformanceExample() throws {