BaseHttpServer.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Threading;
  36. using System.Xml;
  37. using OpenMetaverse.StructuredData;
  38. using log4net;
  39. using Nwc.XmlRpc;
  40. using CoolHTTPListener = HttpServer.HttpListener;
  41. using IHttpClientContext = HttpServer.IHttpClientContext;
  42. using IHttpRequest = HttpServer.IHttpRequest;
  43. namespace OpenSim.Framework.Servers
  44. {
  45. public class BaseHttpServer
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private HttpServerLogWriter httpserverlog = new HttpServerLogWriter();
  49. private volatile int NotSocketErrors = 0;
  50. public volatile bool HTTPDRunning = false;
  51. protected Thread m_workerThread;
  52. protected HttpListener m_httpListener;
  53. protected CoolHTTPListener m_httpListener2;
  54. protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
  55. protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>();
  56. protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/
  57. protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>();
  58. protected Dictionary<string, IRequestHandler> m_streamHandlers = new Dictionary<string, IRequestHandler>();
  59. protected Dictionary<string, GenericHTTPMethod> m_HTTPHandlers = new Dictionary<string, GenericHTTPMethod>();
  60. protected Dictionary<string, IHttpAgentHandler> m_agentHandlers = new Dictionary<string, IHttpAgentHandler>();
  61. protected uint m_port;
  62. protected uint m_sslport;
  63. protected bool m_ssl;
  64. protected bool m_firstcaps = true;
  65. protected string m_SSLCommonName = "";
  66. public uint SSLPort
  67. {
  68. get { return m_sslport; }
  69. }
  70. public string SSLCommonName
  71. {
  72. get { return m_SSLCommonName; }
  73. }
  74. public uint Port
  75. {
  76. get { return m_port; }
  77. }
  78. public bool UseSSL
  79. {
  80. get { return m_ssl; }
  81. }
  82. public BaseHttpServer(uint port)
  83. {
  84. m_port = port;
  85. }
  86. public BaseHttpServer(uint port, bool ssl)
  87. {
  88. m_ssl = ssl;
  89. m_port = port;
  90. }
  91. public BaseHttpServer(uint port, bool ssl, uint sslport, string CN)
  92. {
  93. m_ssl = ssl;
  94. m_port = port;
  95. if (m_ssl)
  96. {
  97. m_sslport = sslport;
  98. }
  99. }
  100. /// <summary>
  101. /// Add a stream handler to the http server. If the handler already exists, then nothing happens.
  102. /// </summary>
  103. /// <param name="handler"></param>
  104. public void AddStreamHandler(IRequestHandler handler)
  105. {
  106. string httpMethod = handler.HttpMethod;
  107. string path = handler.Path;
  108. string handlerKey = GetHandlerKey(httpMethod, path);
  109. lock (m_streamHandlers)
  110. {
  111. if (!m_streamHandlers.ContainsKey(handlerKey))
  112. {
  113. // m_log.DebugFormat("[BASE HTTP SERVER]: Adding handler key {0}", handlerKey);
  114. m_streamHandlers.Add(handlerKey, handler);
  115. }
  116. }
  117. }
  118. private static string GetHandlerKey(string httpMethod, string path)
  119. {
  120. return httpMethod + ":" + path;
  121. }
  122. public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
  123. {
  124. lock (m_rpcHandlers)
  125. {
  126. m_rpcHandlers[method] = handler;
  127. m_rpcHandlersKeepAlive[method] = true; // default
  128. return true;
  129. }
  130. }
  131. public bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive)
  132. {
  133. lock (m_rpcHandlers)
  134. {
  135. m_rpcHandlers[method] = handler;
  136. m_rpcHandlersKeepAlive[method] = keepAlive; // default
  137. return true;
  138. }
  139. }
  140. public bool AddHTTPHandler(string method, GenericHTTPMethod handler)
  141. {
  142. lock (m_HTTPHandlers)
  143. {
  144. if (!m_HTTPHandlers.ContainsKey(method))
  145. {
  146. m_HTTPHandlers.Add(method, handler);
  147. return true;
  148. }
  149. }
  150. //must already have a handler for that path so return false
  151. return false;
  152. }
  153. // Note that the agent string is provided simply to differentiate
  154. // the handlers - it is NOT required to be an actual agent header
  155. // value.
  156. public bool AddAgentHandler(string agent, IHttpAgentHandler handler)
  157. {
  158. lock (m_agentHandlers)
  159. {
  160. if (!m_agentHandlers.ContainsKey(agent))
  161. {
  162. m_agentHandlers.Add(agent, handler);
  163. return true;
  164. }
  165. }
  166. //must already have a handler for that path so return false
  167. return false;
  168. }
  169. /// <summary>
  170. /// Adds a LLSD handler, yay.
  171. /// </summary>
  172. /// <param name="path">/resource/ path</param>
  173. /// <param name="handler">handle the LLSD response</param>
  174. /// <returns></returns>
  175. public bool AddLLSDHandler(string path, LLSDMethod handler)
  176. {
  177. lock (m_llsdHandlers)
  178. {
  179. if (!m_llsdHandlers.ContainsKey(path))
  180. {
  181. m_llsdHandlers.Add(path, handler);
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. public bool SetDefaultLLSDHandler(DefaultLLSDMethod handler)
  188. {
  189. m_defaultLlsdHandler = handler;
  190. return true;
  191. }
  192. public void OnHandleRequestIOThread(IHttpClientContext context, IHttpRequest request)
  193. {
  194. OSHttpRequest req = new OSHttpRequest(context, request);
  195. OSHttpResponse resp = new OSHttpResponse(new HttpServer.HttpResponse(context, request));
  196. //resp.KeepAlive = req.KeepAlive;
  197. //m_log.Info("[Debug BASE HTTP SERVER]: Got Request");
  198. //HttpServerContextObj objstate= new HttpServerContextObj(req,resp);
  199. //ThreadPool.QueueUserWorkItem(new WaitCallback(ConvertIHttpClientContextToOSHttp), (object)objstate);
  200. HandleRequest(req, resp);
  201. }
  202. public void ConvertIHttpClientContextToOSHttp(object stateinfo)
  203. {
  204. HttpServerContextObj objstate = (HttpServerContextObj)stateinfo;
  205. //OSHttpRequest request = new OSHttpRequest(objstate.context,objstate.req);
  206. //OSHttpResponse resp = new OSHttpResponse(new HttpServer.HttpResponse(objstate.context, objstate.req));
  207. OSHttpRequest request = objstate.oreq;
  208. OSHttpResponse resp = objstate.oresp;
  209. //OSHttpResponse resp = new OSHttpResponse(new HttpServer.HttpResponse(objstate.context, objstate.req));
  210. /*
  211. request.AcceptTypes = objstate.req.AcceptTypes;
  212. request.ContentLength = (long)objstate.req.ContentLength;
  213. request.Headers = objstate.req.Headers;
  214. request.HttpMethod = objstate.req.Method;
  215. request.InputStream = objstate.req.Body;
  216. foreach (string str in request.Headers)
  217. {
  218. if (str.ToLower().Contains("content-type: "))
  219. {
  220. request.ContentType = str.Substring(13, str.Length - 13);
  221. break;
  222. }
  223. }
  224. //request.KeepAlive = objstate.req.
  225. foreach (HttpServer.HttpInput httpinput in objstate.req.QueryString)
  226. {
  227. request.QueryString.Add(httpinput.Name, httpinput[httpinput.Name]);
  228. }
  229. //request.Query = objstate.req.//objstate.req.QueryString;
  230. //foreach (
  231. //request.QueryString = objstate.req.QueryString;
  232. */
  233. HandleRequest(request,resp);
  234. }
  235. public virtual void HandleRequest(OSHttpRequest request, OSHttpResponse response)
  236. {
  237. try
  238. {
  239. Culture.SetCurrentCulture();
  240. // This is the REST agent interface. We require an agent to properly identify
  241. // itself. If the REST handler recognizes the prefix it will attempt to
  242. // satisfy the request. If it is not recognizable, and no damage has occurred
  243. // the request can be passed through to the other handlers. This is a low
  244. // probability event; if a request is matched it is normally expected to be
  245. // handled
  246. //m_log.Debug("[BASE HTTP SERVER]: Handling Request" + request.RawUrl);
  247. IHttpAgentHandler agentHandler;
  248. if (TryGetAgentHandler(request, response, out agentHandler))
  249. {
  250. if (HandleAgentRequest(agentHandler, request, response))
  251. {
  252. return;
  253. }
  254. }
  255. IRequestHandler requestHandler;
  256. //response.KeepAlive = true;
  257. response.SendChunked = false;
  258. string path = request.RawUrl;
  259. string handlerKey = GetHandlerKey(request.HttpMethod, path);
  260. //m_log.DebugFormat("[BASE HTTP SERVER]: Handling {0} request for {1}", request.HttpMethod, path);
  261. if (TryGetStreamHandler(handlerKey, out requestHandler))
  262. {
  263. //m_log.Debug("[BASE HTTP SERVER]: Found Stream Handler");
  264. // Okay, so this is bad, but should be considered temporary until everything is IStreamHandler.
  265. byte[] buffer;
  266. if (requestHandler is IStreamedRequestHandler)
  267. {
  268. IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler;
  269. buffer = streamedRequestHandler.Handle(path, request.InputStream, request, response);
  270. }
  271. else if (requestHandler is IGenericHTTPHandler)
  272. {
  273. //m_log.Debug("[BASE HTTP SERVER]: Found Caps based HTTP Handler");
  274. IGenericHTTPHandler HTTPRequestHandler = requestHandler as IGenericHTTPHandler;
  275. Stream requestStream = request.InputStream;
  276. Encoding encoding = Encoding.UTF8;
  277. StreamReader reader = new StreamReader(requestStream, encoding);
  278. string requestBody = reader.ReadToEnd();
  279. reader.Close();
  280. requestStream.Close();
  281. Hashtable keysvals = new Hashtable();
  282. Hashtable headervals = new Hashtable();
  283. //string host = String.Empty;
  284. string[] querystringkeys = request.QueryString.AllKeys;
  285. string[] rHeaders = request.Headers.AllKeys;
  286. foreach (string queryname in querystringkeys)
  287. {
  288. keysvals.Add(queryname, request.QueryString[queryname]);
  289. }
  290. foreach (string headername in rHeaders)
  291. {
  292. //m_log.Warn("[HEADER]: " + headername + "=" + request.Headers[headername]);
  293. headervals[headername] = request.Headers[headername];
  294. }
  295. // if (headervals.Contains("Host"))
  296. // {
  297. // host = (string)headervals["Host"];
  298. // }
  299. keysvals.Add("requestbody", requestBody);
  300. if (keysvals.Contains("method"))
  301. {
  302. //m_log.Warn("[HTTP]: Contains Method");
  303. //string method = (string)keysvals["method"];
  304. //m_log.Warn("[HTTP]: " + requestBody);
  305. }
  306. DoHTTPGruntWork(HTTPRequestHandler.Handle(path, keysvals), response);
  307. return;
  308. }
  309. else
  310. {
  311. IStreamHandler streamHandler = (IStreamHandler)requestHandler;
  312. using (MemoryStream memoryStream = new MemoryStream())
  313. {
  314. streamHandler.Handle(path, request.InputStream, memoryStream, request, response);
  315. memoryStream.Flush();
  316. buffer = memoryStream.ToArray();
  317. }
  318. }
  319. request.InputStream.Close();
  320. if (!response.IsContentTypeSet) response.ContentType = requestHandler.ContentType;
  321. if (!response.SendChunked)
  322. response.ContentLength64 = buffer.LongLength;
  323. try
  324. {
  325. response.OutputStream.Write(buffer, 0, buffer.Length);
  326. //response.OutputStream.Close();
  327. }
  328. catch (HttpListenerException)
  329. {
  330. m_log.WarnFormat("[BASE HTTP SERVER]: HTTP request abnormally terminated.");
  331. }
  332. //response.OutputStream.Close();
  333. try
  334. {
  335. response.Send();
  336. }
  337. catch (SocketException e)
  338. {
  339. // This has to be here to prevent a Linux/Mono crash
  340. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  341. }
  342. return;
  343. }
  344. if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
  345. {
  346. foreach (string strAccept in request.AcceptTypes)
  347. {
  348. if (strAccept.Contains("application/llsd+xml"))
  349. {
  350. //m_log.Info("[Debug BASE HTTP SERVER]: Found an application/llsd+xml accept header");
  351. HandleLLSDRequests(request, response);
  352. return;
  353. }
  354. }
  355. }
  356. switch (request.ContentType)
  357. {
  358. case null:
  359. case "text/html":
  360. //m_log.Info("[Debug BASE HTTP SERVER]: found a text/html content type");
  361. HandleHTTPRequest(request, response);
  362. return;
  363. case "application/llsd+xml":
  364. case "application/xml+llsd":
  365. //m_log.Info("[Debug BASE HTTP SERVER]: found a application/llsd+xml content type");
  366. HandleLLSDRequests(request, response);
  367. return;
  368. case "text/xml":
  369. case "application/xml":
  370. default:
  371. //m_log.Info("[Debug BASE HTTP SERVER]: in default handler");
  372. // Point of note.. the DoWeHaveA methods check for an EXACT path
  373. // if (request.RawUrl.Contains("/CAPS/EQG"))
  374. // {
  375. // int i = 1;
  376. // }
  377. //m_log.Info("[Debug BASE HTTP SERVER]: Checking for LLSD Handler");
  378. if (DoWeHaveALLSDHandler(request.RawUrl))
  379. {
  380. //m_log.Info("[Debug BASE HTTP SERVER]: Found LLSD Handler");
  381. HandleLLSDRequests(request, response);
  382. return;
  383. }
  384. //m_log.Info("[Debug BASE HTTP SERVER]: Checking for HTTP Handler");
  385. if (DoWeHaveAHTTPHandler(request.RawUrl))
  386. {
  387. //m_log.Info("[Debug BASE HTTP SERVER]: found HTTP Handler");
  388. HandleHTTPRequest(request, response);
  389. return;
  390. }
  391. //m_log.Info("[Debug BASE HTTP SERVER]: Generic XMLRPC");
  392. // generic login request.
  393. HandleXmlRpcRequests(request, response);
  394. return;
  395. }
  396. }
  397. catch (SocketException e)
  398. {
  399. // At least on linux, it appears that if the client makes a request without requiring the response,
  400. // an unconnected socket exception is thrown when we close the response output stream. There's no
  401. // obvious way to tell if the client didn't require the response, so instead we'll catch and ignore
  402. // the exception instead.
  403. //
  404. // An alternative may be to turn off all response write exceptions on the HttpListener, but let's go
  405. // with the minimum first
  406. m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e);
  407. }
  408. catch (EndOfStreamException e)
  409. {
  410. m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0}", e);
  411. }
  412. catch (InvalidOperationException e)
  413. {
  414. m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0}", e);
  415. SendHTML500(response);
  416. }
  417. }
  418. private bool TryGetStreamHandler(string handlerKey, out IRequestHandler streamHandler)
  419. {
  420. string bestMatch = null;
  421. foreach (string pattern in m_streamHandlers.Keys)
  422. {
  423. if (handlerKey.StartsWith(pattern))
  424. {
  425. if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
  426. {
  427. bestMatch = pattern;
  428. }
  429. }
  430. }
  431. if (String.IsNullOrEmpty(bestMatch))
  432. {
  433. streamHandler = null;
  434. return false;
  435. }
  436. else
  437. {
  438. streamHandler = m_streamHandlers[bestMatch];
  439. return true;
  440. }
  441. }
  442. private bool TryGetHTTPHandler(string handlerKey, out GenericHTTPMethod HTTPHandler)
  443. {
  444. string bestMatch = null;
  445. foreach (string pattern in m_HTTPHandlers.Keys)
  446. {
  447. if (handlerKey.StartsWith(pattern))
  448. {
  449. if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length)
  450. {
  451. bestMatch = pattern;
  452. }
  453. }
  454. }
  455. if (String.IsNullOrEmpty(bestMatch))
  456. {
  457. HTTPHandler = null;
  458. return false;
  459. }
  460. else
  461. {
  462. HTTPHandler = m_HTTPHandlers[bestMatch];
  463. return true;
  464. }
  465. }
  466. private bool TryGetAgentHandler(OSHttpRequest request, OSHttpResponse response, out IHttpAgentHandler agentHandler)
  467. {
  468. agentHandler = null;
  469. try
  470. {
  471. foreach (IHttpAgentHandler handler in m_agentHandlers.Values)
  472. {
  473. if (handler.Match(request, response))
  474. {
  475. agentHandler = handler;
  476. return true;
  477. }
  478. }
  479. }
  480. catch(KeyNotFoundException)
  481. {
  482. }
  483. return false;
  484. }
  485. /// <summary>
  486. /// Try all the registered xmlrpc handlers when an xmlrpc request is received.
  487. /// Sends back an XMLRPC unknown request response if no handler is registered for the requested method.
  488. /// </summary>
  489. /// <param name="request"></param>
  490. /// <param name="response"></param>
  491. private void HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response)
  492. {
  493. Stream requestStream = request.InputStream;
  494. Encoding encoding = Encoding.UTF8;
  495. StreamReader reader = new StreamReader(requestStream, encoding);
  496. string requestBody = reader.ReadToEnd();
  497. reader.Close();
  498. requestStream.Close();
  499. string responseString = String.Empty;
  500. XmlRpcRequest xmlRprcRequest = null;
  501. try
  502. {
  503. xmlRprcRequest = (XmlRpcRequest) (new XmlRpcRequestDeserializer()).Deserialize(requestBody);
  504. }
  505. catch (XmlException)
  506. {
  507. }
  508. if (xmlRprcRequest != null)
  509. {
  510. string methodName = xmlRprcRequest.MethodName;
  511. if (methodName != null)
  512. {
  513. xmlRprcRequest.Params.Add(request.RemoteIPEndPoint); // Param[1]
  514. XmlRpcResponse xmlRpcResponse;
  515. XmlRpcMethod method;
  516. if (m_rpcHandlers.TryGetValue(methodName, out method))
  517. {
  518. xmlRprcRequest.Params.Add(request.Url); // Param[2]
  519. try
  520. {
  521. xmlRpcResponse = method(xmlRprcRequest);
  522. }
  523. catch(Exception e)
  524. {
  525. // if the registered XmlRpc method threw an exception, we pass a fault-code along
  526. xmlRpcResponse = new XmlRpcResponse();
  527. // Code probably set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
  528. xmlRpcResponse.SetFault(-32603, String.Format("Requested method [{0}] threw exception: {1}",
  529. methodName, e.Message));
  530. }
  531. // if the method wasn't found, we can't determine KeepAlive state anyway, so lets do it only here
  532. response.KeepAlive = m_rpcHandlersKeepAlive[methodName];
  533. }
  534. else
  535. {
  536. xmlRpcResponse = new XmlRpcResponse();
  537. // Code set in accordance with http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
  538. xmlRpcResponse.SetFault(-32601, String.Format("Requested method [{0}] not found", methodName));
  539. }
  540. responseString = XmlRpcResponseSerializer.Singleton.Serialize(xmlRpcResponse);
  541. }
  542. else
  543. {
  544. //HandleLLSDRequests(request, response);
  545. response.ContentType = "text/plain";
  546. response.StatusCode = 404;
  547. response.StatusDescription = "Not Found";
  548. response.ProtocolVersion = "HTTP/1.0";
  549. byte[] buf = Encoding.UTF8.GetBytes("Not found");
  550. response.KeepAlive = false;
  551. m_log.ErrorFormat("[BASE HTTP SERVER] Handler not found for http request {0}", request.RawUrl);
  552. response.SendChunked = false;
  553. response.ContentLength64 = buf.Length;
  554. response.ContentEncoding = Encoding.UTF8;
  555. try
  556. {
  557. response.OutputStream.Write(buf, 0, buf.Length);
  558. }
  559. catch (Exception ex)
  560. {
  561. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  562. }
  563. finally
  564. {
  565. try
  566. {
  567. response.Send();
  568. }
  569. catch (SocketException e)
  570. {
  571. // This has to be here to prevent a Linux/Mono crash
  572. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  573. }
  574. }
  575. return;
  576. //responseString = "Error";
  577. }
  578. }
  579. response.ContentType = "text/xml";
  580. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  581. response.SendChunked = false;
  582. response.ContentLength64 = buffer.Length;
  583. response.ContentEncoding = Encoding.UTF8;
  584. try
  585. {
  586. response.OutputStream.Write(buffer, 0, buffer.Length);
  587. }
  588. catch (Exception ex)
  589. {
  590. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  591. }
  592. finally
  593. {
  594. try
  595. {
  596. response.Send();
  597. }
  598. catch (SocketException e)
  599. {
  600. // This has to be here to prevent a Linux/Mono crash
  601. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  602. }
  603. }
  604. }
  605. private void HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response)
  606. {
  607. //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request");
  608. Stream requestStream = request.InputStream;
  609. Encoding encoding = Encoding.UTF8;
  610. StreamReader reader = new StreamReader(requestStream, encoding);
  611. string requestBody = reader.ReadToEnd();
  612. reader.Close();
  613. requestStream.Close();
  614. //m_log.DebugFormat("[OGP]: {0}:{1}", request.RawUrl, requestBody);
  615. response.KeepAlive = true;
  616. OSD llsdRequest = null;
  617. OSD llsdResponse = null;
  618. bool LegacyLLSDLoginLibOMV = (requestBody.Contains("passwd") && requestBody.Contains("mac") && requestBody.Contains("viewer_digest"));
  619. if (requestBody.Length == 0)
  620. // Get Request
  621. {
  622. requestBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd><map><key>request</key><string>get</string></map></llsd>";
  623. }
  624. try
  625. {
  626. llsdRequest = OSDParser.DeserializeLLSDXml(requestBody);
  627. }
  628. catch (Exception ex)
  629. {
  630. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  631. }
  632. if (llsdRequest != null)// && m_defaultLlsdHandler != null)
  633. {
  634. LLSDMethod llsdhandler = null;
  635. if (TryGetLLSDHandler(request.RawUrl, out llsdhandler) && !LegacyLLSDLoginLibOMV)
  636. {
  637. // we found a registered llsd handler to service this request
  638. llsdResponse = llsdhandler(request.RawUrl, llsdRequest, request.RemoteIPEndPoint.ToString());
  639. }
  640. else
  641. {
  642. // we didn't find a registered llsd handler to service this request
  643. // check if we have a default llsd handler
  644. if (m_defaultLlsdHandler != null)
  645. {
  646. // LibOMV path
  647. llsdResponse = m_defaultLlsdHandler(llsdRequest);
  648. }
  649. else
  650. {
  651. // Oops, no handler for this.. give em the failed message
  652. llsdResponse = GenerateNoLLSDHandlerResponse();
  653. }
  654. }
  655. }
  656. else
  657. {
  658. llsdResponse = GenerateNoLLSDHandlerResponse();
  659. }
  660. byte[] buffer = new byte[0];
  661. if (llsdResponse.ToString() == "shutdown404!")
  662. {
  663. response.ContentType = "text/plain";
  664. response.StatusCode = 404;
  665. response.StatusDescription = "Not Found";
  666. response.ProtocolVersion = "HTTP/1.0";
  667. buffer = Encoding.UTF8.GetBytes("Not found");
  668. }
  669. else
  670. {
  671. response.ContentType = "application/llsd+xml";
  672. //m_log.Info("[Debug BASE HTTP SERVER]: Response: " + llsdResponse.ToString());
  673. buffer = OSDParser.SerializeLLSDXmlBytes(llsdResponse);
  674. }
  675. response.SendChunked = false;
  676. response.ContentLength64 = buffer.Length;
  677. response.ContentEncoding = Encoding.UTF8;
  678. response.KeepAlive = true;
  679. try
  680. {
  681. response.OutputStream.Write(buffer, 0, buffer.Length);
  682. }
  683. catch (Exception ex)
  684. {
  685. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  686. }
  687. finally
  688. {
  689. //response.OutputStream.Close();
  690. try
  691. {
  692. response.Send();
  693. response.OutputStream.Flush();
  694. response.OutputStream.Close();
  695. }
  696. catch (IOException e)
  697. {
  698. m_log.DebugFormat("[BASE HTTP SERVER] LLSD IOException {0}.", e);
  699. }
  700. catch (SocketException e)
  701. {
  702. // This has to be here to prevent a Linux/Mono crash
  703. m_log.WarnFormat("[BASE HTTP SERVER] LLSD issue {0}.\nNOTE: this may be spurious on Linux.", e);
  704. }
  705. }
  706. }
  707. /// <summary>
  708. /// Checks if we have an Exact path in the LLSD handlers for the path provided
  709. /// </summary>
  710. /// <param name="path">URI of the request</param>
  711. /// <returns>true if we have one, false if not</returns>
  712. private bool DoWeHaveALLSDHandler(string path)
  713. {
  714. string[] pathbase = path.Split('/');
  715. string searchquery = "/";
  716. if (pathbase.Length < 1)
  717. return false;
  718. for (int i = 1; i < pathbase.Length; i++)
  719. {
  720. searchquery += pathbase[i];
  721. if (pathbase.Length - 1 != i)
  722. searchquery += "/";
  723. }
  724. string bestMatch = null;
  725. foreach (string pattern in m_llsdHandlers.Keys)
  726. {
  727. if (searchquery.StartsWith(pattern) && searchquery.Length >= pattern.Length)
  728. {
  729. bestMatch = pattern;
  730. }
  731. }
  732. // extra kicker to remove the default XMLRPC login case.. just in case..
  733. if (path != "/" && bestMatch == "/" && searchquery != "/")
  734. return false;
  735. if (path == "/")
  736. return false;
  737. if (String.IsNullOrEmpty(bestMatch))
  738. {
  739. return false;
  740. }
  741. else
  742. {
  743. return true;
  744. }
  745. }
  746. /// <summary>
  747. /// Checks if we have an Exact path in the HTTP handlers for the path provided
  748. /// </summary>
  749. /// <param name="path">URI of the request</param>
  750. /// <returns>true if we have one, false if not</returns>
  751. private bool DoWeHaveAHTTPHandler(string path)
  752. {
  753. string[] pathbase = path.Split('/');
  754. string searchquery = "/";
  755. if (pathbase.Length < 1)
  756. return false;
  757. for (int i = 1; i < pathbase.Length; i++)
  758. {
  759. searchquery += pathbase[i];
  760. if (pathbase.Length - 1 != i)
  761. searchquery += "/";
  762. }
  763. string bestMatch = null;
  764. foreach (string pattern in m_HTTPHandlers.Keys)
  765. {
  766. if (searchquery.StartsWith(pattern) && searchquery.Length >= pattern.Length)
  767. {
  768. bestMatch = pattern;
  769. }
  770. }
  771. // extra kicker to remove the default XMLRPC login case.. just in case..
  772. if (path == "/")
  773. return false;
  774. if (String.IsNullOrEmpty(bestMatch))
  775. {
  776. return false;
  777. }
  778. else
  779. {
  780. return true;
  781. }
  782. }
  783. private bool TryGetLLSDHandler(string path, out LLSDMethod llsdHandler)
  784. {
  785. llsdHandler = null;
  786. // Pull out the first part of the path
  787. // splitting the path by '/' means we'll get the following return..
  788. // {0}/{1}/{2}
  789. // where {0} isn't something we really control 100%
  790. string[] pathbase = path.Split('/');
  791. string searchquery = "/";
  792. if (pathbase.Length < 1)
  793. return false;
  794. for (int i=1; i<pathbase.Length; i++)
  795. {
  796. searchquery += pathbase[i];
  797. if (pathbase.Length-1 != i)
  798. searchquery += "/";
  799. }
  800. // while the matching algorithm below doesn't require it, we're expecting a query in the form
  801. //
  802. // [] = optional
  803. // /resource/UUID/action[/action]
  804. //
  805. // now try to get the closest match to the reigstered path
  806. // at least for OGP, registered path would probably only consist of the /resource/
  807. string bestMatch = null;
  808. foreach (string pattern in m_llsdHandlers.Keys)
  809. {
  810. if (searchquery.ToLower().StartsWith(pattern.ToLower()))
  811. {
  812. if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
  813. {
  814. // You have to specifically register for '/' and to get it, you must specificaly request it
  815. //
  816. if (pattern == "/" && searchquery == "/" || pattern != "/")
  817. bestMatch = pattern;
  818. }
  819. }
  820. }
  821. if (String.IsNullOrEmpty(bestMatch))
  822. {
  823. llsdHandler = null;
  824. return false;
  825. }
  826. else
  827. {
  828. llsdHandler = m_llsdHandlers[bestMatch];
  829. return true;
  830. }
  831. }
  832. private OSDMap GenerateNoLLSDHandlerResponse()
  833. {
  834. OSDMap map = new OSDMap();
  835. map["reason"] = OSD.FromString("LLSDRequest");
  836. map["message"] = OSD.FromString("No handler registered for LLSD Requests");
  837. map["login"] = OSD.FromString("false");
  838. return map;
  839. }
  840. /// <summary>
  841. /// A specific agent handler was provided. Such a handler is expecetd to have an
  842. /// intimate, and highly specific relationship with the client. Consequently,
  843. /// nothing is done here.
  844. /// </summary>
  845. /// <param name="handler"></param>
  846. /// <param name="request"></param>
  847. /// <param name="response"></param>
  848. private bool HandleAgentRequest(IHttpAgentHandler handler, OSHttpRequest request, OSHttpResponse response)
  849. {
  850. // In the case of REST, then handler is responsible for ALL aspects of
  851. // the request/response handling. Nothing is done here, not even encoding.
  852. try
  853. {
  854. return handler.Handle(request, response);
  855. }
  856. catch (Exception e)
  857. {
  858. // If the handler did in fact close the stream, then this will blow
  859. // chunks. So that that doesn't disturb anybody we throw away any
  860. // and all exceptions raised. We've done our best to release the
  861. // client.
  862. try
  863. {
  864. m_log.Warn("[HTTP-AGENT]: Error - " + e.Message);
  865. response.SendChunked = false;
  866. response.KeepAlive = true;
  867. response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError;
  868. //response.OutputStream.Close();
  869. try
  870. {
  871. response.Send();
  872. }
  873. catch (SocketException f)
  874. {
  875. // This has to be here to prevent a Linux/Mono crash
  876. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", f);
  877. }
  878. }
  879. catch(Exception)
  880. {
  881. }
  882. }
  883. // Indicate that the request has been "handled"
  884. return true;
  885. }
  886. public void HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response)
  887. {
  888. switch (request.HttpMethod)
  889. {
  890. case "OPTIONS":
  891. response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
  892. return;
  893. default:
  894. HandleContentVerbs(request, response);
  895. return;
  896. }
  897. }
  898. private void HandleContentVerbs(OSHttpRequest request, OSHttpResponse response)
  899. {
  900. // This is a test. There's a workable alternative.. as this way sucks.
  901. // We'd like to put this into a text file parhaps that's easily editable.
  902. //
  903. // For this test to work, I used the following secondlife.exe parameters
  904. // "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
  905. //
  906. // Even after all that, there's still an error, but it's a start.
  907. //
  908. // I depend on show_login_form being in the secondlife.exe parameters to figure out
  909. // to display the form, or process it.
  910. // a better way would be nifty.
  911. Stream requestStream = request.InputStream;
  912. Encoding encoding = Encoding.UTF8;
  913. StreamReader reader = new StreamReader(requestStream, encoding);
  914. string requestBody = reader.ReadToEnd();
  915. // avoid warning for now
  916. reader.ReadToEnd();
  917. reader.Close();
  918. requestStream.Close();
  919. Hashtable keysvals = new Hashtable();
  920. Hashtable headervals = new Hashtable();
  921. Hashtable requestVars = new Hashtable();
  922. string host = String.Empty;
  923. string[] querystringkeys = request.QueryString.AllKeys;
  924. string[] rHeaders = request.Headers.AllKeys;
  925. keysvals.Add("body", requestBody);
  926. keysvals.Add("uri", request.RawUrl);
  927. keysvals.Add("content-type", request.ContentType);
  928. keysvals.Add("http-method", request.HttpMethod);
  929. foreach (string queryname in querystringkeys)
  930. {
  931. keysvals.Add(queryname, request.QueryString[queryname]);
  932. requestVars.Add(queryname, keysvals[queryname]);
  933. }
  934. foreach (string headername in rHeaders)
  935. {
  936. //m_log.Warn("[HEADER]: " + headername + "=" + request.Headers[headername]);
  937. headervals[headername] = request.Headers[headername];
  938. }
  939. if (headervals.Contains("Host"))
  940. {
  941. host = (string)headervals["Host"];
  942. }
  943. keysvals.Add("headers",headervals);
  944. keysvals.Add("querystringkeys", querystringkeys);
  945. keysvals.Add("requestvars", requestVars);
  946. if (keysvals.Contains("method"))
  947. {
  948. //m_log.Warn("[HTTP]: Contains Method");
  949. string method = (string) keysvals["method"];
  950. //m_log.Warn("[HTTP]: " + requestBody);
  951. GenericHTTPMethod requestprocessor;
  952. bool foundHandler = TryGetHTTPHandler(method, out requestprocessor);
  953. if (foundHandler)
  954. {
  955. Hashtable responsedata1 = requestprocessor(keysvals);
  956. DoHTTPGruntWork(responsedata1,response);
  957. //SendHTML500(response);
  958. }
  959. else
  960. {
  961. //m_log.Warn("[HTTP]: Handler Not Found");
  962. SendHTML404(response, host);
  963. }
  964. }
  965. else
  966. {
  967. GenericHTTPMethod requestprocessor;
  968. bool foundHandler = TryGetHTTPHandlerPathBased(request.RawUrl, out requestprocessor);
  969. if (foundHandler)
  970. {
  971. Hashtable responsedata2 = requestprocessor(keysvals);
  972. DoHTTPGruntWork(responsedata2, response);
  973. //SendHTML500(response);
  974. }
  975. else
  976. {
  977. //m_log.Warn("[HTTP]: Handler Not Found");
  978. SendHTML404(response, host);
  979. }
  980. }
  981. }
  982. private bool TryGetHTTPHandlerPathBased(string path, out GenericHTTPMethod httpHandler)
  983. {
  984. httpHandler = null;
  985. // Pull out the first part of the path
  986. // splitting the path by '/' means we'll get the following return..
  987. // {0}/{1}/{2}
  988. // where {0} isn't something we really control 100%
  989. string[] pathbase = path.Split('/');
  990. string searchquery = "/";
  991. if (pathbase.Length < 1)
  992. return false;
  993. for (int i = 1; i < pathbase.Length; i++)
  994. {
  995. searchquery += pathbase[i];
  996. if (pathbase.Length - 1 != i)
  997. searchquery += "/";
  998. }
  999. // while the matching algorithm below doesn't require it, we're expecting a query in the form
  1000. //
  1001. // [] = optional
  1002. // /resource/UUID/action[/action]
  1003. //
  1004. // now try to get the closest match to the reigstered path
  1005. // at least for OGP, registered path would probably only consist of the /resource/
  1006. string bestMatch = null;
  1007. foreach (string pattern in m_HTTPHandlers.Keys)
  1008. {
  1009. if (searchquery.ToLower().StartsWith(pattern.ToLower()))
  1010. {
  1011. if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
  1012. {
  1013. // You have to specifically register for '/' and to get it, you must specificaly request it
  1014. //
  1015. if (pattern == "/" && searchquery == "/" || pattern != "/")
  1016. bestMatch = pattern;
  1017. }
  1018. }
  1019. }
  1020. if (String.IsNullOrEmpty(bestMatch))
  1021. {
  1022. httpHandler = null;
  1023. return false;
  1024. }
  1025. else
  1026. {
  1027. if (bestMatch == "/" && searchquery != "/")
  1028. return false;
  1029. httpHandler = m_HTTPHandlers[bestMatch];
  1030. return true;
  1031. }
  1032. }
  1033. private static void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
  1034. {
  1035. //m_log.Info("[BASE HTTP SERVER]: Doing HTTP Grunt work with response");
  1036. int responsecode = (int)responsedata["int_response_code"];
  1037. string responseString = (string)responsedata["str_response_string"];
  1038. string contentType = (string)responsedata["content_type"];
  1039. if (responsedata.ContainsKey("error_status_text"))
  1040. {
  1041. response.StatusDescription = (string)responsedata["error_status_text"];
  1042. }
  1043. if (responsedata.ContainsKey("http_protocol_version"))
  1044. {
  1045. response.ProtocolVersion = (string)responsedata["http_protocol_version"];
  1046. }
  1047. if (responsedata.ContainsKey("keepalive"))
  1048. {
  1049. bool keepalive = (bool)responsedata["keepalive"];
  1050. response.KeepAlive = keepalive;
  1051. }
  1052. //Even though only one other part of the entire code uses HTTPHandlers, we shouldn't expect this
  1053. //and should check for NullReferenceExceptions
  1054. if (string.IsNullOrEmpty(contentType))
  1055. {
  1056. contentType = "text/html";
  1057. }
  1058. // The client ignores anything but 200 here for web login, so ensure that this is 200 for that
  1059. response.StatusCode = responsecode;
  1060. if (responsecode == (int)OSHttpStatusCode.RedirectMovedPermanently)
  1061. {
  1062. response.RedirectLocation = (string)responsedata["str_redirect_location"];
  1063. response.StatusCode = responsecode;
  1064. }
  1065. response.AddHeader("Content-Type", contentType);
  1066. byte[] buffer;
  1067. if (!(contentType.Contains("image") || contentType.Contains("x-shockwave-flash")))
  1068. {
  1069. buffer = Encoding.UTF8.GetBytes(responseString);
  1070. }
  1071. else
  1072. {
  1073. buffer = Convert.FromBase64String(responseString);
  1074. }
  1075. response.SendChunked = false;
  1076. response.ContentLength64 = buffer.Length;
  1077. response.ContentEncoding = Encoding.UTF8;
  1078. try
  1079. {
  1080. response.OutputStream.Write(buffer, 0, buffer.Length);
  1081. }
  1082. catch (Exception ex)
  1083. {
  1084. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1085. }
  1086. finally
  1087. {
  1088. //response.OutputStream.Close();
  1089. try
  1090. {
  1091. response.Send();
  1092. }
  1093. catch (SocketException e)
  1094. {
  1095. // This has to be here to prevent a Linux/Mono crash
  1096. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1097. }
  1098. }
  1099. }
  1100. public void SendHTML404(OSHttpResponse response, string host)
  1101. {
  1102. // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
  1103. response.StatusCode = 404;
  1104. response.AddHeader("Content-type", "text/html");
  1105. string responseString = GetHTTP404(host);
  1106. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  1107. response.SendChunked = false;
  1108. response.ContentLength64 = buffer.Length;
  1109. response.ContentEncoding = Encoding.UTF8;
  1110. try
  1111. {
  1112. response.OutputStream.Write(buffer, 0, buffer.Length);
  1113. }
  1114. catch (Exception ex)
  1115. {
  1116. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1117. }
  1118. finally
  1119. {
  1120. //response.OutputStream.Close();
  1121. try
  1122. {
  1123. response.Send();
  1124. }
  1125. catch (SocketException e)
  1126. {
  1127. // This has to be here to prevent a Linux/Mono crash
  1128. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1129. }
  1130. }
  1131. }
  1132. public void SendHTML500(OSHttpResponse response)
  1133. {
  1134. // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
  1135. response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
  1136. response.AddHeader("Content-type", "text/html");
  1137. string responseString = GetHTTP500();
  1138. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  1139. response.SendChunked = false;
  1140. response.ContentLength64 = buffer.Length;
  1141. response.ContentEncoding = Encoding.UTF8;
  1142. try
  1143. {
  1144. response.OutputStream.Write(buffer, 0, buffer.Length);
  1145. }
  1146. catch (Exception ex)
  1147. {
  1148. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1149. }
  1150. finally
  1151. {
  1152. //response.OutputStream.Close();
  1153. try
  1154. {
  1155. response.Send();
  1156. }
  1157. catch (SocketException e)
  1158. {
  1159. // This has to be here to prevent a Linux/Mono crash
  1160. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1161. }
  1162. }
  1163. }
  1164. public void Start()
  1165. {
  1166. m_log.Info("[HTTPD]: Starting up HTTP Server");
  1167. //m_workerThread = new Thread(new ThreadStart(StartHTTP));
  1168. //m_workerThread.Name = "HttpThread";
  1169. //m_workerThread.IsBackground = true;
  1170. //m_workerThread.Start();
  1171. //ThreadTracker.Add(m_workerThread);
  1172. StartHTTP();
  1173. }
  1174. private void StartHTTP()
  1175. {
  1176. try
  1177. {
  1178. m_log.Info("[HTTPD]: Spawned main thread OK");
  1179. //m_httpListener = new HttpListener();
  1180. NotSocketErrors = 0;
  1181. if (!m_ssl)
  1182. {
  1183. //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
  1184. //m_httpListener.Prefixes.Add("http://10.1.1.5:" + m_port + "/");
  1185. m_httpListener2 = new HttpServer.HttpListener(IPAddress.Any, (int)m_port);
  1186. m_httpListener2.ExceptionThrown += httpServerException;
  1187. m_httpListener2.LogWriter = httpserverlog;
  1188. // Uncomment this line in addition to those in HttpServerLogWriter
  1189. // if you want more detailed trace information from the HttpServer
  1190. //m_httpListener2.UseTraceLogs = true;
  1191. m_httpListener2.DisconnectHandler = httpServerDisconnectMonitor;
  1192. }
  1193. else
  1194. {
  1195. //m_httpListener.Prefixes.Add("https://+:" + (m_sslport) + "/");
  1196. //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
  1197. }
  1198. m_httpListener2.RequestHandler += OnHandleRequestIOThread;
  1199. //m_httpListener.Start();
  1200. m_httpListener2.Start(64);
  1201. HTTPDRunning = true;
  1202. //HttpListenerContext context;
  1203. //while (true)
  1204. //{
  1205. // context = m_httpListener.GetContext();
  1206. // ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context);
  1207. // }
  1208. }
  1209. catch (Exception e)
  1210. {
  1211. m_log.Warn("[HTTPD]: Error - " + e.Message);
  1212. m_log.Warn("Tip: Do you have permission to listen on port " + m_port + "," + m_sslport + "?");
  1213. }
  1214. }
  1215. public void httpServerDisconnectMonitor(HttpServer.IHttpClientContext source, SocketError err)
  1216. {
  1217. switch (err)
  1218. {
  1219. case SocketError.NotSocket:
  1220. NotSocketErrors++;
  1221. break;
  1222. }
  1223. }
  1224. public void httpServerException(object source, Exception exception)
  1225. {
  1226. m_log.ErrorFormat("[HTTPSERVER]: {0} had an exception {1}", source.ToString(), exception.ToString());
  1227. /*
  1228. if (HTTPDRunning)// && NotSocketErrors > 5)
  1229. {
  1230. Stop();
  1231. Thread.Sleep(200);
  1232. StartHTTP();
  1233. m_log.Warn("[HTTPSERVER]: Died. Trying to kick.....");
  1234. }
  1235. */
  1236. }
  1237. public void Stop()
  1238. {
  1239. HTTPDRunning = false;
  1240. m_httpListener2.ExceptionThrown -= httpServerException;
  1241. m_httpListener2.DisconnectHandler = null;
  1242. m_httpListener2.LogWriter = null;
  1243. m_httpListener2.RequestHandler -= OnHandleRequestIOThread;
  1244. m_httpListener2.Stop();
  1245. }
  1246. public void RemoveStreamHandler(string httpMethod, string path)
  1247. {
  1248. string handlerKey = GetHandlerKey(httpMethod, path);
  1249. //m_log.DebugFormat("[BASE HTTP SERVER]: Removing handler key {0}", handlerKey);
  1250. m_streamHandlers.Remove(handlerKey);
  1251. }
  1252. public void RemoveHTTPHandler(string httpMethod, string path)
  1253. {
  1254. if (httpMethod != null && httpMethod.Length == 0)
  1255. {
  1256. m_HTTPHandlers.Remove(path);
  1257. return;
  1258. }
  1259. m_HTTPHandlers.Remove(GetHandlerKey(httpMethod, path));
  1260. }
  1261. // Remove the agent IF it is registered. Intercept the possible
  1262. // exception.
  1263. public bool RemoveAgentHandler(string agent, IHttpAgentHandler handler)
  1264. {
  1265. try
  1266. {
  1267. if (handler == m_agentHandlers[agent])
  1268. {
  1269. m_agentHandlers.Remove(agent);
  1270. return true;
  1271. }
  1272. }
  1273. catch(KeyNotFoundException)
  1274. {
  1275. }
  1276. return false;
  1277. }
  1278. public bool RemoveLLSDHandler(string path, LLSDMethod handler)
  1279. {
  1280. try
  1281. {
  1282. if (handler == m_llsdHandlers[path])
  1283. {
  1284. m_llsdHandlers.Remove(path);
  1285. return true;
  1286. }
  1287. }
  1288. catch (KeyNotFoundException)
  1289. {
  1290. // This is an exception to prevent crashing because of invalid code
  1291. }
  1292. return false;
  1293. }
  1294. public string GetHTTP404(string host)
  1295. {
  1296. string file = Path.Combine(Util.configDir(), "http_404.html");
  1297. if (!File.Exists(file))
  1298. return getDefaultHTTP404(host);
  1299. StreamReader sr = File.OpenText(file);
  1300. string result = sr.ReadToEnd();
  1301. sr.Close();
  1302. return result;
  1303. }
  1304. public string GetHTTP500()
  1305. {
  1306. string file = Path.Combine(Util.configDir(), "http_500.html");
  1307. if (!File.Exists(file))
  1308. return getDefaultHTTP500();
  1309. StreamReader sr = File.OpenText(file);
  1310. string result = sr.ReadToEnd();
  1311. sr.Close();
  1312. return result;
  1313. }
  1314. // Fallback HTTP responses in case the HTTP error response files don't exist
  1315. private static string getDefaultHTTP404(string host)
  1316. {
  1317. 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>";
  1318. }
  1319. private static string getDefaultHTTP500()
  1320. {
  1321. 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>";
  1322. }
  1323. }
  1324. public class HttpServerContextObj
  1325. {
  1326. public IHttpClientContext context = null;
  1327. public IHttpRequest req = null;
  1328. public OSHttpRequest oreq = null;
  1329. public OSHttpResponse oresp = null;
  1330. public HttpServerContextObj(IHttpClientContext contxt, IHttpRequest reqs)
  1331. {
  1332. context = contxt;
  1333. req = reqs;
  1334. }
  1335. public HttpServerContextObj(OSHttpRequest osreq, OSHttpResponse osresp)
  1336. {
  1337. oreq = osreq;
  1338. oresp = osresp;
  1339. }
  1340. }
  1341. /// <summary>
  1342. /// Relays HttpServer log messages to our own logging mechanism.
  1343. /// </summary>
  1344. /// There is also a UseTraceLogs line in this file that can be uncommented for more detailed log information
  1345. public class HttpServerLogWriter : HttpServer.ILogWriter
  1346. {
  1347. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1348. public void Write(object source, HttpServer.LogPrio priority, string message)
  1349. {
  1350. /*
  1351. switch (priority)
  1352. {
  1353. case HttpServer.LogPrio.Debug:
  1354. m_log.DebugFormat("[{0}]: {1}", source.ToString(), message);
  1355. break;
  1356. case HttpServer.LogPrio.Error:
  1357. m_log.ErrorFormat("[{0}]: {1}", source.ToString(), message);
  1358. break;
  1359. case HttpServer.LogPrio.Info:
  1360. m_log.InfoFormat("[{0}]: {1}", source.ToString(), message);
  1361. break;
  1362. case HttpServer.LogPrio.Warning:
  1363. m_log.WarnFormat("[{0}]: {1}", source.ToString(), message);
  1364. break;
  1365. case HttpServer.LogPrio.Fatal:
  1366. m_log.ErrorFormat("[{0}]: FATAL! - {1}", source.ToString(), message);
  1367. break;
  1368. default:
  1369. break;
  1370. }
  1371. */
  1372. return;
  1373. }
  1374. }
  1375. }