ObjFW  Check-in [562d4e2f61]

Overview
Comment:OFHTTPClient: Minor type cleanups
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 562d4e2f614cd2baf601f7f93954c30e2075f256b6264f6b191bb864372e84f9
User & Date: js on 2018-02-18 21:26:25
Other Links: manifest | tags
Context
2018-02-19
22:57
OFHTTPClient: Add missing _closed = true check-in: 21d9bced96 user: js tags: trunk
2018-02-18
21:26
OFHTTPClient: Minor type cleanups check-in: 562d4e2f61 user: js tags: trunk
00:20
OFHTTPClient: Add a callback for the request body check-in: 8681bba25e user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [aea3f9620c] to [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;