Skip to content

Commit

Permalink
Do not encode serverURL that includes protocol. Entered hostname by u…
Browse files Browse the repository at this point in the history
…ser is expected to be ok

Closes #531
  • Loading branch information
gdombiak committed Oct 17, 2021
1 parent ef18563 commit 0c5f9b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions OctoPod/OctoPrint/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions OctoPodTests/OctoPodTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0c5f9b0

Please sign in to comment.