forked from Code-Sharp/uHttpSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cs
More file actions
72 lines (62 loc) · 2.76 KB
/
FileHandler.cs
File metadata and controls
72 lines (62 loc) · 2.76 KB
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
/*
* Copyright (C) 2011 uhttpsharp project - http://github.com/raistlinthewiz/uhttpsharp
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using uhttpsharp.Headers;
namespace uhttpsharp.Handlers
{
public class FileHandler : IHttpRequestHandler
{
public static string DefaultMimeType { get; set; }
public static string HttpRootDirectory { get; set; }
public static IDictionary<string, string> MimeTypes { get; private set; }
static FileHandler()
{
DefaultMimeType = "text/plain";
MimeTypes = new Dictionary<string, string>
{
{".css", "text/css"},
{".gif", "image/gif"},
{".htm", "text/html"},
{".html", "text/html"},
{".jpg", "image/jpeg"},
{".js", "application/javascript"},
{".png", "image/png"},
{".xml", "application/xml"},
};
}
private string GetContentType(string path)
{
var extension = Path.GetExtension(path) ?? string.Empty;
if (MimeTypes.ContainsKey(extension))
return MimeTypes[extension];
return DefaultMimeType;
}
public async Task Handle(IHttpContext context, System.Func<Task> next)
{
var requestPath = context.Request.Uri.OriginalString.TrimStart('/');
var httpRoot = Path.GetFullPath(HttpRootDirectory ?? ".");
var path = Path.GetFullPath(Path.Combine(httpRoot, requestPath));
if (!File.Exists(path))
{
await next().ConfigureAwait(false);
return;
}
context.Response = new HttpResponse(GetContentType(path), File.OpenRead(path), context.Request.Headers.KeepAliveConnection());
}
}
}