diff --git a/README.md b/README.md index 3b7e2da..678dcc7 100644 --- a/README.md +++ b/README.md @@ -66,13 +66,21 @@ See the tests for examples of both methods. Equivalent to the [`-U`/`--proxy-user`](https://curl.haxx.se/docs/manpage.html#-U) parameter of curl, which allows specifying the username and password to use when authenticating to the proxy server. -- **timeout(seconds: Int)** +- **timeout(Int)** - Equivalent to the [`-m`/`--max-time`](https://curl.haxx.se/docs/manpage.html#-m) parameter of curl, which allows specifying the maximum time allowed to service the request. + Equivalent to the [`-m`/`--max-time`](https://curl.haxx.se/docs/manpage.html#-m) parameter of curl, which allows specifying the maximum time allowed to service the request in seconds. -- **connectTimeout(seconds: Int)** +- **timeoutMs(Int)** - Equivalent to the [`--connect-timeout`](https://curl.haxx.se/docs/manpage.html#--connect-timeout) parameter of curl, which allows specifying the maximum time allowed for the connection to the server. + Same as `timeout`, but with milliseconds precision. + +- **connectTimeout(Int)** + + Equivalent to the [`--connect-timeout`](https://curl.haxx.se/docs/manpage.html#--connect-timeout) parameter of curl, which allows specifying the maximum time allowed for the connection to the server in seconds. + +- **connectTimeoutMs(Int)** (New in 0.5.0) + + Same as `connectTimeout`, but with milliseconds precision. - **cookieJar(String)** diff --git a/Sources/CurlyClient/CURLRequest.swift b/Sources/CurlyClient/CURLRequest.swift index f5394b3..8ca7721 100755 --- a/Sources/CurlyClient/CURLRequest.swift +++ b/Sources/CurlyClient/CURLRequest.swift @@ -105,10 +105,16 @@ class CURLRequest { /// Maximum time in seconds for the request to complete. /// The default timeout is never. timeout(Int), + /// Maximum time in milliseconds for the request to complete. + /// The default timeout is never. + timeoutMs(Int), /// Maximum time in seconds for the request connection phase. /// The default timeout is 300 seconds. connectTimeout(Int), - /// The average transfer speed in bytes per second that the transfer should be below + /// Maximum time in milliseconds for the request connection phase. + /// The default timeout is 300 seconds. + connectTimeoutMs(Int), + /// The average transfer speed in bytes per second that the transfer should be below /// during `.lowSpeedLimit` seconds for the request to be too slow and abort. lowSpeedLimit(Int), /// The time in seconds that the transfer speed should be below the `.lowSpeedLimit` diff --git a/Sources/CurlyClient/CURLRequestOptions.swift b/Sources/CurlyClient/CURLRequestOptions.swift index 586e4cc..66a7935 100755 --- a/Sources/CurlyClient/CURLRequestOptions.swift +++ b/Sources/CurlyClient/CURLRequestOptions.swift @@ -48,8 +48,12 @@ extension CURLRequest.Option { curl.setOption(CURLOPT_PROXYPORT, int: optInt) case .timeout(let optInt): curl.setOption(CURLOPT_TIMEOUT, int: optInt) + case .timeoutMs(let optInt): + curl.setOption(CURLOPT_TIMEOUT_MS, int: optInt) case .connectTimeout(let optInt): curl.setOption(CURLOPT_CONNECTTIMEOUT, int: optInt) + case .connectTimeoutMs(let optInt): + curl.setOption(CURLOPT_CONNECTTIMEOUT_MS, int: optInt) case .lowSpeedLimit(let optInt): curl.setOption(CURLOPT_LOW_SPEED_LIMIT, int: optInt) case .lowSpeedTime(let optInt): diff --git a/Sources/CurlyClient/CurlyOption+Private.swift b/Sources/CurlyClient/CurlyOption+Private.swift index 44f0a46..398d002 100644 --- a/Sources/CurlyClient/CurlyOption+Private.swift +++ b/Sources/CurlyClient/CurlyOption+Private.swift @@ -17,8 +17,12 @@ extension CurlyOption { switch self { case .connectTimeout(let seconds): return [.connectTimeout(seconds)] + case .connectTimeoutMs(let ms): + return [.connectTimeoutMs(ms)] case .timeout(let seconds): return [.timeout(seconds)] + case .timeoutMs(let ms): + return [.timeoutMs(ms)] case .cookieJar(let file): return [.cookieFile(file), .cookieJar(file)] case .proxy(let proxy): diff --git a/Sources/CurlyClient/CurlyOption.swift b/Sources/CurlyClient/CurlyOption.swift index a0a4d2b..48ec797 100644 --- a/Sources/CurlyClient/CurlyOption.swift +++ b/Sources/CurlyClient/CurlyOption.swift @@ -3,8 +3,10 @@ import Vapor public enum CurlyOption { case proxy(String) case proxyAuth(user: String, password: String) - case timeout(seconds: Int) - case connectTimeout(seconds: Int) + case timeout(Int) + case timeoutMs(Int) + case connectTimeout(Int) + case connectTimeoutMs(Int) case cookieJar(String) case followRedirects(Bool) case insecure(Bool) diff --git a/Tests/CurlyClientTests/CurlyClientTests.swift b/Tests/CurlyClientTests/CurlyClientTests.swift index 14cc000..8209e17 100644 --- a/Tests/CurlyClientTests/CurlyClientTests.swift +++ b/Tests/CurlyClientTests/CurlyClientTests.swift @@ -29,7 +29,7 @@ final class CurlyClientTests: XCTestCase { "baz": "qux" ], as: MediaType.urlEncodedForm) - req.addCurlyOption(.timeout(seconds: 5)) + req.addCurlyOption(.timeout(5)) let res = try app.client().send(req).wait() @@ -55,7 +55,7 @@ final class CurlyClientTests: XCTestCase { func testConvenience() throws { let app = try testApplication() let res = try app.client().get("https://httpbin.org/get", beforeSend: { req in - req.addCurlyOption(.timeout(seconds: 5)) + req.addCurlyOption(.timeout(5)) }).wait() XCTAssertNotNil(res.http.body.data) @@ -93,7 +93,7 @@ final class CurlyClientTests: XCTestCase { func testTimeoutError() throws { let app = try testApplication() let futureRes = try app.client().get("https://httpstat.us/200?sleep=5000", beforeSend: { req in - req.addCurlyOption(.timeout(seconds: 1)) + req.addCurlyOption(.timeoutMs(500)) }) XCTAssertThrowsError(try futureRes.wait(), "Should throw timeout error") { error in