Introducing controllers and controller handler · bonesoul/uhttpsharp@ff1d2ea · GitHub
Skip to content

Commit ff1d2ea

Browse files
committed
Introducing controllers and controller handler
1 parent 26c3b00 commit ff1d2ea

15 files changed

Lines changed: 569 additions & 30 deletions
Lines changed: 57 additions & 0 deletions

‎uhttpsharp-demo/Program.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private static void Main()
5252
//httpServer.Use(new ListenerSslDecorator(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 443)), serverCertificate));
5353

5454
//httpServer.Use(new SessionHandler<DateTime>(() => DateTime.Now));
55+
httpServer.Use(new ControllerHandler(new JsonController(0), new JsonModelBinder(), new JsonView()));
5556
httpServer.Use(new ExceptionHandler());
5657
httpServer.Use(new ClassRouter(new MySuperHandler()));
5758
httpServer.Use(new TimingHandler());

‎uhttpsharp-demo/packages.config‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="log4net" version="2.0.3" targetFramework="net45" />
4-
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
4+
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
55
</packages>

‎uhttpsharp-demo/uhttpsharp.Demo.csproj‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
<Reference Include="log4net">
4343
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
4444
</Reference>
45-
<Reference Include="Newtonsoft.Json">
46-
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
45+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
46+
<SpecificVersion>False</SpecificVersion>
47+
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
4748
</Reference>
4849
<Reference Include="System" />
4950
<Reference Include="System.Core" />
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace uhttpsharp.Attributes
4+
{
5+
public class HttpMethodAttribute : Attribute
6+
{
7+
private readonly HttpMethods _httpMethod;
8+
public HttpMethodAttribute(HttpMethods httpMethod)
9+
{
10+
_httpMethod = httpMethod;
11+
}
12+
13+
public HttpMethods HttpMethod
14+
{
15+
get { return _httpMethod; }
16+
}
17+
}
18+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using uhttpsharp.ModelBinders;
7+
8+
namespace uhttpsharp.Attributes
9+
{
10+
internal interface IModelBinding
11+
{
12+
T Get<T>(IHttpContext context, IModelBinder binder);
13+
}
14+
15+
internal class FromBodyAttribute : Attribute, IModelBinding
16+
{
17+
public T Get<T>(IHttpContext context, IModelBinder binder)
18+
{
19+
return binder.Get<T>(context.Request.Post.Raw);
20+
}
21+
}
22+
23+
public class FromPostAttribute : PrefixAttribute
24+
{
25+
public FromPostAttribute(string prefix = null)
26+
: base(prefix)
27+
{
28+
}
29+
public override T Get<T>(IHttpContext context, IModelBinder binder)
30+
{
31+
return binder.Get<T>(context.Request.Post.Parsed, Prefix);
32+
}
33+
}
34+
35+
public class FromQueryAttribute : PrefixAttribute
36+
{
37+
public FromQueryAttribute(string prefix)
38+
: base(prefix)
39+
{
40+
}
41+
public override T Get<T>(IHttpContext context, IModelBinder binder)
42+
{
43+
return binder.Get<T>(context.Request.QueryString, Prefix);
44+
}
45+
}
46+
47+
public class FromHeadersAttribute : PrefixAttribute
48+
{
49+
public FromHeadersAttribute(string prefix)
50+
: base(prefix)
51+
{
52+
}
53+
54+
public override T Get<T>(IHttpContext context, IModelBinder binder)
55+
{
56+
return binder.Get<T>(context.Request.Headers, Prefix);
57+
}
58+
}
59+
60+
public abstract class PrefixAttribute : Attribute, IModelBinding
61+
{
62+
private readonly string _prefix;
63+
64+
public PrefixAttribute(string prefix)
65+
{
66+
_prefix = prefix;
67+
}
68+
69+
public bool HasPrefix
70+
{
71+
get { return !string.IsNullOrEmpty(_prefix); }
72+
}
73+
74+
public string Prefix
75+
{
76+
get { return _prefix; }
77+
}
78+
79+
public abstract T Get<T>(IHttpContext context, IModelBinder binder);
80+
}
81+
}

‎uhttpsharp/Handlers/ClassRouter.cs‎

Lines changed: 21 additions & 15 deletions

0 commit comments

Comments
 (0)