-
Notifications
You must be signed in to change notification settings - Fork 97
/
HTTPRequest.cs
158 lines (134 loc) · 4.21 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
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace DNWS
{
public class HTTPRequest
{
protected String _url;
protected String _filename;
protected static Dictionary<String, String> _propertyListDictionary = null;
protected static Dictionary<String, String> _requestListDictionary = null;
protected String _body;
protected int _status;
protected String _method;
protected String _pure_request;
public String Url
{
get { return _url; }
}
public String Filename
{
get { return _filename; }
}
public String Body
{
get { return _body; }
}
public int Status
{
get { return _status; }
}
public String Method
{
get { return _method; }
}
public String whole_request
{
get { return _pure_request; }
}
public HTTPRequest(String request)
{
_pure_request = request;
_propertyListDictionary = new Dictionary<String, String>();
String[] lines = Regex.Split(request, "\\n");
if (lines.Length == 0)
{
_status = 500;
return;
}
String[] statusLine = Regex.Split(lines[0], "\\s");
if (statusLine.Length != 4)
{ // too short something is wrong
_status = 401;
return;
}
if (!statusLine[0].ToLower().Equals("get"))
{
_method = "GET";
}
else if (!statusLine[0].ToLower().Equals("post"))
{
_method = "POST";
}
else
{
_status = 501;
return;
}
_status = 200;
_url = statusLine[1];
String[] urls = Regex.Split(_url, "/");
_filename = _url;
String[] parts = Regex.Split(urls[urls.Length - 1], "[?]");
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 (lines.Length == 1) return;
for (int i = 1; i != lines.Length; i++)
{
String[] pair = Regex.Split(lines[i], ": "); //FIXME
if (pair.Length == 0) continue;
if (pair.Length == 1)
{ // handle post body
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
{ // Length == 2, GET url request
addProperty(pair[0], pair[1]);
}
}
}
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;
}
}
}