ObjFW  Diff

Differences From Artifact [376bac39d6]:

  • File src/OFHTTPClient.m — part of check-in [d16ad96cbd] at 2018-12-07 01:33:47 on branch trunk — OFStream: Use a delegate for async operations

    The target / selector approach had several drawbacks:

    * It was inconvenient to use, as for every read or write, a target,
    selector and context would need to be specified.
    * It lacked any kind of type-safety and would not even warn about using
    a callback method with the wrong number of parameters.
    * It encouraged using a different callback method for each read or
    write call, which results in code that is hard to follow and also
    slower (as it needs to recreate the async operation with a new
    callback every time). (user: js, size: 29459) [annotate] [blame] [check-ins using]

To Artifact [66db22090b]:


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFUnsupportedVersionException.h"
#import "OFWriteFailedException.h"

#define REDIRECTS_DEFAULT 10

@interface OFHTTPClientRequestHandler: OFObject <OFStreamDelegate>
{
@public
	OFHTTPClient *_client;
	OFHTTPRequest *_request;
	OFString *_requestString;
	unsigned int _redirects;
	id _context;







|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFUnsupportedVersionException.h"
#import "OFWriteFailedException.h"

#define REDIRECTS_DEFAULT 10

@interface OFHTTPClientRequestHandler: OFObject <OFTCPSocketDelegate>
{
@public
	OFHTTPClient *_client;
	OFHTTPRequest *_request;
	OFString *_requestString;
	unsigned int _redirects;
	id _context;
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
				length: [_requestString UTF8StringLength]];
	} @catch (id e) {
		[self raiseException: e];
		return;
	}
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception
{
	if (exception != nil) {
		[self raiseException: exception];
		return;
	}

	[sock setDelegate: self];

	if ([_client->_delegate respondsToSelector:
	    @selector(client:didCreateSocket:request:context:)])
		[_client->_delegate client: _client
			   didCreateSocket: sock
				   request: _request
				   context: _context];







|
|
|

<
<
<
<
<
|







611
612
613
614
615
616
617
618
619
620
621





622
623
624
625
626
627
628
629
				length: [_requestString UTF8StringLength]];
	} @catch (id e) {
		[self raiseException: e];
		return;
	}
}

-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
{





	[(OFTCPSocket *)sock setDelegate: self];

	if ([_client->_delegate respondsToSelector:
	    @selector(client:didCreateSocket:request:context:)])
		[_client->_delegate client: _client
			   didCreateSocket: sock
				   request: _request
				   context: _context];
693
694
695
696
697
698
699

700
701
702
703
704
705
706
707
708
709
710
711
712
			port = 80;
		}

		URLPort = [URL port];
		if (URLPort != nil)
			port = [URLPort uInt16Value];


		[sock asyncConnectToHost: [URL host]
				    port: port
				  target: self
				selector: @selector(socketDidConnect:context:
					      exception:)
				 context: nil];
	} @catch (id e) {
		[self raiseException: e];
	}
}
@end

@implementation OFHTTPClientRequestBodyStream







>

|
<
<
<
<







688
689
690
691
692
693
694
695
696
697




698
699
700
701
702
703
704
			port = 80;
		}

		URLPort = [URL port];
		if (URLPort != nil)
			port = [URLPort uInt16Value];

		[sock setDelegate: self];
		[sock asyncConnectToHost: [URL host]
				    port: port];




	} @catch (id e) {
		[self raiseException: e];
	}
}
@end

@implementation OFHTTPClientRequestBodyStream