BaseHttpServer.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. private 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.Info("[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.Info("[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.Info("[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. string host = String.Empty;
  922. string[] querystringkeys = request.QueryString.AllKeys;
  923. string[] rHeaders = request.Headers.AllKeys;
  924. keysvals.Add("body", requestBody);
  925. keysvals.Add("uri", request.RawUrl);
  926. keysvals.Add("content-type", request.ContentType);
  927. keysvals.Add("http-method", request.HttpMethod);
  928. foreach (string queryname in querystringkeys)
  929. {
  930. keysvals.Add(queryname, request.QueryString[queryname]);
  931. }
  932. foreach (string headername in rHeaders)
  933. {
  934. //m_log.Warn("[HEADER]: " + headername + "=" + request.Headers[headername]);
  935. headervals[headername] = request.Headers[headername];
  936. }
  937. if (headervals.Contains("Host"))
  938. {
  939. host = (string)headervals["Host"];
  940. }
  941. keysvals.Add("headers",headervals);
  942. if (keysvals.Contains("method"))
  943. {
  944. //m_log.Warn("[HTTP]: Contains Method");
  945. string method = (string) keysvals["method"];
  946. //m_log.Warn("[HTTP]: " + requestBody);
  947. GenericHTTPMethod requestprocessor;
  948. bool foundHandler = TryGetHTTPHandler(method, out requestprocessor);
  949. if (foundHandler)
  950. {
  951. Hashtable responsedata1 = requestprocessor(keysvals);
  952. DoHTTPGruntWork(responsedata1,response);
  953. //SendHTML500(response);
  954. }
  955. else
  956. {
  957. //m_log.Warn("[HTTP]: Handler Not Found");
  958. SendHTML404(response, host);
  959. }
  960. }
  961. else
  962. {
  963. GenericHTTPMethod requestprocessor;
  964. bool foundHandler = TryGetHTTPHandlerPathBased(request.RawUrl, out requestprocessor);
  965. if (foundHandler)
  966. {
  967. Hashtable responsedata2 = requestprocessor(keysvals);
  968. DoHTTPGruntWork(responsedata2, response);
  969. //SendHTML500(response);
  970. }
  971. else
  972. {
  973. //m_log.Warn("[HTTP]: Handler Not Found");
  974. SendHTML404(response, host);
  975. }
  976. }
  977. }
  978. private bool TryGetHTTPHandlerPathBased(string path, out GenericHTTPMethod httpHandler)
  979. {
  980. httpHandler = null;
  981. // Pull out the first part of the path
  982. // splitting the path by '/' means we'll get the following return..
  983. // {0}/{1}/{2}
  984. // where {0} isn't something we really control 100%
  985. string[] pathbase = path.Split('/');
  986. string searchquery = "/";
  987. if (pathbase.Length < 1)
  988. return false;
  989. for (int i = 1; i < pathbase.Length; i++)
  990. {
  991. searchquery += pathbase[i];
  992. if (pathbase.Length - 1 != i)
  993. searchquery += "/";
  994. }
  995. // while the matching algorithm below doesn't require it, we're expecting a query in the form
  996. //
  997. // [] = optional
  998. // /resource/UUID/action[/action]
  999. //
  1000. // now try to get the closest match to the reigstered path
  1001. // at least for OGP, registered path would probably only consist of the /resource/
  1002. string bestMatch = null;
  1003. foreach (string pattern in m_HTTPHandlers.Keys)
  1004. {
  1005. if (searchquery.ToLower().StartsWith(pattern.ToLower()))
  1006. {
  1007. if (String.IsNullOrEmpty(bestMatch) || searchquery.Length > bestMatch.Length)
  1008. {
  1009. // You have to specifically register for '/' and to get it, you must specificaly request it
  1010. //
  1011. if (pattern == "/" && searchquery == "/" || pattern != "/")
  1012. bestMatch = pattern;
  1013. }
  1014. }
  1015. }
  1016. if (String.IsNullOrEmpty(bestMatch))
  1017. {
  1018. httpHandler = null;
  1019. return false;
  1020. }
  1021. else
  1022. {
  1023. if (bestMatch == "/" && searchquery != "/")
  1024. return false;
  1025. httpHandler = m_HTTPHandlers[bestMatch];
  1026. return true;
  1027. }
  1028. }
  1029. private static void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
  1030. {
  1031. //m_log.Info("[BASE HTTP SERVER]: Doing HTTP Grunt work with response");
  1032. int responsecode = (int)responsedata["int_response_code"];
  1033. string responseString = (string)responsedata["str_response_string"];
  1034. string contentType = (string)responsedata["content_type"];
  1035. if (responsedata.ContainsKey("error_status_text"))
  1036. {
  1037. response.StatusDescription = (string)responsedata["error_status_text"];
  1038. }
  1039. if (responsedata.ContainsKey("http_protocol_version"))
  1040. {
  1041. response.ProtocolVersion = (string)responsedata["http_protocol_version"];
  1042. }
  1043. if (responsedata.ContainsKey("keepalive"))
  1044. {
  1045. bool keepalive = (bool)responsedata["keepalive"];
  1046. response.KeepAlive = keepalive;
  1047. }
  1048. //Even though only one other part of the entire code uses HTTPHandlers, we shouldn't expect this
  1049. //and should check for NullReferenceExceptions
  1050. if (string.IsNullOrEmpty(contentType))
  1051. {
  1052. contentType = "text/html";
  1053. }
  1054. // The client ignores anything but 200 here for web login, so ensure that this is 200 for that
  1055. response.StatusCode = responsecode;
  1056. if (responsecode == (int)OSHttpStatusCode.RedirectMovedPermanently)
  1057. {
  1058. response.RedirectLocation = (string)responsedata["str_redirect_location"];
  1059. response.StatusCode = responsecode;
  1060. }
  1061. response.AddHeader("Content-Type", contentType);
  1062. byte[] buffer;
  1063. if (!(contentType.Contains("image") || contentType.Contains("x-shockwave-flash")))
  1064. {
  1065. buffer = Encoding.UTF8.GetBytes(responseString);
  1066. }
  1067. else
  1068. {
  1069. buffer = Convert.FromBase64String(responseString);
  1070. }
  1071. response.SendChunked = false;
  1072. response.ContentLength64 = buffer.Length;
  1073. response.ContentEncoding = Encoding.UTF8;
  1074. try
  1075. {
  1076. response.OutputStream.Write(buffer, 0, buffer.Length);
  1077. }
  1078. catch (Exception ex)
  1079. {
  1080. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1081. }
  1082. finally
  1083. {
  1084. //response.OutputStream.Close();
  1085. try
  1086. {
  1087. response.Send();
  1088. }
  1089. catch (SocketException e)
  1090. {
  1091. // This has to be here to prevent a Linux/Mono crash
  1092. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1093. }
  1094. }
  1095. }
  1096. public void SendHTML404(OSHttpResponse response, string host)
  1097. {
  1098. // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
  1099. response.StatusCode = 404;
  1100. response.AddHeader("Content-type", "text/html");
  1101. string responseString = GetHTTP404(host);
  1102. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  1103. response.SendChunked = false;
  1104. response.ContentLength64 = buffer.Length;
  1105. response.ContentEncoding = Encoding.UTF8;
  1106. try
  1107. {
  1108. response.OutputStream.Write(buffer, 0, buffer.Length);
  1109. }
  1110. catch (Exception ex)
  1111. {
  1112. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1113. }
  1114. finally
  1115. {
  1116. //response.OutputStream.Close();
  1117. try
  1118. {
  1119. response.Send();
  1120. }
  1121. catch (SocketException e)
  1122. {
  1123. // This has to be here to prevent a Linux/Mono crash
  1124. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1125. }
  1126. }
  1127. }
  1128. public void SendHTML500(OSHttpResponse response)
  1129. {
  1130. // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
  1131. response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
  1132. response.AddHeader("Content-type", "text/html");
  1133. string responseString = GetHTTP500();
  1134. byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  1135. response.SendChunked = false;
  1136. response.ContentLength64 = buffer.Length;
  1137. response.ContentEncoding = Encoding.UTF8;
  1138. try
  1139. {
  1140. response.OutputStream.Write(buffer, 0, buffer.Length);
  1141. }
  1142. catch (Exception ex)
  1143. {
  1144. m_log.Warn("[HTTPD]: Error - " + ex.Message);
  1145. }
  1146. finally
  1147. {
  1148. //response.OutputStream.Close();
  1149. try
  1150. {
  1151. response.Send();
  1152. }
  1153. catch (SocketException e)
  1154. {
  1155. // This has to be here to prevent a Linux/Mono crash
  1156. m_log.WarnFormat("[BASE HTTP SERVER] XmlRpcRequest issue {0}.\nNOTE: this may be spurious on Linux.", e);
  1157. }
  1158. }
  1159. }
  1160. public void Start()
  1161. {
  1162. m_log.Info("[HTTPD]: Starting up HTTP Server");
  1163. //m_workerThread = new Thread(new ThreadStart(StartHTTP));
  1164. //m_workerThread.Name = "HttpThread";
  1165. //m_workerThread.IsBackground = true;
  1166. //m_workerThread.Start();
  1167. //ThreadTracker.Add(m_workerThread);
  1168. StartHTTP();
  1169. }
  1170. private void StartHTTP()
  1171. {
  1172. try
  1173. {
  1174. m_log.Info("[HTTPD]: Spawned main thread OK");
  1175. //m_httpListener = new HttpListener();
  1176. NotSocketErrors = 0;
  1177. if (!m_ssl)
  1178. {
  1179. //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
  1180. //m_httpListener.Prefixes.Add("http://10.1.1.5:" + m_port + "/");
  1181. m_httpListener2 = new HttpServer.HttpListener(IPAddress.Any, (int)m_port);
  1182. m_httpListener2.ExceptionThrown += httpServerException;
  1183. m_httpListener2.LogWriter = httpserverlog;
  1184. m_httpListener2.DisconnectHandler = httpServerDisconnectMonitor;
  1185. }
  1186. else
  1187. {
  1188. //m_httpListener.Prefixes.Add("https://+:" + (m_sslport) + "/");
  1189. //m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
  1190. }
  1191. m_httpListener2.RequestHandler += OnHandleRequestIOThread;
  1192. //m_httpListener.Start();
  1193. m_httpListener2.Start(64);
  1194. HTTPDRunning = true;
  1195. //HttpListenerContext context;
  1196. //while (true)
  1197. //{
  1198. // context = m_httpListener.GetContext();
  1199. // ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context);
  1200. // }
  1201. }
  1202. catch (Exception e)
  1203. {
  1204. m_log.Warn("[HTTPD]: Error - " + e.Message);
  1205. m_log.Warn("Tip: Do you have permission to listen on port " + m_port + "," + m_sslport + "?");
  1206. }
  1207. }
  1208. public void httpServerDisconnectMonitor(HttpServer.IHttpClientContext source, SocketError err)
  1209. {
  1210. switch (err)
  1211. {
  1212. case SocketError.NotSocket:
  1213. NotSocketErrors++;
  1214. break;
  1215. }
  1216. }
  1217. public void httpServerException(object source, Exception exception)
  1218. {
  1219. m_log.ErrorFormat("[HTTPSERVER]: {0} had an exception {1}", source.ToString(), exception.ToString());
  1220. /*
  1221. if (HTTPDRunning)// && NotSocketErrors > 5)
  1222. {
  1223. Stop();
  1224. Thread.Sleep(200);
  1225. StartHTTP();
  1226. m_log.Warn("[HTTPSERVER]: Died. Trying to kick.....");
  1227. }
  1228. */
  1229. }
  1230. public void Stop()
  1231. {
  1232. HTTPDRunning = false;
  1233. m_httpListener2.ExceptionThrown -= httpServerException;
  1234. m_httpListener2.DisconnectHandler = null;
  1235. m_httpListener2.LogWriter = null;
  1236. m_httpListener2.RequestHandler -= OnHandleRequestIOThread;
  1237. m_httpListener2.Stop();
  1238. }
  1239. public void RemoveStreamHandler(string httpMethod, string path)
  1240. {
  1241. string handlerKey = GetHandlerKey(httpMethod, path);
  1242. //m_log.DebugFormat("[BASE HTTP SERVER]: Removing handler key {0}", handlerKey);
  1243. m_streamHandlers.Remove(handlerKey);
  1244. }
  1245. public void RemoveHTTPHandler(string httpMethod, string path)
  1246. {
  1247. if (httpMethod != null && httpMethod.Length == 0)
  1248. {
  1249. m_HTTPHandlers.Remove(path);
  1250. return;
  1251. }
  1252. m_HTTPHandlers.Remove(GetHandlerKey(httpMethod, path));
  1253. }
  1254. // Remove the agent IF it is registered. Intercept the possible
  1255. // exception.
  1256. public bool RemoveAgentHandler(string agent, IHttpAgentHandler handler)
  1257. {
  1258. try
  1259. {
  1260. if (handler == m_agentHandlers[agent])
  1261. {
  1262. m_agentHandlers.Remove(agent);
  1263. return true;
  1264. }
  1265. }
  1266. catch(KeyNotFoundException)
  1267. {
  1268. }
  1269. return false;
  1270. }
  1271. public bool RemoveLLSDHandler(string path, LLSDMethod handler)
  1272. {
  1273. try
  1274. {
  1275. if (handler == m_llsdHandlers[path])
  1276. {
  1277. m_llsdHandlers.Remove(path);
  1278. return true;
  1279. }
  1280. }
  1281. catch (KeyNotFoundException)
  1282. {
  1283. // This is an exception to prevent crashing because of invalid code
  1284. }
  1285. return false;
  1286. }
  1287. public string GetHTTP404(string host)
  1288. {
  1289. string file = Path.Combine(Util.configDir(), "http_404.html");
  1290. if (!File.Exists(file))
  1291. return getDefaultHTTP404(host);
  1292. StreamReader sr = File.OpenText(file);
  1293. string result = sr.ReadToEnd();
  1294. sr.Close();
  1295. return result;
  1296. }
  1297. public string GetHTTP500()
  1298. {
  1299. string file = Path.Combine(Util.configDir(), "http_500.html");
  1300. if (!File.Exists(file))
  1301. return getDefaultHTTP500();
  1302. StreamReader sr = File.OpenText(file);
  1303. string result = sr.ReadToEnd();
  1304. sr.Close();
  1305. return result;
  1306. }
  1307. // Fallback HTTP responses in case the HTTP error response files don't exist
  1308. private static string getDefaultHTTP404(string host)
  1309. {
  1310. 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>";
  1311. }
  1312. private static string getDefaultHTTP500()
  1313. {
  1314. 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>";
  1315. }
  1316. }
  1317. public class HttpServerContextObj
  1318. {
  1319. public IHttpClientContext context = null;
  1320. public IHttpRequest req = null;
  1321. public OSHttpRequest oreq = null;
  1322. public OSHttpResponse oresp = null;
  1323. public HttpServerContextObj(IHttpClientContext contxt, IHttpRequest reqs)
  1324. {
  1325. context = contxt;
  1326. req = reqs;
  1327. }
  1328. public HttpServerContextObj(OSHttpRequest osreq, OSHttpResponse osresp)
  1329. {
  1330. oreq = osreq;
  1331. oresp = osresp;
  1332. }
  1333. }
  1334. public class HttpServerLogWriter : HttpServer.ILogWriter
  1335. {
  1336. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1337. public HttpServerLogWriter()
  1338. {
  1339. }
  1340. public void Write(object source, HttpServer.LogPrio priority, string message)
  1341. {
  1342. return;
  1343. /*
  1344. switch (priority)
  1345. {
  1346. case HttpServer.LogPrio.Debug:
  1347. m_log.DebugFormat("[{0}]: {1}", source.ToString(), message);
  1348. break;
  1349. case HttpServer.LogPrio.Error:
  1350. m_log.ErrorFormat("[{0}]: {1}", source.ToString(), message);
  1351. break;
  1352. case HttpServer.LogPrio.Info:
  1353. m_log.InfoFormat("[{0}]: {1}", source.ToString(), message);
  1354. break;
  1355. case HttpServer.LogPrio.Warning:
  1356. m_log.WarnFormat("[{0}]: {1}", source.ToString(), message);
  1357. break;
  1358. case HttpServer.LogPrio.Fatal:
  1359. m_log.ErrorFormat("[{0}]: FATAL! - {1}", source.ToString(), message);
  1360. break;
  1361. default:
  1362. break;
  1363. }
  1364. */
  1365. }
  1366. }
  1367. }