1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867 |
- /*
- * Copyright (c) Contributors, http://opensimulator.org/
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the OpenSimulator Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Reflection;
- using System.Globalization;
- using System.Text;
- using System.Threading;
- using System.Xml;
- using HttpServer;
- using log4net;
- using Nwc.XmlRpc;
- using OpenMetaverse.StructuredData;
- using CoolHTTPListener = HttpServer.HttpListener;
- using CoolHttpServer = HttpServer;
- using HttpListener=System.Net.HttpListener;
- namespace OpenSim.Framework.Servers.HttpServer
- {
- public class BaseHttpServer : IHttpServer
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- // private HttpServerLogWriter httpserverlog = new HttpServerLogWriter();
-
- private volatile int NotSocketErrors = 0;
- public volatile bool HTTPDRunning = false;
- protected Thread m_workerThread;
- // protected HttpListener m_httpListener;
- protected CoolHTTPListener m_httpListener2;
- protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
- protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>();
- protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/
- protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>();
- protected Dictionary<string, IRequestHandler> m_streamHandlers = new Dictionary<string, IRequestHandler>();
- protected Dictionary<string, GenericHTTPMethod> m_HTTPHandlers = new Dictionary<string, GenericHTTPMethod>();
- protected Dictionary<string, IHttpAgentHandler> m_agentHandlers = new Dictionary<string, IHttpAgentHandler>();
- protected Dictionary<string, PollServiceEventArgs> m_pollHandlers =
- new Dictionary<string, PollServiceEventArgs>();
- protected uint m_port;
- protected uint m_sslport;
- protected bool m_ssl;
- protected bool m_firstcaps = true;
- protected string m_SSLCommonName = "";
- protected IPAddress m_listenIPAddress = IPAddress.Any;
- private PollServiceRequestManager m_PollServiceManager;
- public uint SSLPort
- {
- get { return m_sslport; }
- }
- public string SSLCommonName
- {
- get { return m_SSLCommonName; }
- }
- public uint Port
- {
- get { return m_port; }
- }
- public bool UseSSL
- {
- get { return m_ssl; }
- }
- public IPAddress ListenIPAddress
- {
- get { return m_listenIPAddress; }
- set { m_listenIPAddress = value; }
- }
- public BaseHttpServer(uint port)
- {
- m_port = port;
- }
- public BaseHttpServer(uint port, bool ssl) : this (port)
- {
- m_ssl = ssl;
- }
- public BaseHttpServer(uint port, bool ssl, uint sslport, string CN) : this (port, ssl)
- {
- if (m_ssl)
- {
- m_sslport = sslport;
- }
- }
- /// <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(IRequestHandler handler)
- {
- string httpMethod = handler.HttpMethod;
- string path = handler.Path;
- string handlerKey = GetHandlerKey(httpMethod, path);
- lock (m_streamHandlers)
- {
- if (!m_streamHandlers.ContainsKey(handlerKey))
- {
- // m_log.DebugFormat("[BASE HTTP SERVER]: Adding handler key {0}", handlerKey);
- m_streamHandlers.Add(handlerKey, handler);
- }
- }
- }
- private static string GetHandlerKey(string httpMethod, string path)
- {
- return httpMethod + ":" + path;
- }
- public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
- {
- return AddXmlRPCHandler(method, handler, true);
- }
- public bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive)
- {
- lock (m_rpcHandlers)
- {
- m_rpcHandlers[method] = handler;
- m_rpcHandlersKeepAlive[method] = keepAlive; // default
- }
-
- return true;
- }
- public XmlRpcMethod GetXmlRPCHandler(string method)
- {
- lock (m_rpcHandlers)
- {
- if (m_rpcHandlers.ContainsKey(method))
- {
- return m_rpcHandlers[method];
- }
- else
- {
- return null;
- }
- }
- }
- public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler)
- {
- //m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName);
-
- lock (m_HTTPHandlers)
- {
- if (!m_HTTPHandlers.ContainsKey(methodName))
- {
- m_HTTPHandlers.Add(methodName, handler);
- return true;
- }
- }
- //must already have a handler for that path so return false
- return false;
- }
- public bool AddPollServiceHTTPHandler(string methodName, GenericHTTPMethod handler, PollServiceEventArgs args)
- {
- bool pollHandlerResult = false;
- lock (m_pollHandlers)
- {
- if (!m_pollHandlers.ContainsKey(methodName))
- {
- m_pollHandlers.Add(methodName,args);
- pollHandlerResult = true;
-
- }
- }
-
- if (pollHandlerResult)
- return AddHTTPHandler(methodName, handler);
- return false;
-
- }
- // Note that the agent string is provided simply to differentiate
- // the handlers - it is NOT required to be an actual agent header
- // value.
- public bool AddAgentHandler(string agent, IHttpAgentHandler handler)
- {
- lock (m_agentHandlers)
- {
- if (!m_agentHandlers.ContainsKey(agent))
- {
- m_agentHandlers.Add(agent, handler);
- return true;
- }
- }
- //must already have a handler for that path so return false
- return false;
- }
- public bool AddLLSDHandler(string path, LLSDMethod handler)
- {
- lock (m_llsdHandlers)
- {
- if (!m_llsdHandlers.ContainsKey(path))
- {
- m_llsdHandlers.Add(path, handler);
- return true;
- }
- }
- return false;
- }
- public bool SetDefaultLLSDHandler(DefaultLLSDMethod handler)
- {
- m_defaultLlsdHandler = handler;
- return true;
- }
- private void OnRequest(object source, RequestEventArgs args)
- {
- try
- {
- IHttpClientContext context = (IHttpClientContext)source;
- IHttpRequest request = args.Request;
-
- PollServiceEventArgs psEvArgs;
-
- if (TryGetPollServiceHTTPHandler(request.UriPath.ToString(), out psEvArgs))
- {
- PollServiceHttpRequest psreq = new PollServiceHttpRequest(psEvArgs, context, request);
-
- if (psEvArgs.Request != null)
- {
- OSHttpRequest req = new OSHttpRequest(context, request);
-
- Stream requestStream = req.InputStream;
-
- Encoding encoding = Encoding.UTF8;
- StreamReader reader = new StreamReader(requestStream, encoding);
-
- string requestBody = reader.ReadToEnd();
-
- Hashtable keysvals = new Hashtable();
- Hashtable headervals = new Hashtable();
-
- string[] querystringkeys = req.QueryString.AllKeys;
- string[] rHeaders = req.Headers.AllKeys;
-
- keysvals.Add("body", requestBody);
- keysvals.Add("uri", req.RawUrl);
- keysvals.Add("content-type", req.ContentType);
- keysvals.Add("http-method", req.HttpMethod);
-
- foreach (string queryname in querystringkeys)
- {
- keysvals.Add(queryname, req.QueryString[queryname]);
- }
-
- foreach (string headername in rHeaders)
- {
- headervals[headername] = req.Headers[headername];
- }
-
- keysvals.Add("headers",headervals);
- keysvals.Add("querystringkeys", querystringkeys);
-
- psEvArgs.Request(psreq.RequestID, keysvals);
- }
-
- m_PollServiceManager.Enqueue(psreq);
- }
- else
- {
- OnHandleRequestIOThread(context, request);
- }
- }
- catch (Exception e)
- {
- m_log.ErrorFormat("[BASE HTTP SERVER]: OnRequest() failed with {0} {1}", e.Message, e.StackTrace);
- }
- }
- public void OnHandleRequestIOThread(IHttpClientContext context, IHttpRequest request)
- {
- OSHttpRequest req = new OSHttpRequest(context, request);
- OSHttpResponse resp = new OSHttpResponse(new HttpResponse(context, request),context);
- HandleRequest(req, resp);
- }
- public void ConvertIHttpClientContextToOSHttp(object stateinfo)
- {
- HttpServerContextObj objstate = (HttpServerContextObj)stateinfo;
- OSHttpRequest request = objstate.oreq;
- OSHttpResponse resp = objstate.oresp;
- HandleRequest(request,resp);
- }
- public virtual void HandleRequest(OSHttpRequest request, OSHttpResponse response)
- {
- try
- {
- Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", true);
- // This is the REST agent interface. We require an agent to properly identify
- // itself. If the REST handler recognizes the prefix it will attempt to
- // satisfy the request. If it is not recognizable, and no damage has occurred
- // the request can be passed through to the other handlers. This is a low
- // probability event; if a request is matched it is normally expected to be
- // handled
- //m_log.Debug("[BASE HTTP SERVER]: Handling Request" + request.RawUrl);
- IHttpAgentHandler agentHandler;
- if (TryGetAgentHandler(request, response, out agentHandler))
- {
- if (HandleAgentRequest(agentHandler, request, response))
- {
- return;
- }
- }
- //response.KeepAlive = true;
- response.SendChunked = false;
- IRequestHandler requestHandler;
- string path = request.RawUrl;
- string handlerKey = GetHandlerKey(request.HttpMethod, path);
- //m_log.DebugFormat("[BASE HTTP SERVER]: Handling {0} request for {1}", request.HttpMethod, path);
- if (TryGetStreamHandler(handlerKey, out requestHandler))
- {
- //m_log.Debug("[BASE HTTP SERVER]: Found Stream Handler");
- // Okay, so this is bad, but should be considered temporary until everything is IStreamHandler.
- byte[] buffer = null;
- response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type.
- if (requestHandler is IStreamedRequestHandler)
- {
- IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler;
- buffer = streamedRequestHandler.Handle(path, request.InputStream, request, response);
- }
- else if (requestHandler is IGenericHTTPHandler)
- {
- //m_log.Debug("[BASE HTTP SERVER]: Found Caps based HTTP Handler");
- IGenericHTTPHandler HTTPRequestHandler = requestHandler as IGenericHTTPHandler;
- Stream requestStream = request.InputStream;
- Encoding encoding = Encoding.UTF8;
- StreamReader reader = new StreamReader(requestStream, encoding);
- string requestBody = reader.ReadToEnd();
- reader.Close();
- //requestStream.Close();
- Hashtable keysvals = new Hashtable();
- Hashtable headervals = new Hashtable();
- //string host = String.Empty;
- string[] querystringkeys = request.QueryString.AllKeys;
- string[] rHeaders = request.Headers.AllKeys;
- foreach (string queryname in querystringkeys)
- {
- keysvals.Add(queryname, request.QueryString[queryname]);
- }
- foreach (string headername in rHeaders)
- {
- //m_log.Warn("[HEADER]: " + headername + "=" + request.Headers[headername]);
- headervals[headername] = request.Headers[headername];
- }
- // if (headervals.Contains("Host"))
- // {
- // host = (string)headervals["Host"];
- // }
- keysvals.Add("requestbody", requestBody);
- keysvals.Add("headers",headervals);
- if (keysvals.Contains("method"))
- {
- //m_log.Warn("[HTTP]: Contains Method");
- //string method = (string)keysvals["method"];
- //m_log.Warn("[HTTP]: " + requestBody);
- }
- DoHTTPGruntWork(HTTPRequestHandler.Handle(path, keysvals), response);
- return;
- }
- else
- {
- IStreamHandler streamHandler = (IStreamHandler)requestHandler;
- using (MemoryStream memoryStream = new MemoryStream())
- {
- streamHandler.Handle(path, request.InputStream, memoryStream, request, response);
- memoryStream.Flush();
- buffer = memoryStream.ToArray();
- }
- }
- request.InputStream.Close();
-
- // HTTP IN support. The script engine taes it from here
- // Nothing to worry about for us.
- //
- if (buffer == null)
- return;
- if (!response.SendChunked)
- response.ContentLength64 = buffer.LongLength;
- try
- {
- response.OutputStream.Write(buffer, 0, buffer.Length);
- //response.OutputStream.Close();
- }
- catch (HttpListenerException)
- {
- m_log.WarnFormat("[BASE HTTP SERVER]: HTTP request abnormally terminated.");
- }
- //response.OutputStream.Close();
- try
- {
- response.Send();
- //response.FreeContext();
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- catch (IOException e)
- {
- m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
- }
- return;
- }
- if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
- {
- foreach (string strAccept in request.AcceptTypes)
- {
- if (strAccept.Contains("application/llsd+xml") ||
- strAccept.Contains("application/llsd+json"))
- {
- //m_log.Info("[Debug BASE HTTP SERVER]: Found an application/llsd+xml accept header");
- HandleLLSDRequests(request, response);
- return;
- }
- }
- }
- switch (request.ContentType)
- {
- case null:
- case "text/html":
- //m_log.Info("[Debug BASE HTTP SERVER]: found a text/html content type");
- HandleHTTPRequest(request, response);
- return;
- case "application/llsd+xml":
- case "application/xml+llsd":
- case "application/llsd+json":
- //m_log.Info("[Debug BASE HTTP SERVER]: found a application/llsd+xml content type");
- HandleLLSDRequests(request, response);
- return;
- case "text/xml":
- case "application/xml":
- case "application/json":
- default:
- //m_log.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;
- // }
- //m_log.Info("[Debug BASE HTTP SERVER]: Checking for LLSD Handler");
- if (DoWeHaveALLSDHandler(request.RawUrl))
- {
- //m_log.Info("[Debug BASE HTTP SERVER]: Found LLSD Handler");
- HandleLLSDRequests(request, response);
- return;
- }
- //m_log.Info("[Debug BASE HTTP SERVER]: Checking for HTTP Handler");
- if (DoWeHaveAHTTPHandler(request.RawUrl))
- {
- //m_log.Info("[Debug BASE HTTP SERVER]: found HTTP Handler");
- HandleHTTPRequest(request, response);
- return;
- }
- //m_log.Info("[Debug BASE HTTP SERVER]: Generic XMLRPC");
- // generic login request.
- HandleXmlRpcRequests(request, response);
- return;
- }
- }
- catch (SocketException e)
- {
- // At least on linux, it appears that if the client makes a request without requiring the response,
- // an unconnected socket exception is thrown when we close the response output stream. There's no
- // obvious way to tell if the client didn't require the response, so instead we'll catch and ignore
- // the exception instead.
- //
- // An alternative may be to turn off all response write exceptions on the HttpListener, but let's go
- // with the minimum first
- m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e);
- }
- catch (IOException e)
- {
- m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw ", e);
- }
- catch (InvalidOperationException e)
- {
- m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0}", e);
- SendHTML500(response);
- }
- }
- private bool TryGetStreamHandler(string handlerKey, out IRequestHandler streamHandler)
- {
- string bestMatch = null;
- lock (m_streamHandlers)
- {
- 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;
- }
- else
- {
- streamHandler = m_streamHandlers[bestMatch];
- return true;
- }
- }
- }
- private bool TryGetPollServiceHTTPHandler(string handlerKey, out PollServiceEventArgs oServiceEventArgs)
- {
- string bestMatch = null;
- lock (m_pollHandlers)
- {
- foreach (string pattern in m_pollHandlers.Keys)
- {
- if (handlerKey.StartsWith(pattern))
- {
- if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
- {
- bestMatch = pattern;
- }
- }
- }
- if (String.IsNullOrEmpty(bestMatch))
- {
- oServiceEventArgs = null;
- return false;
- }
- else
- {
- oServiceEventArgs = m_pollHandlers[bestMatch];
- return true;
- }
- }
- }
- private bool TryGetHTTPHandler(string handlerKey, out GenericHTTPMethod HTTPHandler)
- {
- //m_log.DebugFormat("[BASE HTTP HANDLER]: Looking for HTTP handler for {0}", handlerKey);
-
- string bestMatch = null;
- lock (m_HTTPHandlers)
- {
- foreach (string pattern in m_HTTPHandlers.Keys)
- {
- if (handlerKey.StartsWith(pattern))
- {
- if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
- {
- bestMatch = pattern;
- }
- }
- }
- if (String.IsNullOrEmpty(bestMatch))
- {
- HTTPHandler = null;
- return false;
- }
- else
- {
- HTTPHandler = m_HTTPHandlers[bestMatch];
- return true;
- }
- }
- }
- private bool TryGetAgentHandler(OSHttpRequest request, OSHttpResponse response, out IHttpAgentHandler agentHandler)
- {
- agentHandler = null;
- try
- {
- foreach (IHttpAgentHandler handler in m_agentHandlers.Values)
- {
- if (handler.Match(request, response))
- {
- agentHandler = handler;
- return true;
- }
- }
- }
- catch(KeyNotFoundException)
- {
- }
- return false;
- }
- /// <summary>
- /// Try all the registered xmlrpc handlers when an xmlrpc request is received.
- /// Sends back an XMLRPC unknown request response if no handler is registered for the requested method.
- /// </summary>
- /// <param name="request"></param>
- /// <param name="response"></param>
- private void HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response)
- {
- Stream requestStream = request.InputStream;
- Encoding encoding = Encoding.UTF8;
- StreamReader reader = new StreamReader(requestStream, encoding);
- string requestBody = reader.ReadToEnd();
- reader.Close();
- requestStream.Close();
- //m_log.Debug(requestBody);
- requestBody = requestBody.Replace("<base64></base64>", "");
- string responseString = String.Empty;
- XmlRpcRequest xmlRprcRequest = null;
- try
- {
- xmlRprcRequest = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody);
- }
- catch (XmlException e)
- {
- // Don't log this for now since HandleXmlRpcRequests() acts as the final processing catch all for all requests
- // that aren't dealt with by another method. Enabling can result in false positives, for instance
- //
- // 14:59:12 - [BASE HTTP SERVER]: Ignoring XMLRPC request from 80.175.122.121:20478
- // due to deserialization exception Data at the root level is invalid. Line 1, pos
- // ition 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e)
- // 14:59:12 - [BASE HTTP SERVER]: Request body auth_token=x3EA9wjAwTjuJwawE6PGqhQ%3
- // D%3D%3A0%3A9303959503950%3A%3A
- //
- // on the region simulator when the client makes a request that we don't deal with
- //
- // m_log.WarnFormat(
- // "[BASE HTTP SERVER]: Ignoring XMLRPC request from {0} due to deserialization exception {1} {2}",
- // request.RemoteIPEndPoint, e.Message, e.StackTrace);
- }
- if (xmlRprcRequest != null)
- {
- string methodName = xmlRprcRequest.MethodName;
- if (methodName != null)
- {
- xmlRprcRequest.Params.Add(request.RemoteIPEndPoint); // Param[1]
- XmlRpcResponse xmlRpcResponse;
-
- XmlRpcMethod method;
- bool methodWasFound;
- lock (m_rpcHandlers)
- {
- methodWasFound = m_rpcHandlers.TryGetValue(methodName, out method);
- }
-
- if (methodWasFound)
- {
- xmlRprcRequest.Params.Add(request.Url); // Param[2]
- try
- {
- xmlRpcResponse = method(xmlRprcRequest, request.RemoteIPEndPoint);
- }
- catch(Exception e)
- {
- string errorMessage
- = String.Format(
- "Requested method [{0}] from {1} threw exception: {2} {3}",
- methodName, request.RemoteIPEndPoint.Address, e.Message, e.StackTrace);
- m_log.ErrorFormat("[BASE HTTP SERVER]: {0}", errorMessage);
- // if the registered XmlRpc method threw an exception, we pass a fault-code along
- xmlRpcResponse = new XmlRpcResponse();
- // Code probably set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
- xmlRpcResponse.SetFault(-32603, errorMessage);
- }
- // if the method wasn't found, we can't determine KeepAlive state anyway, so lets do it only here
- response.KeepAlive = m_rpcHandlersKeepAlive[methodName];
- }
- else
- {
- xmlRpcResponse = new XmlRpcResponse();
-
- // Code set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
- xmlRpcResponse.SetFault(
- XmlRpcErrorCodes.SERVER_ERROR_METHOD,
- String.Format("Requested method [{0}] not found", methodName));
- }
- responseString = XmlRpcResponseSerializer.Singleton.Serialize(xmlRpcResponse);
- }
- else
- {
- //HandleLLSDRequests(request, response);
- response.ContentType = "text/plain";
- response.StatusCode = 404;
- response.StatusDescription = "Not Found";
- response.ProtocolVersion = "HTTP/1.0";
- byte[] buf = Encoding.UTF8.GetBytes("Not found");
- response.KeepAlive = false;
- m_log.ErrorFormat("[BASE HTTP SERVER]: Handler not found for http request {0}", request.RawUrl);
-
- response.SendChunked = false;
- response.ContentLength64 = buf.Length;
- response.ContentEncoding = Encoding.UTF8;
-
- try
- {
- response.OutputStream.Write(buf, 0, buf.Length);
- }
- catch (Exception ex)
- {
- m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
- }
- finally
- {
- try
- {
- response.Send();
- //response.FreeContext();
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- catch (IOException e)
- {
- m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
- }
- }
- return;
- //responseString = "Error";
- }
- }
- response.ContentType = "text/xml";
- byte[] buffer = Encoding.UTF8.GetBytes(responseString);
- response.SendChunked = false;
- response.ContentLength64 = buffer.Length;
- response.ContentEncoding = Encoding.UTF8;
- try
- {
- response.OutputStream.Write(buffer, 0, buffer.Length);
- }
- catch (Exception ex)
- {
- m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
- }
- finally
- {
- try
- {
- response.Send();
- //response.FreeContext();
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- catch (IOException e)
- {
- m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
- }
- }
- }
- private void HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response)
- {
- //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request");
- Stream requestStream = request.InputStream;
- Encoding encoding = Encoding.UTF8;
- StreamReader reader = new StreamReader(requestStream, encoding);
- string requestBody = reader.ReadToEnd();
- reader.Close();
- requestStream.Close();
- //m_log.DebugFormat("[OGP]: {0}:{1}", request.RawUrl, requestBody);
- response.KeepAlive = true;
- OSD llsdRequest = null;
- OSD llsdResponse = null;
-
- bool LegacyLLSDLoginLibOMV = (requestBody.Contains("passwd") && requestBody.Contains("mac") && requestBody.Contains("viewer_digest"));
-
- if (requestBody.Length == 0)
- // Get Request
- {
- requestBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd><map><key>request</key><string>get</string></map></llsd>";
- }
- try
- {
- llsdRequest = OSDParser.Deserialize(requestBody);
- }
- catch (Exception ex)
- {
- m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
- }
- if (llsdRequest != null)// && m_defaultLlsdHandler != null)
- {
- LLSDMethod llsdhandler = null;
- if (TryGetLLSDHandler(request.RawUrl, out llsdhandler) && !LegacyLLSDLoginLibOMV)
- {
- // we found a registered llsd handler to service this request
- llsdResponse = llsdhandler(request.RawUrl, llsdRequest, request.RemoteIPEndPoint.ToString());
- }
- else
- {
- // we didn't find a registered llsd handler to service this request
- // check if we have a default llsd handler
- if (m_defaultLlsdHandler != null)
- {
- // LibOMV path
- llsdResponse = m_defaultLlsdHandler(llsdRequest, request.RemoteIPEndPoint);
- }
- else
- {
- // Oops, no handler for this.. give em the failed message
- llsdResponse = GenerateNoLLSDHandlerResponse();
- }
- }
- }
- else
- {
- llsdResponse = GenerateNoLLSDHandlerResponse();
- }
- byte[] buffer = new byte[0];
- if (llsdResponse.ToString() == "shutdown404!")
- {
- response.ContentType = "text/plain";
- response.StatusCode = 404;
- response.StatusDescription = "Not Found";
- response.ProtocolVersion = "HTTP/1.0";
- buffer = Encoding.UTF8.GetBytes("Not found");
- }
- else
- {
- // Select an appropriate response format
- buffer = BuildLLSDResponse(request, response, llsdResponse);
- }
- response.SendChunked = false;
- response.ContentLength64 = buffer.Length;
- response.ContentEncoding = Encoding.UTF8;
- response.KeepAlive = true;
- try
- {
- response.OutputStream.Write(buffer, 0, buffer.Length);
- }
- catch (Exception ex)
- {
- m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
- }
- finally
- {
- //response.OutputStream.Close();
- try
- {
- response.Send();
- response.OutputStream.Flush();
- //response.FreeContext();
- //response.OutputStream.Close();
- }
- catch (IOException e)
- {
- m_log.DebugFormat("[BASE HTTP SERVER]: LLSD IOException {0}.", e);
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER]: LLSD issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- }
- }
- private byte[] BuildLLSDResponse(OSHttpRequest request, OSHttpResponse response, OSD llsdResponse)
- {
- if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
- {
- foreach (string strAccept in request.AcceptTypes)
- {
- switch (strAccept)
- {
- case "application/llsd+xml":
- case "application/xml":
- case "text/xml":
- response.ContentType = strAccept;
- return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
- case "application/llsd+json":
- case "application/json":
- response.ContentType = strAccept;
- return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
- }
- }
- }
- if (!String.IsNullOrEmpty(request.ContentType))
- {
- switch (request.ContentType)
- {
- case "application/llsd+xml":
- case "application/xml":
- case "text/xml":
- response.ContentType = request.ContentType;
- return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
- case "application/llsd+json":
- case "application/json":
- response.ContentType = request.ContentType;
- return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
- }
- }
- // response.ContentType = "application/llsd+json";
- // return Util.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
- response.ContentType = "application/llsd+xml";
- return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
- }
- /// <summary>
- /// Checks if we have an Exact path in the LLSD handlers for the path provided
- /// </summary>
- /// <param name="path">URI of the request</param>
- /// <returns>true if we have one, false if not</returns>
- private bool DoWeHaveALLSDHandler(string path)
- {
- string[] pathbase = path.Split('/');
- string searchquery = "/";
- if (pathbase.Length < 1)
- return false;
- for (int i = 1; i < pathbase.Length; i++)
- {
- searchquery += pathbase[i];
- if (pathbase.Length - 1 != i)
- searchquery += "/";
- }
- string bestMatch = null;
- foreach (string pattern in m_llsdHandlers.Keys)
- {
- if (searchquery.StartsWith(pattern) && searchquery.Length >= pattern.Length)
- {
- bestMatch = pattern;
- }
- }
- // extra kicker to remove the default XMLRPC login case.. just in case..
- if (path != "/" && bestMatch == "/" && searchquery != "/")
- return false;
- if (path == "/")
- return false;
- if (String.IsNullOrEmpty(bestMatch))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- /// <summary>
- /// Checks if we have an Exact path in the HTTP handlers for the path provided
- /// </summary>
- /// <param name="path">URI of the request</param>
- /// <returns>true if we have one, false if not</returns>
- private bool DoWeHaveAHTTPHandler(string path)
- {
- string[] pathbase = path.Split('/');
- string searchquery = "/";
- if (pathbase.Length < 1)
- return false;
- for (int i = 1; i < pathbase.Length; i++)
- {
- searchquery += pathbase[i];
- if (pathbase.Length - 1 != i)
- searchquery += "/";
- }
- string bestMatch = null;
- //m_log.DebugFormat("[BASE HTTP HANDLER]: Checking if we have an HTTP handler for {0}", searchquery);
-
- lock (m_HTTPHandlers)
- {
- foreach (string pattern in m_HTTPHandlers.Keys)
- {
- if (searchquery.StartsWith(pattern) && searchquery.Length >= pattern.Length)
- {
- bestMatch = pattern;
- }
- }
- // extra kicker to remove the default XMLRPC login case.. just in case..
- if (path == "/")
- return false;
- if (String.IsNullOrEmpty(bestMatch))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- private bool TryGetLLSDHandler(string path, out LLSDMethod llsdHandler)
- {
- llsdHandler = null;
- // Pull out the first part of the path
- // splitting the path by '/' means we'll get the following return..
- // {0}/{1}/{2}
- // where {0} isn't something we really control 100%
- string[] pathbase = path.Split('/');
- string searchquery = "/";
- if (pathbase.Length < 1)
- return false;
- for (int i=1; i<pathbase.Length; i++)
- {
- searchquery += pathbase[i];
- if (pathbase.Length-1 != i)
- searchquery += "/";
- }
- // while the matching algorithm below doesn't require it, we're expecting a query in the form
- //
- // [] = optional
- // /resource/UUID/action[/action]
- //
- // now try to get the closest match to the reigstered path
- // at least for OGP, registered path would probably only consist of the /resource/
- string bestMatch = null;
- foreach (string pattern in m_llsdHandlers.Keys)
- {
- if (searchquery.ToLower().StartsWith(pattern.ToLower()))
- {
- if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
- {
- // You have to specifically register for '/' and to get it, you must specificaly request it
- //
- if (pattern == "/" && searchquery == "/" || pattern != "/")
- bestMatch = pattern;
- }
- }
- }
- if (String.IsNullOrEmpty(bestMatch))
- {
- llsdHandler = null;
- return false;
- }
- else
- {
- llsdHandler = m_llsdHandlers[bestMatch];
- return true;
- }
- }
- private OSDMap GenerateNoLLSDHandlerResponse()
- {
- OSDMap map = new OSDMap();
- map["reason"] = OSD.FromString("LLSDRequest");
- map["message"] = OSD.FromString("No handler registered for LLSD Requests");
- map["login"] = OSD.FromString("false");
- return map;
- }
- /// <summary>
- /// A specific agent handler was provided. Such a handler is expecetd to have an
- /// intimate, and highly specific relationship with the client. Consequently,
- /// nothing is done here.
- /// </summary>
- /// <param name="handler"></param>
- /// <param name="request"></param>
- /// <param name="response"></param>
- private bool HandleAgentRequest(IHttpAgentHandler handler, OSHttpRequest request, OSHttpResponse response)
- {
- // In the case of REST, then handler is responsible for ALL aspects of
- // the request/response handling. Nothing is done here, not even encoding.
- try
- {
- return handler.Handle(request, response);
- }
- catch (Exception e)
- {
- // If the handler did in fact close the stream, then this will blow
- // chunks. So that that doesn't disturb anybody we throw away any
- // and all exceptions raised. We've done our best to release the
- // client.
- try
- {
- m_log.Warn("[HTTP-AGENT]: Error - " + e.Message);
- response.SendChunked = false;
- response.KeepAlive = true;
- response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError;
- //response.OutputStream.Close();
- try
- {
- response.Send();
- //response.FreeContext();
- }
- catch (SocketException f)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat(
- "[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", f);
- }
- }
- catch(Exception)
- {
- }
- }
- // Indicate that the request has been "handled"
- return true;
- }
- public void HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response)
- {
- switch (request.HttpMethod)
- {
- case "OPTIONS":
- response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
- return;
- default:
- HandleContentVerbs(request, response);
- return;
- }
- }
- private void HandleContentVerbs(OSHttpRequest request, OSHttpResponse response)
- {
- // This is a test. There's a workable alternative.. as this way sucks.
- // We'd like to put this into a text file parhaps that's easily editable.
- //
- // For this test to work, I used the following secondlife.exe parameters
- // "C:\Program Files\SecondLifeWindLight\SecondLifeWindLight.exe" -settings settings_windlight.xml -channel "Second Life WindLight" -set SystemLanguage en-us -loginpage http://10.1.1.2:8002/?show_login_form=TRUE -loginuri http://10.1.1.2:8002 -user 10.1.1.2
- //
- // Even after all that, there's still an error, but it's a start.
- //
- // I depend on show_login_form being in the secondlife.exe parameters to figure out
- // to display the form, or process it.
- // a better way would be nifty.
- Stream requestStream = request.InputStream;
- Encoding encoding = Encoding.UTF8;
- StreamReader reader = new StreamReader(requestStream, encoding);
- string requestBody = reader.ReadToEnd();
- // avoid warning for now
- reader.ReadToEnd();
- reader.Close();
- requestStream.Close();
- Hashtable keysvals = new Hashtable();
- Hashtable headervals = new Hashtable();
- Hashtable requestVars = new Hashtable();
- string host = String.Empty;
- string[] querystringkeys = request.QueryString.AllKeys;
- string[] rHeaders = request.Headers.AllKeys;
- keysvals.Add("body", requestBody);
- keysvals.Add("uri", request.RawUrl);
- keysvals.Add("content-type", request.ContentType);
- keysvals.Add("http-method", request.HttpMethod);
- foreach (string queryname in querystringkeys)
- {
- keysvals.Add(queryname, request.QueryString[queryname]);
- requestVars.Add(queryname, keysvals[queryname]);
- }
- foreach (string headername in rHeaders)
- {
- //m_log.Warn("[HEADER]: " + headername + "=" + request.Headers[headername]);
- headervals[headername] = request.Headers[headername];
- }
- if (headervals.Contains("Host"))
- {
- host = (string)headervals["Host"];
- }
- keysvals.Add("headers",headervals);
- keysvals.Add("querystringkeys", querystringkeys);
- keysvals.Add("requestvars", requestVars);
- if (keysvals.Contains("method"))
- {
- //m_log.Warn("[HTTP]: Contains Method");
- string method = (string) keysvals["method"];
- //m_log.Warn("[HTTP]: " + requestBody);
- GenericHTTPMethod requestprocessor;
- bool foundHandler = TryGetHTTPHandler(method, out requestprocessor);
- if (foundHandler)
- {
- Hashtable responsedata1 = requestprocessor(keysvals);
- DoHTTPGruntWork(responsedata1,response);
- //SendHTML500(response);
- }
- else
- {
- //m_log.Warn("[HTTP]: Handler Not Found");
- SendHTML404(response, host);
- }
- }
- else
- {
- GenericHTTPMethod requestprocessor;
- bool foundHandler = TryGetHTTPHandlerPathBased(request.RawUrl, out requestprocessor);
- if (foundHandler)
- {
- Hashtable responsedata2 = requestprocessor(keysvals);
- DoHTTPGruntWork(responsedata2, response);
- //SendHTML500(response);
- }
- else
- {
- //m_log.Warn("[HTTP]: Handler Not Found");
- SendHTML404(response, host);
- }
- }
- }
- private bool TryGetHTTPHandlerPathBased(string path, out GenericHTTPMethod httpHandler)
- {
- httpHandler = null;
- // Pull out the first part of the path
- // splitting the path by '/' means we'll get the following return..
- // {0}/{1}/{2}
- // where {0} isn't something we really control 100%
- string[] pathbase = path.Split('/');
- string searchquery = "/";
- if (pathbase.Length < 1)
- return false;
- for (int i = 1; i < pathbase.Length; i++)
- {
- searchquery += pathbase[i];
- if (pathbase.Length - 1 != i)
- searchquery += "/";
- }
- // while the matching algorithm below doesn't require it, we're expecting a query in the form
- //
- // [] = optional
- // /resource/UUID/action[/action]
- //
- // now try to get the closest match to the reigstered path
- // at least for OGP, registered path would probably only consist of the /resource/
- string bestMatch = null;
- // m_log.DebugFormat(
- // "[BASE HTTP HANDLER]: TryGetHTTPHandlerPathBased() looking for HTTP handler to match {0}", searchquery);
-
- lock (m_HTTPHandlers)
- {
- foreach (string pattern in m_HTTPHandlers.Keys)
- {
- if (searchquery.ToLower().StartsWith(pattern.ToLower()))
- {
- if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
- {
- // You have to specifically register for '/' and to get it, you must specificaly request it
- //
- if (pattern == "/" && searchquery == "/" || pattern != "/")
- bestMatch = pattern;
- }
- }
- }
- if (String.IsNullOrEmpty(bestMatch))
- {
- httpHandler = null;
- return false;
- }
- else
- {
- if (bestMatch == "/" && searchquery != "/")
- return false;
- httpHandler = m_HTTPHandlers[bestMatch];
- return true;
- }
- }
- }
- internal void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
- {
- //m_log.Info("[BASE HTTP SERVER]: Doing HTTP Grunt work with response");
- int responsecode = (int)responsedata["int_response_code"];
- string responseString = (string)responsedata["str_response_string"];
- string contentType = (string)responsedata["content_type"];
- if (responsedata.ContainsKey("error_status_text"))
- {
- response.StatusDescription = (string)responsedata["error_status_text"];
- }
- if (responsedata.ContainsKey("http_protocol_version"))
- {
- response.ProtocolVersion = (string)responsedata["http_protocol_version"];
- }
- if (responsedata.ContainsKey("keepalive"))
- {
- bool keepalive = (bool)responsedata["keepalive"];
- response.KeepAlive = keepalive;
- }
- if (responsedata.ContainsKey("reusecontext"))
- response.ReuseContext = (bool) responsedata["reusecontext"];
- //Even though only one other part of the entire code uses HTTPHandlers, we shouldn't expect this
- //and should check for NullReferenceExceptions
-
- if (string.IsNullOrEmpty(contentType))
- {
- contentType = "text/html";
- }
- // The client ignores anything but 200 here for web login, so ensure that this is 200 for that
- response.StatusCode = responsecode;
- if (responsecode == (int)OSHttpStatusCode.RedirectMovedPermanently)
- {
- response.RedirectLocation = (string)responsedata["str_redirect_location"];
- response.StatusCode = responsecode;
- }
- response.AddHeader("Content-Type", contentType);
- byte[] buffer;
- if (!(contentType.Contains("image")
- || contentType.Contains("x-shockwave-flash")
- || contentType.Contains("application/x-oar")))
- {
- // Text
- buffer = Encoding.UTF8.GetBytes(responseString);
- }
- else
- {
- // Binary!
- buffer = Convert.FromBase64String(responseString);
- }
- response.SendChunked = false;
- response.ContentLength64 = buffer.Length;
- response.ContentEncoding = Encoding.UTF8;
- try
- {
- response.OutputStream.Write(buffer, 0, buffer.Length);
- }
- catch (Exception ex)
- {
- m_log.Warn("[HTTPD]: Error - " + ex.Message);
- }
- finally
- {
- //response.OutputStream.Close();
- try
- {
- response.OutputStream.Flush();
- response.Send();
-
- //if (!response.KeepAlive && response.ReuseContext)
- // response.FreeContext();
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- catch (IOException e)
- {
- m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
- }
- }
- }
- public void SendHTML404(OSHttpResponse response, string host)
- {
- // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
- response.StatusCode = 404;
- response.AddHeader("Content-type", "text/html");
- string responseString = GetHTTP404(host);
- byte[] buffer = Encoding.UTF8.GetBytes(responseString);
- response.SendChunked = false;
- response.ContentLength64 = buffer.Length;
- response.ContentEncoding = Encoding.UTF8;
- try
- {
- response.OutputStream.Write(buffer, 0, buffer.Length);
- }
- catch (Exception ex)
- {
- m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
- }
- finally
- {
- //response.OutputStream.Close();
- try
- {
- response.Send();
- //response.FreeContext();
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- }
- }
- public void SendHTML500(OSHttpResponse response)
- {
- // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
- response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
- response.AddHeader("Content-type", "text/html");
- string responseString = GetHTTP500();
- byte[] buffer = Encoding.UTF8.GetBytes(responseString);
- response.SendChunked = false;
- response.ContentLength64 = buffer.Length;
- response.ContentEncoding = Encoding.UTF8;
- try
- {
- response.OutputStream.Write(buffer, 0, buffer.Length);
- }
- catch (Exception ex)
- {
- m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
- }
- finally
- {
- //response.OutputStream.Close();
- try
- {
- response.Send();
- //response.FreeContext();
- }
- catch (SocketException e)
- {
- // This has to be here to prevent a Linux/Mono crash
- m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
- }
- }
- }
- public void Start()
- {
- m_log.Info("[BASE HTTP SERVER]: Starting up HTTP Server");
- StartHTTP();
- }
- private void StartHTTP()
- {
- try
- {
- m_log.Debug("[BASE HTTP SERVER]: Spawned main thread OK");
- //m_httpListener = new HttpListener();
- NotSocketErrors = 0;
- if (!m_ssl)
- {
- //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
- //m_httpListener.Prefixes.Add("http://10.1.1.5:" + m_port + "/");
- m_httpListener2 = CoolHTTPListener.Create(m_listenIPAddress, (int)m_port);
- m_httpListener2.ExceptionThrown += httpServerException;
- // Uncomment this line (and the httpserverlog field) for HttpServer.dll logging
- //m_httpListener2.LogWriter = httpserverlog;
-
- // Uncomment this line in addition to those in HttpServerLogWriter
- // if you want more detailed trace information from the HttpServer
- //m_httpListener2.UseTraceLogs = true;
-
- //m_httpListener2.DisconnectHandler = httpServerDisconnectMonitor;
- }
- else
- {
- //m_httpListener.Prefixes.Add("https://+:" + (m_sslport) + "/");
- //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
- }
- m_httpListener2.RequestReceived += OnRequest;
- //m_httpListener.Start();
- m_httpListener2.Start(64);
- // Long Poll Service Manager with 3 worker threads a 25 second timeout for no events
- m_PollServiceManager = new PollServiceRequestManager(this, 3, 25000);
- HTTPDRunning = true;
- //HttpListenerContext context;
- //while (true)
- //{
- // context = m_httpListener.GetContext();
- // ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(HandleRequest), context);
- // }
- }
- catch (Exception e)
- {
- m_log.Error("[BASE HTTP SERVER]: Error - " + e.Message);
- m_log.Error("[BASE HTTP SERVER]: Tip: Do you have permission to listen on port " + m_port + ", " + m_sslport + "?");
-
- // We want this exception to halt the entire server since in current configurations we aren't too
- // useful without inbound HTTP.
- throw e;
- }
- }
- public void httpServerDisconnectMonitor(IHttpClientContext source, SocketError err)
- {
- switch (err)
- {
- case SocketError.NotSocket:
- NotSocketErrors++;
-
- break;
- }
- }
- public void httpServerException(object source, Exception exception)
- {
- m_log.ErrorFormat("[BASE HTTP SERVER]: {0} had an exception {1}", source.ToString(), exception.ToString());
- /*
- if (HTTPDRunning)// && NotSocketErrors > 5)
- {
- Stop();
- Thread.Sleep(200);
- StartHTTP();
- m_log.Warn("[HTTPSERVER]: Died. Trying to kick.....");
- }
- */
- }
- public void Stop()
- {
- HTTPDRunning = false;
- try
- {
- m_httpListener2.ExceptionThrown -= httpServerException;
- //m_httpListener2.DisconnectHandler = null;
- m_httpListener2.LogWriter = null;
- m_httpListener2.RequestReceived -= OnRequest;
- m_httpListener2.Stop();
- }
- catch (NullReferenceException)
- {
- m_log.Warn("[BASE HTTP SERVER]: Null Reference when stopping HttpServer.");
- }
-
- }
- public void RemoveStreamHandler(string httpMethod, string path)
- {
- string handlerKey = GetHandlerKey(httpMethod, path);
- //m_log.DebugFormat("[BASE HTTP SERVER]: Removing handler key {0}", handlerKey);
- lock (m_streamHandlers) m_streamHandlers.Remove(handlerKey);
- }
- public void RemoveHTTPHandler(string httpMethod, string path)
- {
- lock (m_HTTPHandlers)
- {
- if (httpMethod != null && httpMethod.Length == 0)
- {
- m_HTTPHandlers.Remove(path);
- return;
- }
-
- m_HTTPHandlers.Remove(GetHandlerKey(httpMethod, path));
- }
- }
- public void RemovePollServiceHTTPHandler(string httpMethod, string path)
- {
- lock (m_pollHandlers)
- {
- if (m_pollHandlers.ContainsKey(httpMethod))
- {
- m_pollHandlers.Remove(httpMethod);
- }
- }
- RemoveHTTPHandler(httpMethod, path);
-
- }
- public bool RemoveAgentHandler(string agent, IHttpAgentHandler handler)
- {
- try
- {
- if (handler == m_agentHandlers[agent])
- {
- m_agentHandlers.Remove(agent);
- return true;
- }
- }
- catch(KeyNotFoundException)
- {
- }
- return false;
- }
- public void RemoveXmlRPCHandler(string method)
- {
- lock (m_rpcHandlers)
- {
- if (m_rpcHandlers.ContainsKey(method))
- {
- m_rpcHandlers.Remove(method);
- }
- }
- }
-
- public bool RemoveLLSDHandler(string path, LLSDMethod handler)
- {
- try
- {
- if (handler == m_llsdHandlers[path])
- {
- m_llsdHandlers.Remove(path);
- return true;
- }
- }
- catch (KeyNotFoundException)
- {
- // This is an exception to prevent crashing because of invalid code
- }
- return false;
- }
- public string GetHTTP404(string host)
- {
- string file = Path.Combine(".", "http_404.html");
- if (!File.Exists(file))
- return getDefaultHTTP404(host);
- StreamReader sr = File.OpenText(file);
- string result = sr.ReadToEnd();
- sr.Close();
- return result;
- }
- public string GetHTTP500()
- {
- string file = Path.Combine(".", "http_500.html");
- if (!File.Exists(file))
- return getDefaultHTTP500();
- StreamReader sr = File.OpenText(file);
- string result = sr.ReadToEnd();
- sr.Close();
- return result;
- }
- // Fallback HTTP responses in case the HTTP error response files don't exist
- private static string getDefaultHTTP404(string host)
- {
- return "<HTML><HEAD><TITLE>404 Page not found</TITLE><BODY><BR /><H1>Ooops!</H1><P>The page you requested has been obsconded with by knomes. Find hippos quick!</P><P>If you are trying to log-in, your link parameters should have: "-loginpage http://" + host + "/?method=login -loginuri http://" + host + "/" in your link </P></BODY></HTML>";
- }
- private static string getDefaultHTTP500()
- {
- return "<HTML><HEAD><TITLE>500 Internal Server Error</TITLE><BODY><BR /><H1>Ooops!</H1><P>The server you requested is overun by knomes! Find hippos quick!</P></BODY></HTML>";
- }
- }
- public class HttpServerContextObj
- {
- public IHttpClientContext context = null;
- public IHttpRequest req = null;
- public OSHttpRequest oreq = null;
- public OSHttpResponse oresp = null;
- public HttpServerContextObj(IHttpClientContext contxt, IHttpRequest reqs)
- {
- context = contxt;
- req = reqs;
- }
- public HttpServerContextObj(OSHttpRequest osreq, OSHttpResponse osresp)
- {
- oreq = osreq;
- oresp = osresp;
- }
- }
-
- /// <summary>
- /// Relays HttpServer log messages to our own logging mechanism.
- /// </summary>
- /// There is also a UseTraceLogs line in this file that can be uncommented for more detailed log information
- public class HttpServerLogWriter : ILogWriter
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- public void Write(object source, LogPrio priority, string message)
- {
- switch (priority)
- {
- case CoolHttpServer.LogPrio.Debug:
- m_log.DebugFormat("[{0}]: {1}", source.ToString(), message);
- break;
- case CoolHttpServer.LogPrio.Error:
- m_log.ErrorFormat("[{0}]: {1}", source.ToString(), message);
- break;
- case CoolHttpServer.LogPrio.Info:
- m_log.InfoFormat("[{0}]: {1}", source.ToString(), message);
- break;
- case CoolHttpServer.LogPrio.Warning:
- m_log.WarnFormat("[{0}]: {1}", source.ToString(), message);
- break;
- case CoolHttpServer.LogPrio.Fatal:
- m_log.ErrorFormat("[{0}]: FATAL! - {1}", source.ToString(), message);
- break;
- default:
- break;
- }
-
- return;
- }
- }
- }
|