1818
1919using System ;
2020using System . Collections . Generic ;
21+ using System . Globalization ;
2122using System . IO ;
2223using System . Runtime . Remoting . Messaging ;
2324using System . Threading . Tasks ;
2425using uhttpsharp ;
26+ using uhttpsharp . Headers ;
2527
2628namespace 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 {
0 commit comments