Repository navigation
Expand file tree
/
Copy pathRequest.cs
More file actions
166 lines (140 loc) · 4.67 KB
/
Copy pathRequest.cs
File metadata and controls
166 lines (140 loc) · 4.67 KB
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
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HTTPServer
{
public enum RequestMethod
{
GET,
POST,
HEAD
}
public enum HTTPVersion
{
HTTP10,
HTTP11,
HTTP09
}
class Request
{
string[] requestLines;
RequestMethod method;
public string relativeURI;
Dictionary<string, string> headerLines;
public Dictionary<string, string> HeaderLines
{
get { return headerLines; }
}
HTTPVersion httpVersion;
string requestString;
string[] contentLines;
public Request(string requestString)
{
this.requestString = requestString;
}
/// <summary>
/// Parses the request string and loads the request line, header lines and content, returns false if there is a parsing error
/// </summary>
/// <returns>True if parsing succeeds, false otherwise.</returns>
public bool ParseRequest()
{
//TODO: parse the receivedRequest using the \r\n delimeter
string[] stringSeparators = new string[] { "\r\n" };
string[] REQSTRINGLINES = requestString.Split(stringSeparators, StringSplitOptions.None);
// check that there is atleast 3 lines: Request line, Host Header, Blank line (usually 4 lines with the last empty line for empty content)
if (REQSTRINGLINES.Length < 3)
{
return false;
}
// Parse Request line
if (ParseRequestLine(REQSTRINGLINES) == false)
{
return false;
}
// Validate blank line exists
if (!ValidateBlankLine(REQSTRINGLINES))
{
return false;
}
// Load header lines into HeaderLines dictionary
string[] HL = new string[REQSTRINGLINES.Length - 3];//REQSTRINGLENGTH-(REQLINE,BLANKLINE,CONTENT)
int count = 0;
for (int i = 1; i < REQSTRINGLINES.Length - 2; i++)//hmshy mn b3d l reqline lhd abl l blank line
{
HL[count] = REQSTRINGLINES[i];
count++;
}
if (LoadHeaderLines(HL) == false)
{
return false;
}
return true;
}
private bool ParseRequestLine(string[] REQSTRINGLINES)
{
string[] stringSeparators = new string[] { " " };
requestLines = REQSTRINGLINES[0].Split(stringSeparators, StringSplitOptions.None);
if (requestLines.Length < 3)//Method uri httpversion
{
return false;
}
method = (RequestMethod)Enum.Parse(typeof(RequestMethod), requestLines[0]);
relativeURI = requestLines[1].Substring(1);
if (ValidateIsURI(relativeURI) == false)
{
return false;
}
if (requestLines[2] == "HTTP/0.9")
{
httpVersion = HTTPVersion.HTTP09;
}
else if (requestLines[2] == "HTTP/1.0")
{
httpVersion = HTTPVersion.HTTP10;
}
else if (requestLines[2] == "HTTP/1.1")
{
httpVersion = HTTPVersion.HTTP11;
}
return true;
}
private bool ValidateIsURI(string uri)//byshof l uri mktob btry2a sa7 wla la
{
return Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute);
}
private bool LoadHeaderLines(string[] HL)
{
if (HL.Length < 1)
{
return false;
}
headerLines = new Dictionary<string, string>();
foreach (string line in HL)
{
int index = 0;
for (int i = 0; i < line.Length; i++)
{
if (line[i] == ':')
{
index = i;
break;
}
}
headerLines.Add(line.Substring(0, index), line.Substring(index + 1));
}
return true;
}
private bool ValidateBlankLine(string[] REQSTRINGLINES)
{
if (REQSTRINGLINES[REQSTRINGLINES.Length - 2] == "")
{
return true;
}
else
{
return false;
}
}
}
}