forked from bonesoul/uhttpsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.cs
More file actions
138 lines (117 loc) · 4.02 KB
/
HttpRequest.cs
File metadata and controls
138 lines (117 loc) · 4.02 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
/*
* Copyright (C) 2011 uhttpsharp project - http://github.com/raistlinthewiz/uhttpsharp
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace uhttpsharp
{
public sealed class HttpRequest
{
public bool Valid { get; private set; }
public Dictionary<string, string> Headers { get; private set; }
public HttpMethod HttpMethod { get; private set; }
public string HttpProtocol { get; private set; }
public Uri Uri { get; private set; }
public string URL { get; private set; }
public HttpRequestParameters Parameters { get; private set; }
private readonly Stream _stream;
public HttpRequest(Stream stream)
{
Headers = new Dictionary<string, string>();
_stream = stream;
Process();
}
private void Process()
{
Valid = false;
// parse the http request
var request = ReadLine();
if (request == null)
return;
var tokens = request.Split(' ');
if (tokens.Length != 3)
{
Console.WriteLine("httpserver: invalid http request.");
return;
}
switch (tokens[0].ToUpper())
{
case "GET":
HttpMethod = HttpMethod.Get;
break;
case "POST":
HttpMethod = HttpMethod.Post;
break;
}
HttpProtocol = tokens[2];
URL = tokens[1];
Uri = new Uri("http://" + HttpServer.Instance.Address + "/" + URL.TrimStart('/'));
Parameters = new HttpRequestParameters(URL);
Console.WriteLine(string.Format("[{0}:{1}] URL: {2}", HttpProtocol, HttpMethod, URL));
// get the headers
string line;
while ((line = ReadLine()) != null)
{
if (line.Equals("")) break;
var keys = line.Split(':');
Headers.Add(keys[0], keys[1]);
}
Valid = true;
}
private string ReadLine()
{
var buffer = string.Empty;
while (true)
{
var _char = _stream.ReadByte();
if (_char == '\n') break;
if (_char == '\r') continue;
if (_char == -1)
{
if (buffer != string.Empty)
return buffer;
return null;
}
buffer += Convert.ToChar(_char);
}
return buffer;
}
}
public enum HttpMethod
{
Get,
Post
}
public sealed class HttpRequestParameters
{
public string Function { get; private set; }
public string[] Params { get; private set; }
public HttpRequestParameters(string url)
{
Function = "";
var tokens = url.Split('/');
if (tokens.Length > 1)
{
Function = tokens[1];
Params = new string[tokens.Length - 2];
for (var i = 2; i < tokens.Length; i++)
{
Params[i - 2] = tokens[i];
}
}
}
}
}