ObjFW  Diff

Differences From Artifact [aea3f9620c]:

To Artifact [55384f926c]:


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
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







-
+











-
+







- (void)closeAndReconnect;
@end

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

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

@interface OFHTTPClientResponse: OFHTTPResponse <OFReadyForReadingObserving>
{
	OFTCPSocket *_socket;
	bool _hasContentLength, _chunked, _keepAlive, _atEndOfStream;
	size_t _toRead;
	uintmax_t _toRead;
}

@property (nonatomic, setter=of_setKeepAlive:) bool of_keepAlive;

- (instancetype)initWithSocket: (OFTCPSocket *)sock;
@end

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
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







+











-
-
+
+

+
+







- (instancetype)initWithHandler: (OFHTTPClientRequestHandler *)handler
			 socket: (OFTCPSocket *)sock
{
	self = [super init];

	@try {
		OFDictionary OF_GENERIC(OFString *, OFString *) *headers;
		intmax_t contentLength;
		OFString *contentLengthString;

		_handler = [handler retain];
		_socket = [sock retain];

		headers = [_handler->_request headers];

		contentLengthString = [headers objectForKey: @"Content-Length"];
		if (contentLengthString == nil)
			@throw [OFInvalidArgumentException exception];

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

		_contentLength = contentLength;

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







805
806
807
808
809
810
811






812
813
814
815
816
817
818
819







-
-
-
-
-
-
-
+
+





-
-
-
-
-
-
+







}

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

#if SIZE_MAX >= INTMAX_MAX
	if (length > INTMAX_MAX)
		@throw [OFOutOfRangeException exception];
#endif

	if (INTMAX_MAX - _written < (intmax_t)length ||
	    _written + (intmax_t)length > _contentLength)
	if (UINTMAX_MAX - _written < length ||
	    _written + length > _contentLength)
		@throw [OFOutOfRangeException exception];

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

#if SIZE_MAX >= INTMAX_MAX
	if (written > INTMAX_MAX)
		@throw [OFOutOfRangeException exception];
#endif

	if (INTMAX_MAX - _written < (intmax_t)written)
	if (UINTMAX_MAX - _written < written)
		@throw [OFOutOfRangeException exception];

	_written += written;

	return written;
}

879
880
881
882
883
884
885
886
887
888
889
890

891
892
893
894
895
896
897
872
873
874
875
876
877
878





879
880
881
882
883
884
885
886







-
-
-
-
-
+







		@try {
			intmax_t toRead = [contentLength decimalValue];

			if (toRead < 0)
				@throw [OFInvalidServerReplyException
				    exception];

			if (sizeof(intmax_t) > sizeof(size_t) &&
			    toRead > (intmax_t)SIZE_MAX)
				@throw [OFOutOfRangeException exception];

			_toRead = (size_t)toRead;
			_toRead = toRead;
		} @catch (OFInvalidFormatException *e) {
			@throw [OFInvalidServerReplyException exception];
		}
	}
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
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
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







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









-
+







				[_socket release];
				_socket = nil;
			}

			return 0;
		}

		if (_toRead < length)
		if (length > _toRead)
			ret = [_socket readIntoBuffer: buffer
					       length: _toRead];
		else
			ret = [_socket readIntoBuffer: buffer
					       length: length];
			length = (size_t)_toRead;

		ret = [_socket readIntoBuffer: buffer
				       length: length];

		if (ret > length)
			@throw [OFOutOfRangeException exception];

		_toRead -= ret;

		return ret;
	}

	/* Chunked */
	if (_toRead > 0) {
		if (length > _toRead)
			length = _toRead;
			length = (size_t)_toRead;

		length = [_socket readIntoBuffer: buffer
					  length: length];

		_toRead -= length;

		if (_toRead == 0)
963
964
965
966
967
968
969
970

971
972

973
974
975

976
977
978
979
980
981
982
954
955
956
957
958
959
960

961
962

963
964
965

966
967
968
969
970
971
972
973







-
+

-
+


-
+








		range = [line rangeOfString: @";"];
		if (range.location != OF_NOT_FOUND)
			line = [line substringWithRange:
			    of_range(0, range.location)];

		@try {
			uintmax_t toRead = [line hexadecimalValue];
			intmax_t toRead = [line hexadecimalValue];

			if (toRead > SIZE_MAX)
			if (toRead < 0)
				@throw [OFOutOfRangeException exception];

			_toRead = (size_t)toRead;
			_toRead = toRead;
		} @catch (OFInvalidFormatException *e) {
			@throw [OFInvalidServerReplyException exception];
		}

		if (_toRead == 0) {
			_atEndOfStream = true;