Differences From Artifact [fddee74eee]:
- File src/OFHTTPClient.m — part of check-in [9036359e2e] at 2018-12-18 17:14:16 on branch trunk — OFHTTPClient: Fix type mismatch (user: js, size: 29387) [annotate] [blame] [check-ins using]
To Artifact [830dad71cc]:
- File
src/OFHTTPClient.m
— part of check-in
[eaf458c1e6]
at
2018-12-21 21:39:47
on branch trunk
— Remove context from OFHTTPClientDelegate
It was not very useful as for any OFHTTPClient there can only be one
active request. (user: js, size: 28132) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
53 54 55 56 57 58 59 | @interface OFHTTPClientRequestHandler: OFObject <OFTCPSocketDelegate> { @public OFHTTPClient *_client; OFHTTPRequest *_request; unsigned int _redirects; | < | < | 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; bool _firstLine; OFString *_version; int _status; OFMutableDictionary OF_GENERIC(OFString *, OFString *) *_serverHeaders; } - (instancetype)initWithClient: (OFHTTPClient *)client request: (OFHTTPRequest *)request redirects: (unsigned int)redirects; - (void)start; - (void)closeAndReconnect; @end @interface OFHTTPClientRequestBodyStream: OFStream <OFReadyForWritingObserving> { OFHTTPClientRequestHandler *_handler; |
︙ | ︙ | |||
101 102 103 104 105 106 107 | OFHTTPClient *_client; OFObject <OFHTTPClientDelegate> *_delegate; OFHTTPResponse *_response; } - (instancetype)initWithClient: (OFHTTPClient *)client; - (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request | | < | 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; @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 | return follow; } @implementation OFHTTPClientRequestHandler - (instancetype)initWithClient: (OFHTTPClient *)client request: (OFHTTPRequest *)request redirects: (unsigned int)redirects | < < < | | < | 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 { self = [super init]; @try { _client = [client retain]; _request = [request retain]; _redirects = redirects; _serverHeaders = [[OFMutableDictionary alloc] init]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_client release]; [_request release]; [_version release]; [_serverHeaders release]; [super dealloc]; } - (void)raiseException: (id)exception { [_client close]; _client->_inProgress = false; [_client->_delegate client: _client didFailWithException: exception request: _request]; } - (void)createResponseWithSocketOrThrow: (OFTCPSocket *)sock { OFURL *URL = [_request URL]; OFHTTPClientResponse *response; OFString *connectionHeader; |
︙ | ︙ | |||
345 346 347 348 349 350 351 | OFURL *newURL; bool follow; newURL = [OFURL URLWithString: location relativeToURL: URL]; if ([_client->_delegate respondsToSelector: @selector(client: | | | < | 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:)]) follow = [_client->_delegate client: _client shouldFollowRedirect: newURL statusCode: _status request: _request response: response]; else follow = defaultShouldFollow( [_request method], _status); if (follow) { OFDictionary OF_GENERIC(OFString *, OFString *) *headers = [_request headers]; |
︙ | ︙ | |||
397 398 399 400 401 402 403 | [newRequest setURL: newURL]; [newRequest setHeaders: newHeaders]; _client->_inProgress = false; [_client asyncPerformRequest: newRequest | | < | < | 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]; return; } } _client->_inProgress = false; if (_status / 100 != 2) @throw [OFHTTPRequestFailedException exceptionWithRequest: _request response: response]; [_client->_delegate performSelector: @selector(client:didPerformRequest: response:) withObject: _client withObject: _request withObject: response afterDelay: 0]; } - (void)createResponseWithSocket: (OFTCPSocket *)sock { @try { [self createResponseWithSocketOrThrow: sock]; |
︙ | ︙ | |||
468 469 470 471 472 473 474 | if (line == nil) @throw [OFInvalidServerReplyException exception]; if ([line length] == 0) { [_serverHeaders makeImmutable]; if ([_client->_delegate respondsToSelector: @selector(client: | | | < | 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:)]) [_client->_delegate client: _client didReceiveHeaders: _serverHeaders statusCode: _status request: _request]; [sock setDelegate: nil]; [self performSelector: @selector(createResponseWithSocket:) withObject: sock afterDelay: 0]; |
︙ | ︙ | |||
580 581 582 583 584 585 586 | [stream setDelegate: nil]; OFStream *requestBody = [[[OFHTTPClientRequestBodyStream alloc] initWithHandler: self socket: stream] autorelease]; if ([_client->_delegate respondsToSelector: | | | < < | 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:)]) [_client->_delegate client: _client wantsRequestBody: requestBody request: _request]; } else [stream asyncReadLine]; return nil; } - (void)handleSocket: (OFTCPSocket *)sock |
︙ | ︙ | |||
624 625 626 627 628 629 630 | if (exception != nil) { [self raiseException: exception]; return; } if ([_client->_delegate respondsToSelector: | | | < | 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:)]) [_client->_delegate client: _client didCreateSocket: sock request: _request]; [self performSelector: @selector(handleSocket:) withObject: sock afterDelay: 0]; } - (void)start |
︙ | ︙ | |||
660 661 662 663 664 665 666 667 668 669 670 671 672 673 | _client->_socket = nil; [_client->_lastURL release]; _client->_lastURL = nil; [_client->_lastResponse release]; _client->_lastResponse = nil; [self performSelector: @selector(handleSocket:) withObject: sock afterDelay: 0]; } else [self closeAndReconnect]; } | > > | 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 | [_client release]; [super dealloc]; } - (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request redirects: (unsigned int)redirects | < | < < | < | | | < < | | < < | | < < | | < < | | < | 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 { [_client asyncPerformRequest: request redirects: redirects]; [[OFRunLoop currentRunLoop] run]; return _response; } - (void)client: (OFHTTPClient *)client didPerformRequest: (OFHTTPRequest *)request response: (OFHTTPResponse *)response { [[OFRunLoop currentRunLoop] stop]; [_response release]; _response = [response retain]; [_delegate client: client didPerformRequest: request response: response]; } - (void)client: (OFHTTPClient *)client didFailWithException: (id)exception request: (OFHTTPRequest *)request { /* * 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 { if ([_delegate respondsToSelector: @selector(client:didCreateSocket:request:)]) [_delegate client: client didCreateSocket: sock request: request]; } - (void)client: (OFHTTPClient *)client wantsRequestBody: (OFStream *)body request: (OFHTTPRequest *)request { if ([_delegate respondsToSelector: @selector(client:wantsRequestBody:request:)]) [_delegate client: client wantsRequestBody: body request: request]; } - (void)client: (OFHTTPClient *)client didReceiveHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers statusCode: (int)statusCode request: (OFHTTPRequest *)request { if ([_delegate respondsToSelector: @selector(client:didReceiveHeaders:statusCode:request:)]) [_delegate client: client didReceiveHeaders: headers statusCode: statusCode request: request]; } - (bool)client: (OFHTTPClient *)client shouldFollowRedirect: (OFURL *)URL statusCode: (int)statusCode request: (OFHTTPRequest *)request response: (OFHTTPResponse *)response { if ([_delegate respondsToSelector: @selector(client: shouldFollowRedirect:statusCode:request:response:)]) return [_delegate client: client shouldFollowRedirect: URL statusCode: statusCode request: request response: response]; else return defaultShouldFollow([request method], statusCode); } @end @implementation OFHTTPClient @synthesize delegate = _delegate; |
︙ | ︙ | |||
1161 1162 1163 1164 1165 1166 1167 | [super dealloc]; } - (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request { return [self performRequest: request | | < < < < < < < < < < < < < < < < < < | < < | < < | < | 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]; } - (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request redirects: (unsigned int)redirects { void *pool = objc_autoreleasePoolPush(); OFHTTPClient_SyncPerformer *syncPerformer = [[[OFHTTPClient_SyncPerformer alloc] initWithClient: self] autorelease]; OFHTTPResponse *response = [syncPerformer performRequest: request redirects: redirects]; [response retain]; objc_autoreleasePoolPop(pool); return [response autorelease]; } - (void)asyncPerformRequest: (OFHTTPRequest *)request { [self asyncPerformRequest: request redirects: REDIRECTS_DEFAULT]; } - (void)asyncPerformRequest: (OFHTTPRequest *)request redirects: (unsigned int)redirects { 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] autorelease] start]; objc_autoreleasePoolPop(pool); } - (void)close { [_socket release]; |
︙ | ︙ |