HttpResultFactory.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Model.Logging;
  5. using ServiceStack.Common;
  6. using ServiceStack.Common.Web;
  7. using ServiceStack.ServiceHost;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Net;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using MimeTypes = MediaBrowser.Common.Net.MimeTypes;
  16. namespace MediaBrowser.Server.Implementations.HttpServer
  17. {
  18. /// <summary>
  19. /// Class HttpResultFactory
  20. /// </summary>
  21. public class HttpResultFactory : IHttpResultFactory
  22. {
  23. /// <summary>
  24. /// The _logger
  25. /// </summary>
  26. private readonly ILogger _logger;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="HttpResultFactory"/> class.
  29. /// </summary>
  30. /// <param name="logManager">The log manager.</param>
  31. public HttpResultFactory(ILogManager logManager)
  32. {
  33. _logger = logManager.GetLogger("HttpResultFactory");
  34. }
  35. /// <summary>
  36. /// Gets the result.
  37. /// </summary>
  38. /// <param name="content">The content.</param>
  39. /// <param name="contentType">Type of the content.</param>
  40. /// <param name="responseHeaders">The response headers.</param>
  41. /// <returns>System.Object.</returns>
  42. public object GetResult(object content, string contentType, IDictionary<string, string> responseHeaders = null)
  43. {
  44. return GetHttpResult(content, contentType, responseHeaders);
  45. }
  46. /// <summary>
  47. /// Gets the HTTP result.
  48. /// </summary>
  49. /// <param name="content">The content.</param>
  50. /// <param name="contentType">Type of the content.</param>
  51. /// <param name="responseHeaders">The response headers.</param>
  52. /// <returns>IHasOptions.</returns>
  53. private IHasOptions GetHttpResult(object content, string contentType, IDictionary<string, string> responseHeaders = null)
  54. {
  55. IHasOptions result;
  56. var stream = content as Stream;
  57. if (stream != null)
  58. {
  59. result = new StreamWriter(stream, contentType, _logger);
  60. }
  61. else
  62. {
  63. var bytes = content as byte[];
  64. if (bytes != null)
  65. {
  66. result = new StreamWriter(bytes, contentType, _logger);
  67. }
  68. else
  69. {
  70. var text = content as string;
  71. if (text != null)
  72. {
  73. result = new StreamWriter(Encoding.UTF8.GetBytes(text), contentType, _logger);
  74. }
  75. else
  76. {
  77. result = new HttpResult(content, contentType);
  78. }
  79. }
  80. }
  81. if (responseHeaders != null)
  82. {
  83. AddResponseHeaders(result, responseHeaders);
  84. }
  85. return result;
  86. }
  87. /// <summary>
  88. /// Gets the optimized result.
  89. /// </summary>
  90. /// <typeparam name="T"></typeparam>
  91. /// <param name="requestContext">The request context.</param>
  92. /// <param name="result">The result.</param>
  93. /// <param name="responseHeaders">The response headers.</param>
  94. /// <returns>System.Object.</returns>
  95. /// <exception cref="System.ArgumentNullException">result</exception>
  96. public object GetOptimizedResult<T>(IRequestContext requestContext, T result, IDictionary<string, string> responseHeaders = null)
  97. where T : class
  98. {
  99. if (result == null)
  100. {
  101. throw new ArgumentNullException("result");
  102. }
  103. var optimizedResult = requestContext.ToOptimizedResult(result);
  104. if (responseHeaders != null)
  105. {
  106. // Apply headers
  107. var hasOptions = optimizedResult as IHasOptions;
  108. if (hasOptions != null)
  109. {
  110. AddResponseHeaders(hasOptions, responseHeaders);
  111. }
  112. }
  113. return optimizedResult;
  114. }
  115. /// <summary>
  116. /// Gets the optimized result using cache.
  117. /// </summary>
  118. /// <typeparam name="T"></typeparam>
  119. /// <param name="requestContext">The request context.</param>
  120. /// <param name="cacheKey">The cache key.</param>
  121. /// <param name="lastDateModified">The last date modified.</param>
  122. /// <param name="cacheDuration">Duration of the cache.</param>
  123. /// <param name="factoryFn">The factory fn.</param>
  124. /// <param name="responseHeaders">The response headers.</param>
  125. /// <returns>System.Object.</returns>
  126. /// <exception cref="System.ArgumentNullException">
  127. /// cacheKey
  128. /// or
  129. /// factoryFn
  130. /// </exception>
  131. public object GetOptimizedResultUsingCache<T>(IRequestContext requestContext, Guid cacheKey, DateTime lastDateModified, TimeSpan? cacheDuration, Func<T> factoryFn, IDictionary<string, string> responseHeaders = null)
  132. where T : class
  133. {
  134. if (cacheKey == Guid.Empty)
  135. {
  136. throw new ArgumentNullException("cacheKey");
  137. }
  138. if (factoryFn == null)
  139. {
  140. throw new ArgumentNullException("factoryFn");
  141. }
  142. var key = cacheKey.ToString("N");
  143. if (responseHeaders == null)
  144. {
  145. responseHeaders = new Dictionary<string, string>();
  146. }
  147. // See if the result is already cached in the browser
  148. var result = GetCachedResult(requestContext, responseHeaders, cacheKey, key, lastDateModified, cacheDuration, null);
  149. if (result != null)
  150. {
  151. return result;
  152. }
  153. return GetOptimizedResult(requestContext, factoryFn(), responseHeaders);
  154. }
  155. /// <summary>
  156. /// To the cached result.
  157. /// </summary>
  158. /// <typeparam name="T"></typeparam>
  159. /// <param name="requestContext">The request context.</param>
  160. /// <param name="cacheKey">The cache key.</param>
  161. /// <param name="lastDateModified">The last date modified.</param>
  162. /// <param name="cacheDuration">Duration of the cache.</param>
  163. /// <param name="factoryFn">The factory fn.</param>
  164. /// <param name="contentType">Type of the content.</param>
  165. /// <param name="responseHeaders">The response headers.</param>
  166. /// <returns>System.Object.</returns>
  167. /// <exception cref="System.ArgumentNullException">cacheKey</exception>
  168. public object GetCachedResult<T>(IRequestContext requestContext, Guid cacheKey, DateTime lastDateModified, TimeSpan? cacheDuration, Func<T> factoryFn, string contentType, IDictionary<string, string> responseHeaders = null)
  169. where T : class
  170. {
  171. if (cacheKey == Guid.Empty)
  172. {
  173. throw new ArgumentNullException("cacheKey");
  174. }
  175. if (factoryFn == null)
  176. {
  177. throw new ArgumentNullException("factoryFn");
  178. }
  179. var key = cacheKey.ToString("N");
  180. if (responseHeaders == null)
  181. {
  182. responseHeaders = new Dictionary<string, string>();
  183. }
  184. // See if the result is already cached in the browser
  185. var result = GetCachedResult(requestContext, responseHeaders, cacheKey, key, lastDateModified, cacheDuration, contentType);
  186. if (result != null)
  187. {
  188. return result;
  189. }
  190. result = factoryFn();
  191. // Apply caching headers
  192. var hasOptions = result as IHasOptions;
  193. if (hasOptions != null)
  194. {
  195. AddResponseHeaders(hasOptions, responseHeaders);
  196. return hasOptions;
  197. }
  198. // Otherwise wrap into an HttpResult
  199. var httpResult = new HttpResult(result, contentType ?? "text/html", HttpStatusCode.NotModified);
  200. AddResponseHeaders(httpResult, responseHeaders);
  201. return httpResult;
  202. }
  203. /// <summary>
  204. /// Pres the process optimized result.
  205. /// </summary>
  206. /// <param name="requestContext">The request context.</param>
  207. /// <param name="responseHeaders">The responseHeaders.</param>
  208. /// <param name="cacheKey">The cache key.</param>
  209. /// <param name="cacheKeyString">The cache key string.</param>
  210. /// <param name="lastDateModified">The last date modified.</param>
  211. /// <param name="cacheDuration">Duration of the cache.</param>
  212. /// <param name="contentType">Type of the content.</param>
  213. /// <returns>System.Object.</returns>
  214. private object GetCachedResult(IRequestContext requestContext, IDictionary<string, string> responseHeaders, Guid cacheKey, string cacheKeyString, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType)
  215. {
  216. responseHeaders["ETag"] = cacheKeyString;
  217. if (IsNotModified(requestContext, cacheKey, lastDateModified, cacheDuration))
  218. {
  219. AddAgeHeader(responseHeaders, lastDateModified);
  220. AddExpiresHeader(responseHeaders, cacheKeyString, cacheDuration);
  221. var result = new HttpResult(new byte[] { }, contentType ?? "text/html", HttpStatusCode.NotModified);
  222. AddResponseHeaders(result, responseHeaders);
  223. return result;
  224. }
  225. AddCachingHeaders(responseHeaders, cacheKeyString, lastDateModified, cacheDuration);
  226. return null;
  227. }
  228. /// <summary>
  229. /// Gets the static file result.
  230. /// </summary>
  231. /// <param name="requestContext">The request context.</param>
  232. /// <param name="path">The path.</param>
  233. /// <param name="fileShare">The file share.</param>
  234. /// <param name="responseHeaders">The response headers.</param>
  235. /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
  236. /// <returns>System.Object.</returns>
  237. /// <exception cref="System.ArgumentNullException">path</exception>
  238. public object GetStaticFileResult(IRequestContext requestContext, string path, FileShare fileShare = FileShare.Read, IDictionary<string, string> responseHeaders = null, bool isHeadRequest = false)
  239. {
  240. if (string.IsNullOrEmpty(path))
  241. {
  242. throw new ArgumentNullException("path");
  243. }
  244. if (fileShare != FileShare.Read && fileShare != FileShare.ReadWrite)
  245. {
  246. throw new ArgumentException("FileShare must be either Read or ReadWrite");
  247. }
  248. var dateModified = File.GetLastWriteTimeUtc(path);
  249. var cacheKey = path + dateModified.Ticks;
  250. return GetStaticResult(requestContext, cacheKey.GetMD5(), dateModified, null, MimeTypes.GetMimeType(path), () => Task.FromResult(GetFileStream(path, fileShare)), responseHeaders, isHeadRequest);
  251. }
  252. /// <summary>
  253. /// Gets the file stream.
  254. /// </summary>
  255. /// <param name="path">The path.</param>
  256. /// <param name="fileShare">The file share.</param>
  257. /// <returns>Stream.</returns>
  258. private Stream GetFileStream(string path, FileShare fileShare)
  259. {
  260. return new FileStream(path, FileMode.Open, FileAccess.Read, fileShare, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
  261. }
  262. /// <summary>
  263. /// Gets the static result.
  264. /// </summary>
  265. /// <param name="requestContext">The request context.</param>
  266. /// <param name="cacheKey">The cache key.</param>
  267. /// <param name="lastDateModified">The last date modified.</param>
  268. /// <param name="cacheDuration">Duration of the cache.</param>
  269. /// <param name="contentType">Type of the content.</param>
  270. /// <param name="factoryFn">The factory fn.</param>
  271. /// <param name="responseHeaders">The response headers.</param>
  272. /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
  273. /// <returns>System.Object.</returns>
  274. /// <exception cref="System.ArgumentNullException">cacheKey
  275. /// or
  276. /// factoryFn</exception>
  277. public object GetStaticResult(IRequestContext requestContext, Guid cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration, string contentType, Func<Task<Stream>> factoryFn, IDictionary<string, string> responseHeaders = null, bool isHeadRequest = false)
  278. {
  279. if (cacheKey == Guid.Empty)
  280. {
  281. throw new ArgumentNullException("cacheKey");
  282. }
  283. if (factoryFn == null)
  284. {
  285. throw new ArgumentNullException("factoryFn");
  286. }
  287. var key = cacheKey.ToString("N");
  288. if (responseHeaders == null)
  289. {
  290. responseHeaders = new Dictionary<string, string>();
  291. }
  292. // See if the result is already cached in the browser
  293. var result = GetCachedResult(requestContext, responseHeaders, cacheKey, key, lastDateModified, cacheDuration, contentType);
  294. if (result != null)
  295. {
  296. return result;
  297. }
  298. var compress = ShouldCompressResponse(requestContext, contentType);
  299. var hasOptions = GetStaticResult(requestContext, responseHeaders, contentType, factoryFn, compress, isHeadRequest).Result;
  300. AddResponseHeaders(hasOptions, responseHeaders);
  301. return hasOptions;
  302. }
  303. /// <summary>
  304. /// Shoulds the compress response.
  305. /// </summary>
  306. /// <param name="requestContext">The request context.</param>
  307. /// <param name="contentType">Type of the content.</param>
  308. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  309. private bool ShouldCompressResponse(IRequestContext requestContext, string contentType)
  310. {
  311. // It will take some work to support compression with byte range requests
  312. if (!string.IsNullOrEmpty(requestContext.GetHeader("Range")))
  313. {
  314. return false;
  315. }
  316. // Don't compress media
  317. if (contentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase) || contentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase))
  318. {
  319. return false;
  320. }
  321. // Don't compress images
  322. if (contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
  323. {
  324. return false;
  325. }
  326. if (contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase))
  327. {
  328. return false;
  329. }
  330. if (contentType.StartsWith("application/", StringComparison.OrdinalIgnoreCase))
  331. {
  332. if (string.Equals(contentType, "application/x-javascript", StringComparison.OrdinalIgnoreCase))
  333. {
  334. return true;
  335. }
  336. if (string.Equals(contentType, "application/xml", StringComparison.OrdinalIgnoreCase))
  337. {
  338. return true;
  339. }
  340. return false;
  341. }
  342. return true;
  343. }
  344. /// <summary>
  345. /// The us culture
  346. /// </summary>
  347. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  348. /// <summary>
  349. /// Gets the static result.
  350. /// </summary>
  351. /// <param name="requestContext">The request context.</param>
  352. /// <param name="responseHeaders">The response headers.</param>
  353. /// <param name="contentType">Type of the content.</param>
  354. /// <param name="factoryFn">The factory fn.</param>
  355. /// <param name="compress">if set to <c>true</c> [compress].</param>
  356. /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
  357. /// <returns>Task{IHasOptions}.</returns>
  358. private async Task<IHasOptions> GetStaticResult(IRequestContext requestContext, IDictionary<string, string> responseHeaders, string contentType, Func<Task<Stream>> factoryFn, bool compress, bool isHeadRequest)
  359. {
  360. if (!compress || string.IsNullOrEmpty(requestContext.CompressionType))
  361. {
  362. var stream = await factoryFn().ConfigureAwait(false);
  363. var rangeHeader = requestContext.GetHeader("Range");
  364. if (!string.IsNullOrEmpty(rangeHeader))
  365. {
  366. return new RangeRequestWriter(rangeHeader, stream, contentType, isHeadRequest);
  367. }
  368. responseHeaders["Content-Length"] = stream.Length.ToString(UsCulture);
  369. if (isHeadRequest)
  370. {
  371. return GetHttpResult(new byte[] { }, contentType);
  372. }
  373. return new StreamWriter(stream, contentType, _logger);
  374. }
  375. if (isHeadRequest)
  376. {
  377. return GetHttpResult(new byte[] { }, contentType);
  378. }
  379. string content;
  380. using (var stream = await factoryFn().ConfigureAwait(false))
  381. {
  382. using (var reader = new StreamReader(stream))
  383. {
  384. content = await reader.ReadToEndAsync().ConfigureAwait(false);
  385. }
  386. }
  387. var contents = content.Compress(requestContext.CompressionType);
  388. return new CompressedResult(contents, requestContext.CompressionType, contentType);
  389. }
  390. /// <summary>
  391. /// Adds the caching responseHeaders.
  392. /// </summary>
  393. /// <param name="responseHeaders">The responseHeaders.</param>
  394. /// <param name="cacheKey">The cache key.</param>
  395. /// <param name="lastDateModified">The last date modified.</param>
  396. /// <param name="cacheDuration">Duration of the cache.</param>
  397. private void AddCachingHeaders(IDictionary<string, string> responseHeaders, string cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration)
  398. {
  399. // Don't specify both last modified and Etag, unless caching unconditionally. They are redundant
  400. // https://developers.google.com/speed/docs/best-practices/caching#LeverageBrowserCaching
  401. if (lastDateModified.HasValue && (string.IsNullOrEmpty(cacheKey) || cacheDuration.HasValue))
  402. {
  403. AddAgeHeader(responseHeaders, lastDateModified);
  404. responseHeaders["LastModified"] = lastDateModified.Value.ToString("r");
  405. }
  406. if (cacheDuration.HasValue)
  407. {
  408. responseHeaders["Cache-Control"] = "public, max-age=" + Convert.ToInt32(cacheDuration.Value.TotalSeconds);
  409. }
  410. else if (!string.IsNullOrEmpty(cacheKey))
  411. {
  412. responseHeaders["Cache-Control"] = "public";
  413. }
  414. else
  415. {
  416. responseHeaders["Cache-Control"] = "no-cache, no-store, must-revalidate";
  417. responseHeaders["pragma"] = "no-cache, no-store, must-revalidate";
  418. }
  419. AddExpiresHeader(responseHeaders, cacheKey, cacheDuration);
  420. }
  421. /// <summary>
  422. /// Adds the expires header.
  423. /// </summary>
  424. /// <param name="responseHeaders">The responseHeaders.</param>
  425. /// <param name="cacheKey">The cache key.</param>
  426. /// <param name="cacheDuration">Duration of the cache.</param>
  427. private void AddExpiresHeader(IDictionary<string, string> responseHeaders, string cacheKey, TimeSpan? cacheDuration)
  428. {
  429. if (cacheDuration.HasValue)
  430. {
  431. responseHeaders["Expires"] = DateTime.UtcNow.Add(cacheDuration.Value).ToString("r");
  432. }
  433. else if (string.IsNullOrEmpty(cacheKey))
  434. {
  435. responseHeaders["Expires"] = "-1";
  436. }
  437. }
  438. /// <summary>
  439. /// Adds the age header.
  440. /// </summary>
  441. /// <param name="responseHeaders">The responseHeaders.</param>
  442. /// <param name="lastDateModified">The last date modified.</param>
  443. private void AddAgeHeader(IDictionary<string, string> responseHeaders, DateTime? lastDateModified)
  444. {
  445. if (lastDateModified.HasValue)
  446. {
  447. responseHeaders["Age"] = Convert.ToInt64((DateTime.UtcNow - lastDateModified.Value).TotalSeconds).ToString(CultureInfo.InvariantCulture);
  448. }
  449. }
  450. /// <summary>
  451. /// Determines whether [is not modified] [the specified cache key].
  452. /// </summary>
  453. /// <param name="requestContext">The request context.</param>
  454. /// <param name="cacheKey">The cache key.</param>
  455. /// <param name="lastDateModified">The last date modified.</param>
  456. /// <param name="cacheDuration">Duration of the cache.</param>
  457. /// <returns><c>true</c> if [is not modified] [the specified cache key]; otherwise, <c>false</c>.</returns>
  458. private bool IsNotModified(IRequestContext requestContext, Guid? cacheKey, DateTime? lastDateModified, TimeSpan? cacheDuration)
  459. {
  460. var isNotModified = true;
  461. var ifModifiedSinceHeader = requestContext.GetHeader("If-Modified-Since");
  462. if (!string.IsNullOrEmpty(ifModifiedSinceHeader))
  463. {
  464. DateTime ifModifiedSince;
  465. if (DateTime.TryParse(ifModifiedSinceHeader, out ifModifiedSince))
  466. {
  467. isNotModified = IsNotModified(ifModifiedSince.ToUniversalTime(), cacheDuration, lastDateModified);
  468. }
  469. }
  470. var ifNoneMatchHeader = requestContext.GetHeader("If-None-Match");
  471. // Validate If-None-Match
  472. if (isNotModified && (cacheKey.HasValue || !string.IsNullOrEmpty(ifNoneMatchHeader)))
  473. {
  474. Guid ifNoneMatch;
  475. if (Guid.TryParse(ifNoneMatchHeader ?? string.Empty, out ifNoneMatch))
  476. {
  477. if (cacheKey.HasValue && cacheKey.Value == ifNoneMatch)
  478. {
  479. return true;
  480. }
  481. }
  482. }
  483. return false;
  484. }
  485. /// <summary>
  486. /// Determines whether [is not modified] [the specified if modified since].
  487. /// </summary>
  488. /// <param name="ifModifiedSince">If modified since.</param>
  489. /// <param name="cacheDuration">Duration of the cache.</param>
  490. /// <param name="dateModified">The date modified.</param>
  491. /// <returns><c>true</c> if [is not modified] [the specified if modified since]; otherwise, <c>false</c>.</returns>
  492. private bool IsNotModified(DateTime ifModifiedSince, TimeSpan? cacheDuration, DateTime? dateModified)
  493. {
  494. if (dateModified.HasValue)
  495. {
  496. var lastModified = NormalizeDateForComparison(dateModified.Value);
  497. ifModifiedSince = NormalizeDateForComparison(ifModifiedSince);
  498. return lastModified <= ifModifiedSince;
  499. }
  500. if (cacheDuration.HasValue)
  501. {
  502. var cacheExpirationDate = ifModifiedSince.Add(cacheDuration.Value);
  503. if (DateTime.UtcNow < cacheExpirationDate)
  504. {
  505. return true;
  506. }
  507. }
  508. return false;
  509. }
  510. /// <summary>
  511. /// When the browser sends the IfModifiedDate, it's precision is limited to seconds, so this will account for that
  512. /// </summary>
  513. /// <param name="date">The date.</param>
  514. /// <returns>DateTime.</returns>
  515. private DateTime NormalizeDateForComparison(DateTime date)
  516. {
  517. return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
  518. }
  519. /// <summary>
  520. /// Adds the response headers.
  521. /// </summary>
  522. /// <param name="hasOptions">The has options.</param>
  523. /// <param name="responseHeaders">The response headers.</param>
  524. private void AddResponseHeaders(IHasOptions hasOptions, IEnumerable<KeyValuePair<string, string>> responseHeaders)
  525. {
  526. foreach (var item in responseHeaders)
  527. {
  528. hasOptions.Options[item.Key] = item.Value;
  529. }
  530. }
  531. /// <summary>
  532. /// Gets the error result.
  533. /// </summary>
  534. /// <param name="statusCode">The status code.</param>
  535. /// <param name="errorMessage">The error message.</param>
  536. /// <param name="responseHeaders">The response headers.</param>
  537. /// <returns>System.Object.</returns>
  538. public void ThrowError(int statusCode, string errorMessage, IDictionary<string, string> responseHeaders = null)
  539. {
  540. var error = new HttpError
  541. {
  542. Status = statusCode,
  543. ErrorCode = errorMessage
  544. };
  545. if (responseHeaders != null)
  546. {
  547. AddResponseHeaders(error, responseHeaders);
  548. }
  549. throw error;
  550. }
  551. }
  552. }