1
0

UserHttp.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright (c) OpenGrid project, http://osgrid.org/
  3. * All rights reserved.
  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 <organization> 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 <copyright holder> ``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 <copyright holder> 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.Text;
  29. using Nwc.XmlRpc;
  30. using System.Threading;
  31. using System.Text.RegularExpressions;
  32. using System.Net;
  33. using System.IO;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using libsecondlife;
  37. using OpenSim.Framework.User;
  38. using OpenSim.Framework.Sims;
  39. using OpenSim.Framework.Inventory;
  40. using OpenSim.Framework.Console;
  41. namespace OpenGridServices.UserServer
  42. {
  43. public class UserHTTPServer
  44. {
  45. public Thread HTTPD;
  46. public HttpListener Listener;
  47. public UserHTTPServer()
  48. {
  49. MainConsole.Instance.WriteLine("Starting up HTTP Server");
  50. HTTPD = new Thread(new ThreadStart(StartHTTP));
  51. HTTPD.Start();
  52. }
  53. public void StartHTTP()
  54. {
  55. MainConsole.Instance.WriteLine("UserHttp.cs:StartHTTP() - Spawned main thread OK");
  56. Listener = new HttpListener();
  57. Listener.Prefixes.Add("http://+:8002/userserver/");
  58. Listener.Prefixes.Add("http://+:8002/usersessions/");
  59. Listener.Start();
  60. HttpListenerContext context;
  61. while (true)
  62. {
  63. context = Listener.GetContext();
  64. ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context);
  65. }
  66. }
  67. static string ParseXMLRPC(string requestBody)
  68. {
  69. return OpenUser_Main.userserver._profilemanager.ParseXMLRPC(requestBody);
  70. }
  71. static string ParseREST(HttpListenerRequest www_req)
  72. {
  73. Console.WriteLine("INCOMING REST - " + www_req.RawUrl);
  74. char[] splitter = { '/' };
  75. string[] rest_params = www_req.RawUrl.Split(splitter);
  76. string req_type = rest_params[1]; // First part of the URL is the type of request - usersessions/userprofiles/inventory/blabla
  77. switch (req_type)
  78. {
  79. case "usersessions":
  80. LLUUID sessionid = new LLUUID(rest_params[2]); // get usersessions/sessionid
  81. if (www_req.HttpMethod == "DELETE")
  82. {
  83. foreach (libsecondlife.LLUUID UUID in OpenUser_Main.userserver._profilemanager.UserProfiles.Keys)
  84. {
  85. if (OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].CurrentSessionID == sessionid)
  86. {
  87. OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].CurrentSessionID = null;
  88. OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].CurrentSecureSessionID = null;
  89. OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].Circuits.Clear();
  90. }
  91. }
  92. }
  93. return "OK";
  94. }
  95. return "";
  96. }
  97. static void HandleRequest(Object stateinfo)
  98. {
  99. HttpListenerContext context = (HttpListenerContext)stateinfo;
  100. HttpListenerRequest request = context.Request;
  101. HttpListenerResponse response = context.Response;
  102. response.KeepAlive = false;
  103. response.SendChunked = false;
  104. System.IO.Stream body = request.InputStream;
  105. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  106. System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
  107. string requestBody = reader.ReadToEnd();
  108. body.Close();
  109. reader.Close();
  110. string responseString = "";
  111. switch (request.ContentType)
  112. {
  113. case "text/xml":
  114. // must be XML-RPC, so pass to the XML-RPC parser
  115. responseString = ParseXMLRPC(requestBody);
  116. response.AddHeader("Content-type", "text/xml");
  117. break;
  118. case "text/plaintext":
  119. responseString = ParseREST(request);
  120. response.AddHeader("Content-type", "text/plaintext");
  121. break;
  122. }
  123. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  124. System.IO.Stream output = response.OutputStream;
  125. response.SendChunked = false;
  126. response.ContentLength64 = buffer.Length;
  127. output.Write(buffer, 0, buffer.Length);
  128. output.Close();
  129. }
  130. }
  131. }