Skip to content

Commit af33f52

Browse files
committed
Added IHttpPost (With Raw and Parsed data).
1 parent 4b12be5 commit af33f52

File tree

4 files changed

+121
-32
lines changed

4 files changed

+121
-32
lines changed

uhttpsharp.sln

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.21005.1
5-
MinimumVisualStudioVersion = 10.0.40219.1
3+
# Visual Studio 2012
64
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uhttpsharp.Demo", "uhttpsharp-demo\uhttpsharp.Demo.csproj", "{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}"
75
EndProject
86
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uhttpsharp", "uhttpsharp\uhttpsharp.csproj", "{3D681959-4DA3-4A71-A68B-704D6411D5EA}"
@@ -32,16 +30,6 @@ Global
3230
Release|x86 = Release|x86
3331
EndGlobalSection
3432
GlobalSection(ProjectConfigurationPlatforms) = postSolution
35-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Any CPU.ActiveCfg = Debug|x86
36-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
37-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Mixed Platforms.Build.0 = Debug|x86
38-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|x86.ActiveCfg = Debug|x86
39-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|x86.Build.0 = Debug|x86
40-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|Any CPU.ActiveCfg = Release|x86
41-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|Mixed Platforms.ActiveCfg = Release|x86
42-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|Mixed Platforms.Build.0 = Release|x86
43-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|x86.ActiveCfg = Release|x86
44-
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|x86.Build.0 = Release|x86
4533
{3D681959-4DA3-4A71-A68B-704D6411D5EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4634
{3D681959-4DA3-4A71-A68B-704D6411D5EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
4735
{3D681959-4DA3-4A71-A68B-704D6411D5EA}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -62,6 +50,22 @@ Global
6250
{41C9BDAC-21BE-4C50-933D-9E047F767E63}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
6351
{41C9BDAC-21BE-4C50-933D-9E047F767E63}.Release|Mixed Platforms.Build.0 = Release|Any CPU
6452
{41C9BDAC-21BE-4C50-933D-9E047F767E63}.Release|x86.ActiveCfg = Release|Any CPU
53+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Any CPU.ActiveCfg = Debug|x86
54+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Any CPU.Build.0 = Debug|x86
55+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
56+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|Mixed Platforms.Build.0 = Debug|x86
57+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|x86.ActiveCfg = Debug|x86
58+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Debug|x86.Build.0 = Debug|x86
59+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|Any CPU.ActiveCfg = Release|x86
60+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|Mixed Platforms.ActiveCfg = Release|x86
61+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|Mixed Platforms.Build.0 = Release|x86
62+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|x86.ActiveCfg = Release|x86
63+
{55C5E8D2-F55E-4B9F-B96F-FCE201739D07}.Release|x86.Build.0 = Release|x86
64+
EndGlobalSection
65+
GlobalSection(NestedProjects) = preSolution
66+
EndGlobalSection
67+
GlobalSection(MonoDevelopProperties) = preSolution
68+
StartupItem = uhttpsharp-demo\uhttpsharp.Demo.csproj
6569
EndGlobalSection
6670
GlobalSection(SolutionProperties) = preSolution
6771
HideSolutionNode = FALSE

uhttpsharp/Headers/HttpHeaders.cs

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,96 @@
55
using System.IO;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using System.Security.AccessControl;
89

910
namespace uhttpsharp.Headers
1011
{
12+
internal class EmptyHttpPost : IHttpPost
13+
{
14+
private static readonly byte[] _emptyBytes = new byte[0];
15+
16+
public static readonly IHttpPost Empty = new EmptyHttpPost();
17+
18+
private EmptyHttpPost()
19+
{
20+
21+
}
22+
23+
#region IHttpPost implementation
24+
25+
public byte[] Raw
26+
{
27+
get
28+
{
29+
return _emptyBytes;
30+
}
31+
}
32+
33+
public IHttpHeaders Parsed
34+
{
35+
get
36+
{
37+
return EmptyHttpHeaders.Empty;
38+
}
39+
}
40+
41+
#endregion
42+
}
43+
44+
internal class HttpPost : IHttpPost
45+
{
46+
public static async Task<IHttpPost> Create(StreamReader reader, int postContentLength)
47+
{
48+
byte[] raw = new byte[postContentLength];
49+
int readBytes = await reader.BaseStream.ReadAsync(raw, 0, postContentLength);
50+
51+
return new HttpPost(raw, readBytes);
52+
}
53+
54+
private readonly int _readBytes;
55+
56+
private readonly byte[] _raw;
57+
58+
private readonly Lazy<IHttpHeaders> _parsed;
59+
60+
public HttpPost(byte[] raw, int readBytes)
61+
{
62+
_raw = raw;
63+
_readBytes = readBytes;
64+
_parsed = new Lazy<IHttpHeaders>(Parse);
65+
}
66+
67+
private IHttpHeaders Parse()
68+
{
69+
string body = Encoding.UTF8.GetString(_raw, 0, _readBytes);
70+
var parsed = new QueryStringHttpHeaders(body);
71+
72+
return parsed;
73+
}
74+
75+
#region IHttpPost implementation
76+
77+
public byte[] Raw
78+
{
79+
get
80+
{
81+
return _raw;
82+
}
83+
}
84+
85+
public IHttpHeaders Parsed
86+
{
87+
get
88+
{
89+
return _parsed.Value;
90+
}
91+
}
92+
93+
#endregion
94+
95+
96+
}
97+
1198
[DebuggerDisplay("{Count} Headers")]
1299
[DebuggerTypeProxy(typeof(HttpHeadersDebuggerProxy))]
13100
internal class HttpHeaders : IHttpHeaders
@@ -38,16 +125,6 @@ IEnumerator IEnumerable.GetEnumerator()
38125
return GetEnumerator();
39126
}
40127

41-
public static async Task<IHttpHeaders> FromPost(StreamReader reader, int postContentLength)
42-
{
43-
byte[] buffer = new byte[postContentLength];
44-
var readBytes = await reader.BaseStream.ReadAsync(buffer, 0, postContentLength);
45-
46-
string body = Encoding.UTF8.GetString(buffer, 0, readBytes);
47-
48-
return new QueryStringHttpHeaders(body);
49-
}
50-
51128

52129
internal int Count
53130
{

uhttpsharp/HttpRequest.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ internal class HttpRequest : IHttpRequest
3333
private readonly Uri _uri;
3434
private readonly string[] _requestParameters;
3535
private readonly IHttpHeaders _queryString;
36-
private readonly IHttpHeaders _post;
36+
private readonly IHttpPost _post;
3737

38-
public HttpRequest(IHttpHeaders headers, HttpMethods method, string protocol, Uri uri, string[] requestParameters, IHttpHeaders queryString, IHttpHeaders post)
38+
public HttpRequest(IHttpHeaders headers, HttpMethods method, string protocol, Uri uri, string[] requestParameters, IHttpHeaders queryString, IHttpPost post)
3939
{
4040
_headers = headers;
4141
_method = method;
@@ -71,7 +71,7 @@ public string[] RequestParameters
7171
get { return _requestParameters; }
7272
}
7373

74-
public IHttpHeaders Post
74+
public IHttpPost Post
7575
{
7676
get { return _post; }
7777
}
@@ -108,10 +108,18 @@ public interface IHttpRequest
108108

109109
string[] RequestParameters { get; }
110110

111-
IHttpHeaders Post { get; }
111+
IHttpPost Post {get;}
112112

113113
IHttpHeaders QueryString { get; }
114114

115+
}
116+
117+
public interface IHttpPost
118+
{
119+
120+
byte[] Raw {get;}
121+
122+
IHttpHeaders Parsed {get;}
115123

116124
}
117125

uhttpsharp/RequestProviders/HttpRequestProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task<IHttpRequest> Provide(StreamReader streamReader)
4343
}
4444

4545
IHttpHeaders headers = new HttpHeaders(headersRaw);
46-
IHttpHeaders post = await GetPostData(streamReader, headers);
46+
IHttpPost post = await GetPostData(streamReader, headers);
4747

4848
return new HttpRequest(headers, httpMethod, httpProtocol, uri,
4949
uri.OriginalString.Split(Separators, StringSplitOptions.RemoveEmptyEntries), queryString, post);
@@ -64,17 +64,17 @@ private static IHttpHeaders GetQueryStringData(ref string url)
6464
return queryString;
6565
}
6666

67-
private static async Task<IHttpHeaders> GetPostData(StreamReader streamReader, IHttpHeaders headers)
67+
private static async Task<IHttpPost> GetPostData(StreamReader streamReader, IHttpHeaders headers)
6868
{
6969
int postContentLength;
70-
IHttpHeaders post;
70+
IHttpPost post;
7171
if (headers.TryGetByName("content-length", out postContentLength))
7272
{
73-
post = await HttpHeaders.FromPost(streamReader, postContentLength);
73+
post = await HttpPost.Create(streamReader, postContentLength);
7474
}
7575
else
7676
{
77-
post = EmptyHttpHeaders.Empty;
77+
post = EmptyHttpPost.Empty;
7878
}
7979
return post;
8080
}

0 commit comments

Comments
 (0)