blob: 2e3828512812a443d37b47b2f8718da3ff6fb00e (
plain)
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
|
using System;
using System.IO;
using System.Net;
using MediaBrowser.Model.Logging;
using ServiceStack;
using ServiceStack.Host;
using ServiceStack.Web;
using HttpListenerResponse = WebSocketSharp.Net.HttpListenerResponse;
namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
public class WebSocketSharpResponse : IHttpResponse
{
private readonly ILogger _logger;
private readonly HttpListenerResponse response;
public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response)
{
_logger = logger;
this.response = response;
}
public bool UseBufferedStream { get; set; }
public object OriginalResponse
{
get { return response; }
}
public int StatusCode
{
get { return this.response.StatusCode; }
set { this.response.StatusCode = value; }
}
public string StatusDescription
{
get { return this.response.StatusDescription; }
set { this.response.StatusDescription = value; }
}
public string ContentType
{
get { return response.ContentType; }
set { response.ContentType = value; }
}
public ICookies Cookies { get; set; }
public void AddHeader(string name, string value)
{
if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
{
ContentType = value;
return;
}
response.AddHeader(name, value);
}
public void Redirect(string url)
{
response.Redirect(url);
}
public Stream OutputStream
{
get { return response.OutputStream; }
}
public object Dto { get; set; }
public void Write(string text)
{
try
{
var bOutput = System.Text.Encoding.UTF8.GetBytes(text);
response.ContentLength64 = bOutput.Length;
var outputStream = response.OutputStream;
outputStream.Write(bOutput, 0, bOutput.Length);
Close();
}
catch (Exception ex)
{
_logger.ErrorException("Could not WriteTextToResponse: " + ex.Message, ex);
throw;
}
}
public void Close()
{
if (!this.IsClosed)
{
this.IsClosed = true;
try
{
this.response.CloseOutputStream(_logger);
}
catch (Exception ex)
{
_logger.ErrorException("Error closing HttpListener output stream", ex);
}
}
}
public void End()
{
Close();
}
public void Flush()
{
response.OutputStream.Flush();
}
public bool IsClosed
{
get;
private set;
}
public void SetContentLength(long contentLength)
{
//you can happily set the Content-Length header in Asp.Net
//but HttpListener will complain if you do - you have to set ContentLength64 on the response.
//workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
response.ContentLength64 = contentLength;
}
public void SetCookie(Cookie cookie)
{
var cookieStr = cookie.AsHeaderValue();
response.Headers.Add(HttpHeaders.SetCookie, cookieStr);
}
public bool SendChunked
{
get { return response.SendChunked; }
set { response.SendChunked = value; }
}
}
}
|