Overview
Comment: | Add utils/ofhttpd |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | ofhttpd |
Files: | files | file ages | folders |
SHA3-256: |
849f08ce02cda94a0b30d885d5fd457b |
User & Date: | js on 2023-10-08 11:37:48 |
Other Links: | branch diff | manifest | tags |
Context
2023-10-08
| ||
20:53 | ofhttpd: Never allow backslashes on Windows check-in: 53efa86ce8 user: js tags: ofhttpd | |
11:37 | Add utils/ofhttpd check-in: 849f08ce02 user: js tags: ofhttpd | |
11:36 | OFFile: Fix closing fd 0 on failed init check-in: 416094d227 user: js tags: trunk | |
Changes
Modified .fossil-settings/clean-glob from [0ae1b8a1ac] to [29e0d846ed].
︙ | ︙ | |||
50 51 52 53 54 55 56 | tests/tests.rpx utils/objfw-config utils/objfw-new/objfw-new utils/ofarc/ofarc utils/ofdns/ofdns utils/ofhash/ofhash utils/ofhttp/ofhttp | > | 50 51 52 53 54 55 56 57 | tests/tests.rpx utils/objfw-config utils/objfw-new/objfw-new utils/ofarc/ofarc utils/ofdns/ofdns utils/ofhash/ofhash utils/ofhttp/ofhttp utils/ofhttpd/ofhttpd |
Modified .fossil-settings/ignore-glob from [eed8ea3523] to [2fc8869973].
︙ | ︙ | |||
55 56 57 58 59 60 61 | tests/tests.rpx utils/objfw-config utils/objfw-new/objfw-new utils/ofarc/ofarc utils/ofdns/ofdns utils/ofhash/ofhash utils/ofhttp/ofhttp | > | 55 56 57 58 59 60 61 62 | tests/tests.rpx utils/objfw-config utils/objfw-new/objfw-new utils/ofarc/ofarc utils/ofdns/ofdns utils/ofhash/ofhash utils/ofhttp/ofhttp utils/ofhttpd/ofhttpd |
Modified .gitignore from [9fa51c850c] to [62eb322ca1].
︙ | ︙ | |||
55 56 57 58 59 60 61 | tests/tests.rpx utils/objfw-config utils/objfw-new/objfw-new utils/ofarc/ofarc utils/ofdns/ofdns utils/ofhash/ofhash utils/ofhttp/ofhttp | > | 55 56 57 58 59 60 61 62 | tests/tests.rpx utils/objfw-config utils/objfw-new/objfw-new utils/ofarc/ofarc utils/ofdns/ofdns utils/ofhash/ofhash utils/ofhttp/ofhttp utils/ofhttpd/ofhttpd |
Modified configure.ac from [f54f1e9278] to [483cee4261].
︙ | ︙ | |||
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 | AC_SUBST(OF_HTTP_CLIENT_TESTS_M, "OFHTTPClientTests.m") ]) AC_SUBST(OFDNS, "ofdns") AS_IF([test x"$enable_files" != x"no"], [ AC_SUBST(OFHTTP, "ofhttp") AC_SUBST(OFHTTP_LIBS) ]) ]) AC_DEFUN([CHECK_BUILTIN_BSWAP], [ AC_MSG_CHECKING(for __builtin_bswap$1) AC_LINK_IFELSE([ AC_LANG_PROGRAM([ | > | 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 | AC_SUBST(OF_HTTP_CLIENT_TESTS_M, "OFHTTPClientTests.m") ]) AC_SUBST(OFDNS, "ofdns") AS_IF([test x"$enable_files" != x"no"], [ AC_SUBST(OFHTTP, "ofhttp") AC_SUBST(OFHTTP_LIBS) AC_SUBST(OFHTTPD, "ofhttpd") ]) ]) AC_DEFUN([CHECK_BUILTIN_BSWAP], [ AC_MSG_CHECKING(for __builtin_bswap$1) AC_LINK_IFELSE([ AC_LANG_PROGRAM([ |
︙ | ︙ |
Modified extra.mk.in from [4a516d87b1] to [b28c1602b0].
︙ | ︙ | |||
49 50 51 52 53 54 55 56 57 58 59 60 61 62 | OBJC_SYNC = @OBJC_SYNC@ OBJFW_NEW = @OBJFW_NEW@ OFARC = @OFARC@ OFDNS = @OFDNS@ OFHASH = @OFHASH@ OFHTTP = @OFHTTP@ OFHTTP_LIBS = @OFHTTP_LIBS@ OF_BLOCK_TESTS_M = @OF_BLOCK_TESTS_M@ OF_EPOLL_KERNEL_EVENT_OBSERVER_M = @OF_EPOLL_KERNEL_EVENT_OBSERVER_M@ OF_GNUTLS_TLS_STREAM_M = @OF_GNUTLS_TLS_STREAM_M@ OF_HTTP_CLIENT_TESTS_M = @OF_HTTP_CLIENT_TESTS_M@ OF_KQUEUE_KERNEL_EVENT_OBSERVER_M = @OF_KQUEUE_KERNEL_EVENT_OBSERVER_M@ OF_OPENSSL_TLS_STREAM_M = @OF_OPENSSL_TLS_STREAM_M@ OF_POLL_KERNEL_EVENT_OBSERVER_M = @OF_POLL_KERNEL_EVENT_OBSERVER_M@ | > | 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | OBJC_SYNC = @OBJC_SYNC@ OBJFW_NEW = @OBJFW_NEW@ OFARC = @OFARC@ OFDNS = @OFDNS@ OFHASH = @OFHASH@ OFHTTP = @OFHTTP@ OFHTTP_LIBS = @OFHTTP_LIBS@ OFHTTPD = @OFHTTPD@ OF_BLOCK_TESTS_M = @OF_BLOCK_TESTS_M@ OF_EPOLL_KERNEL_EVENT_OBSERVER_M = @OF_EPOLL_KERNEL_EVENT_OBSERVER_M@ OF_GNUTLS_TLS_STREAM_M = @OF_GNUTLS_TLS_STREAM_M@ OF_HTTP_CLIENT_TESTS_M = @OF_HTTP_CLIENT_TESTS_M@ OF_KQUEUE_KERNEL_EVENT_OBSERVER_M = @OF_KQUEUE_KERNEL_EVENT_OBSERVER_M@ OF_OPENSSL_TLS_STREAM_M = @OF_OPENSSL_TLS_STREAM_M@ OF_POLL_KERNEL_EVENT_OBSERVER_M = @OF_POLL_KERNEL_EVENT_OBSERVER_M@ |
︙ | ︙ |
Modified utils/Makefile from [66810ae4c9] to [d405bf2c59].
1 2 3 4 5 6 | include ../extra.mk SUBDIRS += ${OBJFW_NEW} \ ${OFARC} \ ${OFDNS} \ ${OFHASH} \ | | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | include ../extra.mk SUBDIRS += ${OBJFW_NEW} \ ${OFARC} \ ${OFDNS} \ ${OFHASH} \ ${OFHTTP} \ ${OFHTTPD} include ../buildsys.mk DISTCLEAN = objfw-config install-extra: objfw-config objfw-compile objfw-embed for i in objfw-config objfw-compile objfw-embed; do \ |
︙ | ︙ |
Added utils/ofhttpd/Makefile version [1efe45fb3c].
> > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | include ../../extra.mk PROG = ofhttpd${PROG_SUFFIX} SRCS = OFHTTPD.m include ../../buildsys.mk PACKAGE_NAME = ofhttpd ${PROG}: ${LIBOBJFW_DEP_LVL2} ${LIBOBJFWRT_DEP_LVL2} CPPFLAGS += -I../../src \ -I../../src/runtime \ -I../../src/exceptions \ -I../.. LIBS := -L../../src -lobjfw -L../../src/runtime ${RUNTIME_LIBS} ${LIBS} LD = ${OBJC} LDFLAGS += ${LDFLAGS_RPATH} |
Added utils/ofhttpd/OFHTTPD.m version [e430368038].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 215 216 217 218 219 220 221 222 223 224 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <errno.h> #import "OFApplication.h" #import "OFFile.h" #import "OFFileManager.h" #import "OFHTTPRequest.h" #import "OFHTTPResponse.h" #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; } @end OF_APPLICATION_DELEGATE(OFHTTPD) static OFString * safeLocalPathForIRI(OFIRI *IRI) { OFString *path = IRI.IRIByStandardizingPath.path; if (![path hasPrefix: @"/"]) return nil; path = [path substringWithRange: OFMakeRange(1, path.length - 1)]; #if defined(OF_WINDOWS) || defined(OF_MSDOS) if ([path containsString: @":"] || [path hasPrefix: @"\\"]) #elif defined(OF_AMIGAOS) if ([path containsString: @":"] || [path hasPrefix: @"/"]) #else /* Shouldn't even be possible after standardization, but just in case */ if ([path hasPrefix: @"/"]) #endif return nil; /* * After -[IRIByStandardizingPath], everything representing parent * directory should be at the beginning, so in theory checking the * first component should be enough. But it does not hurt being * paranoid and checking all components, just in case. */ for (OFString *component in [path componentsSeparatedByString: @"/"]) if ([component isEqual: @".."]) return nil; return path; } @implementation OFHTTPD - (void)applicationDidFinishLaunching: (OFNotification *)notification { OFString *directory, *host; unsigned long long port = 0; const OFOptionsParserOption options[] = { { 'd', @"directory", 1, NULL, &directory }, { 'H', @"host", 1, NULL, &host }, { 'p', @"port", 1, NULL, NULL }, { '\0', nil, 0, NULL, NULL } }; OFFileManager *fileManager = [OFFileManager defaultManager]; OFOptionsParser *optionsParser; OFUnichar option; OFMutableIRI *serverIRI; optionsParser = [OFOptionsParser parserWithOptions: options]; while ((option = [optionsParser nextOption]) != '\0') { switch (option) { case 'd': [fileManager changeCurrentDirectoryPath: optionsParser.argument]; OFLog(@"Serving directory %@", fileManager.currentDirectoryPath); break; case 'p': @try { port = optionsParser.argument.longLongValue; if (port > UINT16_MAX) @throw [OFInvalidFormatException exception]; } @catch (OFInvalidFormatException *e) { [OFStdErr writeLine: OF_LOCALIZED( @"invalid_port", @"%[prog]: Port must be between 0 and " @"65536!", @"prog", [OFApplication programName])]; [OFApplication terminateWithStatus: 1]; } break; case ':': if (optionsParser.lastLongOption != nil) [OFStdErr writeLine: OF_LOCALIZED( @"long_argument_missing", @"%[prog]: Argument for option --%[opt] " @"missing", @"prog", [OFApplication programName], @"opt", optionsParser.lastLongOption)]; else { OFString *optStr = [OFString stringWithFormat: @"%C", optionsParser.lastOption]; [OFStdErr writeLine: OF_LOCALIZED( @"argument_missing", @"%[prog]: Argument for option -%[opt] " @"missing", @"prog", [OFApplication programName], @"opt", optStr)]; } [OFApplication terminateWithStatus: 1]; break; /* case '=': */ case '?': if (optionsParser.lastLongOption != nil) [OFStdErr writeLine: OF_LOCALIZED( @"unknown_long_option", @"%[prog]: Unknown option: --%[opt]", @"prog", [OFApplication programName], @"opt", optionsParser.lastLongOption)]; else { OFString *optStr = [OFString stringWithFormat: @"%C", optionsParser.lastOption]; [OFStdErr writeLine: OF_LOCALIZED( @"unknown_option", @"%[prog]: Unknown option: -%[opt]", @"prog", [OFApplication programName], @"opt", optStr)]; } [OFApplication terminateWithStatus: 1]; break; } } if (host == nil) host = @"127.0.0.1"; _server = [[OFHTTPServer alloc] init]; _server.host = host; _server.port = (uint16_t)port; _server.delegate = self; [_server start]; serverIRI = [OFMutableIRI IRIWithScheme: @"http"]; serverIRI.host = _server.host; serverIRI.port = [OFNumber numberWithUnsignedShort: _server.port]; OFLog(@"Started server on %@", serverIRI.string); } - (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 |