ObjFW  Check-in [336a6e129b]

Overview
Comment:utils/ofhttp: Support for continuing downloads
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 336a6e129b3a367fe08bf4217b6132a96990d0f6706108053ff2e5980d7d8333
User & Date: js on 2015-04-26 14:57:19
Other Links: manifest | tags
Context
2015-04-26
16:06
utils/ofhttp: Better error output and small bugfix check-in: 06b8800f20 user: js tags: trunk
14:57
utils/ofhttp: Support for continuing downloads check-in: 336a6e129b user: js tags: trunk
14:08
utils/ofhttp: Better error output check-in: 9fb003e423 user: js tags: trunk
Changes

Modified utils/ofhttp/OFHTTP.m from [0c3783e492] to [157aaf05ca].

30
31
32
33
34
35
36


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

74
75
76
77
78
79
80

#import "OFAddressTranslationFailedException.h"
#import "OFConnectionFailedException.h"
#import "OFHTTPRequestFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerReplyException.h"
#import "OFOpenItemFailedException.h"


#import "OFUnsupportedProtocolException.h"

#import "ProgressBar.h"

#define GIBIBYTE (1024 * 1024 * 1024)
#define MEBIBYTE (1024 * 1024)
#define KIBIBYTE (1024)

@interface OFHTTP: OFObject
{
	OFArray *_URLs;
	size_t _URLIndex;
	int _errorCode;
	OFString *_outputPath;
	bool _continue, _quiet;
	OFHTTPClient *_HTTPClient;
	char *_buffer;
	OFStream *_output;
	intmax_t _received, _length;
	ProgressBar *_progressBar;
}

- (void)downloadNextURL;
@end

OF_APPLICATION_DELEGATE(OFHTTP)

static void
help(OFStream *stream, bool full, int status)
{
	[of_stderr writeFormat:
	    @"Usage: %@ -[hoq] url1 [url2 ...]\n",
	    [OFApplication programName]];

	if (full)
		[stream writeString:
		    @"\nOptions:\n"

		    @"    -h  Show this help\n"
		    @"    -o  Output filename\n"
		    @"    -q  Quiet mode (no output, except errors)\n"];

	[OFApplication terminateWithStatus: status];
}








>
>


















|












|





>







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#import "OFAddressTranslationFailedException.h"
#import "OFConnectionFailedException.h"
#import "OFHTTPRequestFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerReplyException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFStatItemFailedException.h"
#import "OFUnsupportedProtocolException.h"

#import "ProgressBar.h"

#define GIBIBYTE (1024 * 1024 * 1024)
#define MEBIBYTE (1024 * 1024)
#define KIBIBYTE (1024)

@interface OFHTTP: OFObject
{
	OFArray *_URLs;
	size_t _URLIndex;
	int _errorCode;
	OFString *_outputPath;
	bool _continue, _quiet;
	OFHTTPClient *_HTTPClient;
	char *_buffer;
	OFStream *_output;
	intmax_t _received, _length, _resumedFrom;
	ProgressBar *_progressBar;
}

- (void)downloadNextURL;
@end

OF_APPLICATION_DELEGATE(OFHTTP)

static void
help(OFStream *stream, bool full, int status)
{
	[of_stderr writeFormat:
	    @"Usage: %@ -[choq] url1 [url2 ...]\n",
	    [OFApplication programName]];

	if (full)
		[stream writeString:
		    @"\nOptions:\n"
		    @"    -c  Continue download of existing file\n"
		    @"    -h  Show this help\n"
		    @"    -o  Output filename\n"
		    @"    -q  Quiet mode (no output, except errors)\n"];

	[OFApplication terminateWithStatus: status];
}

95
96
97
98
99
100
101
102
103
104
105
106



107
108
109
110
111
112
113

	return self;
}

- (void)applicationDidFinishLaunching
{
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: @"ho:q"];
	of_unichar_t option;

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {



		case 'h':
			help(of_stdout, true, 0);
			break;
		case 'o':
			[_outputPath release];
			_outputPath = [[optionsParser argument] retain];
			break;







|




>
>
>







98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

	return self;
}

- (void)applicationDidFinishLaunching
{
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: @"cho:q"];
	of_unichar_t option;

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case 'c':
			_continue = true;
			break;
		case 'h':
			help(of_stdout, true, 0);
			break;
		case 'o':
			[_outputPath release];
			_outputPath = [[optionsParser argument] retain];
			break;
209
210
211
212
213
214
215

216
217
218
219
220
221
222
223
224
225
226
227
228
229
	return false;
}

- (void)downloadNextURL
{
	OFString *URLString = nil;
	OFURL *URL;

	OFHTTPRequest *request;
	OFHTTPResponse *response;
	OFDictionary *headers;
	OFString *fileName, *lengthString, *type;

	_length = -1;
	_received = 0;

	if (_output != of_stdout)
		[_output release];
	_output = nil;

	if (_URLIndex >= [_URLs count])
		[OFApplication terminateWithStatus: _errorCode];







>






|







215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	return false;
}

- (void)downloadNextURL
{
	OFString *URLString = nil;
	OFURL *URL;
	OFMutableDictionary *clientHeaders;
	OFHTTPRequest *request;
	OFHTTPResponse *response;
	OFDictionary *headers;
	OFString *fileName, *lengthString, *type;

	_length = -1;
	_received = _resumedFrom = 0;

	if (_output != of_stdout)
		[_output release];
	_output = nil;

	if (_URLIndex >= [_URLs count])
		[OFApplication terminateWithStatus: _errorCode];
248
249
250
251
252
253
254
255



























256

257
258
259
260
261
262
263

		_errorCode = 1;
		goto next;
	}

	if (!_quiet)
		[of_stdout writeFormat: @"⇣ %@", [URL string]];




























	request = [OFHTTPRequest requestWithURL: URL];


	@try {
		response = [_HTTPClient performRequest: request];
	} @catch (OFHTTPRequestFailedException *e) {
		if (!_quiet)
			[of_stdout writeFormat: @" ➜ %d\n",
						[[e response] statusCode]];








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

>







255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298

		_errorCode = 1;
		goto next;
	}

	if (!_quiet)
		[of_stdout writeFormat: @"⇣ %@", [URL string]];

	if (_outputPath != nil)
		fileName = _outputPath;
	else
		fileName = [[URL path] lastPathComponent];

	clientHeaders = [OFMutableDictionary
	    dictionaryWithObject: @"OFHTTP"
			  forKey: @"User-Agent"];

	if (_continue) {
		@try {
			off_t size = [OFFile sizeOfFileAtPath: fileName];
			OFString *range;

			if (size > INTMAX_MAX)
				@throw [OFOutOfRangeException exception];

			_resumedFrom = (intmax_t)size;

			range = [OFString stringWithFormat: @"bytes=%jd-",
							    _resumedFrom];
			[clientHeaders setObject: range
					  forKey: @"Range"];
		} @catch (OFStatItemFailedException *e) {
		}
	}

	request = [OFHTTPRequest requestWithURL: URL];
	[request setHeaders: clientHeaders];

	@try {
		response = [_HTTPClient performRequest: request];
	} @catch (OFHTTPRequestFailedException *e) {
		if (!_quiet)
			[of_stdout writeFormat: @" ➜ %d\n",
						[[e response] statusCode]];
318
319
320
321
322
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
365
366
367
368
369
370


371
372
373
374
375
376
377
378
379
380
381
382
383
384



385
386
387
388
389
390
391
	if (!_quiet)
		[of_stdout writeFormat: @" ➜ %d\n", [response statusCode]];

	headers = [response headers];
	lengthString = [headers objectForKey: @"Content-Length"];
	type = [headers objectForKey: @"Content-Type"];

	if (_outputPath != nil)
		fileName = _outputPath;
	else
		fileName = [[URL path] lastPathComponent];

	if (lengthString != nil)
		_length = [lengthString decimalValue];

	if (!_quiet) {
		if (type == nil)
			type = @"unknown";

		if (lengthString != nil) {
			if (_length >= GIBIBYTE)
				lengthString = [OFString stringWithFormat:
				    @"%.2f GiB", (float)_length / GIBIBYTE];

			else if (_length >= MEBIBYTE)
				lengthString = [OFString stringWithFormat:
				    @"%.2f MiB", (float)_length / MEBIBYTE];

			else if (_length >= KIBIBYTE)
				lengthString = [OFString stringWithFormat:
				    @"%.2f KiB", (float)_length / KIBIBYTE];

			else
				lengthString = [OFString stringWithFormat:
				    @"%jd bytes", _length];
		} else
			lengthString = @"unknown";

		[of_stdout writeFormat: @"  Name: %@\n", fileName];
		[of_stdout writeFormat: @"  Type: %@\n", type];
		[of_stdout writeFormat: @"  Size: %@\n", lengthString];
	}

	if ([_outputPath isEqual: @"-"])
		_output = of_stdout;
	else {
		if ([OFFile fileExistsAtPath: fileName]) {
			[of_stderr writeFormat:
			    @"%@: File %@ already exists!\n",
			    [OFApplication programName], fileName];

			_errorCode = 1;
			goto next;
		}

		@try {


			_output = [[OFFile alloc] initWithPath: fileName
							  mode: @"wb"];
		} @catch (OFOpenItemFailedException *e) {
			[of_stderr writeFormat:
			    @"%@: Failed to open file %@!\n",
			    [OFApplication programName], fileName];

			_errorCode = 1;
			goto next;
		}
	}

	if (!_quiet) {
		_progressBar = [[ProgressBar alloc] initWithLength: _length];



		[_progressBar draw];
	}

	[response asyncReadIntoBuffer: _buffer
			       length: [OFSystemInfo pageSize]
			       target: self
			     selector: @selector(stream:didReadIntoBuffer:







<
<
<
<
<










|
>


|
>


|
>


|











|









>
>

|











|
>
>
>







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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
	if (!_quiet)
		[of_stdout writeFormat: @" ➜ %d\n", [response statusCode]];

	headers = [response headers];
	lengthString = [headers objectForKey: @"Content-Length"];
	type = [headers objectForKey: @"Content-Type"];






	if (lengthString != nil)
		_length = [lengthString decimalValue];

	if (!_quiet) {
		if (type == nil)
			type = @"unknown";

		if (lengthString != nil) {
			if (_length >= GIBIBYTE)
				lengthString = [OFString stringWithFormat:
				    @"%.2f GiB",
				    (float)(_resumedFrom + _length) / GIBIBYTE];
			else if (_length >= MEBIBYTE)
				lengthString = [OFString stringWithFormat:
				    @"%.2f MiB",
				    (float)(_resumedFrom + _length) / MEBIBYTE];
			else if (_length >= KIBIBYTE)
				lengthString = [OFString stringWithFormat:
				    @"%.2f KiB",
				    (float)(_resumedFrom + _length) / KIBIBYTE];
			else
				lengthString = [OFString stringWithFormat:
				    @"%jd bytes", _resumedFrom + _length];
		} else
			lengthString = @"unknown";

		[of_stdout writeFormat: @"  Name: %@\n", fileName];
		[of_stdout writeFormat: @"  Type: %@\n", type];
		[of_stdout writeFormat: @"  Size: %@\n", lengthString];
	}

	if ([_outputPath isEqual: @"-"])
		_output = of_stdout;
	else {
		if (!_continue && [OFFile fileExistsAtPath: fileName]) {
			[of_stderr writeFormat:
			    @"%@: File %@ already exists!\n",
			    [OFApplication programName], fileName];

			_errorCode = 1;
			goto next;
		}

		@try {
			OFString *mode =
			    ([response statusCode] == 206 ? @"ab" : @"wb");
			_output = [[OFFile alloc] initWithPath: fileName
							  mode: mode];
		} @catch (OFOpenItemFailedException *e) {
			[of_stderr writeFormat:
			    @"%@: Failed to open file %@!\n",
			    [OFApplication programName], fileName];

			_errorCode = 1;
			goto next;
		}
	}

	if (!_quiet) {
		_progressBar = [[ProgressBar alloc]
		    initWithLength: _length
		       resumedFrom: _resumedFrom];
		[_progressBar setReceived: _received];
		[_progressBar draw];
	}

	[response asyncReadIntoBuffer: _buffer
			       length: [OFSystemInfo pageSize]
			       target: self
			     selector: @selector(stream:didReadIntoBuffer:

Modified utils/ofhttp/ProgressBar.h from [e0dcfb0362] to [fea9147d40].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
#import "OFObject.h"

@class OFDate;
@class OFTimer;

@interface ProgressBar: OFObject
{
	intmax_t _length, _received, _lastReceived;
	OFDate *_startDate;
	double _lastDrawn;
	OFTimer *_timer;
	bool _stopped;
}

- initWithLength: (intmax_t)length;

- (void)setReceived: (intmax_t)received;
- (void)draw;
- (void)stop;
@end







|






|
>




17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#import "OFObject.h"

@class OFDate;
@class OFTimer;

@interface ProgressBar: OFObject
{
	intmax_t _received, _lastReceived, _length, _resumedFrom;
	OFDate *_startDate;
	double _lastDrawn;
	OFTimer *_timer;
	bool _stopped;
}

- initWithLength: (intmax_t)length
     resumedFrom: (intmax_t)resumedFrom;
- (void)setReceived: (intmax_t)received;
- (void)draw;
- (void)stop;
@end

Modified utils/ofhttp/ProgressBar.m from [c13bb41aea] to [15aadddba2].

29
30
31
32
33
34
35

36
37
38
39
40
41
42

43
44
45
46
47
48
49
#define KIBIBYTE (1024)

#define BAR_WIDTH 52
#define UPDATE_INTERVAL 0.1

@implementation ProgressBar
- initWithLength: (intmax_t)length

{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();

		_length = length;

		_startDate = [[OFDate alloc] init];
		_timer = [[OFTimer
		    scheduledTimerWithTimeInterval: UPDATE_INTERVAL
					    target: self
					  selector: @selector(draw)
					   repeats: true] retain];








>







>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define KIBIBYTE (1024)

#define BAR_WIDTH 52
#define UPDATE_INTERVAL 0.1

@implementation ProgressBar
- initWithLength: (intmax_t)length
     resumedFrom: (intmax_t)resumedFrom
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();

		_length = length;
		_resumedFrom = resumedFrom;
		_startDate = [[OFDate alloc] init];
		_timer = [[OFTimer
		    scheduledTimerWithTimeInterval: UPDATE_INTERVAL
					    target: self
					  selector: @selector(draw)
					   repeats: true] retain];

71
72
73
74
75
76
77
78

79

80
81
82
83
84
85
86
}

- (void)_drawProgress
{
	uint_fast8_t i;
	float bars, percent, bps;

	bars = (float)_received / (float)_length * BAR_WIDTH;

	percent = (float)_received / (float)_length * 100;


	[of_stdout writeString: @"\r  ▕"];

	for (i = 0; i < (uint_fast8_t)bars; i++)
		[of_stdout writeString: @"█"];
	if (bars < BAR_WIDTH) {
		float rest = bars - floorf(bars);







|
>
|
>







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
}

- (void)_drawProgress
{
	uint_fast8_t i;
	float bars, percent, bps;

	bars = (float)(_resumedFrom + _received) /
	    (_resumedFrom + _length) * BAR_WIDTH;
	percent = (float)(_resumedFrom + _received) /
	    (_resumedFrom + _length) * 100;

	[of_stdout writeString: @"\r  ▕"];

	for (i = 0; i < (uint_fast8_t)bars; i++)
		[of_stdout writeString: @"█"];
	if (bars < BAR_WIDTH) {
		float rest = bars - floorf(bars);
127
128
129
130
131
132
133
134

135
136
137

138
139
140

141
142
143

144
145
146
147
148
149
150
}

- (void)_drawReceived
{
	float bps;

	if (_received >= GIBIBYTE)
		[of_stdout writeFormat: @"\r  %7.2f GiB (",

					(float)_received / GIBIBYTE];
	else if (_received >= MEBIBYTE)
		[of_stdout writeFormat: @"\r  %7.2f MiB ",

					(float)_received / MEBIBYTE];
	else if (_received >= KIBIBYTE)
		[of_stdout writeFormat: @"\r  %7.2f KiB ",

					(float)_received / KIBIBYTE];
	else
		[of_stdout writeFormat: @"\r  %jd bytes ", _received];


	if (_stopped)
		bps = (float)_received / -[_startDate timeIntervalSinceNow];
	else
		bps = (float)(_received - _lastReceived) / UPDATE_INTERVAL;

	if (bps >= GIBIBYTE)







|
>
|

|
>
|

|
>
|

|
>







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}

- (void)_drawReceived
{
	float bps;

	if (_received >= GIBIBYTE)
		[of_stdout writeFormat:
		    @"\r  %7.2f GiB ",
		    (float)(_resumedFrom + _received) / GIBIBYTE];
	else if (_received >= MEBIBYTE)
		[of_stdout writeFormat:
		    @"\r  %7.2f MiB ",
		    (float)(_resumedFrom + _received) / MEBIBYTE];
	else if (_received >= KIBIBYTE)
		[of_stdout writeFormat:
		    @"\r  %7.2f KiB ",
		    (float)(_resumedFrom + _received) / KIBIBYTE];
	else
		[of_stdout writeFormat:
		    @"\r  %jd bytes ", _resumedFrom + _received];

	if (_stopped)
		bps = (float)_received / -[_startDate timeIntervalSinceNow];
	else
		bps = (float)(_received - _lastReceived) / UPDATE_INTERVAL;

	if (bps >= GIBIBYTE)