bool CheckPathForWriting(string basePath, string relPath, out string failReason, out string realPath, out WebSvrHelper.PathResult r) { r = WebSvrHelper.PathResult.IllegalPath; failReason = null; if (relPath.IsNullOrEmpty()) { failReason = "Empty filename."; realPath = null; return(false); } r = WebSvrHelper.CheckPath(basePath, relPath, out realPath); if (r == WebSvrHelper.PathResult.IllegalPath) { failReason = ($"Illegal filename '{relPath}'"); return(false); } if ((r == WebSvrHelper.PathResult.File || r == WebSvrHelper.PathResult.Directory) && !allow_edit) { failReason = ($"File '{relPath}' exists and {strMissingPermission("edit")}."); return(false); } if (r == WebSvrHelper.PathResult.NotFound && !allow_create) { failReason = ($"File '{relPath}' doesn't exist and {strMissingPermission("create")}."); return(false); } return(true); }
public override async Task HandleRequestAsyncImpl(HttpConnection p) { var realPath = p.Url_path; if (index != null && p.Url_path == "/") { p.Url_path = index; } try { string dirPath = null; if (dir_hosts != null) { string hosts = Controller.ProcessFilePath(dir_hosts); string host = p.Host; // TODO: check host for security var rr = WebSvrHelper.CheckPath(hosts, host, out var hostsSubDir); if (rr == WebSvrHelper.PathResult.Directory) { dirPath = hostsSubDir; } } if (dirPath == null) { dirPath = Controller.ProcessFilePath(dir); } WebSvrHelper.PathResult r = WebSvrHelper.CheckPath(dirPath, p.Url_path, out var fsFilePath); if (p.Url_qstr == "dlstatus" && (r == WebSvrHelper.PathResult.File || r == WebSvrHelper.PathResult.NotFound)) { if (downloadTasks.TryGetValue(fsFilePath, out var dlTask)) { p.Handled = true; p.setContentTypeTextPlain(); await p.writeLineAsync(dlTask.StatusText); } } else if (r == WebSvrHelper.PathResult.File) { p.Handled = true; p.ResponseStatusCode = "200 OK"; await HandleFile(p, fsFilePath); } else if (r == WebSvrHelper.PathResult.Directory && allow_list) { p.Handled = true; p.ResponseStatusCode = "200 OK"; await HandleDir(p, fsFilePath); } } finally { p.Url_path = realPath; } }