ObjFW  Diff

Differences From Artifact [97fcaf53fd]:

To Artifact [908217ca1f]:


61
62
63
64
65
66
67
68

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

112
113

114
115
116
117
118
119
120
121

122
123

124
125
126
127
128
129
130
131

132
133

134
135
136
137
138
139
140
61
62
63
64
65
66
67

68
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

111
112

113
114
115
116
117
118
119
120

121
122

123
124
125
126
127
128
129
130

131
132

133
134
135
136
137
138
139
140







-
+

-
+
















-
+




-
+


















-
+

-
+







-
+

-
+







-
+

-
+








@implementation OFHTTPRequest
+ request
{
	return [[[self alloc] init] autorelease];
}

+ requestWithURL: (OFURL*)url
+ requestWithURL: (OFURL*)URL
{
	return [[[self alloc] initWithURL: url] autorelease];
	return [[[self alloc] initWithURL: URL] autorelease];
}

- init
{
	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
- initWithURL: (OFURL*)URL_
{
	self = [self init];

	@try {
		[self setURL: url];
		[self setURL: URL_];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[URL release];
	[queryString release];
	[headers release];
	[(id)delegate release];

	[super dealloc];
}

- (void)setURL: (OFURL*)url
- (void)setURL: (OFURL*)URL_
{
	OF_SETTER(URL, url, YES, YES)
	OF_SETTER(URL, URL_, YES, YES)
}

- (OFURL*)URL
{
	OF_GETTER(URL, YES)
}

- (void)setRequestType: (of_http_request_type_t)type
- (void)setRequestType: (of_http_request_type_t)requestType_
{
	requestType = type;
	requestType = requestType_;
}

- (of_http_request_type_t)requestType
{
	return requestType;
}

- (void)setQueryString: (OFString*)qs
- (void)setQueryString: (OFString*)queryString_
{
	OF_SETTER(queryString, qs, YES, YES)
	OF_SETTER(queryString, queryString_, YES, YES)
}

- (OFString*)queryString
{
	OF_GETTER(queryString, YES)
}

164
165
166
167
168
169
170
171

172
173

174
175
176
177
178
179
180
164
165
166
167
168
169
170

171
172

173
174
175
176
177
178
179
180







-
+

-
+







}

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

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

- (BOOL)storesData
{
	return storesData;
}

209
210
211
212
213
214
215
216
217


218
219
220
221
222
223
224
225
226
227
228
229
230
231

232
233

234
235

236
237
238
239
240
241
242

243
244

245
246
247
248
249
250
251
209
210
211
212
213
214
215


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

231
232

233
234

235
236
237
238
239
240
241

242
243

244
245
246
247
248
249
250
251







-
-
+
+













-
+

-
+

-
+






-
+

-
+







	@try {
		OFString *line, *path;
		OFMutableDictionary *serverHeaders;
		OFDataArray *data;
		OFEnumerator *enumerator;
		OFString *key;
		int status;
		const char *t = NULL;
		char *buf;
		const char *type = NULL;
		char *buffer;
		size_t bytesReceived;
		OFString *contentLengthHeader;

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

		/*
		 * Work around a bug with packet bisection in lighttpd when
		 * using HTTPS.
		 */
		[sock setBuffersWrites: YES];

		if (requestType == OF_HTTP_REQUEST_TYPE_GET)
			t = "GET";
			type = "GET";
		if (requestType == OF_HTTP_REQUEST_TYPE_HEAD)
			t = "HEAD";
			type = "HEAD";
		if (requestType == OF_HTTP_REQUEST_TYPE_POST)
			t = "POST";
			type = "POST";

		if ([(path = [URL path]) isEqual: @""])
			path = @"/";

		if ([URL query] != nil)
			[sock writeFormat: @"%s %@?%@ HTTP/1.0\r\n",
					   t, path, [URL query]];
					   type, path, [URL query]];
		else
			[sock writeFormat: @"%s %@ HTTP/1.0\r\n", t, path];
			[sock writeFormat: @"%s %@ HTTP/1.0\r\n", type, path];

		if ([URL port] == 80)
			[sock writeFormat: @"Host: %@\r\n", [URL host]];
		else
			[sock writeFormat: @"Host: %@:%d\r\n", [URL host],
					   [URL port]];

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







-
+





-
+

-
+




-
+


-
+







		   withStatusCode: status];

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

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

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

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

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

			if (cl > SIZE_MAX)