ObjFW  Check-in [1cc6f3659f]

Overview
Comment:OFHTTPRequest: Make it possible to not store the data in an OFDataArray.

This way, it's possible to do downloads without keeping the whole file
in memory.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1cc6f3659f07e643dbbb4614a1de42d05877a2ea28b1c2685d834391aed03660
User & Date: js on 2011-04-22 14:35:13
Other Links: manifest | tags
Context
2011-04-22
14:56
OFHTTPRequest: Normalize server header keys. check-in: 7f52d7f931 user: js tags: trunk
14:35
OFHTTPRequest: Make it possible to not store the data in an OFDataArray. check-in: 1cc6f3659f user: js tags: trunk
14:13
Add OFHTTPRequestDelegate.
This allows status updates etc.
check-in: 9ed387bacb user: js tags: trunk
Changes

Modified src/OFHTTPRequest.h from [50bcc3a5fa] to [cb8dc6b01c].

91
92
93
94
95
96
97

98
99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
{
	OFURL *URL;
	of_http_request_type_t requestType;
	OFString *queryString;
	OFDictionary *headers;
	BOOL redirectsFromHTTPSToHTTPAllowed;
	id <OFHTTPRequestDelegate> delegate;

}

#ifdef OF_HAVE_PROPERTIES
@property (copy) OFURL *URL;
@property (assign) of_http_request_type_t requestType;
@property (copy) OFString *queryString;
@property (copy) OFDictionary *headers;
@property (assign) BOOL redirectsFromHTTPSToHTTPAllowed;
@property (retain) id <OFHTTPRequestDelegate> delegate;

#endif

/**
 * \return A new, autoreleased OFHTTPRequest
 */
+ request;








>









>







91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
	OFURL *URL;
	of_http_request_type_t requestType;
	OFString *queryString;
	OFDictionary *headers;
	BOOL redirectsFromHTTPSToHTTPAllowed;
	id <OFHTTPRequestDelegate> delegate;
	BOOL storesData;
}

#ifdef OF_HAVE_PROPERTIES
@property (copy) OFURL *URL;
@property (assign) of_http_request_type_t requestType;
@property (copy) OFString *queryString;
@property (copy) OFDictionary *headers;
@property (assign) BOOL redirectsFromHTTPSToHTTPAllowed;
@property (retain) id <OFHTTPRequestDelegate> delegate;
@property (assign) BOOL storesData;
#endif

/**
 * \return A new, autoreleased OFHTTPRequest
 */
+ request;

193
194
195
196
197
198
199















200
201
202
203
204
205
206
- (void)setDelegate: (id <OFHTTPRequestDelegate>)delegate;

/**
 * \return The delegate for the HTTP request.
 */
- (id <OFHTTPRequestDelegate>)delegate;
















/**
 * Performs the HTTP request and returns an OFHTTPRequestResult.
 *
 * \return An OFHTTPRequestResult with the result of the HTTP request
 */
- (OFHTTPRequestResult*)perform;








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
- (void)setDelegate: (id <OFHTTPRequestDelegate>)delegate;

/**
 * \return The delegate for the HTTP request.
 */
- (id <OFHTTPRequestDelegate>)delegate;

/**
 * Sets whether an OFDataArray with the data is created.
 *
 * Setting this to NO is only useful if you are using the delegate to handle the
 * data.
 *
 * \param enabled Whether to store the data in an OFDataArray
 */
- (void)setStoresData: (BOOL)enabled;

/**
 * \return Whether an OFDataArray with the data is created
 */
- (BOOL)storesData;

/**
 * Performs the HTTP request and returns an OFHTTPRequestResult.
 *
 * \return An OFHTTPRequestResult with the result of the HTTP request
 */
- (OFHTTPRequestResult*)perform;

Modified src/OFHTTPRequest.m from [f1713b1a20] to [0e781dce42].

54
55
56
57
58
59
60

61
62
63
64
65
66
67
	self = [super init];

	requestType = OF_HTTP_REQUEST_TYPE_GET;
	headers = [[OFDictionary alloc]
	    initWithObject: @"Something using ObjFW "
			    @"<https://webkeks.org/objfw/>"
		    forKey: @"User-Agent"];


	return self;
}

- initWithURL: (OFURL*)url
{
	self = [self init];







>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	self = [super init];

	requestType = OF_HTTP_REQUEST_TYPE_GET;
	headers = [[OFDictionary alloc]
	    initWithObject: @"Something using ObjFW "
			    @"<https://webkeks.org/objfw/>"
		    forKey: @"User-Agent"];
	storesData = YES;

	return self;
}

- initWithURL: (OFURL*)url
{
	self = [self init];
140
141
142
143
144
145
146










147
148
149
150
151
152
153
	OF_SETTER(delegate, delegate_, YES, NO)
}

- (id <OFHTTPRequestDelegate>)delegate
{
	OF_GETTER(delegate, YES)
}











- (OFHTTPRequestResult*)perform
{
	return [self performWithRedirects: 10];
}

- (OFHTTPRequestResult*)performWithRedirects: (size_t)redirects







>
>
>
>
>
>
>
>
>
>







141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	OF_SETTER(delegate, delegate_, YES, NO)
}

- (id <OFHTTPRequestDelegate>)delegate
{
	OF_GETTER(delegate, YES)
}

- (void)setStoresData: (BOOL)enabled
{
	storesData = enabled;
}

- (BOOL)storesData
{
	return storesData;
}

- (OFHTTPRequestResult*)perform
{
	return [self performWithRedirects: 10];
}

- (OFHTTPRequestResult*)performWithRedirects: (size_t)redirects
178
179
180
181
182
183
184

185
186
187
188
189
190
191
		OFMutableDictionary *s_headers;
		OFDataArray *data;
		OFEnumerator *enumerator;
		OFString *key;
		int status;
		const char *t = NULL;
		char *buf;


		[sock connectToHost: [URL host]
			     onPort: [URL port]];

		/*
		 * Work around a bug with packet bisection in lighttpd when
		 * using HTTPS.







>







189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
		OFMutableDictionary *s_headers;
		OFDataArray *data;
		OFEnumerator *enumerator;
		OFString *key;
		int status;
		const char *t = NULL;
		char *buf;
		size_t bytesReceived;

		[sock connectToHost: [URL host]
			     onPort: [URL port]];

		/*
		 * Work around a bug with packet bisection in lighttpd when
		 * using HTTPS.
323
324
325
326
327
328
329

330



331

332
333
334
335
336
337
338
339
340
341




342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
				      forKey: key];
		}

		[delegate request: self
		didReceiveHeaders: s_headers
		   withStatusCode: status];


		data = [OFDataArray dataArrayWithItemSize: 1];



		buf = [self allocMemoryWithSize: of_pagesize];

		@try {
			size_t len;

			while ((len = [sock readNBytes: of_pagesize
					    intoBuffer: buf]) > 0) {
				[data addNItems: len
				     fromCArray: buf];
				[delegate request: self
				   didReceiveData: buf
				       withLength: len];




			}
		} @finally {
			[self freeMemory: buf];
		}

		if ([s_headers objectForKey: @"Content-Length"] != nil) {
			intmax_t cl;

			cl = [[s_headers objectForKey: @"Content-Length"]
			    decimalValue];

			if (cl > SIZE_MAX)
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			if (cl != [data count])
				@throw [OFTruncatedDataException
				    newWithClass: isa];
		}

		/*
		 * Class swizzle the dictionary to be immutable. We pass it as
		 * OFDictionary*, so it can't be modified anyway. But not







>
|
>
>
>

>





<
<



>
>
>
>















|







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
				      forKey: key];
		}

		[delegate request: self
		didReceiveHeaders: s_headers
		   withStatusCode: status];

		if (storesData)
			data = [OFDataArray dataArrayWithItemSize: 1];
		else
			data = nil;

		buf = [self allocMemoryWithSize: of_pagesize];
		bytesReceived = 0;
		@try {
			size_t len;

			while ((len = [sock readNBytes: of_pagesize
					    intoBuffer: buf]) > 0) {


				[delegate request: self
				   didReceiveData: buf
				       withLength: len];

				bytesReceived += len;
				[data addNItems: len
				     fromCArray: buf];
			}
		} @finally {
			[self freeMemory: buf];
		}

		if ([s_headers objectForKey: @"Content-Length"] != nil) {
			intmax_t cl;

			cl = [[s_headers objectForKey: @"Content-Length"]
			    decimalValue];

			if (cl > SIZE_MAX)
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			if (cl != bytesReceived)
				@throw [OFTruncatedDataException
				    newWithClass: isa];
		}

		/*
		 * Class swizzle the dictionary to be immutable. We pass it as
		 * OFDictionary*, so it can't be modified anyway. But not