An alternative to UBB.threads
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
FLocal/IISMainHandler/handlers/StaticHandler.cs

57 lines
1.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using FLocal.Core;
namespace FLocal.IISHandler.handlers {
class StaticHandler : ISpecificHandler {
private string[] requestParts;
public StaticHandler(string[] requestParts) {
this.requestParts = requestParts;
}
public void Handle(WebContext context) {
if(this.requestParts.Length < 2) {
throw new HttpException(403, "listing not allowed");
}
Regex checker = new Regex("^[a-z][0-9a-z\\-_]*(\\.[a-zA-Z]+)?$", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline);
string path = "";
for(int i=1; i<this.requestParts.Length; i++) {
if(!checker.IsMatch(this.requestParts[i])) {
throw new HttpException(400, "wrong url (checker='" + checker.ToString() + "'; string='" + this.requestParts[i] + "'");
}
path += FLocal.Common.Config.instance.DirSeparator + this.requestParts[i];
}
string fullPath = FLocal.Common.Config.instance.dataDir + "Static" + path;
if(!File.Exists(fullPath)) {
throw new HttpException(404, "not found");
}
FileInfo fileinfo = new FileInfo(fullPath);
if(!fileinfo.FullName.StartsWith(FLocal.Common.Config.instance.dataDir + "Static")) {
throw new HttpException(403, "forbidden");
}
string mime = Util.getMimeByExtension(fileinfo.Extension);
if(mime != null) {
context.httpresponse.ContentType = mime;
} else {
throw new HttpException(403, "wrong file type");
}
context.httpresponse.Cache.SetExpires(DateTime.Now.AddDays(10));
context.httpresponse.Cache.SetLastModified(fileinfo.LastWriteTime);
context.httpresponse.Cache.SetCacheability(HttpCacheability.Public);
context.httpresponse.TransmitFile(fileinfo.FullName);
}
}
}