29 using System.Collections.Generic;
31 using System.Net.Sockets;
32 using System.Reflection;
33 using System.Text.RegularExpressions;
34 using System.Threading;
35 using System.Security.Cryptography.X509Certificates;
41 namespace OpenSim.Framework.Servers.HttpServer
51 private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53 private object _syncObject =
new object();
69 public string EngineID
71 get {
return _engineId; }
80 get {
return _isSecure; }
85 get {
return _pumps.Length; }
91 protected List<OSHttpHandler> _httpHandlers =
new List<OSHttpHandler>();
92 public List<OSHttpHandler> OSHttpHandlers
98 return new List<OSHttpHandler>(_httpHandlers);
109 _engineId = String.Format(
"OSHttpServer (HTTP:{0})", port);
111 _log.DebugFormat(
"[{0}] HTTP server instantiated", EngineID);
115 _pumps = OSHttpRequestPump.Pumps(
this, _queue, poolSize);
121 public OSHttpServer(IPAddress address,
int port, X509Certificate certificate,
int poolSize)
123 _engineId = String.Format(
"OSHttpServer [HTTPS:{0}/ps:{1}]", port, poolSize);
125 _log.DebugFormat(
"[{0}] HTTPS server instantiated", EngineID);
127 _listener =
new HttpListener(address, port, certificate);
129 _pumps = OSHttpRequestPump.Pumps(
this, _queue, poolSize);
152 _engine =
new Thread(
new ThreadStart(Engine));
153 _engine.IsBackground =
true;
155 _engine.Name = string.Format (
"Engine:{0}",_engineId);
157 ThreadTracker.Add(_engine);
160 for (
int i = 0; i < _pumps.Length; i++)
166 lock (_syncObject) Monitor.Pulse(_syncObject);
172 private void Engine()
175 _listener.RequestHandler += OnHttpRequest;
176 _listener.Start(QueueSize);
177 _log.InfoFormat(
"[{0}] HTTP server started", EngineID);
179 lock (_syncObject) Monitor.Wait(_syncObject);
183 _log.DebugFormat(
"[{0}] HTTP server startup failed: {1}", EngineID, ex.ToString());
186 _log.InfoFormat(
"[{0}] HTTP server terminated", EngineID);
201 if (_httpHandlers.Contains(handler))
203 _log.DebugFormat(
"[OSHttpServer] attempt to add already existing handler ignored");
206 _httpHandlers.Add(handler);
HttpServer.HttpListener HttpListener
OSHttpServer(IPAddress address, int port, int poolSize)
Instantiate an HTTP server.
void AddHandler(OSHttpHandler handler)
Add an HTTP request handler.
OSHttpServer(IPAddress address, int port, X509Certificate certificate, int poolSize)
Instantiate an HTTPS server.
OSHttpRequestPump[] _pumps
OSHttpRequestQueue _queue
An OSHttpRequestPump fetches incoming OSHttpRequest objects from the OSHttpRequestQueue and feeds the...
bool _isSecure
True if this is an HTTPS connection; false otherwise.
OSHttpServer provides an HTTP server bound to a specific port. When instantiated with just address an...
void OnHttpRequest(HttpClientContext client, HttpRequest request)
Turn an HttpRequest into an OSHttpRequestItem and place it in the queue. The OSHttpRequestQueue objec...
void Start()
Start the HTTP server engine.
OSHttpRequestQueues are used to hand over incoming HTTP requests to OSHttpRequestPump objects...
System.Net.HttpListener HttpListener