-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponse.cs
More file actions
153 lines (126 loc) · 3.61 KB
/
HttpResponse.cs
File metadata and controls
153 lines (126 loc) · 3.61 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
namespace SimpleHttp
{
public sealed class HttpResponse
{
HttpListenerResponse response;
public bool IsOpen { get; private set; }
public bool IsDataSent { get; private set; }
public int? StatusCode { get; set; }
public string StatusMessage { get; set; }
public bool? Chunked { get; set; }
public Dictionary<string, string> Headers { get; set; }
public Dictionary<string, string> Cookies { get; set; }
public Dictionary<string, string> CookiePath { get; set; }
public Dictionary<string, long> CookieExpire { get; set; }
public string ContentType { get; set; }
public string RedirectLocation { get; set; }
public bool AllowCors { get; set; }
public static string UrlEncode(string str) => WebUtility.UrlEncode(str);
public static string Base64Encode(byte[] data) => Convert.ToBase64String(data);
public static string Base64Encode(string str) => Base64Encode(HttpHelperExtensions.UTF8.GetBytes(str));
public HttpResponse(HttpListenerResponse response)
{
this.response = response;
this.response.KeepAlive = false;
IsOpen = true;
IsDataSent = false;
Reset();
}
public void Reset()
{
if (IsDataSent)
return;
StatusCode = null;
StatusMessage = null;
Chunked = null;
Headers = new Dictionary<string, string>();
Cookies = new Dictionary<string, string>();
CookiePath = new Dictionary<string, string>();
CookieExpire = new Dictionary<string, long>();
ContentType = null;
RedirectLocation = null;
}
void SetHeaders()
{
if (IsDataSent)
return;
if (StatusCode != null)
response.StatusCode = (int)StatusCode;
if (StatusMessage != null)
response.StatusDescription = StatusMessage;
if (Chunked != null)
response.SendChunked = (bool)Chunked;
if (Headers != null)
{
foreach (var header in Headers)
response.AppendHeader(header.Key, header.Value);
}
if (Cookies != null)
{
foreach (var cookie in Cookies)
{
var cookieObj = new Cookie(cookie.Key, cookie.Value);
if (CookiePath != null && CookiePath.ContainsKey(cookie.Key))
cookieObj.Path = CookiePath[cookie.Key];
if (CookieExpire != null && CookieExpire.ContainsKey(cookie.Key))
cookieObj.Expires = DateTime.Now.AddSeconds(CookieExpire[cookie.Key]);
response.AppendCookie(cookieObj);
}
}
if (ContentType != null)
response.ContentType = ContentType;
if (RedirectLocation != null)
{
if (StatusCode == null)
response.StatusCode = 302;
response.RedirectLocation = RedirectLocation;
}
if (AllowCors)
{
response.AppendHeader("Access-Control-Expose-Headers", String.Join(", ", response.Headers.AllKeys));
response.AppendHeader("Access-Control-Allow-Origin", "*");
}
IsDataSent = true;
}
public void WriteBodyData(byte[] data)
{
if (ContentType == null)
ContentType = "application/octet-stream";
SetHeaders();
response.WriteBodyData(data);
response.FlushBodyStream();
}
public void WriteBodyText(string text)
{
if (ContentType == null)
ContentType = "text/plain";
SetHeaders();
response.WriteBodyText(text);
response.FlushBodyStream();
}
public void WriteBodyJson<T>(T obj)
{
if (ContentType == null)
ContentType = "application/json";
SetHeaders();
response.WriteBodyText(JsonConvert.SerializeObject(obj));
response.FlushBodyStream();
}
public Stream GetBodyStream()
{
SetHeaders();
return response.OutputStream;
}
public void Close()
{
SetHeaders();
response.Close();
IsOpen = false;
}
}
}