blob: 51d56719d97ca411cc8f24200b75b8206193787d (
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
145
146
147
148
149
150
151
152
153
154
|
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
namespace MediaBrowser.Common.Net.Handlers
{
public abstract class BaseHandler
{
/// <summary>
/// Response headers
/// </summary>
public IDictionary<string, string> Headers = new Dictionary<string, string>();
private Stream CompressedStream { get; set; }
public virtual bool UseChunkedEncoding
{
get
{
return true;
}
}
public virtual long? ContentLength
{
get
{
return null;
}
}
/// <summary>
/// Returns true or false indicating if the handler writes to the stream asynchronously.
/// If so the subclass will be responsible for disposing the stream when complete.
/// </summary>
protected virtual bool IsAsyncHandler
{
get
{
return false;
}
}
/// <summary>
/// The action to write the response to the output stream
/// </summary>
public virtual Action<Stream> WriteStream
{
get
{
return s =>
{
WriteReponse(s);
if (!IsAsyncHandler)
{
DisposeResponseStream();
}
};
}
}
/// <summary>
/// The original RequestContext
/// </summary>
public RequestContext RequestContext { get; set; }
/// <summary>
/// The original QueryString
/// </summary>
protected NameValueCollection QueryString
{
get
{
return RequestContext.Request.QueryString;
}
}
/// <summary>
/// Gets the MIME type to include in the response headers
/// </summary>
public abstract string ContentType { get; }
/// <summary>
/// Gets the status code to include in the response headers
/// </summary>
public virtual int StatusCode
{
get
{
return 200;
}
}
/// <summary>
/// Gets the cache duration to include in the response headers
/// </summary>
public virtual TimeSpan CacheDuration
{
get
{
return TimeSpan.FromTicks(0);
}
}
/// <summary>
/// Gets the last date modified of the content being returned, if this can be determined.
/// This will be used to invalidate the cache, so it's not needed if CacheDuration is 0.
/// </summary>
public virtual DateTime? LastDateModified
{
get
{
return null;
}
}
public virtual bool CompressResponse
{
get
{
return true;
}
}
private void WriteReponse(Stream stream)
{
if (CompressResponse)
{
CompressedStream = new DeflateStream(stream, CompressionLevel.Fastest, false);
WriteResponseToOutputStream(CompressedStream);
}
else
{
WriteResponseToOutputStream(stream);
}
}
protected abstract void WriteResponseToOutputStream(Stream stream);
protected void DisposeResponseStream()
{
if (CompressedStream != null)
{
CompressedStream.Dispose();
}
RequestContext.Response.OutputStream.Dispose();
}
}
}
|