OSHttpServer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Net;
  29. using System.Net.Sockets;
  30. using System.Reflection;
  31. using System.Threading;
  32. using System.Security.Cryptography.X509Certificates;
  33. using log4net;
  34. using HttpServer;
  35. using HttpListener = HttpServer.HttpListener;
  36. namespace OpenSim.Framework.Servers
  37. {
  38. /// <summary>
  39. /// OSHttpServer provides an HTTP server bound to a specific
  40. /// port. When instantiated with just address and port it uses
  41. /// normal HTTP, when instantiated with address, port, and X509
  42. /// certificate, it uses HTTPS.
  43. /// </summary>
  44. public class OSHttpServer
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. // underlying HttpServer.HttpListener
  48. protected HttpListener _listener;
  49. protected Thread _engine;
  50. // OSHttpRequestPumps "pumping" incoming OSHttpRequests
  51. // upwards
  52. protected OSHttpRequestPump[] _pumps;
  53. // thread identifier
  54. protected string _engineId;
  55. public string EngineID
  56. {
  57. get { return _engineId; }
  58. }
  59. /// <summary>
  60. /// True if this is an HTTPS connection; false otherwise.
  61. /// </summary>
  62. protected bool _isSecure;
  63. public bool IsSecure
  64. {
  65. get { return _isSecure; }
  66. }
  67. /// <summary>
  68. /// Instantiate an HTTP server.
  69. /// </summary>
  70. public OSHttpServer(IPAddress address, int port, int poolSize)
  71. {
  72. _engineId = String.Format("OSHttpServer [HTTP:{0}/ps:{1}]", port, poolSize);
  73. _isSecure = false;
  74. _pumps = OSHttpRequestPump.Pumps(this, poolSize);
  75. }
  76. /// <summary>
  77. /// Instantiate an HTTPS server.
  78. /// </summary>
  79. public OSHttpServer(IPAddress address, int port, X509Certificate certificate, int poolSize) :
  80. this(address, port, poolSize)
  81. {
  82. _engineId = String.Format("OSHttpServer [HTTPS:{0}/ps:{1}]", port, poolSize);
  83. _isSecure = true;
  84. }
  85. /// <summary>
  86. /// Start the HTTP server engine.
  87. /// </summary>
  88. public void Start()
  89. {
  90. _engine = new Thread(new ThreadStart(Engine));
  91. _engine.Name = _engineId;
  92. _engine.IsBackground = true;
  93. _engine.Start();
  94. ThreadTracker.Add(_engine);
  95. }
  96. /// <summary>
  97. /// </summary>
  98. private void Engine()
  99. {
  100. while (true)
  101. {
  102. // do stuff
  103. }
  104. }
  105. }
  106. }