DiscoveryRequestHandler.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // System.Web.Services.Discovery.DiscoveryRequestHandler.cs
  3. //
  4. // Author:
  5. // Dave Bettin ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Dave Bettin, 2002
  9. //
  10. using System.Web;
  11. using System.IO;
  12. using System.Web.Services.Discovery;
  13. namespace System.Web.Services.Discovery {
  14. public sealed class DiscoveryRequestHandler : IHttpHandler {
  15. #region Constructors
  16. public DiscoveryRequestHandler ()
  17. {
  18. }
  19. #endregion // Constructors
  20. #region Properties
  21. public bool IsReusable {
  22. get { return true; }
  23. }
  24. #endregion // Properties
  25. #region Methods
  26. public void ProcessRequest (HttpContext context)
  27. {
  28. string path = context.Request.PhysicalPath;
  29. FileStream fs = new FileStream (path, FileMode.Open);
  30. DynamicDiscoveryDocument ddoc = DynamicDiscoveryDocument.Load (fs);
  31. fs.Close ();
  32. string url = context.Request.Url.ToString();
  33. int i = url.LastIndexOf ('/');
  34. if (i != -1) url = url.Substring (0,i+1);
  35. DiscoveryDocument doc = new DiscoveryDocument ();
  36. GetFiles (url, "", Path.GetDirectoryName(path), doc, ddoc);
  37. context.Response.ContentType = "text/xml; charset=utf-8";
  38. doc.Write (context.Response.OutputStream);
  39. }
  40. void GetFiles (string baseUrl, string relPath, string path, DiscoveryDocument doc, DynamicDiscoveryDocument ddoc)
  41. {
  42. string url = baseUrl + relPath;
  43. if (!url.EndsWith ("/")) url += "/";
  44. string[] files = Directory.GetFiles (path);
  45. foreach (string file in files)
  46. {
  47. string ext = Path.GetExtension (file).ToLower();
  48. if (ext == ".asmx")
  49. {
  50. ContractReference cref = new ContractReference ();
  51. cref.DocRef = url + Path.GetFileName (file);
  52. cref.Ref = cref.DocRef + "?wsdl";
  53. doc.References.Add (cref);
  54. }
  55. else if (ext == ".disco")
  56. {
  57. DiscoveryDocumentReference dref = new DiscoveryDocumentReference ();
  58. dref.Ref = url + Path.GetFileName (file);
  59. doc.References.Add (dref);
  60. }
  61. }
  62. string[] dirs = Directory.GetDirectories (path);
  63. foreach (string dir in dirs)
  64. {
  65. string rel = Path.Combine (relPath, Path.GetFileName(dir));
  66. if (!ddoc.IsExcluded (rel))
  67. GetFiles (baseUrl, rel, Path.Combine (path, dir), doc, ddoc);
  68. }
  69. }
  70. #endregion // Methods
  71. }
  72. }