| 78 | } |
| 79 | |
| 80 | Result HttpAsyncFileServer::handleRequest(HttpAsyncFileServer::Stream& stream, HttpConnection& connection) |
| 81 | { |
| 82 | StringSpan filePath; |
| 83 | const Result safePath = Internal::extractSafeFilePath(connection.request.getRequestTarget(), filePath); |
| 84 | if (not safePath) |
| 85 | { |
| 86 | return Internal::sendEmptyResponse(connection.response, 400); |
| 87 | } |
| 88 | |
| 89 | const bool uploadRequest = connection.request.isMultipart() or |
| 90 | connection.request.getParser().method == HttpParser::Method::HttpPOST or |
| 91 | connection.request.getParser().method == HttpParser::Method::HttpPUT; |
| 92 | if (uploadRequest) |
| 93 | { |
| 94 | if (not options.enableUploads) |
| 95 | { |
| 96 | return Internal::sendEmptyResponse(connection.response, 403); |
| 97 | } |
| 98 | if (options.maxUploadBytes > 0 and |
| 99 | connection.request.getBodyFramingKind() == HttpBodyFramingKind::ContentLength and |
| 100 | connection.request.getBodyBytesRemaining() > options.maxUploadBytes) |
| 101 | { |
| 102 | return Internal::sendEmptyResponse(connection.response, 413); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (connection.request.isMultipart()) |
| 107 | { |
| 108 | return postMultipart(stream, connection); |
| 109 | } |
| 110 | |
| 111 | FileSystem fileSystem; |
| 112 | SC_TRY(fileSystem.init(directory.view())); |
| 113 | switch (connection.request.getParser().method) |
| 114 | { |
| 115 | case HttpParser::Method::HttpPOST: |
| 116 | case HttpParser::Method::HttpPUT: return putFile(stream, connection, filePath); |
| 117 | case HttpParser::Method::HttpGET: return getFile(stream, connection, filePath, true, true); |
| 118 | case HttpParser::Method::HttpHEAD: return getFile(stream, connection, filePath, false, true); |
| 119 | case HttpParser::Method::HttpOPTIONS: |
| 120 | SC_TRY(connection.response.startResponse(204)); |
| 121 | SC_TRY(connection.response.addHeader("Allow", "GET, HEAD, PUT, POST, OPTIONS")); |
| 122 | SC_TRY(connection.response.addHeader("Server", "SC")); |
| 123 | SC_TRY(connection.response.addContentLength(0)); |
| 124 | SC_TRY(connection.response.sendHeaders()); |
| 125 | SC_TRY(connection.response.end()); |
| 126 | break; |
| 127 | default: { |
| 128 | SC_TRY(connection.response.startResponse(405)); |
| 129 | SC_TRY(connection.response.addHeader("Allow", "GET, HEAD, PUT, POST, OPTIONS")); |
| 130 | SC_TRY(connection.response.addHeader("Server", "SC")); |
| 131 | SC_TRY(connection.response.addContentLength(0)); |
| 132 | SC_TRY(connection.response.sendHeaders()); |
| 133 | SC_TRY(connection.response.end()); |
| 134 | } |
| 135 | break; |
| 136 | } |
| 137 | return Result(true); |