1
0

ConciergeServer.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. #
  4. # Copyright (c) Contributors, http://opensimulator.org/
  5. # See CONTRIBUTORS.TXT for a full list of copyright holders.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are met:
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # * Neither the name of the OpenSim Project nor the
  15. # names of its contributors may be used to endorse or promote products
  16. # derived from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  19. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  22. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. import logging
  30. import BaseHTTPServer
  31. import optparse
  32. import xml.etree.ElementTree as ET
  33. import xml.parsers.expat
  34. # enable debug level logging
  35. logging.basicConfig(level = logging.DEBUG,
  36. format='%(asctime)s %(levelname)s %(message)s')
  37. options = None
  38. # subclassed HTTPRequestHandler
  39. class ConciergeHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  40. def logRequest(self):
  41. logging.info('[ConciergeHandler] %(command)s request: %(host)s:%(port)d --- %(path)s',
  42. dict(command = self.command,
  43. host = self.client_address[0],
  44. port = self.client_address[1],
  45. path = self.path))
  46. def logResponse(self, status):
  47. logging.info('[ConciergeHandler] %(command)s returned %(status)d',
  48. dict(command = self.command,
  49. status = status))
  50. def do_HEAD(self):
  51. self.logRequest()
  52. self.send_response(200)
  53. self.send_header('Content-type', 'text/html')
  54. self.end_headers()
  55. self.logResponse(200)
  56. def dumpXml(self, xml):
  57. logging.debug('[ConciergeHandler] %s', xml.tag)
  58. for attr in xml.attrib:
  59. logging.debug('[ConciergeHandler] %s [%s] %s', xml.tag, attr, xml.attrib[attr])
  60. for kid in xml.getchildren():
  61. self.dumpXml(kid)
  62. def do_POST(self):
  63. self.logRequest()
  64. hdrs = {}
  65. for hdr in self.headers.headers:
  66. logging.debug('[ConciergeHandler] POST: header: %s', hdr.rstrip())
  67. length = int(self.headers.getheader('Content-Length'))
  68. content = self.rfile.read(length)
  69. self.rfile.close()
  70. logging.debug('[ConciergeHandler] POST: content: %s', content)
  71. try:
  72. postXml = ET.fromstring(content)
  73. self.dumpXml(postXml)
  74. except xml.parsers.expat.ExpatError, xmlError:
  75. logging.error('[ConciergeHandler] POST illformed:%s', xmlError)
  76. self.send_response(500)
  77. return
  78. if not options.fail:
  79. self.send_response(200)
  80. self.send_header('Content-Type', 'text/html')
  81. self.send_header('Content-Length', len('<success/>'))
  82. self.end_headers()
  83. self.logResponse(200)
  84. self.wfile.write('<success/>')
  85. self.wfile.close()
  86. else:
  87. self.send_response(500)
  88. self.send_header('Content-Type', 'text/html')
  89. self.send_header('Content-Length', len('<error>gotcha!</error>'))
  90. self.end_headers()
  91. self.wfile.write('<error>gotcha!</error>')
  92. self.wfile.close()
  93. self.logResponse(500)
  94. def log_request(code, size):
  95. pass
  96. if __name__ == '__main__':
  97. logging.info('[ConciergeServer] Concierge Broker Test Server starting')
  98. parser = optparse.OptionParser()
  99. parser.add_option('-p', '--port', dest = 'port', help = 'port to listen on', metavar = 'PORT')
  100. parser.add_option('-f', '--fail', dest = 'fail', action = 'store_true', help = 'always fail POST requests')
  101. (options, args) = parser.parse_args()
  102. httpServer = BaseHTTPServer.HTTPServer(('', 8080), ConciergeHandler)
  103. try:
  104. httpServer.serve_forever()
  105. except KeyboardInterrupt:
  106. logging.info('[ConciergeServer] terminating')
  107. httpServer.server_close()