RestHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // RestHandler.cs
  3. //
  4. // Author:
  5. // Konstantin Triger <[email protected]>
  6. //
  7. // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Web.Script.Serialization;
  33. using System.Collections.Specialized;
  34. using System.IO;
  35. namespace System.Web.Script.Services
  36. {
  37. class RestHandler : IHttpHandler
  38. {
  39. sealed class NameValueCollectionDictionary : JavaScriptSerializer.LazyDictionary
  40. {
  41. readonly NameValueCollection _nmc;
  42. public NameValueCollectionDictionary (NameValueCollection nmc) {
  43. _nmc = nmc;
  44. }
  45. protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator () {
  46. for (int i = 0, max = _nmc.Count; i < max; i++)
  47. yield return new KeyValuePair<string, object> (_nmc.GetKey (i), _nmc.Get (i));
  48. }
  49. }
  50. readonly LogicalTypeInfo _logicalTypeInfo;
  51. public RestHandler (Type type, string filePath) {
  52. _logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (type, filePath);
  53. }
  54. #region IHttpHandler Members
  55. public bool IsReusable {
  56. get { return false; }
  57. }
  58. public void ProcessRequest (HttpContext context) {
  59. HttpRequest request = context.Request;
  60. HttpResponse response = context.Response;
  61. response.ContentType = "application/json";
  62. string method = request.PathInfo.Substring(1);
  63. if ("GET".Equals (request.RequestType, StringComparison.OrdinalIgnoreCase))
  64. _logicalTypeInfo.Invoke (method,
  65. new NameValueCollectionDictionary (request.QueryString),
  66. response.Output);
  67. else
  68. _logicalTypeInfo.Invoke(method,
  69. new StreamReader(request.InputStream, request.ContentEncoding),
  70. response.Output);
  71. }
  72. /*
  73. static Encoding GetContentEncoding (string cts, out string content_type) {
  74. string encoding;
  75. if (cts == null)
  76. cts = "";
  77. encoding = "utf-8";
  78. int start = 0;
  79. int idx = cts.IndexOf (';');
  80. if (idx == -1)
  81. content_type = cts;
  82. else
  83. content_type = cts.Substring (0, idx);
  84. content_type = content_type.Trim ();
  85. for (start = idx + 1; idx != -1; ) {
  86. idx = cts.IndexOf (';', start);
  87. string body;
  88. if (idx == -1)
  89. body = cts.Substring (start);
  90. else {
  91. body = cts.Substring (start, idx - start);
  92. start = idx + 1;
  93. }
  94. body = body.Trim ();
  95. if (String.CompareOrdinal (body, 0, "charset=", 0, 8) == 0) {
  96. encoding = body.Substring (8);
  97. encoding = encoding.TrimStart (trimChars).TrimEnd (trimChars);
  98. }
  99. }
  100. return Encoding.GetEncoding (encoding);
  101. }*/
  102. static readonly char [] trimChars = { '"', '\'' };
  103. #endregion
  104. }
  105. }