Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
        private Task HandleFile(HttpConnection p, string realPath)
        {
            if (p.Url_qstr == "dlcancel")
            {
                p.Handled = true;
                p.setContentTypeTextPlain();
                //if (p.Method != "POST")
                //    return p.writeLineAsync("E: POST needed.");
                if (this.allow_netdl == false)
                {
                    return(p.writeLineAsync($"E: {strMissingPermission("netdl")}."));
                }

                if (downloadTasks.TryGetValue(realPath, out var dlTask))
                {
                    dlTask.Cancel();
                    return(p.writeLineAsync("task is canceled."));
                }
                else
                {
                    return(p.writeLineAsync("E: task not found."));
                }
            }
            foreach (var item in gzip_wildcard)
            {
                if (Wildcard.IsMatch(realPath, item))
                {
                    p.outputStream.EnableGzipIfClientSupports();
                    break;
                }
            }
            return(WebSvrHelper.HandleFileAsync(p, realPath));
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        private Task RichList(HttpConnection pp, string path, string infoText)
        {
            lock (tmplLock) {
                var tmpl = Controller.ProcessFilePath(this.tmpl);
                if (tmpl != null && File.Exists(tmpl))
                {
                    var      fi  = new FileInfo(tmpl);
                    DateTime lwt = fi.LastWriteTimeUtc;
                    if (_tmplLwt != lwt)
                    {
                        _tmpl    = new NaiveTemplate.Template(File.ReadAllText(tmpl, Encoding.UTF8)).TrimExcess();
                        _tmplLwt = lwt;
                    }
                }
                else
                {
                    _tmpl = null;
                }
            }
            var dat = new Dictionary <string, object>();

            if (data != null)
            {
                foreach (var item in data)
                {
                    dat.Add(item.Key, item.Value);
                }
            }
            if (infoText != null)
            {
                dat["info"] = infoText;
            }
            dat["can_upload"] = allow_create | allow_edit;
            if (gzip_listpage)
            {
                pp.outputStream.EnableGzipIfClientSupports();
            }
            return(WebSvrHelper.WriteDirListPage(pp, path, _tmpl, new NaiveTemplate.TemplaterData(dat)));
        }