serverhandler.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #! /usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. #
  4. # gitlab irc sender
  5. # Copyright (C) 2016-2017 Andrei Karas (4144)
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import json
  20. import sys
  21. try:
  22. from BaseHTTPServer import BaseHTTPRequestHandler
  23. except ImportError:
  24. from http.server import BaseHTTPRequestHandler
  25. from code.actionhandler import ActionHandler
  26. from code.configuration import Configuration
  27. from code.core import Core
  28. class ServerHandler(BaseHTTPRequestHandler):
  29. def __init__(self, *args):
  30. self.project = None
  31. BaseHTTPRequestHandler.__init__(self, *args)
  32. def toStr(self, data):
  33. try:
  34. return data.decode()
  35. except AttributeError:
  36. return data
  37. def fromStr(self, data):
  38. try:
  39. return data.encode()
  40. except AttributeError:
  41. return data
  42. def returnError(self, code):
  43. self.send_response(code)
  44. self.end_headers()
  45. self.wfile.close()
  46. def returnData(self, code, response):
  47. self.send_response(code)
  48. self.send_header("Content-type", "text")
  49. self.send_header("Content-length", str(len(response)))
  50. self.end_headers()
  51. self.wfile.write(self.fromStr(response))
  52. def version_string(self):
  53. return Configuration.http_web_server_name
  54. def do_HEAD(self):
  55. self.send_response(400)
  56. self.end_headers()
  57. self.wfile.close()
  58. def do_GET(self):
  59. self.send_response(400)
  60. self.end_headers()
  61. self.wfile.close()
  62. def do_POST(self):
  63. try:
  64. self.rfile._sock.settimeout(5)
  65. except AttributeError:
  66. pass # python3
  67. data = self.rfile.read(int(self.headers['Content-Length']))
  68. data = json.loads(self.toStr(data))
  69. self.actionHandler = ActionHandler()
  70. if Core.config.global_options.http_save_json == True or \
  71. Core.config.global_options.http_save_json == "all" or \
  72. Core.config.global_options.http_save_json == "any":
  73. self.actionHandler.saveJson(data)
  74. if self.actionHandler.validate(self.path, data) == False:
  75. if Core.config.global_options.http_return_error_if_not_supported == True:
  76. self.returnError(400)
  77. else:
  78. self.returnData(200, "OK")
  79. return
  80. self.returnData(200, "OK")
  81. self.actionHandler.processAction(self.path, data, self.headers)
  82. def log_message(self, format, *args):
  83. data = "{0}:{1} - - [{2}] {3}\n".format(self.client_address[0], self.client_address[1], self.log_date_time_string(), format % args)
  84. sys.stderr.write(data)
  85. Core.log.info(data)