forked from pruet/DNWS
-
Notifications
You must be signed in to change notification settings - Fork 46
/
HTTPResponse.cs
110 lines (99 loc) · 3.18 KB
/
HTTPResponse.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace DNWS
{
// RFC1945
public class HTTPResponse
{
protected int _status = 404;
public int Status
{
get { return _status; }
set { _status = value; }
}
protected byte[] _body;
public byte[] Body
{
get { return _body; }
set { _body = value; }
}
public void SetBodyString(string str)
{
Body = Encoding.UTF8.GetBytes(str);
}
public void SetBodyJson(Object obj)
{
string json = JsonConvert.SerializeObject(obj,
Newtonsoft.Json.Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
obj = null;
SetBodyString(json);
}
protected string _type = "text/html";
public string Type
{
get { return _type; }
set { _type = value; }
}
public Dictionary<string, string> Headers
{
get
{
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Content-Type"] = Type;
headers["Connection"] = "close";
headers["Server"] = "DNWS 1.0 (Werewolf)";
return headers;
}
}
public String Header
{
get
{
StringBuilder headerResponse = new StringBuilder("HTTP/1.0 ");
switch (_status)
{
case 200:
headerResponse.Append("200 OK");
break;
case 201:
headerResponse.Append("201 Created");
break;
case 400:
headerResponse.Append("400 Bad Request");
break;
case 403:
headerResponse.Append("403 Forbidden");
break;
case 404:
headerResponse.Append("404 Not Found");
break;
case 500:
headerResponse.Append("500 Internal Server Error");
break;
case 501:
default:
headerResponse.Append("501 Not Implemented");
break;
}
headerResponse.Append("\r\n");
headerResponse.Append("Content-Type: ").Append(Type).Append("\r\n");
headerResponse.Append("Connection: close\r\n");
headerResponse.Append("Server: DNWS 1.0\r\n");
headerResponse.Append("\r\n");
return headerResponse.ToString();
}
}
public HTTPResponse(int status)
{
_status = status;
}
}
}