-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHttpRequest.cs
153 lines (132 loc) · 4.37 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
using System.Text;
using SmileGatewayCore.Http.Feature;
namespace SmileGatewayCore.Http.Context;
public partial class HttpRequest
{
public HttpRequest()
{
_requestFeature = new RequestFeature();
}
public bool ByteParse(ArraySegment<byte> bytes)
{
// 헤더와 바디의 중간 부분 찾기
string header = "";
byte[] headerEnd = Encoding.UTF8.GetBytes("\r\n\r\n");
int i = 0;
for (; i <= bytes.Count - headerEnd.Length; i++)
{
if (bytes.Array!.Skip(bytes.Offset + i).Take(headerEnd.Length).SequenceEqual(headerEnd))
{
header = Encoding.UTF8.GetString(bytes.Array!, bytes.Offset, i);
break;
}
}
// 헤더 파싱
ParseHeader(header);
// 바디 파싱
if (IsMultipart)
return AppendMultipartBody(new ArraySegment<byte>(bytes.Array!, bytes.Offset + i, bytes.Count - i));
else
Body = Encoding.UTF8.GetString(bytes.Array!, bytes.Offset + i, bytes.Count - i) + "\r\n";
return true;
}
private void ParseHeader(string requestString)
{
// System.Console.WriteLine("\n" + requestString + "\n");
string[] requestLines = requestString.Split("\r\n");
MakeRequestInfo(requestLines[0].Split(" "));
// HttpContext 분리 생성
for (int i = 1; i < requestLines.Count(); i++)
{
string[] header = requestLines[i].Split(": ");
// System.Console.WriteLine(header[0] + ": " + header[1]);
if (header[0] == "Cookie")
{
Cookie = new RequestCookie(header[1]);
continue;
}
if (header[0] == "Content-Type")
{
if (header[1].StartsWith("multipart/form-data"))
{
IsMultipart = true;
_boundary = "--" + requestLines[i].Split("boundary=")[1];
_endBoundary = Encoding.UTF8.GetBytes(_boundary + "--");
}
}
if (header[0] == "Content-Length")
{
ContentLength = int.Parse(header[1]);
continue;
}
if (header[0] == "Origin")
{
if (header[1].StartsWith("https"))
IsHttps = true;
else
IsHttps = false;
}
if (header.Length > 1)
Header[header[0]] = header[1];
}
MakeDeafultHeader();
}
public bool AppendMultipartBody(ArraySegment<byte> bodys)
{
MultipartBody.AddRange(bodys);
bool contains = false;
for (int i = 0; i <= bodys.Count - _endBoundary.Length; i++)
{
if (bodys.Array!.Skip(bodys.Offset + i).Take(_endBoundary.Length).SequenceEqual(_endBoundary))
{
contains = true;
break;
}
}
return contains;
}
private void MakeRequestInfo(string[] requestInfos)
{
Method = requestInfos[0];
string[] temp = requestInfos[1].Split("?");
Path = temp[0];
if (temp.Length > 1)
{
QueryString = "?" + temp[1] + " ";
}
else
{
QueryString = " ";
}
Protocol = requestInfos[2];
}
private string HeaderToString()
{
string requestString = $"{Method} {Path}{QueryString}{Protocol}\r\n";
requestString += $"trace-id: {TraceId}\r\n";
requestString += $"user-id: {UserId}\r\n";
foreach (var header in Header)
{
requestString += $"{header.Key}: {header.Value}\r\n";
}
if (ContentLength != -1)
requestString += $"Content-Length: {ContentLength}";
return requestString;
}
public byte[] GetStringToBytes()
{
string requestString = HeaderToString();
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(requestString);
if (IsMultipart)
buffer = buffer.Concat(MultipartBody).ToArray();
else
buffer = buffer.Concat(Encoding.UTF8.GetBytes(Body!)).ToArray();
System.Console.WriteLine(requestString);
return buffer;
}
private void MakeDeafultHeader()
{
Header["Connection"] = "keep-alive";
Header["Keep-Alive"] = "timeout=1800";
}
}