︙ | | |
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
|
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
|
-
-
-
+
-
-
-
-
-
-
+
+
+
-
-
-
-
+
-
-
-
-
+
-
-
-
-
+
-
|
@protocol OFHTTPClientDelegate <OFObject>
/*!
* @brief A callback which is called when an OFHTTPClient performed a request.
*
* @param client The OFHTTPClient which performed the request
* @param request The request the OFHTTPClient performed
* @param response The response to the request performed
* @param context The context object that was passed to
* @ref asyncPerformRequest:context:
*/
- (void)client: (OFHTTPClient *)client
didPerformRequest: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
response: (OFHTTPResponse *)response;
context: (nullable id)context;
/*!
* @brief A callback which is called when an OFHTTPClient encountered an
* exception while performing a request.
*
* @param client The client which encountered an exception
* @param exception The exception the client encountered
* @param request The request during which the client encountered the exception
* @param context The context object that was passed to
* @ref asyncPerformRequest:context:
*/
- (void)client: (OFHTTPClient *)client
didEncounterException: (id)exception
request: (OFHTTPRequest *)request
- (void)client: (OFHTTPClient *)client
didFailWithException: (id)exception
request: (OFHTTPRequest *)request;
context: (nullable id)context;
@optional
/*!
* @brief A callback which is called when an OFHTTPClient creates a socket.
*
* This is useful if the connection is using HTTPS and the server requires a
* client certificate. This callback can then be used to tell the TLS socket
* about the certificate. Another use case is to tell the socket about a SOCKS5
* proxy it should use for this connection.
*
* @param client The OFHTTPClient that created a socket
* @param socket The socket created by the OFHTTPClient
* @param request The request for which the socket was created
* @param context The context object that was passed to
* @ref asyncPerformRequest:context:
*/
- (void)client: (OFHTTPClient *)client
didCreateSocket: (OF_KINDOF(OFTCPSocket *))socket
request: (OFHTTPRequest *)request
request: (OFHTTPRequest *)request;
context: (nullable id)context;
/*!
* @brief A callback which is called when an OFHTTPClient wants to send the
* body for a request.
*
* @param client The OFHTTPClient that wants to send the body
* @param requestBody A stream into which the body of the request should be
* written
* @param request The request for which the OFHTTPClient wants to send the body
* @param context The context object that was passed to
* @ref asyncPerformRequest:context:
*/
- (void)client: (OFHTTPClient *)client
wantsRequestBody: (OFStream *)requestBody
request: (OFHTTPRequest *)request
request: (OFHTTPRequest *)request;
context: (nullable id)context;
/*!
* @brief A callback which is called when an OFHTTPClient received headers.
*
* @param client The OFHTTPClient which received the headers
* @param headers The headers received
* @param statusCode The status code received
* @param request The request for which the headers and status code have been
* received
* @param context The context object that was passed to
* @ref asyncPerformRequest:context:
*/
- (void)client: (OFHTTPClient *)client
didReceiveHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
request: (OFHTTPRequest *)request;
context: (nullable id)context;
/*!
* @brief A callback which is called when an OFHTTPClient wants to follow a
* redirect.
*
* If you want to get the headers and data for each redirect, set the number of
* redirects to 0 and perform a new OFHTTPClient for each redirect. However,
|
︙ | | |
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
-
-
-
+
-
|
* @param statusCode The status code for the redirection
* @param request The request for which the OFHTTPClient wants to redirect.
* You are allowed to change the request's headers from this
* callback and they will be used when following the redirect
* (e.g. to set the cookies for the new URL), however, keep in
* mind that this will change the request you originally passed.
* @param response The response indicating the redirect
* @param context The context object that was passed to
* @ref asyncPerformRequest:context:
* @return A boolean whether the OFHTTPClient should follow the redirect
*/
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirect: (OFURL *)URL
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
response: (OFHTTPResponse *)response;
context: (nullable id)context;
@end
/*!
* @class OFHTTPClient OFHTTPClient.h ObjFW/OFHTTPClient.h
*
* @brief A class for performing HTTP requests.
*/
|
︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
|
* attempt is done to follow the redirect, but instead the
* redirect is treated as an OFHTTPResponse
* @return The OFHTTPResponse for the request
*/
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects;
/*!
* @brief Synchronously performs the specified HTTP request.
*
* @note You must not change the delegate while a synchronous request is
* running! If you want to change the delegate during the request,
* perform an asynchronous request instead!
*
* @param request The request to perform
* @param context A context object to be passed to the delegate
* @return The OFHTTPResponse for the request
*/
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
context: (nullable id)context;
/*!
* @brief Synchronously performs the specified HTTP request.
*
* @note You must not change the delegate while a synchronous request is
* running! If you want to change the delegate during the request,
* perform an asynchronous request instead!
*
* @param request The request to perform
* @param redirects The maximum number of redirects after which no further
* attempt is done to follow the redirect, but instead the
* redirect is treated as an OFHTTPResponse
* @param context A context object to be passed to the delegate
* @return The OFHTTPResponse for the request
*/
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
context: (nullable id)context;
/*!
* @brief Asynchronously performs the specified HTTP request.
*
* @param request The request to perform
* @param context A context object to be passed to the delegate
*/
- (void)asyncPerformRequest: (OFHTTPRequest *)request
- (void)asyncPerformRequest: (OFHTTPRequest *)request;
context: (nullable id)context;
/*!
* @brief Asynchronously performs the specified HTTP request.
*
* @param request The request to perform
* @param redirects The maximum number of redirects after which no further
* attempt is done to follow the redirect, but instead the
* redirect is treated as an OFHTTPResponse
* @param context A context object to be passed to the delegate
*/
- (void)asyncPerformRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
redirects: (unsigned int)redirects;
context: (nullable id)context;
/*!
* @brief Closes connections that are still open due to keep-alive.
*/
- (void)close;
@end
OF_ASSUME_NONNULL_END
|
︙ | | |
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
|
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
-
-
+
-
|
@interface OFHTTPClientRequestHandler: OFObject <OFTCPSocketDelegate>
{
@public
OFHTTPClient *_client;
OFHTTPRequest *_request;
unsigned int _redirects;
id _context;
bool _firstLine;
OFString *_version;
int _status;
OFMutableDictionary OF_GENERIC(OFString *, OFString *) *_serverHeaders;
}
- (instancetype)initWithClient: (OFHTTPClient *)client
request: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
redirects: (unsigned int)redirects;
context: (id)context;
- (void)start;
- (void)closeAndReconnect;
@end
@interface OFHTTPClientRequestBodyStream: OFStream <OFReadyForWritingObserving>
{
OFHTTPClientRequestHandler *_handler;
|
︙ | | |
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
-
+
-
|
OFHTTPClient *_client;
OFObject <OFHTTPClientDelegate> *_delegate;
OFHTTPResponse *_response;
}
- (instancetype)initWithClient: (OFHTTPClient *)client;
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
redirects: (unsigned int)redirects;
context: (id)context;
@end
static OFString *
constructRequestString(OFHTTPRequest *request)
{
void *pool = objc_autoreleasePoolPush();
of_http_request_method_t method = [request method];
|
︙ | | |
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
|
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
|
-
-
-
-
-
+
+
-
|
return follow;
}
@implementation OFHTTPClientRequestHandler
- (instancetype)initWithClient: (OFHTTPClient *)client
request: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
context: (id)context
{
self = [super init];
@try {
_client = [client retain];
_request = [request retain];
_redirects = redirects;
_context = [context retain];
_serverHeaders = [[OFMutableDictionary alloc] init];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_client release];
[_request release];
[_context release];
[_version release];
[_serverHeaders release];
[super dealloc];
}
- (void)raiseException: (id)exception
{
[_client close];
_client->_inProgress = false;
[_client->_delegate client: _client
didEncounterException: exception
request: _request
didFailWithException: exception
request: _request];
context: _context];
}
- (void)createResponseWithSocketOrThrow: (OFTCPSocket *)sock
{
OFURL *URL = [_request URL];
OFHTTPClientResponse *response;
OFString *connectionHeader;
|
︙ | | |
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
-
+
-
+
-
|
OFURL *newURL;
bool follow;
newURL = [OFURL URLWithString: location
relativeToURL: URL];
if ([_client->_delegate respondsToSelector: @selector(client:
shouldFollowRedirect:statusCode:request:response:context:)])
shouldFollowRedirect:statusCode:request:response:)])
follow = [_client->_delegate client: _client
shouldFollowRedirect: newURL
statusCode: _status
request: _request
response: response
response: response];
context: _context];
else
follow = defaultShouldFollow(
[_request method], _status);
if (follow) {
OFDictionary OF_GENERIC(OFString *, OFString *)
*headers = [_request headers];
|
︙ | | |
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
|
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
|
-
+
-
-
+
-
|
[newRequest setURL: newURL];
[newRequest setHeaders: newHeaders];
_client->_inProgress = false;
[_client asyncPerformRequest: newRequest
redirects: _redirects - 1
redirects: _redirects - 1];
context: _context];
return;
}
}
_client->_inProgress = false;
if (_status / 100 != 2)
@throw [OFHTTPRequestFailedException
exceptionWithRequest: _request
response: response];
[_client->_delegate performSelector: @selector(client:didPerformRequest:
response:context:)
response:)
withObject: _client
withObject: _request
withObject: response
withObject: _context
afterDelay: 0];
}
- (void)createResponseWithSocket: (OFTCPSocket *)sock
{
@try {
[self createResponseWithSocketOrThrow: sock];
|
︙ | | |
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
-
+
-
+
-
|
if (line == nil)
@throw [OFInvalidServerReplyException exception];
if ([line length] == 0) {
[_serverHeaders makeImmutable];
if ([_client->_delegate respondsToSelector: @selector(client:
didReceiveHeaders:statusCode:request:context:)])
didReceiveHeaders:statusCode:request:)])
[_client->_delegate client: _client
didReceiveHeaders: _serverHeaders
statusCode: _status
request: _request
request: _request];
context: _context];
[sock setDelegate: nil];
[self performSelector: @selector(createResponseWithSocket:)
withObject: sock
afterDelay: 0];
|
︙ | | |
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
-
+
-
+
-
-
|
[stream setDelegate: nil];
OFStream *requestBody = [[[OFHTTPClientRequestBodyStream alloc]
initWithHandler: self
socket: stream] autorelease];
if ([_client->_delegate respondsToSelector:
@selector(client:wantsRequestBody:request:context:)])
@selector(client:wantsRequestBody:request:)])
[_client->_delegate client: _client
wantsRequestBody: requestBody
request: _request
request: _request];
context: _context];
} else
[stream asyncReadLine];
return nil;
}
- (void)handleSocket: (OFTCPSocket *)sock
|
︙ | | |
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
-
+
-
+
-
|
if (exception != nil) {
[self raiseException: exception];
return;
}
if ([_client->_delegate respondsToSelector:
@selector(client:didCreateSocket:request:context:)])
@selector(client:didCreateSocket:request:)])
[_client->_delegate client: _client
didCreateSocket: sock
request: _request
request: _request];
context: _context];
[self performSelector: @selector(handleSocket:)
withObject: sock
afterDelay: 0];
}
- (void)start
|
︙ | | |
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
+
+
|
_client->_socket = nil;
[_client->_lastURL release];
_client->_lastURL = nil;
[_client->_lastResponse release];
_client->_lastResponse = nil;
[sock setDelegate: self];
[self performSelector: @selector(handleSocket:)
withObject: sock
afterDelay: 0];
} else
[self closeAndReconnect];
}
|
︙ | | |
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
|
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
|
-
-
+
-
-
-
+
-
-
-
-
+
+
+
-
-
-
+
-
+
-
-
-
+
-
+
-
-
-
+
-
+
-
-
-
+
-
+
-
|
[_client release];
[super dealloc];
}
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
context: (id)context
{
[_client asyncPerformRequest: request
redirects: redirects
redirects: redirects];
context: context];
[[OFRunLoop currentRunLoop] run];
return _response;
}
- (void)client: (OFHTTPClient *)client
didPerformRequest: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
context: (id)context
{
[[OFRunLoop currentRunLoop] stop];
[_response release];
_response = [response retain];
[_delegate client: client
didPerformRequest: request
response: response
response: response];
context: context];
}
- (void)client: (OFHTTPClient *)client
didEncounterException: (id)exception
request: (OFHTTPRequest *)request
- (void)client: (OFHTTPClient *)client
didFailWithException: (id)exception
request: (OFHTTPRequest *)request
context: (id)context
{
/*
* Restore the delegate - we're giving up, but not reaching the release
* of the autorelease pool that contains us, so resetting it via
* -[dealloc] might be too late.
*/
[_client setDelegate: _delegate];
@throw exception;
}
- (void)client: (OFHTTPClient *)client
didCreateSocket: (OF_KINDOF(OFTCPSocket *))sock
request: (OFHTTPRequest *)request
context: (id)context
{
if ([_delegate respondsToSelector:
@selector(client:didCreateSocket:request:context:)])
@selector(client:didCreateSocket:request:)])
[_delegate client: client
didCreateSocket: sock
request: request
request: request];
context: context];
}
- (void)client: (OFHTTPClient *)client
wantsRequestBody: (OFStream *)body
request: (OFHTTPRequest *)request
context: (id)context
{
if ([_delegate respondsToSelector:
@selector(client:wantsRequestBody:request:context:)])
@selector(client:wantsRequestBody:request:)])
[_delegate client: client
wantsRequestBody: body
request: request
request: request];
context: context];
}
- (void)client: (OFHTTPClient *)client
didReceiveHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
context: (id)context
{
if ([_delegate respondsToSelector:
@selector(client:didReceiveHeaders:statusCode:request:context:)])
@selector(client:didReceiveHeaders:statusCode:request:)])
[_delegate client: client
didReceiveHeaders: headers
statusCode: statusCode
request: request
request: request];
context: context];
}
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirect: (OFURL *)URL
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
context: (id)context
{
if ([_delegate respondsToSelector: @selector(client:
shouldFollowRedirect:statusCode:request:response:context:)])
shouldFollowRedirect:statusCode:request:response:)])
return [_delegate client: client
shouldFollowRedirect: URL
statusCode: statusCode
request: request
response: response
response: response];
context: context];
else
return defaultShouldFollow([request method], statusCode);
}
@end
@implementation OFHTTPClient
@synthesize delegate = _delegate;
|
︙ | | |
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
|
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
+
-
|
[super dealloc];
}
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
{
return [self performRequest: request
redirects: REDIRECTS_DEFAULT
redirects: REDIRECTS_DEFAULT];
context: nil];
}
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
{
return [self performRequest: request
redirects: redirects
context: nil];
}
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
context: (id)context
{
return [self performRequest: request
redirects: REDIRECTS_DEFAULT
context: context];
}
- (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFHTTPClient_SyncPerformer *syncPerformer =
[[[OFHTTPClient_SyncPerformer alloc] initWithClient: self]
autorelease];
OFHTTPResponse *response = [syncPerformer performRequest: request
redirects: redirects
redirects: redirects];
context: context];
[response retain];
objc_autoreleasePoolPop(pool);
return [response autorelease];
}
- (void)asyncPerformRequest: (OFHTTPRequest *)request
context: (id)context
{
[self asyncPerformRequest: request
redirects: REDIRECTS_DEFAULT
redirects: REDIRECTS_DEFAULT];
context: context];
}
- (void)asyncPerformRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
context: (id)context
{
void *pool = objc_autoreleasePoolPush();
OFURL *URL = [request URL];
OFString *scheme = [URL scheme];
if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
if (_inProgress)
/* TODO: Find a better exception */
@throw [OFAlreadyConnectedException exception];
_inProgress = true;
[[[[OFHTTPClientRequestHandler alloc]
initWithClient: self
request: request
redirects: redirects
redirects: redirects] autorelease] start];
context: context] autorelease] start];
objc_autoreleasePoolPop(pool);
}
- (void)close
{
[_socket release];
|
︙ | | |
︙ | | |
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
-
-
+
+
|
@interface OFHTTP: OFObject <OFApplicationDelegate, OFHTTPClientDelegate,
OFStreamDelegate>
{
OFArray OF_GENERIC(OFString *) *_URLs;
size_t _URLIndex;
int _errorCode;
OFString *_outputPath, *_currentFileName;
bool _continue, _force, _detectFileName, _detectedFileName;
bool _quiet, _verbose, _insecure;
bool _continue, _force, _detectFileName, _detectFileNameRequest;
bool _detectedFileName, _quiet, _verbose, _insecure;
OFStream *_body;
of_http_request_method_t _method;
OFMutableDictionary *_clientHeaders;
OFHTTPClient *_HTTPClient;
char *_buffer;
OFStream *_output;
intmax_t _received, _length, _resumedFrom;
|
︙ | | |
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
|
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
|
-
-
-
|
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
}
- (void)client: (OFHTTPClient *)client
didCreateSocket: (OF_KINDOF(OFTCPSocket *))sock
request: (OFHTTPRequest *)request
context: (id)context
{
if (_insecure && [sock respondsToSelector:
@selector(setCertificateVerificationEnabled:)])
[sock setCertificateVerificationEnabled: false];
}
- (void)client: (OFHTTPClient *)client
wantsRequestBody: (OFStream *)body
request: (OFHTTPRequest *)request
context: (id)context
{
/* TODO: Do asynchronously and print status */
while (![_body isAtEndOfStream]) {
char buffer[4096];
size_t length;
length = [_body readIntoBuffer: buffer
length: 4096];
[body writeBuffer: buffer
length: length];
}
}
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirect: (OFURL *)URL
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
context: (id)context
{
if (_verbose) {
void *pool = objc_autoreleasePoolPush();
OFDictionary OF_GENERIC(OFString *, OFString *) *headers =
[response headers];
OFEnumerator *keyEnumerator = [headers keyEnumerator];
OFEnumerator *objectEnumerator =
|
︙ | | |
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
-
-
-
+
+
+
-
|
if (!_quiet)
[of_stdout writeFormat: @"☇ %@", [URL string]];
return true;
}
- (void)client: (OFHTTPClient *)client
didEncounterException: (id)e
request: (OFHTTPRequest *)request
- (void)client: (OFHTTPClient *)client
didFailWithException: (id)e
request: (OFHTTPRequest *)request
context: (id)context
{
if ([e isKindOfClass: [OFResolveHostFailedException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
OF_LOCALIZED(@"download_failed_resolve_host_failed",
|
︙ | | |
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
-
|
return true;
}
- (void)client: (OFHTTPClient *)client
didReceiveHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
context: (id)context
{
if (!_quiet) {
OFString *lengthString =
[headers objectForKey: @"Content-Length"];
OFString *type = [headers objectForKey: @"Content-Type"];
[of_stdout writeFormat: @" ➜ %d\n", statusCode];
|
︙ | | |
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
-
-
+
|
}
}
}
- (void)client: (OFHTTPClient *)client
didPerformRequest: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
context: (id)context
{
if ([context isEqual: @"detectFileName"]) {
if (_detectFileNameRequest) {
_currentFileName = [fileNameFromContentDisposition(
[[response headers] objectForKey: @"Content-Disposition"])
copy];
_detectedFileName = true;
/* Handle this URL on the next -[downloadNextURL] call */
_URLIndex--;
|
︙ | | |
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
|
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
|
+
-
+
-
|
if (!_quiet)
[of_stdout writeFormat: @"⠒ %@", [URL string]];
request = [OFHTTPRequest requestWithURL: URL];
[request setHeaders: clientHeaders];
[request setMethod: OF_HTTP_REQUEST_METHOD_HEAD];
_detectFileNameRequest = true;
[_HTTPClient asyncPerformRequest: request
[_HTTPClient asyncPerformRequest: request];
context: @"detectFileName"];
return;
}
[_currentFileName release];
_currentFileName = nil;
_detectedFileName = false;
|
︙ | | |
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
|
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
+
-
+
-
|
}
}
request = [OFHTTPRequest requestWithURL: URL];
[request setHeaders: clientHeaders];
[request setMethod: _method];
_detectFileNameRequest = false;
[_HTTPClient asyncPerformRequest: request
[_HTTPClient asyncPerformRequest: request];
context: nil];
return;
next:
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
}
@end
|