forked from Kitura/Kitura-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientRequest.swift
executable file
·1009 lines (793 loc) · 34.7 KB
/
ClientRequest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright IBM Corporation 2016
*
* 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 LoggerAPI
import CCurl
import Socket
import Foundation
// The public API for ClientRequest erroneously defines the port as an Int16, which is
// insufficient to hold all possible port values. To avoid a breaking change, we allow
// UInt16 bit patterns to be passed in, under the guises of an Int16, which we will
// then convert back to UInt16.
//
// User code must perform the equivalent conversion in order to pass in a value that
// is greater than Int16.max.
//
fileprivate extension Int16 {
func toUInt16() -> UInt16 {
return UInt16(bitPattern: self)
}
}
// MARK: ClientRequest
/**
This class provides a set of low level APIs for issuing HTTP requests to another server. A new instance of the request can be created, along with options if the user would like to specify certain parameters such as HTTP headers, HTTP methods, host names, and SSL credentials. `Data` and `String` objects can be added to a `ClientRequest` too, and URLs can be parsed.
### Usage Example: ###
````swift
//Function to create a new `ClientRequest` using a URL.
public static func request(_ url: String, callback: @escaping ClientRequest.Callback) -> ClientRequest {
return ClientRequest(url: url, callback: callback)
}
//Create a new `ClientRequest` using a URL.
let request = HTTP.request("http://localhost/8080") {response in
...
}
````
*/
public class ClientRequest {
/// Initialize the one time initialization struct to cause one time initializations to occur
static private let oneTime = OneTimeInitializations()
/**
The set of HTTP headers to be sent with the request.
### Usage Example: ###
````swift
clientRequest.headers["Content-Type"] = ["text/plain"]
````
*/
public var headers = [String: String]()
/**
The URL for the request.
### Usage Example: ###
````swift
clientRequest.url = "https://localhost:8080"
````
*/
public private(set) var url: String = ""
/**
The HTTP method (i.e. GET, POST, PUT, DELETE) for the request.
### Usage Example: ###
````swift
clientRequest.method = "post"
````
*/
public private(set) var method: String = "get"
/**
The username to be used if using Basic Auth authentication.
### Usage Example: ###
````swift
clientRequest.userName = "user1"
````
*/
public private(set) var userName: String?
/**
The password to be used if using Basic Auth authentication.
### Usage Example: ###
````swift
clientRequest.password = "sUpeR_seCurE_paSsw0rd"
````
*/
public private(set) var password: String?
/**
The maximum number of redirects before failure.
- Note: The `ClientRequest` class will automatically follow redirect responses. To avoid redirect loops, it will at maximum follow `maxRedirects` redirects.
### Usage Example: ###
````swift
clientRequest.maxRedirects = 10
````
*/
public private(set) var maxRedirects = 10
/**
If true, the "Connection: close" header will be added to the request that is sent.
### Usage Example: ###
````swift
ClientRequest.closeConnection = false
````
*/
public private(set) var closeConnection = false
/**
Ca File used to check against Certified Authority
*/
public private(set) var caFile: String?
/**
Ca Path used to check against Certified Authority
*/
public private(set) var caPath: String?
/**
CA File used to check against Client Revoked List
*/
public private(set) var crlFile: String?
/// Handle for working with libCurl
private var handle: UnsafeMutableRawPointer?
/// List of header information
private var headersList: UnsafeMutablePointer<curl_slist>?
/// BufferList to store bytes to be written
fileprivate var writeBuffers = BufferList()
/// Response instance for communicating with client
fileprivate var response: ClientResponse?
/// The callback to receive the response
private var callback: Callback
/// Should SSL verification be disabled
private var disableSSLVerification = false
/// Should OCSP be enabled
private var enableOCSP = false
/// Should HTTP/2 protocol be used
private var useHTTP2 = false
/// The Unix domain socket path used for the request
private var unixDomainSocketPath: String? = nil
/// Should Verbose logging be enabled
private var enableVerboseLogging = false
/// The format of the SSL client certificate
public private(set) var sslCertificateFormat: String = "PEM"
/// The filename of the SSL client certificate
public private(set) var sslCertificate: String?
/// The filename of the private key for the SSL client certificate
public private(set) var sslKey: String?
/// The private key passphrase
public private(set) var sslKeyPassphrase: String?
/// Client request option enum
public enum SSLOptions {
/// Specifies the format of the SSL client certificate
case sslCertificateFormat(String)
/// Specifies the filename of the SSL client certificate
case sslCertificate(String)
/// Specifies the filename of the private key for the SSL client certificate
case sslKey(String)
/// Specifies the private key passphrase
case sslKeyPassphrase(String)
}
/// Data that represents the "HTTP/2 " header status line prefix
fileprivate static let Http2StatusLineVersion = "HTTP/2 ".data(using: .utf8)!
/// Data that represents the "HTTP/2.0 " (with a minor) header status line prefix
fileprivate static let Http2StatusLineVersionWithMinor = "HTTP/2.0 ".data(using: .utf8)!
/// The hostname of the remote server
private var hostName: String?
/// The port number of the remote server
private var port: Int?
private var path = ""
/**
Client request options enum. This allows the client to specify certain parameteres such as HTTP headers, HTTP methods, host names, and SSL credentials.
### Usage Example: ###
````swift
//If present in the options provided, the client will try to use HTTP/2 protocol for the connection.
Options.useHTTP2
````
*/
public enum Options {
/// Specifies the HTTP method (i.e. PUT, POST...) to be sent in the request
case method(String)
/// Specifies the schema (i.e. HTTP, HTTPS) to be used in the URL of request
case schema(String)
/// Specifies the host name to be used in the URL of request
case hostname(String)
/// Specifies the port to be used in the URL of request.
///
/// Note that an Int16 is incapable of representing all possible port values, however
/// it forms part of the Kitura-net 2.0 API. In order to pass a port number greater
/// than 32,767 (Int16.max), use the following code:
/// ```
/// let portNumber: UInt16 = 65535
/// let portOption: ClientRequest.Options = .port(Int16(bitPattern: portNumber))
/// ```
case port(Int16)
/// Specifies the path to be used in the URL of request
case path(String)
/// Specifies the HTTP headers to be sent with the request
case headers([String: String])
/// Specifies the user name to be sent with the request, when using basic auth authentication
case username(String)
/// Specifies the password to be sent with the request, when using basic auth authentication
case password(String)
/// Specifies the maximum number of redirect responses that will be followed (i.e. re-issue the
/// request to the location received in the redirect response)
case maxRedirects(Int)
/// If present, the SSL credentials of the remote server will not be verified.
///
/// - Note: This is very useful when working with self signed certificates.
case disableSSLVerification
/// If present, enable OCSP
case enableOCSP
/// If present, the client will try to use HTTP/2 protocol for the connection.
case useHTTP2
/// If present, CURL will log using verbose level.
///
/// - Note: This is very useful for debugging the Mutual TLS connection
case enableVerboseLogging
/// Specifies the CA File to use
case caFile(String?)
/// Specifies the CA Path to use
case caPath(String?)
/// Specifies the CA File to use
case crlFile(String?)
}
/**
Response callback closure type.
### Usage Example: ###
````swift
var ClientRequest.headers["Content-Type"] = ["text/plain"]
````
- Parameter ClientResponse: The `ClientResponse` object that describes the response that was received from the remote server.
*/
public typealias Callback = (ClientResponse?) -> Void
/// Initializes a `ClientRequest` instance
///
/// - Parameter url: url for the request
/// - Parameter callback: The closure of type `Callback` to be used for the callback.
init(url: String, callback: @escaping Callback) {
self.url = url
self.callback = callback
if let url = URL(string: url) {
removeHttpCredentialsFromUrl(url)
}
}
private func removeHttpCredentialsFromUrl(_ url: URL) {
if let host = url.host {
self.hostName = host
}
if let port = url.port {
self.port = port
}
if let username = url.user {
self.userName = username
}
if let password = url.password {
self.password = password
}
var fullPath = url.path
// query strings and parameters need to be appended here
if let query = url.query {
fullPath += "?"
fullPath += query
}
self.path = fullPath
self.url = "\(url.scheme ?? "http")://\(self.hostName ?? "unknown")\(self.port.map { ":\($0)" } ?? "")\(fullPath)"
if let username = self.userName, let password = self.password {
self.headers["Authorization"] = createHTTPBasicAuthHeader(username: username, password: password)
}
return
}
/// Initializes a `ClientRequest` instance
///
/// - Parameter options: An array of `Options' describing the request.
/// - Parameter unixDomainSocketPath: Specifies the path of a Unix domain socket that the client should connect to.
/// - Parameter callback: The closure of type `Callback` to be used for the callback.
init(options: [Options], unixDomainSocketPath: String? = nil, sslOptions: [SSLOptions] = [], callback: @escaping Callback) {
self.unixDomainSocketPath = unixDomainSocketPath
self.callback = callback
var theSchema = "http://"
var hostName = "localhost"
var path = ""
var port = ""
for option in options {
switch(option) {
case .method, .headers, .maxRedirects, .disableSSLVerification, .useHTTP2, .enableVerboseLogging, .enableOCSP:
// call set() for Options that do not construct the URL
set(option)
case .schema(var schema):
if !schema.contains("://") && !schema.isEmpty {
schema += "://"
}
theSchema = schema
case .hostname(let host):
hostName = host
case .port(let thePort):
let portNumber = thePort.toUInt16()
port = ":\(portNumber)"
case .path(var thePath):
if thePath.first != "/" {
thePath = "/" + thePath
}
path = thePath
case .username(let userName):
self.userName = userName
case .password(let password):
self.password = password
case .caFile(let file):
self.caFile = file
case .caPath(let path):
self.caPath = path
case .crlFile(let file):
self.crlFile = file
}
}
for sslOption in sslOptions {
set(sslOption)
}
if let username = self.userName, let password = self.password {
self.headers["Authorization"] = createHTTPBasicAuthHeader(username: username, password: password)
}
url = "\(theSchema)\(hostName)\(port)\(path)"
}
/**
Set a single option in the request. URL parameters must be set in init().
### Usage Example: ###
````swift
var options: [ClientRequest.Options] = []
options.append(.port(Int16(port)))
clientRequest.set(options)
````
- Parameter option: An `Options` instance describing the change to be made to the request.
*/
public func set(_ option: Options) {
switch(option) {
case .schema, .hostname, .port, .path, .username, .password, .caFile, .caPath, .crlFile:
Log.error("Must use ClientRequest.init() to set URL components")
case .method(let method):
self.method = method
case .headers(let headers):
for (key, value) in headers {
self.headers[key] = value
}
case .maxRedirects(let maxRedirects):
self.maxRedirects = maxRedirects
case .disableSSLVerification:
self.disableSSLVerification = true
case .useHTTP2:
self.useHTTP2 = true
case .enableVerboseLogging:
self.enableVerboseLogging = true
case .enableOCSP:
self.enableOCSP = true
}
}
/// Set a single ssloption in the request. URL parameters must be set in init()
///
/// - Parameter option: an `SSLOption` instance describing the change to be made to the request
public func set(_ sslOption: SSLOptions) {
switch(sslOption) {
case .sslCertificateFormat(let format):
self.sslCertificateFormat = format
case .sslCertificate(let sslCertificate):
self.sslCertificate = sslCertificate
case .sslKey(let sslKey):
self.sslKey = sslKey
case .sslKeyPassphrase(let sslKeyPassphrase):
self.sslKeyPassphrase = sslKeyPassphrase
}
}
/**
Parse an URL (String) into an array of ClientRequest.Options.
### Usage Example: ###
````swift
let url: String = "http://www.website.com"
let parsedOptions = clientRequest.parse(url)
````
- Parameter urlString: A String object referencing a URL.
- Returns: An array of `ClientRequest.Options`
*/
public class func parse(_ urlString: String) -> [ClientRequest.Options] {
if let url = URL(string: urlString) {
return parse(url)
}
return []
}
/**
Parse an URL Foudation object into an array of ClientRequest.Options.
### Usage Example: ###
````swift
let url: URL = URL(string: "http://www.website.com")!
let parsedOptions = clientRequest.parse(url)
````
- Parameter url: Foundation URL object.
- Returns: An array of `ClientRequest.Options`
*/
public class func parse(_ url: URL) -> [ClientRequest.Options] {
var options: [ClientRequest.Options] = []
if let scheme = url.scheme {
options.append(.schema("\(scheme)://"))
}
if let host = url.host {
options.append(.hostname(host))
}
var fullPath = url.path
// query strings and parameters need to be appended here
if let query = url.query {
fullPath += "?"
fullPath += query
}
options.append(.path(fullPath))
if let port = url.port {
options.append(.port(Int16(bitPattern: UInt16(port))))
}
if let username = url.user {
options.append(.username(username))
}
if let password = url.password {
options.append(.password(password))
}
return options
}
/// Instance destruction
deinit {
if let handle = handle {
curl_easy_cleanup(handle)
}
if headersList != nil {
curl_slist_free_all(headersList)
}
}
/**
Add a String to the body of the request to be sent.
### Usage Example: ###
````swift
let stringToSend: String = "send something"
clientRequest.write(from: stringToSend)
````
- Parameter from: The String to be added to the request.
*/
public func write(from string: String) {
if let data = string.data(using: .utf8) {
write(from: data)
}
}
/**
Add the bytes in a Data struct to the body of the request to be sent.
### Usage Example: ###
````swift
let string = "some some more stuff"
if let data: Data = string.data(using: .utf8) {
clientRequest.write(from: data)
}
````
- Parameter from: The Data Struct containing the bytes to be added to the request.
*/
public func write(from data: Data) {
writeBuffers.append(data: data)
}
/**
Add a String to the body of the request to be sent and then send the request to the remote server.
### Usage Example: ###
````swift
let data: String = "send something"
clientRequest.end(from: data, close: true)
````
- Parameter data: The String to be added to the request.
- Parameter close: If true, add the "Connection: close" header to the set of headers sent with the request.
*/
public func end(_ data: String, close: Bool = false) {
write(from: data)
end(close: close)
}
/**
Add the bytes in a Data struct to the body of the request to be sent and then send the request to the remote server.
### Usage Example: ###
````swift
let stringToSend = "send this"
let data: Data = stringToSend.data(using: .utf8) {
clientRequest.end(from: data, close: true)
}
````
- Parameter data: The Data struct containing the bytes to be added to the request.
- Parameter close: If true, add the "Connection: close" header to the set of headers sent with the request.
*/
public func end(_ data: Data, close: Bool = false) {
write(from: data)
end(close: close)
}
/**
Send the request to the remote server.
### Usage Example: ###
````swift
clientRequest.end(true)
````
- Parameter close: If true, add the "Connection: close" header to the set of headers sent with the request.
*/
public func end(close: Bool = false) {
closeConnection = close
guard let urlBuffer = url.cString(using: .utf8) else {
callback(nil)
return
}
prepareHandle(using: urlBuffer)
let invoker = CurlInvoker(handle: handle!, maxRedirects: maxRedirects)
invoker.delegate = self
let skipBody = (method.uppercased() == "HEAD")
response = ClientResponse(skipBody: skipBody)
var code = invoker.invoke()
guard code == CURLE_OK else {
Log.error("ClientRequest Error, Failed to invoke HTTP request. CURL Return code=\(code)")
callback(nil)
return
}
code = curlHelperGetInfoLong(handle!, CURLINFO_RESPONSE_CODE, &response!.status)
guard code == CURLE_OK else {
Log.error("ClientRequest Error. Failed to get response code. CURL Return code=\(code)")
callback(nil)
return
}
var httpStatusCode = response!.httpStatusCode
repeat {
let parseStatus = response!.parse()
guard parseStatus.error == nil else {
Log.error("ClientRequest error. Failed to parse response. Error=\(parseStatus.error!)")
callback(nil)
return
}
guard parseStatus.state == .messageComplete else {
Log.error("ClientRequest error. Failed to parse response. Status=\(parseStatus.state)")
callback(nil)
return
}
httpStatusCode = response!.httpStatusCode
} while httpStatusCode == .continue || httpStatusCode == .switchingProtocols
self.callback(self.response)
}
/// Prepare the handle
///
/// Parameter using: The URL to use when preparing the handle
private func prepareHandle(using urlBuffer: [CChar]) {
handle = curl_easy_init()
// HTTP parser does the decoding
curlHelperSetOptInt(handle!, CURLOPT_HTTP_TRANSFER_DECODING, 0)
curlHelperSetOptString(self.handle!, CURLOPT_URL, UnsafePointer(urlBuffer))
setMethodAndContentLength()
setupHeaders()
curlHelperSetOptString(handle!, CURLOPT_COOKIEFILE, "")
// Set HTTP 1.0
curlHelperSetOptInt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0)
// To see the messages sent by libCurl
curlHelperSetOptInt(handle, CURLOPT_VERBOSE, enableVerboseLogging ? 1 : 0)
if useHTTP2 {
curlHelperSetOptInt(handle!, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0)
}
if let socketPath = unixDomainSocketPath?.cString(using: .utf8) {
curlHelperSetUnixSocketPath(handle!, UnsafePointer(socketPath))
}
if let sslCertificate = self.sslCertificate {
curlHelperSetOptString(handle!, CURLOPT_SSLCERT, sslCertificate)
curlHelperSetOptString(handle!, CURLOPT_SSLCERTTYPE, sslCertificateFormat)
}
if let sslKey = self.sslKey {
curlHelperSetOptString(handle!, CURLOPT_SSLKEY, sslKey)
curlHelperSetOptString(handle!, CURLOPT_SSLKEYTYPE, sslCertificateFormat)
}
if let sslKeyPassphrase = self.sslKeyPassphrase {
curlHelperSetOptString(handle!, CURLOPT_KEYPASSWD, sslKeyPassphrase)
}
curlHelperSetOptInt(handle!, CURLOPT_SSL_VERIFYHOST, 2)
curlHelperSetOptInt(handle!, CURLOPT_SSL_VERIFYPEER, 1)
if disableSSLVerification {
Log.info("Disabled SSL Verification")
curlHelperSetOptInt(handle!, CURLOPT_SSL_VERIFYHOST, 0)
curlHelperSetOptInt(handle!, CURLOPT_SSL_VERIFYPEER, 0)
}
if let file = self.caFile {
Log.info("Set CA File Info \(file)")
curlHelperSetOptString(handle!, CURLOPT_CAINFO, file)
}
if let path = self.caPath {
Log.info("Set CA Path Info \(path)")
curlHelperSetOptString(handle!, CURLOPT_CAPATH, path)
}
if let crl = self.crlFile {
Log.info("Set CRL Info \(crl)")
curlHelperSetOptString(handle!, CURLOPT_CRLFILE, crl)
}
if enableOCSP {
// Use unsafePointer to forward url to the callback
let unsafePointer = UnsafeMutablePointer<Int8>(mutating: (self.url as NSString).utf8String)
curlHelperSetOptString(handle!, CURLOPT_SSL_CTX_DATA, unsafePointer)
curlHelperSetOptSSLCtxFunc(handle!) {(
curl: UnsafeMutableRawPointer?,
context: UnsafeMutableRawPointer?,
params: UnsafeMutableRawPointer?) -> UInt32 in
guard let params = params else {
return CURLE_SSL_CONNECT_ERROR.rawValue
}
let url = String(cString: params.assumingMemoryBound(to: UInt8.self))
if OCSPChecker(url: url).checkOCSP() {
return CURLE_OK.rawValue
} else {
return CURLE_ABORTED_BY_CALLBACK.rawValue
}
}
}
}
/// Sets the HTTP method and Content-Length in libCurl
private func setMethodAndContentLength() {
let methodUpperCase = method.uppercased()
let count = writeBuffers.count
switch(methodUpperCase) {
case "GET":
curlHelperSetOptBool(handle!, CURLOPT_HTTPGET, CURL_TRUE)
case "POST":
curlHelperSetOptBool(handle!, CURLOPT_POST, CURL_TRUE)
curlHelperSetOptInt(handle!, CURLOPT_POSTFIELDSIZE, count)
case "PUT":
curlHelperSetOptBool(handle!, CURLOPT_UPLOAD, CURL_TRUE)
curlHelperSetOptInt(handle!, CURLOPT_INFILESIZE, count)
case "HEAD":
curlHelperSetOptBool(handle!, CURLOPT_NOBODY, CURL_TRUE)
case "PATCH":
curlHelperSetOptString(handle!, CURLOPT_CUSTOMREQUEST, methodUpperCase)
curlHelperSetOptBool(handle!, CURLOPT_UPLOAD, CURL_TRUE)
curlHelperSetOptInt(handle!, CURLOPT_INFILESIZE, count)
default:
curlHelperSetOptString(handle!, CURLOPT_CUSTOMREQUEST, methodUpperCase)
}
}
/// Sets the headers in libCurl to the ones in headers
private func setupHeaders() {
if closeConnection {
headers["Connection"] = "close"
}
// Unless the user has provided an Expect header, set an empty one to disable
// curl's default Expect: 100-continue behaviour, since Kitura does not support it.
if !headers.keys.contains("Expect") {
headers["Expect"] = ""
}
for (headerKey, headerValue) in headers {
if let headerString = "\(headerKey): \(headerValue)".cString(using: .utf8) {
headersList = curl_slist_append(headersList, UnsafePointer(headerString))
}
}
curlHelperSetOptList(handle!, CURLOPT_HTTPHEADER, headersList)
}
private func createHTTPBasicAuthHeader(username: String, password: String) -> String {
let authHeader = "\(username):\(password)"
let data = Data(authHeader.utf8)
return "Basic \(data.base64EncodedString())"
}
}
// MARK: CurlInvokerDelegate extension
extension ClientRequest: CurlInvokerDelegate {
/// libCurl callback to recieve data sent by the server
fileprivate func curlWriteCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {
response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
return size
}
/// libCurl callback to provide the data to send to the server
fileprivate func curlReadCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {
let count = writeBuffers.fill(buffer: UnsafeMutableRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
return count
}
/// libCurl callback to recieve header sent by the server. Being called per each header line.
fileprivate func curlHeaderCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int {
// If the header status line begins with 'HTTP/2 ' we replace it with 'HTTP/2.0' because
// otherwise the CHTTPParser will parse this line incorrectly and won't extract the status code
#if swift(>=5.0)
ClientRequest.Http2StatusLineVersion.withUnsafeBytes() { (rawPtr: UnsafeRawBufferPointer) -> Void in
if memcmp(rawPtr.baseAddress!, buf, ClientRequest.Http2StatusLineVersion.count) == 0 {
ClientRequest.Http2StatusLineVersionWithMinor.withUnsafeBytes() { (p: UnsafeRawBufferPointer) -> Void in
response?.responseBuffers.append(bytes: p.bindMemory(to: UInt8.self).baseAddress!, length: ClientRequest.Http2StatusLineVersionWithMinor.count)
response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self) + ClientRequest.Http2StatusLineVersion.count,
length: size - ClientRequest.Http2StatusLineVersion.count)
}
}
else {
response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
}
}
#else
ClientRequest.Http2StatusLineVersion.withUnsafeBytes() { (ptr: UnsafePointer<UInt8>) -> Void in
if memcmp(ptr, buf, ClientRequest.Http2StatusLineVersion.count) == 0 {
ClientRequest.Http2StatusLineVersionWithMinor.withUnsafeBytes() { (p: UnsafePointer<UInt8>) -> Void in
response?.responseBuffers.append(bytes: p, length: ClientRequest.Http2StatusLineVersionWithMinor.count)
response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self) + ClientRequest.Http2StatusLineVersion.count,
length: size - ClientRequest.Http2StatusLineVersion.count)
}
}
else {
response?.responseBuffers.append(bytes: UnsafeRawPointer(buf).assumingMemoryBound(to: UInt8.self), length: size)
}
}
#endif
return size
}
/// libCurl callback invoked when a redirect is about to be done
fileprivate func prepareForRedirect() {
response?.responseBuffers.reset()
writeBuffers.rewind()
}
}
/// Helper class for invoking commands through libCurl
private class CurlInvoker {
/// Pointer to the libCurl handle
private var handle: UnsafeMutableRawPointer
/// Delegate that can have a read or write callback
fileprivate weak var delegate: CurlInvokerDelegate?
/// Maximum number of redirects
private let maxRedirects: Int
/// Initializes a new CurlInvoker instance
fileprivate init(handle: UnsafeMutableRawPointer, maxRedirects: Int) {
self.handle = handle
self.maxRedirects = maxRedirects
}
/// Run the HTTP method through the libCurl library
///
/// - Returns: a status code for the success of the operation
fileprivate func invoke() -> CURLcode {
var rc: CURLcode = CURLE_FAILED_INIT
guard let delegate = self.delegate else {
return rc
}
withUnsafeMutablePointer(to: &self.delegate) {ptr in
self.prepareHandle(ptr)
var redirected = false
var redirectCount = 0
repeat {
rc = curl_easy_perform(handle)
if rc == CURLE_OK {
var redirectUrl: UnsafeMutablePointer<Int8>? = nil
let infoRc = curlHelperGetInfoCString(handle, CURLINFO_REDIRECT_URL, &redirectUrl)
if infoRc == CURLE_OK {
if redirectUrl != nil {
redirectCount += 1
if redirectCount <= maxRedirects {
// Prepare to do a redirect
curlHelperSetOptString(handle, CURLOPT_URL, redirectUrl)
var status: Int = -1
let codeRc = curlHelperGetInfoLong(handle, CURLINFO_RESPONSE_CODE, &status)
// If the status code was 303 See Other, ensure that
// the redirect is done with a GET query rather than
// whatever might have just been used.
if codeRc == CURLE_OK && status == 303 {
_ = curlHelperSetOptInt(handle, CURLOPT_HTTPGET, 1)
}
redirected = true
delegate.prepareForRedirect()
}
else {
redirected = false
}
}
else {
redirected = false
}
}
}
} while rc == CURLE_OK && redirected
}
return rc
}
/// Prepare the handle
///
/// - Parameter ptr: pointer to the CurlInvokerDelegat
private func prepareHandle(_ ptr: UnsafeMutablePointer<CurlInvokerDelegate?>) {
curlHelperSetOptReadFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutableRawPointer?) -> Int in
let p = privateData?.assumingMemoryBound(to: CurlInvokerDelegate.self).pointee
return (p?.curlReadCallback(buf!, size: size*nMemb))!
}
curlHelperSetOptWriteFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutableRawPointer?) -> Int in
let p = privateData?.assumingMemoryBound(to: CurlInvokerDelegate.self).pointee
return (p?.curlWriteCallback(buf!, size: size*nMemb))!
}
curlHelperSetOptHeaderFunc(handle, ptr) { (buf: UnsafeMutablePointer<Int8>?, size: Int, nMemb: Int, privateData: UnsafeMutableRawPointer?) -> Int in
let p = privateData?.assumingMemoryBound(to: CurlInvokerDelegate.self).pointee
return (p?.curlHeaderCallback(buf!, size: size*nMemb))!
}
}
}
/// Delegate protocol for objects operated by CurlInvoker
private protocol CurlInvokerDelegate: class {
func curlWriteCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int
func curlReadCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int
func curlHeaderCallback(_ buf: UnsafeMutablePointer<Int8>, size: Int) -> Int
func prepareForRedirect()
}