internal bool TryGetStreamHandler(string handlerKey, out IStreamedRequestHandler streamHandler) { string bestMatch = null; lock (m_streamHandlers) { if (m_streamHandlers.TryGetValue(handlerKey, out streamHandler)) { return(true); } foreach (string pattern in m_streamHandlers.Keys) { if (handlerKey.StartsWith(pattern)) { if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length) { bestMatch = pattern; } } } if (String.IsNullOrEmpty(bestMatch)) { streamHandler = null; return(false); } streamHandler = m_streamHandlers[bestMatch]; return(true); } }
/// <summary> /// Add a stream handler to the http server. If the handler already exists, then nothing happens. /// </summary> /// <param name="handler"></param> public void AddStreamHandler (IStreamedRequestHandler handler) { string httpMethod = handler.HttpMethod; string path = handler.Path; string handlerKey = GetHandlerKey (httpMethod, path); lock (m_streamHandlers) { if (!m_streamHandlers.ContainsKey (handlerKey)) { // MainConsole.Instance.DebugFormat("[Base HTTP Server]: Adding handler key {0}", handlerKey); m_streamHandlers.Add (handlerKey, handler); } } }
public bool AddHTTPHandler(IStreamedRequestHandler handler) { //MainConsole.Instance.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName); lock (m_HTTPStreamHandlers) { if (!m_HTTPStreamHandlers.ContainsKey(handler.Path)) { m_HTTPStreamHandlers.Add(handler.Path, handler); return(true); } } //must already have a handler for that path so return false return(false); }
internal bool TryGetStreamHandler(string handlerKey, out IStreamedRequestHandler streamHandler) { string bestMatch = null; lock (m_streamHandlers) { if (m_streamHandlers.TryGetValue(handlerKey, out streamHandler)) return true; foreach (string pattern in m_streamHandlers.Keys) { if (handlerKey.StartsWith(pattern)) { if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length) { bestMatch = pattern; } } } if (String.IsNullOrEmpty(bestMatch)) { streamHandler = null; return false; } streamHandler = m_streamHandlers[bestMatch]; return true; } }
/// <summary> /// Add a stream handler to the http server. If the handler already exists, then nothing happens. /// </summary> /// <param name="handler"></param> public void AddStreamHandler(IStreamedRequestHandler handler) { string httpMethod = handler.HttpMethod; string path = handler.Path; string handlerKey = GetHandlerKey(httpMethod, path); lock (m_streamHandlers) { if (!m_streamHandlers.ContainsKey(handlerKey)) { // MainConsole.Instance.DebugFormat("[BASE HTTP SERVER]: Adding handler key {0}", handlerKey); m_streamHandlers.Add(handlerKey, handler); } } }
private void LogIncomingToStreamHandler(OSHttpRequest request, IStreamedRequestHandler requestHandler) { MainConsole.Instance.DebugFormat( "[BASE HTTP SERVER]: HTTP IN :{0} stream handler {1} {2} from {3}", Port, request.HttpMethod, request.Url.PathAndQuery, request.RemoteIPEndPoint); if (DebugLevel >= 5) LogIncomingInDetail(request); }
public bool AddHTTPHandler(IStreamedRequestHandler handler) { //MainConsole.Instance.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName); lock (m_HTTPStreamHandlers) { if (!m_HTTPStreamHandlers.ContainsKey(handler.Path)) { m_HTTPStreamHandlers.Add(handler.Path, handler); return true; } } //must already have a handler for that path so return false return false; }
/// <summary> /// This methods is the start of incoming HTTP request handling. /// </summary> /// <param name="context"></param> public virtual void HandleRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; using (HttpListenerResponse response = context.Response) { OSHttpRequest req = new OSHttpRequest(context); OSHttpResponse resp = new OSHttpResponse(context); if (request.HttpMethod == String.Empty) // Can't handle empty requests, not wasting a thread { byte[] buffer = GetHTML500(response); response.ContentLength64 = buffer.LongLength; response.Close(buffer, true); return; } response.KeepAlive = false; string requestMethod = request.HttpMethod; string uriString = request.RawUrl; int requestStartTick = Environment.TickCount; // Will be adjusted later on. int requestEndTick = requestStartTick; IStreamedRequestHandler requestHandler = null; try { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US", true); string path = request.RawUrl; string handlerKey = GetHandlerKey(request.HttpMethod, path); byte[] buffer = null; if ((request.ContentType == "application/xml" || request.ContentType == "text/xml") && GetXmlRPCHandler(request.RawUrl) != null) { buffer = HandleXmlRpcRequests(req, resp); } else if (TryGetStreamHandler(handlerKey, out requestHandler)) { response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type. buffer = requestHandler.Handle(path, request.InputStream, req, resp); } request.InputStream.Close(); try { if (buffer != null) { if (request.ProtocolVersion.Minor == 0) { //HTTP 1.0... no chunking response.ContentLength64 = buffer.Length; using (Stream stream = response.OutputStream) { HttpServerHandlerHelpers.WriteNonChunked(stream, buffer); } } else { response.SendChunked = true; using (Stream stream = response.OutputStream) { HttpServerHandlerHelpers.WriteChunked(stream, buffer); } } //response.ContentLength64 = buffer.LongLength; response.Close(); } else { response.Close(new byte[0], true); } } catch (Exception ex) { if (!(ex is HttpListenerException) || !HttpListenerManager.IGNORE_ERROR_CODES.Contains(((HttpListenerException)ex).ErrorCode)) { MainConsole.Instance.WarnFormat( "[BASE HTTP SERVER]: HandleRequest failed to write all data to the stream: {0}", ex.ToString()); } response.Abort(); } requestEndTick = Environment.TickCount; } catch (Exception e) { MainConsole.Instance.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0} ", e.ToString()); response.Abort(); } finally { // Every month or so this will wrap and give bad numbers, not really a problem // since its just for reporting int tickdiff = requestEndTick - requestStartTick; if (tickdiff > 3000 && requestHandler != null) { MainConsole.Instance.InfoFormat( "[BASE HTTP SERVER]: Slow handling of {0} {1} took {2}ms", requestMethod, uriString, tickdiff); } else if (MainConsole.Instance.IsTraceEnabled) { MainConsole.Instance.TraceFormat( "[BASE HTTP SERVER]: Handling {0} {1} took {2}ms", requestMethod, uriString, tickdiff); } } } }
private static void HandleStreamHandler(OSHttpRequest request, OSHttpResponse response, string path, ref int respcontentLength, IStreamedRequestHandler requestHandler) { byte[] buffer = null; response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type. try { IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler; buffer = streamedRequestHandler.Handle(path, request.InputStream, request, response); } catch (Exception ex) { MainConsole.Instance.WarnFormat("[BASE HTTP SERVER]: HTTP handler threw an exception " + ex + "."); } if (request.InputStream != null) request.InputStream.Dispose(); if (buffer == null) { if (response.OutputStream.CanWrite) { response.StatusCode = (int)HttpStatusCode.InternalServerError; buffer = Encoding.UTF8.GetBytes("Internal Server Error"); } else return;//The handler took care of sending it for us } else if (buffer == MainServer.BadRequest) { response.StatusCode = (int)HttpStatusCode.BadRequest; buffer = Encoding.UTF8.GetBytes("Bad Request"); } respcontentLength = buffer.Length; try { if (buffer != MainServer.NoResponse) response.OutputStream.Write(buffer, 0, buffer.Length); response.Send(); } catch (SocketException e) { // This has to be here to prevent a Linux/Mono crash MainConsole.Instance.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e); } catch (HttpListenerException) { MainConsole.Instance.WarnFormat("[BASE HTTP SERVER]: HTTP request abnormally terminated."); } catch (IOException e) { MainConsole.Instance.Debug("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e); } finally { buffer = null; } }
/// <summary> /// This methods is the start of incoming HTTP request handling. /// </summary> /// <param name="context"></param> public virtual void HandleRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; OSHttpRequest req = new OSHttpRequest(context); OSHttpResponse resp = new OSHttpResponse(context); if (request.HttpMethod == String.Empty) // Can't handle empty requests, not wasting a thread { byte[] buffer = GetHTML500(response); response.ContentLength64 = buffer.LongLength; response.Close(buffer, true); return; } string requestMethod = request.HttpMethod; string uriString = request.RawUrl; string remoteIP = request.RemoteEndPoint.ToString(); int requestStartTick = Environment.TickCount; // Will be adjusted later on. int requestEndTick = requestStartTick; IStreamedRequestHandler requestHandler = null; try { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US", true); string path = request.RawUrl; string handlerKey = GetHandlerKey(request.HttpMethod, path); byte[] buffer = null; if (TryGetStreamHandler(handlerKey, out requestHandler)) { response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type. buffer = requestHandler.Handle(path, request.InputStream, req, resp); } else { switch (request.ContentType) { case null: case "text/html": buffer = HandleHTTPRequest(req, resp); break; case "text/xml": case "application/xml": case "application/json": default: //MainConsole.Instance.Info("[Debug BASE HTTP SERVER]: in default handler"); // Point of note.. the DoWeHaveA methods check for an EXACT path // if (request.RawUrl.Contains("/CAPS/EQG")) // { // int i = 1; // } //MainConsole.Instance.Info("[Debug BASE HTTP SERVER]: Checking for LLSD Handler"); if (GetXmlRPCHandler(request.RawUrl) != null) { // generic login request. buffer = HandleXmlRpcRequests(req, resp); } // MainConsole.Instance.DebugFormat("[BASE HTTP SERVER]: Checking for HTTP Handler for request {0}", request.RawUrl); else if (DoWeHaveAHTTPHandler(request.RawUrl)) { buffer = HandleHTTPRequest(req, resp); } else { // generic login request. buffer = HandleXmlRpcRequests(req, resp); } break; } } request.InputStream.Close(); try { if (buffer != null) { response.ContentLength64 = buffer.LongLength; response.Close(buffer, true); } } catch (Exception ex) { MainConsole.Instance.WarnFormat( "[BASE HTTP SERVER]: HandleRequest failed to write all data to the stream: {0}", ex.ToString()); } requestEndTick = Environment.TickCount; } catch (Exception e) { MainConsole.Instance.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0} ", e.ToString()); response.Close(new byte[0], false); } finally { // Every month or so this will wrap and give bad numbers, not really a problem // since its just for reporting int tickdiff = requestEndTick - requestStartTick; if (tickdiff > 3000 && requestHandler != null) { MainConsole.Instance.InfoFormat( "[BASE HTTP SERVER]: Slow handling of {0} {1} from {2} took {3}ms", requestMethod, uriString, remoteIP, tickdiff); } else if (MainConsole.Instance.IsTraceEnabled) { MainConsole.Instance.TraceFormat( "[BASE HTTP SERVER]: Handling {0} {1} from {2} took {3}ms", requestMethod, uriString, remoteIP, tickdiff); } } }
public void AddStreamHandler(string method, IStreamedRequestHandler handler) { Server.AddStreamHandler(handler); AddCAPS(method, handler.Path); }