forked from cpe200-161/WerewolfServer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTTPRequest.cs
152 lines (140 loc) · 4.91 KB
/
HTTPRequest.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DNWS
{
public class HTTPRequest
{
protected String _url;
protected String _filename;
protected String _path;
protected static Dictionary<String, String> _propertyListDictionary = null;
protected static Dictionary<String, String> _requestListDictionary = null;
protected String _body;
protected int _status;
protected String _method;
public const string CONTENT_TYPE_APP_X_HTTP_FORM_URLENCODED = "application/x-www-form-urlencoded";
public const string CONTENT_TYPE_APP_JSON = "application/json";
public String Url
{
get { return _url; }
}
public String Filename
{
get { return _filename; }
}
public String Path
{
get { return _path; }
}
public String Body
{
get { return _body; }
}
public int Status
{
get { return _status; }
}
public String Method
{
get { return _method; }
}
public HTTPRequest(HttpListenerRequest req)
{
_propertyListDictionary = new Dictionary<String, String>();
_method = req.HttpMethod;
if (!(new [] {"get", "post", "put", "delete"}).Contains(_method.ToLower())) {
_status = 501;
return;
}
_status = 200;
//_url = req.Url.ToString();
_url = req.Url.AbsolutePath;
String[] urls = Regex.Split(_url, "/");
_filename = urls[urls.Length - 1];
_path = urls[1];
String[] parts = Regex.Split(_filename, "[?]");
if (parts.Length > 1 && parts[1].Contains('&'))
{
//Ref: http://stackoverflow.com/a/4982122
_requestListDictionary = parts[1].Split('&').Select(x => x.Split('=')).ToDictionary(x => x[0].ToLower(), x => x[1]);
}
else
{
_requestListDictionary = new Dictionary<String, String>();
if (parts.Length > 1)
{
String[] requestParts = Regex.Split(parts[1], "[=]");
if (requestParts.Length > 1)
{
_requestListDictionary.Add(requestParts[0], requestParts[1]);
}
}
}
foreach (string key in req.Headers)
{
addProperty(key, req.Headers.GetValues(key)[0]);
}
if (req.HasEntityBody)
{
System.IO.Stream rs = req.InputStream;
System.Text.Encoding encoding = req.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(rs, encoding);
string body = reader.ReadToEnd();
rs.Close();
reader.Close();
if (req.ContentType.ToLower() == CONTENT_TYPE_APP_X_HTTP_FORM_URLENCODED)
{
string[] lines = Regex.Split(body, "\r\n");
foreach (string line in lines)
{
String[] pair = Regex.Split(line, ":"); //FIXME
pair = pair.Where(x => !string.IsNullOrEmpty(x)).ToArray();
if (pair[0].Length > 1)
{ //FIXME, this is a quick hack
Dictionary<String, String> _bodys = pair[0].Split('&').Select(x => x.Split('=')).ToDictionary(x => x[0].ToLower(), x => x[1]);
_requestListDictionary = _requestListDictionary.Concat(_bodys).ToDictionary(x => x.Key, x => x.Value);
}
}
}
else if (req.ContentType.ToLower() == CONTENT_TYPE_APP_JSON)
{
_body = body;
}
}
}
public String getPropertyByKey(String key)
{
if (_propertyListDictionary.ContainsKey(key.ToLower()))
{
return _propertyListDictionary[key.ToLower()];
}
else
{
return null;
}
}
public String getRequestByKey(String key)
{
if (_requestListDictionary.ContainsKey(key.ToLower()))
{
return _requestListDictionary[key.ToLower()];
}
else
{
return null;
}
}
public void addProperty(String key, String value)
{
_propertyListDictionary[key.ToLower()] = value;
}
public void addRequest(String key, String value)
{
_requestListDictionary[key.ToLower()] = value;
}
}
}