Index: utils/ofhttpd/OFHTTPD.m ================================================================== --- utils/ofhttpd/OFHTTPD.m +++ utils/ofhttpd/OFHTTPD.m @@ -27,10 +27,11 @@ #import "OFLocale.h" #import "OFNumber.h" #import "OFOptionsParser.h" #import "OFStdIOStream.h" +#import "OFGetItemAttributesFailedException.h" #import "OFInvalidFormatException.h" #import "OFOpenItemFailedException.h" @interface OFHTTPD: OFObject { @@ -180,11 +181,18 @@ requestBody: (OFStream *)requestBody response: (OFHTTPResponse *)response { OFString *path; - OFLog(@"Handling request %@", request); + 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; @@ -191,14 +199,47 @@ } if ([[OFFileManager defaultManager] directoryExistsAtPath: path]) path = [path stringByAppendingPathComponent: @"index.html"]; - OFLog(@"Sending file %@", path); + 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; - @try { - OFFile *file = [OFFile fileWithPath: path mode: @"r"]; + return; + } response.statusCode = 200; /* TODO: Async stream copy */ @@ -207,18 +248,8 @@ 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