HttpRequestExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using MediaBrowser.Model.Services;
  4. using ServiceStack.Host;
  5. namespace ServiceStack
  6. {
  7. public static class HttpRequestExtensions
  8. {
  9. /**
  10. *
  11. Input: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?q=item#fragment
  12. Some HttpRequest path and URL properties:
  13. Request.ApplicationPath: /Cambia3
  14. Request.CurrentExecutionFilePath: /Cambia3/Temp/Test.aspx
  15. Request.FilePath: /Cambia3/Temp/Test.aspx
  16. Request.Path: /Cambia3/Temp/Test.aspx/path/info
  17. Request.PathInfo: /path/info
  18. Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
  19. Request.QueryString: /Cambia3/Temp/Test.aspx/path/info?query=arg
  20. Request.Url.AbsolutePath: /Cambia3/Temp/Test.aspx/path/info
  21. Request.Url.AbsoluteUri: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?query=arg
  22. Request.Url.Fragment:
  23. Request.Url.Host: localhost
  24. Request.Url.LocalPath: /Cambia3/Temp/Test.aspx/path/info
  25. Request.Url.PathAndQuery: /Cambia3/Temp/Test.aspx/path/info?query=arg
  26. Request.Url.Port: 96
  27. Request.Url.Query: ?query=arg
  28. Request.Url.Scheme: http
  29. Request.Url.Segments: /
  30. Cambia3/
  31. Temp/
  32. Test.aspx/
  33. path/
  34. info
  35. * */
  36. /// <summary>
  37. /// Duplicate Params are given a unique key by appending a #1 suffix
  38. /// </summary>
  39. public static Dictionary<string, string> GetRequestParams(this IRequest request)
  40. {
  41. var map = new Dictionary<string, string>();
  42. foreach (var name in request.QueryString.Keys)
  43. {
  44. if (name == null) continue; //thank you ASP.NET
  45. var values = request.QueryString.GetValues(name);
  46. if (values.Length == 1)
  47. {
  48. map[name] = values[0];
  49. }
  50. else
  51. {
  52. for (var i = 0; i < values.Length; i++)
  53. {
  54. map[name + (i == 0 ? "" : "#" + i)] = values[i];
  55. }
  56. }
  57. }
  58. if ((request.Verb == HttpMethods.Post || request.Verb == HttpMethods.Put)
  59. && request.FormData != null)
  60. {
  61. foreach (var name in request.FormData.Keys)
  62. {
  63. if (name == null) continue; //thank you ASP.NET
  64. var values = request.FormData.GetValues(name);
  65. if (values.Length == 1)
  66. {
  67. map[name] = values[0];
  68. }
  69. else
  70. {
  71. for (var i = 0; i < values.Length; i++)
  72. {
  73. map[name + (i == 0 ? "" : "#" + i)] = values[i];
  74. }
  75. }
  76. }
  77. }
  78. return map;
  79. }
  80. /// <summary>
  81. /// Duplicate params have their values joined together in a comma-delimited string
  82. /// </summary>
  83. public static Dictionary<string, string> GetFlattenedRequestParams(this IRequest request)
  84. {
  85. var map = new Dictionary<string, string>();
  86. foreach (var name in request.QueryString.Keys)
  87. {
  88. if (name == null) continue; //thank you ASP.NET
  89. map[name] = request.QueryString[name];
  90. }
  91. if ((request.Verb == HttpMethods.Post || request.Verb == HttpMethods.Put)
  92. && request.FormData != null)
  93. {
  94. foreach (var name in request.FormData.Keys)
  95. {
  96. if (name == null) continue; //thank you ASP.NET
  97. map[name] = request.FormData[name];
  98. }
  99. }
  100. return map;
  101. }
  102. public static void SetRoute(this IRequest req, RestPath route)
  103. {
  104. req.Items["__route"] = route;
  105. }
  106. public static RestPath GetRoute(this IRequest req)
  107. {
  108. object route;
  109. req.Items.TryGetValue("__route", out route);
  110. return route as RestPath;
  111. }
  112. }
  113. }