AssetHttpServer.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. //using OpenSim.CAPS;
  8. using Nwc.XmlRpc;
  9. using System.Collections;
  10. using OpenSim.Framework.Console;
  11. using OpenSim.Servers;
  12. namespace OpenGridServices.AssetServer
  13. {
  14. public class AssetHttpServer :BaseHttpServer
  15. {
  16. public AssetHttpServer(int port)
  17. : base(port)
  18. {
  19. }
  20. public override void HandleRequest(Object stateinfo)
  21. {
  22. try
  23. {
  24. HttpListenerContext context = (HttpListenerContext)stateinfo;
  25. HttpListenerRequest request = context.Request;
  26. HttpListenerResponse response = context.Response;
  27. response.KeepAlive = false;
  28. response.SendChunked = false;
  29. System.IO.Stream body = request.InputStream;
  30. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  31. System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
  32. string requestBody = reader.ReadToEnd();
  33. body.Close();
  34. reader.Close();
  35. //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType);
  36. //Console.WriteLine(requestBody);
  37. string responseString = "";
  38. switch (request.ContentType)
  39. {
  40. case "text/xml":
  41. // must be XML-RPC, so pass to the XML-RPC parser
  42. responseString = ParseXMLRPC(requestBody);
  43. responseString = Regex.Replace(responseString, "utf-16", "utf-8");
  44. response.AddHeader("Content-type", "text/xml");
  45. break;
  46. case "application/xml":
  47. // probably LLSD we hope, otherwise it should be ignored by the parser
  48. responseString = ParseLLSDXML(requestBody);
  49. response.AddHeader("Content-type", "application/xml");
  50. break;
  51. case "application/x-www-form-urlencoded":
  52. // a form data POST so send to the REST parser
  53. responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
  54. response.AddHeader("Content-type", "text/plain");
  55. break;
  56. case null:
  57. // must be REST or invalid crap, so pass to the REST parser
  58. responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
  59. response.AddHeader("Content-type", "text/plain");
  60. break;
  61. }
  62. Encoding Windows1252Encoding = Encoding.GetEncoding(1252);
  63. byte[] buffer = Windows1252Encoding.GetBytes(responseString);
  64. System.IO.Stream output = response.OutputStream;
  65. response.SendChunked = false;
  66. response.ContentLength64 = buffer.Length;
  67. output.Write(buffer, 0, buffer.Length);
  68. output.Close();
  69. }
  70. catch (Exception e)
  71. {
  72. Console.WriteLine(e.ToString());
  73. }
  74. }
  75. }
  76. }