ObjFW  Check-in [b80f70a59b]

Overview
Comment:ofhttpd: Handle HEAD requests
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ofhttpd
Files: files | file ages | folders
SHA3-256: b80f70a59b34ec66451bf6ef606f9e93377f5a03f491269a366e8053f7a7d13e
User & Date: js on 2023-10-15 14:33:34
Other Links: branch diff | manifest | tags
Context
2023-10-15
14:57
Merge trunk into branch "ofhttpd" Leaf check-in: e2a818c0b5 user: js tags: ofhttpd
14:33
ofhttpd: Handle HEAD requests check-in: b80f70a59b user: js tags: ofhttpd
2023-10-08
20:53
ofhttpd: Never allow backslashes on Windows check-in: 53efa86ce8 user: js tags: ofhttpd
Changes

Modified utils/ofhttpd/OFHTTPD.m from [a5fd48a4c7] to [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