ObjFW  Check-in [9a983052eb]

Overview
Comment:OFHTTPClient: Add isAtEndOfStream for body stream
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9a983052ebf09e96e43fe579e7fc8238ead0bc843a8ae78472b4466267129da4
User & Date: js on 2018-02-19 23:41:51
Other Links: manifest | tags
Context
2018-02-25
02:38
OFHTTPServer: Move the body out of the request check-in: f756fb2ff6 user: js tags: trunk
2018-02-19
23:41
OFHTTPClient: Add isAtEndOfStream for body stream check-in: 9a983052eb user: js tags: trunk
22:57
OFHTTPClient: Add missing _closed = true check-in: 21d9bced96 user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [4da0a43011] to [0896b047fb].

69
70
71
72
73
74
75
76
77


78
79
80
81
82
83
84
69
70
71
72
73
74
75


76
77
78
79
80
81
82
83
84







-
-
+
+







- (void)closeAndReconnect;
@end

@interface OFHTTPClientRequestBodyStream: OFStream <OFReadyForWritingObserving>
{
	OFHTTPClientRequestHandler *_handler;
	OFTCPSocket *_socket;
	uintmax_t _contentLength, _written;
	bool _closed;
	uintmax_t _toWrite;
	bool _atEndOfStream;
}

- (instancetype)initWithHandler: (OFHTTPClientRequestHandler *)handler
			 socket: (OFTCPSocket *)sock;
@end

@interface OFHTTPClientResponse: OFHTTPResponse <OFReadyForReadingObserving>
771
772
773
774
775
776
777
778

779
780
781
782
783
784
785
771
772
773
774
775
776
777

778
779
780
781
782
783
784
785







-
+







		if (contentLengthString == nil)
			@throw [OFInvalidArgumentException exception];

		contentLength = [contentLengthString decimalValue];
		if (contentLength < 0)
			@throw [OFOutOfRangeException exception];

		_contentLength = contentLength;
		_toWrite = contentLength;

		if ([headers objectForKey: @"Transfer-Encoding"] != nil)
			@throw [OFInvalidArgumentException exception];
	} @catch (id e) {
		[self release];
		@throw e;
	}
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
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







+
-
+

+
+
-
-
-
+
+
+
+
+
+
+
+
+
+

-
-
+
+

-
+


+
-
+
+
+

+
+
+
+
+
+
+
-
+


+
+
+
+
+


-
+


-
+


-
-




+
+
+








	[super dealloc];
}

- (size_t)lowlevelWriteBuffer: (const void *)buffer
		       length: (size_t)length
{
	size_t requestedLength = length;
	size_t written;
	size_t ret;

	if (_socket == nil)
		@throw [OFNotOpenException exceptionWithObject: self];
	if (UINTMAX_MAX - _written < length ||
	    _written + length > _contentLength)
		@throw [OFOutOfRangeException exception];

	if (_atEndOfStream)
		@throw [OFWriteFailedException
		    exceptionWithObject: self
			requestedLength: requestedLength
			   bytesWritten: 0
				  errNo: 0];

	if (length > _toWrite)
		length = (size_t)_toWrite;

	written = [_socket writeBuffer: buffer
				length: length];
	ret = [_socket writeBuffer: buffer
			    length: length];

	if (UINTMAX_MAX - _written < written)
	if (ret > length)
		@throw [OFOutOfRangeException exception];

	_toWrite -= ret;
	_written += written;

	if (_toWrite == 0)
		_atEndOfStream = true;

	if (requestedLength > length)
		@throw [OFWriteFailedException
		    exceptionWithObject: self
			requestedLength: requestedLength
			   bytesWritten: ret
				  errNo: 0];

	return written;
	return ret;
}

- (bool)lowlevelIsAtEndOfStream
{
	return _atEndOfStream;
}

- (void)close
{
	if (_closed)
	if (_socket == nil)
		return;

	if (_written < _contentLength)
	if (_toWrite > 0)
		@throw [OFTruncatedDataException exception];

	_closed = true;

	[_socket asyncReadLineWithTarget: _handler
				selector: @selector(socket:didReadLine:context:
					      exception:)
				 context: nil];

	[_socket release];
	_socket = nil;
}

- (int)fileDescriptorForWriting
{
	return [_socket fileDescriptorForWriting];
}
@end