IHttpResponse is now nicer to implement :) · bonesoul/uhttpsharp@fd8e284 · GitHub
Skip to content

Commit fd8e284

Browse files
committed
IHttpResponse is now nicer to implement :)
1 parent 7aa25b7 commit fd8e284

4 files changed

Lines changed: 39 additions & 41 deletions

File tree

‎uhttpsharp.dll.nuspec‎

Lines changed: 1 addition & 1 deletion

‎uhttpsharp/HttpClient.cs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19-
using System.Linq;
20-
using System.Runtime.CompilerServices;
21-
using System.Runtime.Remoting.Contexts;
2219
using System.Text;
2320
using log4net;
2421
using System.Net;
2522
using System.Reflection;
2623
using System;
2724
using System.Collections.Generic;
2825
using System.IO;
29-
using System.Net.Sockets;
3026
using System.Threading.Tasks;
3127
using uhttpsharp.Clients;
3228
using uhttpsharp.Headers;
@@ -115,16 +111,24 @@ private async Task WriteResponse(HttpContext context, StreamWriter writer)
115111
IHttpRequest request = context.Request;
116112

117113
// Headers
118-
await response.WriteHeaders(writer).ConfigureAwait(false);
114+
await writer.WriteLineAsync(string.Format("HTTP/1.1 {0} {1}",
115+
(int)response.ResponseCode,
116+
response.ResponseCode));
117+
118+
foreach (var header in response.Headers)
119+
{
120+
await writer.WriteLineAsync(string.Format("{0}: {1}", header.Key, header.Value));
121+
}
119122

120123
// Cookies
121124
if (context.Cookies.Touched)
122125
{
123126
await writer.WriteAsync(context.Cookies.ToCookieData())
124127
.ConfigureAwait(false);
125-
await writer.FlushAsync().ConfigureAwait(false);
126128
}
127129

130+
await writer.FlushAsync().ConfigureAwait(false);
131+
128132
// Empty Line
129133
await writer.BaseStream.WriteAsync(CrLfBuffer, 0, CrLfBuffer.Length).ConfigureAwait(false);
130134

‎uhttpsharp/HttpResponse.cs‎

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,38 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Globalization;
2122
using System.IO;
2223
using System.Runtime.Remoting.Messaging;
2324
using System.Threading.Tasks;
2425
using uhttpsharp;
26+
using uhttpsharp.Headers;
2527

2628
namespace uhttpsharp
2729
{
2830
public interface IHttpResponse
2931
{
3032
Task WriteResponse(StreamWriter writer);
3133

32-
Task WriteHeaders(StreamWriter writer);
34+
/// <summary>
35+
/// Gets the status line of this http response,
36+
/// The first line that will be sent to the client.
37+
/// </summary>
38+
HttpResponseCode ResponseCode { get; }
39+
40+
IHttpHeaders Headers { get; }
3341

3442
bool CloseConnection { get; }
3543
}
3644

3745
public sealed class HttpResponse : IHttpResponse
3846
{
39-
private static readonly Dictionary<int, string> ResponseTexts =
40-
new Dictionary<int, string>
41-
{
42-
{200, "OK"},
43-
{302, "Found"},
44-
{303, "See Other"},
45-
{400, "BadRequest"},
46-
{404, "Not Found"},
47-
{502, "Server Busy"},
48-
{500, "Internal Server Error"}
49-
};
50-
51-
public string Protocol { get; private set; }
52-
public string ContentType { get; private set; }
53-
public HttpResponseCode Code { get; private set; }
5447
private Stream ContentStream { get; set; }
5548

5649
private readonly Stream _headerStream = new MemoryStream();
5750
private readonly bool _closeConnection;
51+
private readonly IHttpHeaders _headers;
52+
private readonly HttpResponseCode _responseCode;
5853

5954
public HttpResponse(HttpResponseCode code, string content, bool closeConnection)
6055
: this(code, "text/html; charset=utf-8", StringToStream(content), closeConnection)
@@ -66,14 +61,18 @@ public HttpResponse(string contentType, Stream contentStream, bool closeConnecti
6661
}
6762
public HttpResponse(HttpResponseCode code, string contentType, Stream contentStream, bool keepAliveConnection)
6863
{
69-
Protocol = "HTTP/1.1";
70-
ContentType = contentType;
71-
72-
Code = code;
7364
ContentStream = contentStream;
65+
7466
_closeConnection = !keepAliveConnection;
7567

76-
CacheHeaders(new StreamWriter(_headerStream));
68+
_responseCode = code;
69+
_headers = new ListHttpHeaders(new[]
70+
{
71+
new KeyValuePair<string, string>("Date", DateTime.UtcNow.ToString("R")),
72+
new KeyValuePair<string, string>("Connection", _closeConnection ? "Close" : "Keep-Alive"),
73+
new KeyValuePair<string, string>("Content-Type", contentType),
74+
new KeyValuePair<string, string>("Content-Length", ContentStream.Length.ToString(CultureInfo.InvariantCulture)),
75+
});
7776
}
7877
public HttpResponse(HttpResponseCode code, byte[] contentStream, bool keepAliveConnection)
7978
: this (code, "text/html; charset=utf-8", new MemoryStream(contentStream), keepAliveConnection)
@@ -103,23 +102,19 @@ public async Task WriteResponse(StreamWriter writer)
103102

104103
await writer.BaseStream.FlushAsync().ConfigureAwait(false);
105104
}
106-
107-
public bool CloseConnection
105+
public HttpResponseCode ResponseCode
108106
{
109-
get { return _closeConnection; }
107+
get { return _responseCode; }
110108
}
111-
112-
private void CacheHeaders(StreamWriter tempWriter)
109+
public IHttpHeaders Headers
113110
{
114-
tempWriter.WriteLine("{0} {1} {2}", Protocol, (int)Code, ResponseTexts[(int)Code]);
115-
tempWriter.WriteLine("Date: {0}", DateTime.UtcNow.ToString("R"));
116-
tempWriter.WriteLine("Connection: {0}", _closeConnection ? "Close" : "Keep-Alive");
117-
tempWriter.WriteLine("Content-Type: {0}", ContentType);
118-
tempWriter.WriteLine("Content-Length: {0}", ContentStream.Length);
119-
//tempWriter.WriteLine();
120-
tempWriter.Flush();
111+
get { return _headers; }
121112
}
122113

114+
public bool CloseConnection
115+
{
116+
get { return _closeConnection; }
117+
}
123118

124119
public async Task WriteHeaders(StreamWriter writer)
125120
{

‎uhttpsharp/HttpResponseCode.cs‎

Lines changed: 0 additions & 1 deletion

0 commit comments

Comments
 (0)