-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.cs
84 lines (77 loc) · 2.65 KB
/
Request.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace SimpleHttpListener
{
using System;
using System.Collections.Specialized;
using System.IO;
using System.Management.Automation;
using System.Net;
using System.Text;
public class Request : IDisposable
{
private readonly AtomicBool closed = new AtomicBool();
private readonly HttpListenerContext cx;
public Request(Listener listener, HttpListenerContext cx)
{
this.cx = cx;
Listener = listener;
Response = new Response(this, cx.Response);
if (HasEntityBody)
{
Encoding encoding = cx.Request.ContentEncoding;
if (encoding == null) encoding = Encoding.UTF8;
using (var reader = new StreamReader(cx.Request.InputStream, encoding))
{
Body = reader.ReadToEnd();
}
}
}
[Hidden]
internal Listener Listener { get; private set; }
[Hidden]
internal Response Response { get; private set; }
[Hidden]
public HttpListenerRequest HttpRequest { get { return cx.Request; } }
public bool IsLocal { get { return HttpRequest.IsLocal; } }
public string HttpMethod { get { return HttpRequest.HttpMethod; } }
public Uri Url { get { return HttpRequest.Url; } }
public NameValueCollection QueryString { get { return HttpRequest.QueryString; } }
public string ContentType { get { return HttpRequest.ContentType; } }
public NameValueCollection Headers { get { return HttpRequest.Headers; } }
public bool HasEntityBody { get { return HttpRequest.HasEntityBody; } }
public string Body { get; private set; }
internal void Close()
{
if (!closed.GetAndSet(true))
{
Response.Close();
}
}
public override string ToString()
{
string s = HttpMethod + " " + Url;
if(!string.IsNullOrWhiteSpace(ContentType))
{
s += " Content-Type: " + ContentType;
}
return s;
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Close();
}
disposedValue = true;
}
}
void IDisposable.Dispose()
{
Dispose(true);
}
#endregion
}
}