ObjFW  Diff

Differences From Artifact [a5fd48a4c7]:

To Artifact [578af30764]:


25
26
27
28
29
30
31

32
33
34
35
36
37
38
#import "OFHTTPServer.h"
#import "OFIRI.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFOptionsParser.h"
#import "OFStdIOStream.h"


#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

@interface OFHTTPD: OFObject <OFApplicationDelegate, OFHTTPServerDelegate>
{
	OFHTTPServer *_server;
}







>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import "OFHTTPServer.h"
#import "OFIRI.h"
#import "OFLocale.h"
#import "OFNumber.h"
#import "OFOptionsParser.h"
#import "OFStdIOStream.h"

#import "OFGetItemAttributesFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenItemFailedException.h"

@interface OFHTTPD: OFObject <OFApplicationDelegate, OFHTTPServerDelegate>
{
	OFHTTPServer *_server;
}
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
215
216
217
218
219
220
221
222
223
224
-      (void)server: (OFHTTPServer *)server
  didReceiveRequest: (OFHTTPRequest *)request
	requestBody: (OFStream *)requestBody
	   response: (OFHTTPResponse *)response
{
	OFString *path;

	OFLog(@"Handling request %@", request);








	path = safeLocalPathForIRI(request.IRI);
	if (path == nil) {
		response.statusCode = 403;
		return;
	}

	if ([[OFFileManager defaultManager] directoryExistsAtPath: path])
		path = [path stringByAppendingPathComponent: @"index.html"];


























	OFLog(@"Sending file %@", path);

	@try {
		OFFile *file = [OFFile fileWithPath: path mode: @"r"];









		response.statusCode = 200;

		/* TODO: Async stream copy */

		while (!file.atEndOfStream) {
			char buffer[4096];
			size_t length;

			length = [file readIntoBuffer: buffer length: 4096];
			[response writeBuffer: buffer length: length];
		}
	} @catch (OFOpenItemFailedException *e) {
		switch (e.errNo) {
		case EACCES:
			response.statusCode = 403;
			return;
		case ENOENT:
		case ENOTDIR:
			response.statusCode = 404;
			return;
		}
	}
}
@end







|
>
>
>
>
>
>
>










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

|
|
>
>
>
>
>
>
>
>












<
<
<
<
<
<
<
<
<
<



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










253
254
255
-      (void)server: (OFHTTPServer *)server
  didReceiveRequest: (OFHTTPRequest *)request
	requestBody: (OFStream *)requestBody
	   response: (OFHTTPResponse *)response
{
	OFString *path;

	OFLog(@"Handling %s request %@",
	    OFHTTPRequestMethodName(request.method), request);

	if (request.method != OFHTTPRequestMethodGet &&
	    request.method != OFHTTPRequestMethodHead) {
		response.statusCode = 405;
		return;
	}

	path = safeLocalPathForIRI(request.IRI);
	if (path == nil) {
		response.statusCode = 403;
		return;
	}

	if ([[OFFileManager defaultManager] directoryExistsAtPath: path])
		path = [path stringByAppendingPathComponent: @"index.html"];

	if (request.method == OFHTTPRequestMethodHead) {
		OFFileAttributes attributes;
		OFNumber *size;

		@try {
			attributes = [[OFFileManager defaultManager]
			    attributesOfItemAtPath: path];
		} @catch (OFGetItemAttributesFailedException *e) {
			if (e.errNo == EACCES)
				response.statusCode = 403;
			else
				response.statusCode = 404;

			return;
		}

		response.statusCode = 200;

		if ((size = [attributes objectForKey: OFFileSize]) != nil)
			response.headers = [OFDictionary
			    dictionaryWithObject: size.description
					  forKey: @"Content-Length"];
	} else {
		OFFile *file;

		OFLog(@"Sending file %@", path);

		@try {
			file = [OFFile fileWithPath: path mode: @"r"];
		} @catch (OFOpenItemFailedException *e) {
			if (e.errNo == EACCES)
				response.statusCode = 403;
			else
				response.statusCode = 404;

			return;
		}

		response.statusCode = 200;

		/* TODO: Async stream copy */

		while (!file.atEndOfStream) {
			char buffer[4096];
			size_t length;

			length = [file readIntoBuffer: buffer length: 4096];
			[response writeBuffer: buffer length: length];
		}










	}
}
@end