BaseHttpServer.cs 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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 OpenSimulator 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.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Reflection;
  36. using System.Globalization;
  37. using System.Text;
  38. using System.Threading;
  39. using System.Xml;
  40. using HttpServer;
  41. using log4net;
  42. using Nwc.XmlRpc;
  43. using OpenMetaverse.StructuredData;
  44. using CoolHTTPListener = HttpServer.HttpListener;
  45. using HttpListener=System.Net.HttpListener;
  46. using LogPrio=HttpServer.LogPrio;
  47. namespace OpenSim.Framework.Servers.HttpServer
  48. {
  49. public class BaseHttpServer : IHttpServer
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private HttpServerLogWriter httpserverlog = new HttpServerLogWriter();
  53. private volatile int NotSocketErrors = 0;
  54. public volatile bool HTTPDRunning = false;
  55. // protected HttpListener m_httpListener;
  56. protected CoolHTTPListener m_httpListener2;
  57. protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
  58. protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>();
  59. protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/
  60. protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>();
  61. protected Dictionary<string, IRequestHandler> m_streamHandlers = new Dictionary<string, IRequestHandler>();
  62. protected Dictionary<string, GenericHTTPMethod> m_HTTPHandlers = new Dictionary<string, GenericHTTPMethod>();
  63. protected Dictionary<string, IHttpAgentHandler> m_agentHandlers = new Dictionary<string, IHttpAgentHandler>();
  64. protected Dictionary<string, PollServiceEventArgs> m_pollHandlers =
  65. new Dictionary<string, PollServiceEventArgs>();
  66. protected uint m_port;
  67. protected uint m_sslport;
  68. protected bool m_ssl;
  69. private X509Certificate2 m_cert;
  70. protected bool m_firstcaps = true;
  71. protected string m_SSLCommonName = "";
  72. protected IPAddress m_listenIPAddress = IPAddress.Any;
  73. private PollServiceRequestManager m_PollServiceManager;
  74. /// <summary>
  75. /// Control the printing of certain debug messages.
  76. /// </summary>
  77. public int DebugLevel { get; set; }
  78. public uint SSLPort
  79. {
  80. get { return m_sslport; }
  81. }
  82. public string SSLCommonName
  83. {
  84. get { return m_SSLCommonName; }
  85. }
  86. public uint Port
  87. {
  88. get { return m_port; }
  89. }
  90. public bool UseSSL
  91. {
  92. get { return m_ssl; }
  93. }
  94. public IPAddress ListenIPAddress
  95. {
  96. get { return m_listenIPAddress; }
  97. set { m_listenIPAddress = value; }
  98. }
  99. public BaseHttpServer(uint port)
  100. {
  101. m_port = port;
  102. }
  103. public BaseHttpServer(uint port, bool ssl) : this (port)
  104. {
  105. m_ssl = ssl;
  106. }
  107. public BaseHttpServer(uint port, bool ssl, uint sslport, string CN) : this (port, ssl)
  108. {
  109. if (m_ssl)
  110. {
  111. m_sslport = sslport;
  112. }
  113. }
  114. public BaseHttpServer(uint port, bool ssl, string CPath, string CPass) : this (port, ssl)
  115. {
  116. if (m_ssl)
  117. {
  118. m_cert = new X509Certificate2(CPath, CPass);
  119. }
  120. }
  121. /// <summary>
  122. /// Add a stream handler to the http server. If the handler already exists, then nothing happens.
  123. /// </summary>
  124. /// <param name="handler"></param>
  125. public void AddStreamHandler(IRequestHandler handler)
  126. {
  127. string httpMethod = handler.HttpMethod;
  128. string path = handler.Path;
  129. string handlerKey = GetHandlerKey(httpMethod, path);
  130. lock (m_streamHandlers)
  131. {
  132. if (!m_streamHandlers.ContainsKey(handlerKey))
  133. {
  134. // m_log.DebugFormat("[BASE HTTP SERVER]: Adding handler key {0}", handlerKey);
  135. m_streamHandlers.Add(handlerKey, handler);
  136. }
  137. }
  138. }
  139. public List<string> GetStreamHandlerKeys()
  140. {
  141. lock (m_streamHandlers)
  142. return new List<string>(m_streamHandlers.Keys);
  143. }
  144. private static string GetHandlerKey(string httpMethod, string path)
  145. {
  146. return httpMethod + ":" + path;
  147. }
  148. public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
  149. {
  150. return AddXmlRPCHandler(method, handler, true);
  151. }
  152. public bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive)
  153. {
  154. lock (m_rpcHandlers)
  155. {
  156. m_rpcHandlers[method] = handler;
  157. m_rpcHandlersKeepAlive[method] = keepAlive; // default
  158. }
  159. return true;
  160. }
  161. public XmlRpcMethod GetXmlRPCHandler(string method)
  162. {
  163. lock (m_rpcHandlers)
  164. {
  165. if (m_rpcHandlers.ContainsKey(method))
  166. {
  167. return m_rpcHandlers[method];
  168. }
  169. else
  170. {
  171. return null;
  172. }
  173. }
  174. }
  175. public List<string> GetXmlRpcHandlerKeys()
  176. {
  177. lock (m_rpcHandlers)
  178. return new List<string>(m_rpcHandlers.Keys);
  179. }
  180. public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler)
  181. {
  182. //m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName);
  183. lock (m_HTTPHandlers)
  184. {
  185. if (!m_HTTPHandlers.ContainsKey(methodName))
  186. {
  187. m_HTTPHandlers.Add(methodName, handler);
  188. return true;
  189. }
  190. }
  191. //must already have a handler for that path so return false
  192. return false;
  193. }
  194. public List<string> GetHTTPHandlerKeys()
  195. {
  196. lock (m_HTTPHandlers)
  197. return new List<string>(m_HTTPHandlers.Keys);
  198. }
  199. public bool AddPollServiceHTTPHandler(string methodName, PollServiceEventArgs args)
  200. {
  201. lock (m_pollHandlers)
  202. {
  203. if (!m_pollHandlers.ContainsKey(methodName))
  204. {
  205. m_pollHandlers.Add(methodName, args);
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. public List<string> GetPollServiceHandlerKeys()
  212. {
  213. lock (m_pollHandlers)
  214. return new List<string>(m_pollHandlers.Keys);
  215. }
  216. // Note that the agent string is provided simply to differentiate
  217. // the handlers - it is NOT required to be an actual agent header
  218. // value.
  219. public bool AddAgentHandler(string agent, IHttpAgentHandler handler)
  220. {
  221. lock (m_agentHandlers)
  222. {
  223. if (!m_agentHandlers.ContainsKey(agent))
  224. {
  225. m_agentHandlers.Add(agent, handler);
  226. return true;
  227. }
  228. }
  229. //must already have a handler for that path so return false
  230. return false;
  231. }
  232. public List<string> GetAgentHandlerKeys()
  233. {
  234. lock (m_agentHandlers)
  235. return new List<string>(m_agentHandlers.Keys);
  236. }
  237. public bool AddLLSDHandler(string path, LLSDMethod handler)
  238. {
  239. lock (m_llsdHandlers)
  240. {
  241. if (!m_llsdHandlers.ContainsKey(path))
  242. {
  243. m_llsdHandlers.Add(path, handler);
  244. return true;
  245. }
  246. }
  247. return false;
  248. }
  249. public List<string> GetLLSDHandlerKeys()
  250. {
  251. lock (m_llsdHandlers)
  252. return new List<string>(m_llsdHandlers.Keys);
  253. }
  254. public bool SetDefaultLLSDHandler(DefaultLLSDMethod handler)
  255. {
  256. m_defaultLlsdHandler = handler;
  257. return true;
  258. }
  259. private void OnRequest(object source, RequestEventArgs args)
  260. {
  261. try
  262. {
  263. IHttpClientContext context = (IHttpClientContext)source;
  264. IHttpRequest request = args.Request;
  265. PollServiceEventArgs psEvArgs;
  266. if (TryGetPollServiceHTTPHandler(request.UriPath.ToString(), out psEvArgs))
  267. {
  268. PollServiceHttpRequest psreq = new PollServiceHttpRequest(psEvArgs, context, request);
  269. if (psEvArgs.Request != null)
  270. {
  271. OSHttpRequest req = new OSHttpRequest(context, request);
  272. Stream requestStream = req.InputStream;
  273. Encoding encoding = Encoding.UTF8;
  274. StreamReader reader = new StreamReader(requestStream, encoding);
  275. string requestBody = reader.ReadToEnd();
  276. Hashtable keysvals = new Hashtable();
  277. Hashtable headervals = new Hashtable();
  278. string[] querystringkeys = req.QueryString.AllKeys;
  279. string[] rHeaders = req.Headers.AllKeys;
  280. keysvals.Add("body", requestBody);
  281. keysvals.Add("uri", req.RawUrl);
  282. keysvals.Add("content-type", req.ContentType);
  283. keysvals.Add("http-method", req.HttpMethod);
  284. foreach (string queryname in querystringkeys)
  285. {
  286. keysvals.Add(queryname, req.QueryString[queryname]);
  287. }
  288. foreach (string headername in rHeaders)
  289. {
  290. headervals[headername] = req.Headers[headername];
  291. }
  292. keysvals.Add("headers", headervals);
  293. keysvals.Add("querystringkeys", querystringkeys);
  294. psEvArgs.Request(psreq.RequestID, keysvals);
  295. }
  296. m_PollServiceManager.Enqueue(psreq);
  297. }
  298. else
  299. {
  300. OnHandleRequestIOThread(context, request);
  301. }
  302. }
  303. catch (Exception e)
  304. {
  305. m_log.ErrorFormat("[BASE HTTP SERVER]: OnRequest() failed with {0}{1}", e.Message, e.StackTrace);
  306. }
  307. }
  308. public void OnHandleRequestIOThread(IHttpClientContext context, IHttpRequest request)
  309. {
  310. OSHttpRequest req = new OSHttpRequest(context, request);
  311. OSHttpResponse resp = new OSHttpResponse(new HttpResponse(context, request),context);
  312. HandleRequest(req, resp);
  313. // !!!HACK ALERT!!!
  314. // There seems to be a bug in the underlying http code that makes subsequent requests
  315. // come up with trash in Accept headers. Until that gets fixed, we're cleaning them up here.
  316. if (request.AcceptTypes != null)
  317. for (int i = 0; i < request.AcceptTypes.Length; i++)
  318. request.AcceptTypes[i] = string.Empty;
  319. }
  320. // public void ConvertIHttpClientContextToOSHttp(object stateinfo)
  321. // {
  322. // HttpServerContextObj objstate = (HttpServerContextObj)stateinfo;
  323. // OSHttpRequest request = objstate.oreq;
  324. // OSHttpResponse resp = objstate.oresp;
  325. // HandleRequest(request,resp);
  326. // }
  327. /// <summary>
  328. /// This methods is the start of incoming HTTP request handling.
  329. /// </summary>
  330. /// <param name="request"></param>
  331. /// <param name="response"></param>
  332. public virtual void HandleRequest(OSHttpRequest request, OSHttpResponse response)
  333. {
  334. if (request.HttpMethod == String.Empty) // Can't handle empty requests, not wasting a thread
  335. {
  336. try
  337. {
  338. SendHTML500(response);
  339. }
  340. catch
  341. {
  342. }
  343. return;
  344. }
  345. string requestMethod = request.HttpMethod;
  346. string uriString = request.RawUrl;
  347. // string reqnum = "unknown";
  348. int tickstart = Environment.TickCount;
  349. try
  350. {
  351. // OpenSim.Framework.WebUtil.OSHeaderRequestID
  352. // if (request.Headers["opensim-request-id"] != null)
  353. // reqnum = String.Format("{0}:{1}",request.RemoteIPEndPoint,request.Headers["opensim-request-id"]);
  354. //m_log.DebugFormat("[BASE HTTP SERVER]: <{0}> handle request for {1}",reqnum,request.RawUrl);
  355. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", true);
  356. // This is the REST agent interface. We require an agent to properly identify
  357. // itself. If the REST handler recognizes the prefix it will attempt to
  358. // satisfy the request. If it is not recognizable, and no damage has occurred
  359. // the request can be passed through to the other handlers. This is a low
  360. // probability event; if a request is matched it is normally expected to be
  361. // handled
  362. IHttpAgentHandler agentHandler;
  363. if (TryGetAgentHandler(request, response, out agentHandler))
  364. {
  365. if (HandleAgentRequest(agentHandler, request, response))
  366. {
  367. return;
  368. }
  369. }
  370. //response.KeepAlive = true;
  371. response.SendChunked = false;
  372. IRequestHandler requestHandler;
  373. string path = request.RawUrl;
  374. string handlerKey = GetHandlerKey(request.HttpMethod, path);
  375. if (TryGetStreamHandler(handlerKey, out requestHandler))
  376. {
  377. if (DebugLevel >= 1)
  378. m_log.DebugFormat(
  379. "[BASE HTTP SERVER]: Found stream handler for {0} {1}",
  380. request.HttpMethod, request.Url.PathAndQuery);
  381. // Okay, so this is bad, but should be considered temporary until everything is IStreamHandler.
  382. byte[] buffer = null;
  383. response.ContentType = requestHandler.ContentType; // Lets do this defaulting before in case handler has varying content type.
  384. if (requestHandler is IStreamedRequestHandler)
  385. {
  386. IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler;
  387. buffer = streamedRequestHandler.Handle(path, request.InputStream, request, response);
  388. }
  389. else if (requestHandler is IGenericHTTPHandler)
  390. {
  391. //m_log.Debug("[BASE HTTP SERVER]: Found Caps based HTTP Handler");
  392. IGenericHTTPHandler HTTPRequestHandler = requestHandler as IGenericHTTPHandler;
  393. Stream requestStream = request.InputStream;
  394. Encoding encoding = Encoding.UTF8;
  395. StreamReader reader = new StreamReader(requestStream, encoding);
  396. string requestBody = reader.ReadToEnd();
  397. reader.Close();
  398. //requestStream.Close();
  399. Hashtable keysvals = new Hashtable();
  400. Hashtable headervals = new Hashtable();
  401. //string host = String.Empty;
  402. string[] querystringkeys = request.QueryString.AllKeys;
  403. string[] rHeaders = request.Headers.AllKeys;
  404. foreach (string queryname in querystringkeys)
  405. {
  406. keysvals.Add(queryname, request.QueryString[queryname]);
  407. }
  408. foreach (string headername in rHeaders)
  409. {
  410. //m_log.Warn("[HEADER]: " + headername + "=" + request.Headers[headername]);
  411. headervals[headername] = request.Headers[headername];
  412. }
  413. // if (headervals.Contains("Host"))
  414. // {
  415. // host = (string)headervals["Host"];
  416. // }
  417. keysvals.Add("requestbody", requestBody);
  418. keysvals.Add("headers",headervals);
  419. if (keysvals.Contains("method"))
  420. {
  421. //m_log.Warn("[HTTP]: Contains Method");
  422. //string method = (string)keysvals["method"];
  423. //m_log.Warn("[HTTP]: " + requestBody);
  424. }
  425. DoHTTPGruntWork(HTTPRequestHandler.Handle(path, keysvals), response);
  426. return;
  427. }
  428. else
  429. {
  430. IStreamHandler streamHandler = (IStreamHandler)requestHandler;
  431. using (MemoryStream memoryStream = new MemoryStream())
  432. {
  433. streamHandler.Handle(path, request.InputStream, memoryStream, request, response);
  434. memoryStream.Flush();
  435. buffer = memoryStream.ToArray();
  436. }
  437. }
  438. request.InputStream.Close();
  439. // HTTP IN support. The script engine takes it from here
  440. // Nothing to worry about for us.
  441. //
  442. if (buffer == null)
  443. return;
  444. if (!response.SendChunked)
  445. response.ContentLength64 = buffer.LongLength;
  446. try
  447. {
  448. response.OutputStream.Write(buffer, 0, buffer.Length);
  449. //response.OutputStream.Close();
  450. }
  451. catch (HttpListenerException)
  452. {
  453. m_log.WarnFormat("[BASE HTTP SERVER]: HTTP request abnormally terminated.");
  454. }
  455. //response.OutputStream.Close();
  456. try
  457. {
  458. response.Send();
  459. //response.FreeContext();
  460. }
  461. catch (SocketException e)
  462. {
  463. // This has to be here to prevent a Linux/Mono crash
  464. m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  465. }
  466. catch (IOException e)
  467. {
  468. m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
  469. }
  470. return;
  471. }
  472. if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
  473. {
  474. foreach (string strAccept in request.AcceptTypes)
  475. {
  476. if (strAccept.Contains("application/llsd+xml") ||
  477. strAccept.Contains("application/llsd+json"))
  478. {
  479. if (DebugLevel >= 1)
  480. m_log.DebugFormat(
  481. "[BASE HTTP SERVER]: Found application/llsd+xml accept header handler for {0} {1}",
  482. request.HttpMethod, request.Url.PathAndQuery);
  483. HandleLLSDRequests(request, response);
  484. return;
  485. }
  486. }
  487. }
  488. switch (request.ContentType)
  489. {
  490. case null:
  491. case "text/html":
  492. if (DebugLevel >= 1)
  493. m_log.DebugFormat(
  494. "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}",
  495. request.ContentType, request.HttpMethod, request.Url.PathAndQuery);
  496. HandleHTTPRequest(request, response);
  497. return;
  498. case "application/llsd+xml":
  499. case "application/xml+llsd":
  500. case "application/llsd+json":
  501. if (DebugLevel >= 1)
  502. m_log.DebugFormat(
  503. "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}",
  504. request.ContentType, request.HttpMethod, request.Url.PathAndQuery);
  505. HandleLLSDRequests(request, response);
  506. return;
  507. case "text/xml":
  508. case "application/xml":
  509. case "application/json":
  510. default:
  511. //m_log.Info("[Debug BASE HTTP SERVER]: in default handler");
  512. // Point of note.. the DoWeHaveA methods check for an EXACT path
  513. // if (request.RawUrl.Contains("/CAPS/EQG"))
  514. // {
  515. // int i = 1;
  516. // }
  517. //m_log.Info("[Debug BASE HTTP SERVER]: Checking for LLSD Handler");
  518. if (DoWeHaveALLSDHandler(request.RawUrl))
  519. {
  520. if (DebugLevel >= 1)
  521. m_log.DebugFormat(
  522. "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}",
  523. request.ContentType, request.HttpMethod, request.Url.PathAndQuery);
  524. HandleLLSDRequests(request, response);
  525. return;
  526. }
  527. // m_log.DebugFormat("[BASE HTTP SERVER]: Checking for HTTP Handler for request {0}", request.RawUrl);
  528. if (DoWeHaveAHTTPHandler(request.RawUrl))
  529. {
  530. if (DebugLevel >= 1)
  531. m_log.DebugFormat(
  532. "[BASE HTTP SERVER]: Found a {0} content type handler for {1} {2}",
  533. request.ContentType, request.HttpMethod, request.Url.PathAndQuery);
  534. HandleHTTPRequest(request, response);
  535. return;
  536. }
  537. if (DebugLevel >= 1)
  538. m_log.DebugFormat(
  539. "[BASE HTTP SERVER]: Assuming a generic XMLRPC request for {0} {1}",
  540. request.HttpMethod, request.Url.PathAndQuery);
  541. // generic login request.
  542. HandleXmlRpcRequests(request, response);
  543. return;
  544. }
  545. }
  546. catch (SocketException e)
  547. {
  548. // At least on linux, it appears that if the client makes a request without requiring the response,
  549. // an unconnected socket exception is thrown when we close the response output stream. There's no
  550. // obvious way to tell if the client didn't require the response, so instead we'll catch and ignore
  551. // the exception instead.
  552. //
  553. // An alternative may be to turn off all response write exceptions on the HttpListener, but let's go
  554. // with the minimum first
  555. m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e);
  556. }
  557. catch (IOException e)
  558. {
  559. m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw ", e);
  560. }
  561. catch (Exception e)
  562. {
  563. m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw " + e.ToString());
  564. SendHTML500(response);
  565. }
  566. finally
  567. {
  568. // Every month or so this will wrap and give bad numbers, not really a problem
  569. // since its just for reporting, tickdiff limit can be adjusted
  570. int tickdiff = Environment.TickCount - tickstart;
  571. if (tickdiff > 3000)
  572. m_log.InfoFormat(
  573. "[BASE HTTP SERVER]: slow {0} request for {1} from {2} took {3} ms", requestMethod, uriString, request.RemoteIPEndPoint.ToString(), tickdiff);
  574. }
  575. }
  576. private bool TryGetStreamHandler(string handlerKey, out IRequestHandler streamHandler)
  577. {
  578. string bestMatch = null;
  579. lock (m_streamHandlers)
  580. {
  581. foreach (string pattern in m_streamHandlers.Keys)
  582. {
  583. if (handlerKey.StartsWith(pattern))
  584. {
  585. if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
  586. {
  587. bestMatch = pattern;
  588. }
  589. }
  590. }
  591. if (String.IsNullOrEmpty(bestMatch))
  592. {
  593. streamHandler = null;
  594. return false;
  595. }
  596. else
  597. {
  598. streamHandler = m_streamHandlers[bestMatch];
  599. return true;
  600. }
  601. }
  602. }
  603. private bool TryGetPollServiceHTTPHandler(string handlerKey, out PollServiceEventArgs oServiceEventArgs)
  604. {
  605. string bestMatch = null;
  606. lock (m_pollHandlers)
  607. {
  608. foreach (string pattern in m_pollHandlers.Keys)
  609. {
  610. if (handlerKey.StartsWith(pattern))
  611. {
  612. if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
  613. {
  614. bestMatch = pattern;
  615. }
  616. }
  617. }
  618. if (String.IsNullOrEmpty(bestMatch))
  619. {
  620. oServiceEventArgs = null;
  621. return false;
  622. }
  623. else
  624. {
  625. oServiceEventArgs = m_pollHandlers[bestMatch];
  626. return true;
  627. }
  628. }
  629. }
  630. private bool TryGetHTTPHandler(string handlerKey, out GenericHTTPMethod HTTPHandler)
  631. {
  632. // m_log.DebugFormat("[BASE HTTP HANDLER]: Looking for HTTP handler for {0}", handlerKey);
  633. string bestMatch = null;
  634. lock (m_HTTPHandlers)
  635. {
  636. foreach (string pattern in m_HTTPHandlers.Keys)
  637. {
  638. if (handlerKey.StartsWith(pattern))
  639. {
  640. if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
  641. {
  642. bestMatch = pattern;
  643. }
  644. }
  645. }
  646. if (String.IsNullOrEmpty(bestMatch))
  647. {
  648. HTTPHandler = null;
  649. return false;
  650. }
  651. else
  652. {
  653. HTTPHandler = m_HTTPHandlers[bestMatch];
  654. return true;
  655. }
  656. }
  657. }
  658. private bool TryGetAgentHandler(OSHttpRequest request, OSHttpResponse response, out IHttpAgentHandler agentHandler)
  659. {
  660. agentHandler = null;
  661. lock (m_agentHandlers)
  662. {
  663. foreach (IHttpAgentHandler handler in m_agentHandlers.Values)
  664. {
  665. if (handler.Match(request, response))
  666. {
  667. agentHandler = handler;
  668. return true;
  669. }
  670. }
  671. }
  672. return false;
  673. }
  674. /// <summary>
  675. /// Try all the registered xmlrpc handlers when an xmlrpc request is received.
  676. /// Sends back an XMLRPC unknown request response if no handler is registered for the requested method.
  677. /// </summary>
  678. /// <param name="request"></param>
  679. /// <param name="response"></param>
  680. private void HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response)
  681. {
  682. Stream requestStream = request.InputStream;
  683. Encoding encoding = Encoding.UTF8;
  684. StreamReader reader = new StreamReader(requestStream, encoding);
  685. string requestBody = reader.ReadToEnd();
  686. reader.Close();
  687. requestStream.Close();
  688. //m_log.Debug(requestBody);
  689. requestBody = requestBody.Replace("<base64></base64>", "");
  690. string responseString = String.Empty;
  691. XmlRpcRequest xmlRprcRequest = null;
  692. try
  693. {
  694. xmlRprcRequest = (XmlRpcRequest) (new XmlRpcRequestDeserializer()).Deserialize(requestBody);
  695. }
  696. catch (XmlException)
  697. {
  698. }
  699. if (xmlRprcRequest != null)
  700. {
  701. string methodName = xmlRprcRequest.MethodName;
  702. if (methodName != null)
  703. {
  704. xmlRprcRequest.Params.Add(request.RemoteIPEndPoint); // Param[1]
  705. XmlRpcResponse xmlRpcResponse;
  706. XmlRpcMethod method;
  707. bool methodWasFound;
  708. bool keepAlive = false;
  709. lock (m_rpcHandlers)
  710. {
  711. methodWasFound = m_rpcHandlers.TryGetValue(methodName, out method);
  712. if (methodWasFound)
  713. keepAlive = m_rpcHandlersKeepAlive[methodName];
  714. }
  715. if (methodWasFound)
  716. {
  717. xmlRprcRequest.Params.Add(request.Url); // Param[2]
  718. string xff = "X-Forwarded-For";
  719. string xfflower = xff.ToLower();
  720. foreach (string s in request.Headers.AllKeys)
  721. {
  722. if (s != null && s.Equals(xfflower))
  723. {
  724. xff = xfflower;
  725. break;
  726. }
  727. }
  728. xmlRprcRequest.Params.Add(request.Headers.Get(xff)); // Param[3]
  729. try
  730. {
  731. xmlRpcResponse = method(xmlRprcRequest, request.RemoteIPEndPoint);
  732. }
  733. catch(Exception e)
  734. {
  735. string errorMessage
  736. = String.Format(
  737. "Requested method [{0}] from {1} threw exception: {2} {3}",
  738. methodName, request.RemoteIPEndPoint.Address, e.Message, e.StackTrace);
  739. m_log.ErrorFormat("[BASE HTTP SERVER]: {0}", errorMessage);
  740. // if the registered XmlRpc method threw an exception, we pass a fault-code along
  741. xmlRpcResponse = new XmlRpcResponse();
  742. // Code probably set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
  743. xmlRpcResponse.SetFault(-32603, errorMessage);
  744. }
  745. // if the method wasn't found, we can't determine KeepAlive state anyway, so lets do it only here
  746. response.KeepAlive = keepAlive;
  747. }
  748. else
  749. {
  750. xmlRpcResponse = new XmlRpcResponse();
  751. // Code set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
  752. xmlRpcResponse.SetFault(
  753. XmlRpcErrorCodes.SERVER_ERROR_METHOD,
  754. String.Format("Requested method [{0}] not found", methodName));
  755. }
  756. responseString = XmlRpcResponseSerializer.Singleton.Serialize(xmlRpcResponse);
  757. }
  758. else
  759. {
  760. //HandleLLSDRequests(request, response);
  761. response.ContentType = "text/plain";
  762. response.StatusCode = 404;
  763. response.StatusDescription = "Not Found";
  764. response.ProtocolVersion = "HTTP/1.0";
  765. byte[] buf = Encoding.UTF8.GetBytes("Not found");
  766. response.KeepAlive = false;
  767. m_log.ErrorFormat(
  768. "[BASE HTTP SERVER]: Handler not found for http request {0} {1}",
  769. request.HttpMethod, request.Url.PathAndQuery);
  770. response.SendChunked = false;
  771. response.ContentLength64 = buf.Length;
  772. response.ContentEncoding = Encoding.UTF8;
  773. try
  774. {
  775. response.OutputStream.Write(buf, 0, buf.Length);
  776. }
  777. catch (Exception ex)
  778. {
  779. m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
  780. }
  781. finally
  782. {
  783. try
  784. {
  785. response.Send();
  786. //response.FreeContext();
  787. }
  788. catch (SocketException e)
  789. {
  790. // This has to be here to prevent a Linux/Mono crash
  791. m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  792. }
  793. catch (IOException e)
  794. {
  795. m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
  796. }
  797. }
  798. return;
  799. //responseString = "Error";
  800. }
  801. }
  802. response.ContentType = "text/xml";
  803. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  804. response.SendChunked = false;
  805. response.ContentLength64 = buffer.Length;
  806. response.ContentEncoding = Encoding.UTF8;
  807. try
  808. {
  809. response.OutputStream.Write(buffer, 0, buffer.Length);
  810. }
  811. catch (Exception ex)
  812. {
  813. m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
  814. }
  815. finally
  816. {
  817. try
  818. {
  819. response.Send();
  820. //response.FreeContext();
  821. }
  822. catch (SocketException e)
  823. {
  824. // This has to be here to prevent a Linux/Mono crash
  825. m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  826. }
  827. catch (IOException e)
  828. {
  829. m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
  830. }
  831. }
  832. }
  833. private void HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response)
  834. {
  835. //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request");
  836. Stream requestStream = request.InputStream;
  837. Encoding encoding = Encoding.UTF8;
  838. StreamReader reader = new StreamReader(requestStream, encoding);
  839. string requestBody = reader.ReadToEnd();
  840. reader.Close();
  841. requestStream.Close();
  842. //m_log.DebugFormat("[OGP]: {0}:{1}", request.RawUrl, requestBody);
  843. response.KeepAlive = true;
  844. OSD llsdRequest = null;
  845. OSD llsdResponse = null;
  846. bool LegacyLLSDLoginLibOMV = (requestBody.Contains("passwd") && requestBody.Contains("mac") && requestBody.Contains("viewer_digest"));
  847. if (requestBody.Length == 0)
  848. // Get Request
  849. {
  850. requestBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd><map><key>request</key><string>get</string></map></llsd>";
  851. }
  852. try
  853. {
  854. llsdRequest = OSDParser.Deserialize(requestBody);
  855. }
  856. catch (Exception ex)
  857. {
  858. m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
  859. }
  860. if (llsdRequest != null)// && m_defaultLlsdHandler != null)
  861. {
  862. LLSDMethod llsdhandler = null;
  863. if (TryGetLLSDHandler(request.RawUrl, out llsdhandler) && !LegacyLLSDLoginLibOMV)
  864. {
  865. // we found a registered llsd handler to service this request
  866. llsdResponse = llsdhandler(request.RawUrl, llsdRequest, request.RemoteIPEndPoint.ToString());
  867. }
  868. else
  869. {
  870. // we didn't find a registered llsd handler to service this request
  871. // check if we have a default llsd handler
  872. if (m_defaultLlsdHandler != null)
  873. {
  874. // LibOMV path
  875. llsdResponse = m_defaultLlsdHandler(llsdRequest, request.RemoteIPEndPoint);
  876. }
  877. else
  878. {
  879. // Oops, no handler for this.. give em the failed message
  880. llsdResponse = GenerateNoLLSDHandlerResponse();
  881. }
  882. }
  883. }
  884. else
  885. {
  886. llsdResponse = GenerateNoLLSDHandlerResponse();
  887. }
  888. byte[] buffer = new byte[0];
  889. if (llsdResponse.ToString() == "shutdown404!")
  890. {
  891. response.ContentType = "text/plain";
  892. response.StatusCode = 404;
  893. response.StatusDescription = "Not Found";
  894. response.ProtocolVersion = "HTTP/1.0";
  895. buffer = Encoding.UTF8.GetBytes("Not found");
  896. }
  897. else
  898. {
  899. // Select an appropriate response format
  900. buffer = BuildLLSDResponse(request, response, llsdResponse);
  901. }
  902. response.SendChunked = false;
  903. response.ContentLength64 = buffer.Length;
  904. response.ContentEncoding = Encoding.UTF8;
  905. response.KeepAlive = true;
  906. try
  907. {
  908. response.OutputStream.Write(buffer, 0, buffer.Length);
  909. }
  910. catch (Exception ex)
  911. {
  912. m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
  913. }
  914. finally
  915. {
  916. //response.OutputStream.Close();
  917. try
  918. {
  919. response.Send();
  920. response.OutputStream.Flush();
  921. //response.FreeContext();
  922. //response.OutputStream.Close();
  923. }
  924. catch (IOException e)
  925. {
  926. m_log.WarnFormat("[BASE HTTP SERVER]: LLSD IOException {0}.", e);
  927. }
  928. catch (SocketException e)
  929. {
  930. // This has to be here to prevent a Linux/Mono crash
  931. m_log.WarnFormat("[BASE HTTP SERVER]: LLSD issue {0}.\nNOTE: this may be spurious on Linux.", e);
  932. }
  933. }
  934. }
  935. private byte[] BuildLLSDResponse(OSHttpRequest request, OSHttpResponse response, OSD llsdResponse)
  936. {
  937. if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
  938. {
  939. foreach (string strAccept in request.AcceptTypes)
  940. {
  941. switch (strAccept)
  942. {
  943. case "application/llsd+xml":
  944. case "application/xml":
  945. case "text/xml":
  946. response.ContentType = strAccept;
  947. return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
  948. case "application/llsd+json":
  949. case "application/json":
  950. response.ContentType = strAccept;
  951. return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
  952. }
  953. }
  954. }
  955. if (!String.IsNullOrEmpty(request.ContentType))
  956. {
  957. switch (request.ContentType)
  958. {
  959. case "application/llsd+xml":
  960. case "application/xml":
  961. case "text/xml":
  962. response.ContentType = request.ContentType;
  963. return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
  964. case "application/llsd+json":
  965. case "application/json":
  966. response.ContentType = request.ContentType;
  967. return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
  968. }
  969. }
  970. // response.ContentType = "application/llsd+json";
  971. // return Util.UTF8.GetBytes(OSDParser.SerializeJsonString(llsdResponse));
  972. response.ContentType = "application/llsd+xml";
  973. return OSDParser.SerializeLLSDXmlBytes(llsdResponse);
  974. }
  975. /// <summary>
  976. /// Checks if we have an Exact path in the LLSD handlers for the path provided
  977. /// </summary>
  978. /// <param name="path">URI of the request</param>
  979. /// <returns>true if we have one, false if not</returns>
  980. private bool DoWeHaveALLSDHandler(string path)
  981. {
  982. string[] pathbase = path.Split('/');
  983. string searchquery = "/";
  984. if (pathbase.Length < 1)
  985. return false;
  986. for (int i = 1; i < pathbase.Length; i++)
  987. {
  988. searchquery += pathbase[i];
  989. if (pathbase.Length - 1 != i)
  990. searchquery += "/";
  991. }
  992. string bestMatch = null;
  993. lock (m_llsdHandlers)
  994. {
  995. foreach (string pattern in m_llsdHandlers.Keys)
  996. {
  997. if (searchquery.StartsWith(pattern) && searchquery.Length >= pattern.Length)
  998. bestMatch = pattern;
  999. }
  1000. }
  1001. // extra kicker to remove the default XMLRPC login case.. just in case..
  1002. if (path != "/" && bestMatch == "/" && searchquery != "/")
  1003. return false;
  1004. if (path == "/")
  1005. return false;
  1006. if (String.IsNullOrEmpty(bestMatch))
  1007. {
  1008. return false;
  1009. }
  1010. else
  1011. {
  1012. return true;
  1013. }
  1014. }
  1015. /// <summary>
  1016. /// Checks if we have an Exact path in the HTTP handlers for the path provided
  1017. /// </summary>
  1018. /// <param name="path">URI of the request</param>
  1019. /// <returns>true if we have one, false if not</returns>
  1020. private bool DoWeHaveAHTTPHandler(string path)
  1021. {
  1022. string[] pathbase = path.Split('/');
  1023. string searchquery = "/";
  1024. if (pathbase.Length < 1)
  1025. return false;
  1026. for (int i = 1; i < pathbase.Length; i++)
  1027. {
  1028. searchquery += pathbase[i];
  1029. if (pathbase.Length - 1 != i)
  1030. searchquery += "/";
  1031. }
  1032. string bestMatch = null;
  1033. //m_log.DebugFormat("[BASE HTTP HANDLER]: Checking if we have an HTTP handler for {0}", searchquery);
  1034. lock (m_HTTPHandlers)
  1035. {
  1036. foreach (string pattern in m_HTTPHandlers.Keys)
  1037. {
  1038. if (searchquery.StartsWith(pattern) && searchquery.Length >= pattern.Length)
  1039. {
  1040. bestMatch = pattern;
  1041. }
  1042. }
  1043. // extra kicker to remove the default XMLRPC login case.. just in case..
  1044. if (path == "/")
  1045. return false;
  1046. if (String.IsNullOrEmpty(bestMatch))
  1047. {
  1048. return false;
  1049. }
  1050. else
  1051. {
  1052. return true;
  1053. }
  1054. }
  1055. }
  1056. private bool TryGetLLSDHandler(string path, out LLSDMethod llsdHandler)
  1057. {
  1058. llsdHandler = null;
  1059. // Pull out the first part of the path
  1060. // splitting the path by '/' means we'll get the following return..
  1061. // {0}/{1}/{2}
  1062. // where {0} isn't something we really control 100%
  1063. string[] pathbase = path.Split('/');
  1064. string searchquery = "/";
  1065. if (pathbase.Length < 1)
  1066. return false;
  1067. for (int i=1; i<pathbase.Length; i++)
  1068. {
  1069. searchquery += pathbase[i];
  1070. if (pathbase.Length-1 != i)
  1071. searchquery += "/";
  1072. }
  1073. // while the matching algorithm below doesn't require it, we're expecting a query in the form
  1074. //
  1075. // [] = optional
  1076. // /resource/UUID/action[/action]
  1077. //
  1078. // now try to get the closest match to the reigstered path
  1079. // at least for OGP, registered path would probably only consist of the /resource/
  1080. string bestMatch = null;
  1081. lock (m_llsdHandlers)
  1082. {
  1083. foreach (string pattern in m_llsdHandlers.Keys)
  1084. {
  1085. if (searchquery.ToLower().StartsWith(pattern.ToLower()))
  1086. {
  1087. if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
  1088. {
  1089. // You have to specifically register for '/' and to get it, you must specificaly request it
  1090. //
  1091. if (pattern == "/" && searchquery == "/" || pattern != "/")
  1092. bestMatch = pattern;
  1093. }
  1094. }
  1095. }
  1096. if (String.IsNullOrEmpty(bestMatch))
  1097. {
  1098. llsdHandler = null;
  1099. return false;
  1100. }
  1101. else
  1102. {
  1103. llsdHandler = m_llsdHandlers[bestMatch];
  1104. return true;
  1105. }
  1106. }
  1107. }
  1108. private OSDMap GenerateNoLLSDHandlerResponse()
  1109. {
  1110. OSDMap map = new OSDMap();
  1111. map["reason"] = OSD.FromString("LLSDRequest");
  1112. map["message"] = OSD.FromString("No handler registered for LLSD Requests");
  1113. map["login"] = OSD.FromString("false");
  1114. return map;
  1115. }
  1116. /// <summary>
  1117. /// A specific agent handler was provided. Such a handler is expecetd to have an
  1118. /// intimate, and highly specific relationship with the client. Consequently,
  1119. /// nothing is done here.
  1120. /// </summary>
  1121. /// <param name="handler"></param>
  1122. /// <param name="request"></param>
  1123. /// <param name="response"></param>
  1124. private bool HandleAgentRequest(IHttpAgentHandler handler, OSHttpRequest request, OSHttpResponse response)
  1125. {
  1126. // In the case of REST, then handler is responsible for ALL aspects of
  1127. // the request/response handling. Nothing is done here, not even encoding.
  1128. try
  1129. {
  1130. return handler.Handle(request, response);
  1131. }
  1132. catch (Exception e)
  1133. {
  1134. // If the handler did in fact close the stream, then this will blow
  1135. // chunks. So that that doesn't disturb anybody we throw away any
  1136. // and all exceptions raised. We've done our best to release the
  1137. // client.
  1138. try
  1139. {
  1140. m_log.Warn("[HTTP-AGENT]: Error - " + e.Message);
  1141. response.SendChunked = false;
  1142. response.KeepAlive = true;
  1143. response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError;
  1144. //response.OutputStream.Close();
  1145. try
  1146. {
  1147. response.Send();
  1148. //response.FreeContext();
  1149. }
  1150. catch (SocketException f)
  1151. {
  1152. // This has to be here to prevent a Linux/Mono crash
  1153. m_log.WarnFormat(
  1154. "[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", f);
  1155. }
  1156. }
  1157. catch(Exception)
  1158. {
  1159. }
  1160. }
  1161. // Indicate that the request has been "handled"
  1162. return true;
  1163. }
  1164. public void HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response)
  1165. {
  1166. // m_log.DebugFormat(
  1167. // "[BASE HTTP SERVER]: HandleHTTPRequest for request to {0}, method {1}",
  1168. // request.RawUrl, request.HttpMethod);
  1169. switch (request.HttpMethod)
  1170. {
  1171. case "OPTIONS":
  1172. response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
  1173. return;
  1174. default:
  1175. HandleContentVerbs(request, response);
  1176. return;
  1177. }
  1178. }
  1179. private void HandleContentVerbs(OSHttpRequest request, OSHttpResponse response)
  1180. {
  1181. // m_log.DebugFormat("[BASE HTTP SERVER]: HandleContentVerbs for request to {0}", request.RawUrl);
  1182. // This is a test. There's a workable alternative.. as this way sucks.
  1183. // We'd like to put this into a text file parhaps that's easily editable.
  1184. //
  1185. // For this test to work, I used the following secondlife.exe parameters
  1186. // "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
  1187. //
  1188. // Even after all that, there's still an error, but it's a start.
  1189. //
  1190. // I depend on show_login_form being in the secondlife.exe parameters to figure out
  1191. // to display the form, or process it.
  1192. // a better way would be nifty.
  1193. Stream requestStream = request.InputStream;
  1194. Encoding encoding = Encoding.UTF8;
  1195. StreamReader reader = new StreamReader(requestStream, encoding);
  1196. string requestBody = reader.ReadToEnd();
  1197. // avoid warning for now
  1198. reader.ReadToEnd();
  1199. reader.Close();
  1200. requestStream.Close();
  1201. Hashtable keysvals = new Hashtable();
  1202. Hashtable headervals = new Hashtable();
  1203. Hashtable requestVars = new Hashtable();
  1204. string host = String.Empty;
  1205. string[] querystringkeys = request.QueryString.AllKeys;
  1206. string[] rHeaders = request.Headers.AllKeys;
  1207. keysvals.Add("body", requestBody);
  1208. keysvals.Add("uri", request.RawUrl);
  1209. keysvals.Add("content-type", request.ContentType);
  1210. keysvals.Add("http-method", request.HttpMethod);
  1211. foreach (string queryname in querystringkeys)
  1212. {
  1213. // m_log.DebugFormat(
  1214. // "[BASE HTTP SERVER]: Got query paremeter {0}={1}", queryname, request.QueryString[queryname]);
  1215. keysvals.Add(queryname, request.QueryString[queryname]);
  1216. requestVars.Add(queryname, keysvals[queryname]);
  1217. }
  1218. foreach (string headername in rHeaders)
  1219. {
  1220. // m_log.Debug("[BASE HTTP SERVER]: " + headername + "=" + request.Headers[headername]);
  1221. headervals[headername] = request.Headers[headername];
  1222. }
  1223. if (headervals.Contains("Host"))
  1224. {
  1225. host = (string)headervals["Host"];
  1226. }
  1227. keysvals.Add("headers", headervals);
  1228. keysvals.Add("querystringkeys", querystringkeys);
  1229. keysvals.Add("requestvars", requestVars);
  1230. // keysvals.Add("form", request.Form);
  1231. if (keysvals.Contains("method"))
  1232. {
  1233. // m_log.Debug("[BASE HTTP SERVER]: Contains Method");
  1234. string method = (string) keysvals["method"];
  1235. // m_log.Debug("[BASE HTTP SERVER]: " + requestBody);
  1236. GenericHTTPMethod requestprocessor;
  1237. bool foundHandler = TryGetHTTPHandler(method, out requestprocessor);
  1238. if (foundHandler)
  1239. {
  1240. Hashtable responsedata1 = requestprocessor(keysvals);
  1241. DoHTTPGruntWork(responsedata1,response);
  1242. //SendHTML500(response);
  1243. }
  1244. else
  1245. {
  1246. // m_log.Warn("[BASE HTTP SERVER]: Handler Not Found");
  1247. SendHTML404(response, host);
  1248. }
  1249. }
  1250. else
  1251. {
  1252. GenericHTTPMethod requestprocessor;
  1253. bool foundHandler = TryGetHTTPHandlerPathBased(request.RawUrl, out requestprocessor);
  1254. if (foundHandler)
  1255. {
  1256. Hashtable responsedata2 = requestprocessor(keysvals);
  1257. DoHTTPGruntWork(responsedata2, response);
  1258. //SendHTML500(response);
  1259. }
  1260. else
  1261. {
  1262. // m_log.Warn("[BASE HTTP SERVER]: Handler Not Found2");
  1263. SendHTML404(response, host);
  1264. }
  1265. }
  1266. }
  1267. private bool TryGetHTTPHandlerPathBased(string path, out GenericHTTPMethod httpHandler)
  1268. {
  1269. httpHandler = null;
  1270. // Pull out the first part of the path
  1271. // splitting the path by '/' means we'll get the following return..
  1272. // {0}/{1}/{2}
  1273. // where {0} isn't something we really control 100%
  1274. string[] pathbase = path.Split('/');
  1275. string searchquery = "/";
  1276. if (pathbase.Length < 1)
  1277. return false;
  1278. for (int i = 1; i < pathbase.Length; i++)
  1279. {
  1280. searchquery += pathbase[i];
  1281. if (pathbase.Length - 1 != i)
  1282. searchquery += "/";
  1283. }
  1284. // while the matching algorithm below doesn't require it, we're expecting a query in the form
  1285. //
  1286. // [] = optional
  1287. // /resource/UUID/action[/action]
  1288. //
  1289. // now try to get the closest match to the reigstered path
  1290. // at least for OGP, registered path would probably only consist of the /resource/
  1291. string bestMatch = null;
  1292. // m_log.DebugFormat(
  1293. // "[BASE HTTP HANDLER]: TryGetHTTPHandlerPathBased() looking for HTTP handler to match {0}", searchquery);
  1294. lock (m_HTTPHandlers)
  1295. {
  1296. foreach (string pattern in m_HTTPHandlers.Keys)
  1297. {
  1298. if (searchquery.ToLower().StartsWith(pattern.ToLower()))
  1299. {
  1300. if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
  1301. {
  1302. // You have to specifically register for '/' and to get it, you must specifically request it
  1303. if (pattern == "/" && searchquery == "/" || pattern != "/")
  1304. bestMatch = pattern;
  1305. }
  1306. }
  1307. }
  1308. if (String.IsNullOrEmpty(bestMatch))
  1309. {
  1310. httpHandler = null;
  1311. return false;
  1312. }
  1313. else
  1314. {
  1315. if (bestMatch == "/" && searchquery != "/")
  1316. return false;
  1317. httpHandler = m_HTTPHandlers[bestMatch];
  1318. return true;
  1319. }
  1320. }
  1321. }
  1322. internal void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
  1323. {
  1324. //m_log.Info("[BASE HTTP SERVER]: Doing HTTP Grunt work with response");
  1325. int responsecode = (int)responsedata["int_response_code"];
  1326. string responseString = (string)responsedata["str_response_string"];
  1327. string contentType = (string)responsedata["content_type"];
  1328. if (responsedata.ContainsKey("error_status_text"))
  1329. {
  1330. response.StatusDescription = (string)responsedata["error_status_text"];
  1331. }
  1332. if (responsedata.ContainsKey("http_protocol_version"))
  1333. {
  1334. response.ProtocolVersion = (string)responsedata["http_protocol_version"];
  1335. }
  1336. if (responsedata.ContainsKey("keepalive"))
  1337. {
  1338. bool keepalive = (bool)responsedata["keepalive"];
  1339. response.KeepAlive = keepalive;
  1340. }
  1341. if (responsedata.ContainsKey("reusecontext"))
  1342. response.ReuseContext = (bool) responsedata["reusecontext"];
  1343. // Cross-Origin Resource Sharing with simple requests
  1344. if (responsedata.ContainsKey("access_control_allow_origin"))
  1345. response.AddHeader("Access-Control-Allow-Origin", (string)responsedata["access_control_allow_origin"]);
  1346. //Even though only one other part of the entire code uses HTTPHandlers, we shouldn't expect this
  1347. //and should check for NullReferenceExceptions
  1348. if (string.IsNullOrEmpty(contentType))
  1349. {
  1350. contentType = "text/html";
  1351. }
  1352. // The client ignores anything but 200 here for web login, so ensure that this is 200 for that
  1353. response.StatusCode = responsecode;
  1354. if (responsecode == (int)OSHttpStatusCode.RedirectMovedPermanently)
  1355. {
  1356. response.RedirectLocation = (string)responsedata["str_redirect_location"];
  1357. response.StatusCode = responsecode;
  1358. }
  1359. response.AddHeader("Content-Type", contentType);
  1360. byte[] buffer;
  1361. if (!(contentType.Contains("image")
  1362. || contentType.Contains("x-shockwave-flash")
  1363. || contentType.Contains("application/x-oar")
  1364. || contentType.Contains("application/vnd.ll.mesh")))
  1365. {
  1366. // Text
  1367. buffer = Encoding.UTF8.GetBytes(responseString);
  1368. }
  1369. else
  1370. {
  1371. // Binary!
  1372. buffer = Convert.FromBase64String(responseString);
  1373. }
  1374. response.SendChunked = false;
  1375. response.ContentLength64 = buffer.Length;
  1376. response.ContentEncoding = Encoding.UTF8;
  1377. try
  1378. {
  1379. response.OutputStream.Write(buffer, 0, buffer.Length);
  1380. }
  1381. catch (Exception ex)
  1382. {
  1383. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1384. }
  1385. finally
  1386. {
  1387. //response.OutputStream.Close();
  1388. try
  1389. {
  1390. response.OutputStream.Flush();
  1391. response.Send();
  1392. //if (!response.KeepAlive && response.ReuseContext)
  1393. // response.FreeContext();
  1394. }
  1395. catch (SocketException e)
  1396. {
  1397. // This has to be here to prevent a Linux/Mono crash
  1398. m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1399. }
  1400. catch (IOException e)
  1401. {
  1402. m_log.Warn("[BASE HTTP SERVER]: XmlRpcRequest issue: " + e.Message);
  1403. }
  1404. }
  1405. }
  1406. public void SendHTML404(OSHttpResponse response, string host)
  1407. {
  1408. // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
  1409. response.StatusCode = 404;
  1410. response.AddHeader("Content-type", "text/html");
  1411. string responseString = GetHTTP404(host);
  1412. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  1413. response.SendChunked = false;
  1414. response.ContentLength64 = buffer.Length;
  1415. response.ContentEncoding = Encoding.UTF8;
  1416. try
  1417. {
  1418. response.OutputStream.Write(buffer, 0, buffer.Length);
  1419. }
  1420. catch (Exception ex)
  1421. {
  1422. m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
  1423. }
  1424. finally
  1425. {
  1426. //response.OutputStream.Close();
  1427. try
  1428. {
  1429. response.Send();
  1430. //response.FreeContext();
  1431. }
  1432. catch (SocketException e)
  1433. {
  1434. // This has to be here to prevent a Linux/Mono crash
  1435. m_log.WarnFormat("[BASE HTTP SERVER]: XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1436. }
  1437. }
  1438. }
  1439. public void SendHTML500(OSHttpResponse response)
  1440. {
  1441. // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
  1442. response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
  1443. response.AddHeader("Content-type", "text/html");
  1444. string responseString = GetHTTP500();
  1445. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  1446. response.SendChunked = false;
  1447. response.ContentLength64 = buffer.Length;
  1448. response.ContentEncoding = Encoding.UTF8;
  1449. try
  1450. {
  1451. response.OutputStream.Write(buffer, 0, buffer.Length);
  1452. }
  1453. catch (Exception ex)
  1454. {
  1455. m_log.Warn("[BASE HTTP SERVER]: Error - " + ex.Message);
  1456. }
  1457. finally
  1458. {
  1459. //response.OutputStream.Close();
  1460. try
  1461. {
  1462. response.Send();
  1463. //response.FreeContext();
  1464. }
  1465. catch (SocketException e)
  1466. {
  1467. // This has to be here to prevent a Linux/Mono crash
  1468. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1469. }
  1470. }
  1471. }
  1472. public void Start()
  1473. {
  1474. StartHTTP();
  1475. }
  1476. private void StartHTTP()
  1477. {
  1478. try
  1479. {
  1480. //m_httpListener = new HttpListener();
  1481. NotSocketErrors = 0;
  1482. if (!m_ssl)
  1483. {
  1484. //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
  1485. //m_httpListener.Prefixes.Add("http://10.1.1.5:" + m_port + "/");
  1486. m_httpListener2 = CoolHTTPListener.Create(m_listenIPAddress, (int)m_port);
  1487. m_httpListener2.ExceptionThrown += httpServerException;
  1488. m_httpListener2.LogWriter = httpserverlog;
  1489. // Uncomment this line in addition to those in HttpServerLogWriter
  1490. // if you want more detailed trace information from the HttpServer
  1491. //m_httpListener2.UseTraceLogs = true;
  1492. //m_httpListener2.DisconnectHandler = httpServerDisconnectMonitor;
  1493. }
  1494. else
  1495. {
  1496. //m_httpListener.Prefixes.Add("https://+:" + (m_sslport) + "/");
  1497. //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
  1498. m_httpListener2 = CoolHTTPListener.Create(IPAddress.Any, (int)m_port, m_cert);
  1499. m_httpListener2.ExceptionThrown += httpServerException;
  1500. m_httpListener2.LogWriter = httpserverlog;
  1501. }
  1502. m_httpListener2.RequestReceived += OnRequest;
  1503. //m_httpListener.Start();
  1504. m_httpListener2.Start(64);
  1505. // Long Poll Service Manager with 3 worker threads a 25 second timeout for no events
  1506. m_PollServiceManager = new PollServiceRequestManager(this, 3, 25000);
  1507. HTTPDRunning = true;
  1508. //HttpListenerContext context;
  1509. //while (true)
  1510. //{
  1511. // context = m_httpListener.GetContext();
  1512. // ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(HandleRequest), context);
  1513. // }
  1514. }
  1515. catch (Exception e)
  1516. {
  1517. m_log.Error("[BASE HTTP SERVER]: Error - " + e.Message);
  1518. m_log.Error("[BASE HTTP SERVER]: Tip: Do you have permission to listen on port " + m_port + ", " + m_sslport + "?");
  1519. // We want this exception to halt the entire server since in current configurations we aren't too
  1520. // useful without inbound HTTP.
  1521. throw e;
  1522. }
  1523. }
  1524. public void httpServerDisconnectMonitor(IHttpClientContext source, SocketError err)
  1525. {
  1526. switch (err)
  1527. {
  1528. case SocketError.NotSocket:
  1529. NotSocketErrors++;
  1530. break;
  1531. }
  1532. }
  1533. public void httpServerException(object source, Exception exception)
  1534. {
  1535. m_log.ErrorFormat("[BASE HTTP SERVER]: {0} had an exception {1}", source.ToString(), exception.ToString());
  1536. /*
  1537. if (HTTPDRunning)// && NotSocketErrors > 5)
  1538. {
  1539. Stop();
  1540. Thread.Sleep(200);
  1541. StartHTTP();
  1542. m_log.Warn("[HTTPSERVER]: Died. Trying to kick.....");
  1543. }
  1544. */
  1545. }
  1546. public void Stop()
  1547. {
  1548. HTTPDRunning = false;
  1549. try
  1550. {
  1551. m_httpListener2.ExceptionThrown -= httpServerException;
  1552. //m_httpListener2.DisconnectHandler = null;
  1553. m_httpListener2.LogWriter = null;
  1554. m_httpListener2.RequestReceived -= OnRequest;
  1555. m_httpListener2.Stop();
  1556. }
  1557. catch (NullReferenceException)
  1558. {
  1559. m_log.Warn("[BASE HTTP SERVER]: Null Reference when stopping HttpServer.");
  1560. }
  1561. }
  1562. public void RemoveStreamHandler(string httpMethod, string path)
  1563. {
  1564. string handlerKey = GetHandlerKey(httpMethod, path);
  1565. //m_log.DebugFormat("[BASE HTTP SERVER]: Removing handler key {0}", handlerKey);
  1566. lock (m_streamHandlers)
  1567. m_streamHandlers.Remove(handlerKey);
  1568. }
  1569. public void RemoveHTTPHandler(string httpMethod, string path)
  1570. {
  1571. lock (m_HTTPHandlers)
  1572. {
  1573. if (httpMethod != null && httpMethod.Length == 0)
  1574. {
  1575. m_HTTPHandlers.Remove(path);
  1576. return;
  1577. }
  1578. m_HTTPHandlers.Remove(GetHandlerKey(httpMethod, path));
  1579. }
  1580. }
  1581. public void RemovePollServiceHTTPHandler(string httpMethod, string path)
  1582. {
  1583. lock (m_pollHandlers)
  1584. m_pollHandlers.Remove(path);
  1585. }
  1586. public bool RemoveAgentHandler(string agent, IHttpAgentHandler handler)
  1587. {
  1588. lock (m_agentHandlers)
  1589. {
  1590. IHttpAgentHandler foundHandler;
  1591. if (m_agentHandlers.TryGetValue(agent, out foundHandler) && foundHandler == handler)
  1592. {
  1593. m_agentHandlers.Remove(agent);
  1594. return true;
  1595. }
  1596. }
  1597. return false;
  1598. }
  1599. public void RemoveXmlRPCHandler(string method)
  1600. {
  1601. lock (m_rpcHandlers)
  1602. m_rpcHandlers.Remove(method);
  1603. }
  1604. public bool RemoveLLSDHandler(string path, LLSDMethod handler)
  1605. {
  1606. lock (m_llsdHandlers)
  1607. {
  1608. LLSDMethod foundHandler;
  1609. if (m_llsdHandlers.TryGetValue(path, out foundHandler) && foundHandler == handler)
  1610. {
  1611. m_llsdHandlers.Remove(path);
  1612. return true;
  1613. }
  1614. }
  1615. return false;
  1616. }
  1617. public string GetHTTP404(string host)
  1618. {
  1619. string file = Path.Combine(".", "http_404.html");
  1620. if (!File.Exists(file))
  1621. return getDefaultHTTP404(host);
  1622. StreamReader sr = File.OpenText(file);
  1623. string result = sr.ReadToEnd();
  1624. sr.Close();
  1625. return result;
  1626. }
  1627. public string GetHTTP500()
  1628. {
  1629. string file = Path.Combine(".", "http_500.html");
  1630. if (!File.Exists(file))
  1631. return getDefaultHTTP500();
  1632. StreamReader sr = File.OpenText(file);
  1633. string result = sr.ReadToEnd();
  1634. sr.Close();
  1635. return result;
  1636. }
  1637. // Fallback HTTP responses in case the HTTP error response files don't exist
  1638. private static string getDefaultHTTP404(string host)
  1639. {
  1640. 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: &quot;-loginpage http://" + host + "/?method=login -loginuri http://" + host + "/&quot; in your link </P></BODY></HTML>";
  1641. }
  1642. private static string getDefaultHTTP500()
  1643. {
  1644. 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>";
  1645. }
  1646. }
  1647. public class HttpServerContextObj
  1648. {
  1649. public IHttpClientContext context = null;
  1650. public IHttpRequest req = null;
  1651. public OSHttpRequest oreq = null;
  1652. public OSHttpResponse oresp = null;
  1653. public HttpServerContextObj(IHttpClientContext contxt, IHttpRequest reqs)
  1654. {
  1655. context = contxt;
  1656. req = reqs;
  1657. }
  1658. public HttpServerContextObj(OSHttpRequest osreq, OSHttpResponse osresp)
  1659. {
  1660. oreq = osreq;
  1661. oresp = osresp;
  1662. }
  1663. }
  1664. /// <summary>
  1665. /// Relays HttpServer log messages to our own logging mechanism.
  1666. /// </summary>
  1667. /// To use this you must uncomment the switch section
  1668. ///
  1669. /// You may also be able to get additional trace information from HttpServer if you uncomment the UseTraceLogs
  1670. /// property in StartHttp() for the HttpListener
  1671. public class HttpServerLogWriter : ILogWriter
  1672. {
  1673. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1674. public void Write(object source, LogPrio priority, string message)
  1675. {
  1676. /*
  1677. switch (priority)
  1678. {
  1679. case LogPrio.Trace:
  1680. m_log.DebugFormat("[{0}]: {1}", source, message);
  1681. break;
  1682. case LogPrio.Debug:
  1683. m_log.DebugFormat("[{0}]: {1}", source, message);
  1684. break;
  1685. case LogPrio.Error:
  1686. m_log.ErrorFormat("[{0}]: {1}", source, message);
  1687. break;
  1688. case LogPrio.Info:
  1689. m_log.InfoFormat("[{0}]: {1}", source, message);
  1690. break;
  1691. case LogPrio.Warning:
  1692. m_log.WarnFormat("[{0}]: {1}", source, message);
  1693. break;
  1694. case LogPrio.Fatal:
  1695. m_log.ErrorFormat("[{0}]: FATAL! - {1}", source, message);
  1696. break;
  1697. default:
  1698. break;
  1699. }
  1700. */
  1701. return;
  1702. }
  1703. }
  1704. }