forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResultExtensions.cs
More file actions
164 lines (144 loc) · 6.45 KB
/
HttpResultExtensions.cs
File metadata and controls
164 lines (144 loc) · 6.45 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
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.IO;
using System.Net;
using ServiceStack.Text;
using ServiceStack.Common.Utils;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.ServiceModel;
namespace ServiceStack.Common.Web
{
public static class HttpResultExtensions
{
/// <summary>
/// Shortcut to get the ResponseDTO whether it's bare or inside a IHttpResult
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static object ToDto(this object response)
{
if (response == null) return null;
var httpResult = response as IHttpResult;
return httpResult != null ? httpResult.Response : response;
}
/// <summary>
/// Alias of ToDto
/// </summary>
public static object ToResponseDto(this object response)
{
return ToDto(response);
}
/// <summary>
/// Shortcut to get the ResponseDTO whether it's bare or inside a IHttpResult
/// </summary>
/// <param name="response"></param>
/// <returns>TResponse if found; otherwise null</returns>
public static TResponse ToDto<TResponse>(this object response) where TResponse : class
{
if (response == null) return default(TResponse);
var httpResult = response as IHttpResult;
return (httpResult != null ? httpResult.Response : response) as TResponse;
}
/// <summary>
/// Alias of ToDto
/// </summary>
public static TResponse ToResponseDto<TResponse>(this object response) where TResponse : class
{
return ToDto<TResponse>(response);
}
public static object ToErrorResponse(this IHttpError httpError)
{
if (httpError == null) return null;
var errorDto = httpError.ToDto();
if (errorDto != null) return errorDto;
var error = httpError as HttpError;
return new ErrorResponse {
ResponseStatus = new ResponseStatus {
ErrorCode = httpError.ErrorCode,
Message = httpError.Message,
StackTrace = error != null ? error.StackTrace : null,
}
};
}
/// <summary>
/// Shortcut to get the ResponseStatus whether it's bare or inside a IHttpResult
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static ResponseStatus ToResponseStatus(this object response)
{
if (response == null) return null;
var hasResponseStatus = response as IHasResponseStatus;
if (hasResponseStatus != null)
return hasResponseStatus.ResponseStatus;
var propertyInfo = response.GetType().GetPropertyInfo("ResponseStatus");
if (propertyInfo == null)
return null;
return ReflectionUtils.GetProperty(response, propertyInfo) as ResponseStatus;
}
/// <summary>
/// Whether the response is an IHttpError or Exception
/// </summary>
public static bool IsErrorResponse(this object response)
{
return response != null && (response is IHttpError || response is Exception);
}
/// <summary>
/// rangeHeader should be of the format "bytes=0-" or "bytes=0-12345" or "bytes=123-456"
/// </summary>
public static void ExtractHttpRanges(this string rangeHeader, long contentLength, out long rangeStart, out long rangeEnd)
{
var rangeParts = rangeHeader.SplitOnFirst("=")[1].SplitOnFirst("-");
rangeStart = Int64.Parse(rangeParts[0]);
rangeEnd = rangeParts.Length == 2 && !String.IsNullOrEmpty(rangeParts[1])
? Int32.Parse(rangeParts[1]) //the client requested a chunk
: contentLength - 1;
}
/// <summary>
/// Adds 206 PartialContent Status, Content-Range and Content-Length headers
/// </summary>
public static void AddHttpRangeResponseHeaders(this IHttpResponse response, long rangeStart, long rangeEnd, long contentLength)
{
response.AddHeader(HttpHeaders.ContentRange, "bytes {0}-{1}/{2}".Fmt(rangeStart, rangeEnd, contentLength));
response.StatusCode = (int)HttpStatusCode.PartialContent;
response.SetContentLength(rangeEnd - rangeStart + 1);
}
/// <summary>
/// Writes partial range as specified by start-end, from fromStream to toStream.
/// </summary>
public static void WritePartialTo(this Stream fromStream, Stream toStream, long start, long end)
{
if (!fromStream.CanSeek)
throw new InvalidOperationException(
"Sending Range Responses requires a seekable stream eg. FileStream or MemoryStream");
long totalBytesToSend = end - start + 1;
const int bufferSize = 0x1000;
var buffer = new byte[bufferSize];
long bytesRemaining = totalBytesToSend;
fromStream.Seek(start, SeekOrigin.Begin);
while (bytesRemaining > 0)
{
var count = bytesRemaining <= buffer.Length
? fromStream.Read(buffer, 0, (int)Math.Min(bytesRemaining, int.MaxValue))
: fromStream.Read(buffer, 0, buffer.Length);
try
{
//Log.DebugFormat("Writing {0} to response",System.Text.Encoding.UTF8.GetString(buffer));
toStream.Write(buffer, 0, count);
toStream.Flush();
bytesRemaining -= count;
}
catch (Exception httpException)
{
/* in Asp.Net we can call HttpResponseBase.IsClientConnected
* to see if the client broke off the connection
* and avoid trying to flush the response stream.
* I'm not quite I can do the same here without some invasive changes,
* so instead I'll swallow the exception that IIS throws in this situation.*/
if (httpException.Message == "An error occurred while communicating with the remote host. The error code is 0x80070057.")
return;
throw;
}
}
}
}
}