forked from stevenh/HttpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleServer.cs
65 lines (57 loc) · 2.29 KB
/
SimpleServer.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using HttpServer.Modules;
namespace HttpServer
{
/// <summary>
/// Convention over configuration server.
/// </summary>
/// <remarks>
/// Used to make it easy to create and use a web server.
/// <para>
/// All resources must exist in the "YourProject.Content" namespace (or a subdirectory called "Content" relative to yourapp.exe).
/// </para>
/// </remarks>
public class SimpleServer : Server
{
/// <summary>
/// Initializes a new instance of the <see cref="SimpleServer"/> class.
/// </summary>
public SimpleServer()
{
Add(new BodyDecoders.MultiPartDecoder());
Add(new BodyDecoders.UrlDecoder());
var fileModule = new FileModule();
fileModule.AddDefaultMimeTypes();
AddEmbeddedResources(Assembly.GetCallingAssembly(), fileModule);
AddFileResources(Assembly.GetCallingAssembly(), fileModule);
}
private void AddFileResources(Assembly assembly, FileModule fileModule)
{
var assemblyPath = Path.GetDirectoryName(assembly.Location);
var filePath = Path.Combine(assemblyPath, "Public");
if (Directory.Exists(filePath))
fileModule.Resources.Add(new Resources.FileResources("/content/", filePath));
}
private void AddEmbeddedResources(Assembly assembly, FileModule fileModule)
{
string contentNamespace = null;
foreach (var resourceName in assembly.GetManifestResourceNames())
{
if (!resourceName.Contains("Content"))
continue;
contentNamespace = resourceName;
break;
}
if (contentNamespace == null)
return;
int pos = contentNamespace.IndexOf("Content");
contentNamespace = contentNamespace.Substring(0, pos);
fileModule.Resources.Add(new Resources.EmbeddedResourceLoader("/content/", Assembly.GetCallingAssembly(),
contentNamespace));
}
}
}