ObjFW  Check-in [b87a5d3b46]

Overview
Comment:OFHTTPServer: Don't close the socket manually

Closing it manually causes the fd to become invalid before it is removed
from the OFKernelEventObserver.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b87a5d3b461e6d678c4838c501c5292f08bc5e8f377c73dc1806c08c06e091fc
User & Date: js on 2014-08-21 19:00:28
Other Links: manifest | tags
Context
2014-08-22
19:09
Retain sockets until after removal from observer check-in: 1dbe9a4e4e user: js tags: trunk
2014-08-21
19:00
OFHTTPServer: Don't close the socket manually check-in: b87a5d3b46 user: js tags: trunk
2014-08-18
10:53
OFTCPSocket: Clear read buffer for new connection check-in: c6a4b0a95f user: js tags: trunk
Changes

Modified src/OFHTTPServer.m from [3efdaebeee] to [36dd983976].

29
30
31
32
33
34
35

36
37
38


39
40
41
42
43
44
45
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48







+



+
+







#import "OFHTTPResponse.h"
#import "OFTCPSocket.h"
#import "OFTimer.h"

#import "OFAlreadyConnectedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotConnectedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFWriteFailedException.h"

#import "socket_helpers.h"

#define BUFFER_SIZE 1024

/*
 * FIXME: Key normalization replaces headers like "DNT" with "Dnt".
 * FIXME: Errors are not reported to the user.
 */
167
168
169
170
171
172
173
174

175
176
177
178
179
180
181
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184







-
+







				       freeWhenDone: true];
}

@interface OFHTTPServerResponse: OFHTTPResponse
{
	OFTCPSocket *_socket;
	OFHTTPServer *_server;
	bool _chunked, _headersSent, _closed;
	bool _chunked, _headersSent;
}

- initWithSocket: (OFTCPSocket*)socket
	  server: (OFHTTPServer*)server;
@end

@implementation OFHTTPServerResponse
189
190
191
192
193
194
195
196
197


198
199
200
201
202
203
204
205
206
192
193
194
195
196
197
198


199
200
201

202
203
204
205
206
207
208







-
-
+
+

-







	_server = [server retain];

	return self;
}

- (void)dealloc
{
	if (!_closed)
		[self close];
	if (_socket != nil)
		[self close];	/* includes [_socket release] */

	[_socket release];
	[_server release];

	[super dealloc];
}

- (void)OF_sendHeaders
{
234
235
236
237
238
239
240



241
242
243
244
245
246
247
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252







+
+
+







}

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

	if (_socket == nil)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (!_headersSent)
		[self OF_sendHeaders];

	if (!_chunked) {
		[_socket writeBuffer: buffer
			      length: length];
		return;
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
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







+
+
+







-
-
+
+
-




+
+
+







		      length: length];
	[_socket writeBuffer: "\r\n"
		      length: 2];
}

- (void)close
{
	if (_socket == nil)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (!_headersSent)
		[self OF_sendHeaders];

	if (_chunked)
		[_socket writeBuffer: "0\r\n\r\n"
			      length: 5];

	[_socket close];

	[_socket release];
	_socket = nil;
	_closed = true;
}

- (int)fileDescriptorForWriting
{
	if (_socket == nil)
		return INVALID_SOCKET;

	return [_socket fileDescriptorForWriting];
}
@end

@interface OFHTTPServer_Connection: OFObject
{
	OFTCPSocket *_socket;
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
568
569
570
571
572
573
574

575
576
577
578
579
580
581







-








	[_socket writeFormat: @"HTTP/1.1 %d %s\r\n"
			      @"Date: %@\r\n"
			      @"Server: %@\r\n"
			      @"\r\n",
			      statusCode, statusCodeToString(statusCode),
			      date, [_server name]];
	[_socket close];

	return false;
}

- (void)createResponse
{
	OFURL *URL;