ObjFW  Check-in [3acdce88f5]

Overview
Comment:OFHTTPServer: Delegate for client socket exception
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3acdce88f59088aea9a9f005de3161fe73810279c375ad0c1add69b154d2eb01
User & Date: js on 2016-03-20 15:24:08
Other Links: manifest | tags
Context
2016-03-20
18:28
Nintendo 3DS: Call gfxExit() at exit check-in: bd02f7426c user: js tags: trunk
15:24
OFHTTPServer: Delegate for client socket exception check-in: 3acdce88f5 user: js tags: trunk
14:58
OFKernelEventObserverTests: Properly count fails check-in: b5277f0444 user: js tags: trunk
Changes

Modified src/OFHTTPServer.h from [79f127a3bc] to [c2275afafe].

56
57
58
59
60
61
62



















63
64
65
66
67
68
69
 *		    socket
 * @return Whether to continue listening. If you return false, existing
 *	   connections will still be handled and you can start accepting new
 *	   connections again by calling @ref OFHTTPServer::start again.
 */
-			  (bool)server: (OFHTTPServer*)server
  didReceiveExceptionOnListeningSocket: (OFException*)exception;



















@end

/*!
 * @class OFHTTPServer OFHTTPServer.h ObjFW/OFHTTPServer.h
 *
 * @brief A class for creating a simple HTTP server inside of applications.
 */







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







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
84
85
86
87
88
 *		    socket
 * @return Whether to continue listening. If you return false, existing
 *	   connections will still be handled and you can start accepting new
 *	   connections again by calling @ref OFHTTPServer::start again.
 */
-			  (bool)server: (OFHTTPServer*)server
  didReceiveExceptionOnListeningSocket: (OFException*)exception;

/*!
 * @brief This method is called when a client socket encountered an exception.
 *
 * This can happen when the OFHTTPServer tries to properly close the
 * connection. If no headers have been sent yet, it will send headers, and if
 * chunked transfer encoding was used, it will send a chunk of size 0. However,
 * if the other end already closed the connection before that, this will raise
 * an exception.
 *
 * @param server The HTTP server which encountered an exception
 * @param response The response for which the exception occurred
 * @param request The request for the response for which the exception occurred
 * @param exception The exception which occurred
 */
-		    (void)server: (OFHTTPServer*)server
  didReceiveExceptionForResponse: (OFHTTPResponse*)response
			 request: (OFHTTPRequest*)request
		       exception: (OFException*)exception;
@end

/*!
 * @class OFHTTPServer OFHTTPServer.h ObjFW/OFHTTPServer.h
 *
 * @brief A class for creating a simple HTTP server inside of applications.
 */

Modified src/OFHTTPServer.m from [e2c16ad886] to [eb56a06a9a].

170
171
172
173
174
175
176

177
178
179
180
181

182
183
184
185
186

187
188
189
190
191
192

193
194
195
196
197
198
199
200
201
202

203
204
205
206
207
208
209
				       freeWhenDone: true];
}

@interface OFHTTPServerResponse: OFHTTPResponse
{
	OFTCPSocket *_socket;
	OFHTTPServer *_server;

	bool _chunked, _headersSent;
}

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

@end

@implementation OFHTTPServerResponse
- initWithSocket: (OFTCPSocket*)socket
	  server: (OFHTTPServer*)server

{
	self = [super init];

	_statusCode = 500;
	_socket = [socket retain];
	_server = [server retain];


	return self;
}

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

	[_server release];


	[super dealloc];
}

- (void)OF_sendHeaders
{
	void *pool = objc_autoreleasePoolPush();







>




|
>





>






>










>







170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
				       freeWhenDone: true];
}

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

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

@implementation OFHTTPServerResponse
- initWithSocket: (OFTCPSocket*)socket
	  server: (OFHTTPServer*)server
	 request: (OFHTTPRequest*)request
{
	self = [super init];

	_statusCode = 500;
	_socket = [socket retain];
	_server = [server retain];
	_request = [request retain];

	return self;
}

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

	[_server release];
	[_request release];

	[super dealloc];
}

- (void)OF_sendHeaders
{
	void *pool = objc_autoreleasePoolPush();
276
277
278
279
280
281
282

283
284
285
286
287
288










289
290
291
292
293
294
295
}

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


	if (!_headersSent)
		[self OF_sendHeaders];

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











	[_socket release];
	_socket = nil;
}

- (int)fileDescriptorForWriting
{







>
|
|

|
|
|
>
>
>
>
>
>
>
>
>
>







281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
}

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

	@try {
		if (!_headersSent)
			[self OF_sendHeaders];

		if (_chunked)
			[_socket writeBuffer: "0\r\n\r\n"
				      length: 5];
	} @catch (OFWriteFailedException *e) {
		id <OFHTTPServerDelegate> delegate = [_server delegate];

		if ([delegate respondsToSelector: @selector(server:
		  didReceiveExceptionForResponse:request:exception:)])
			[delegate		    server: _server
			    didReceiveExceptionForResponse: self
						   request: _request
						 exception: e];
	}

	[_socket release];
	_socket = nil;
}

- (int)fileDescriptorForWriting
{
646
647
648
649
650
651
652
653

654
655
656
657
658
659
660
	    (of_http_request_protocol_version_t){ 1, _HTTPMinorVersion }];
	[request setHeaders: _headers];
	[request setBody: _body];
	[request setRemoteAddress: [_socket remoteAddress]];

	response = [[[OFHTTPServerResponse alloc]
	    initWithSocket: _socket
		    server: _server] autorelease];


	[[_server delegate] server: _server
		 didReceiveRequest: request
			  response: response];
}
@end








|
>







662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
	    (of_http_request_protocol_version_t){ 1, _HTTPMinorVersion }];
	[request setHeaders: _headers];
	[request setBody: _body];
	[request setRemoteAddress: [_socket remoteAddress]];

	response = [[[OFHTTPServerResponse alloc]
	    initWithSocket: _socket
		    server: _server
		   request: request] autorelease];

	[[_server delegate] server: _server
		 didReceiveRequest: request
			  response: response];
}
@end