You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import urllib
  20. import logging
  21. import requests
  22. import werkzeug
  23. from odoo.http import request
  24. from odoo.tools import config
  25. _logger = logging.getLogger(__name__)
  26. def get_route(url):
  27. url_parts = url.split('?')
  28. path = url_parts[0]
  29. query_string = url_parts[1] if len(url_parts) > 1 else None
  30. router = request.httprequest.app.get_db_router(request.db).bind('')
  31. match = router.match(path, query_args=query_string)
  32. method = router.match(path, query_args=query_string)[0]
  33. params = dict(urllib.parse.parse_qsl(query_string))
  34. if len(match) > 1:
  35. params.update(match[1])
  36. return method, params, path
  37. def make_error_response(status, message):
  38. exception = werkzeug.exceptions.HTTPException()
  39. exception.code = status
  40. exception.description = message
  41. return exception
  42. def get_response(url):
  43. _logger.info(url)
  44. if not bool(urllib.parse.urlparse(url).netloc):
  45. base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
  46. method, params, path = get_route(url)
  47. params.update({'csrf_token': request.csrf_token()})
  48. session = requests.Session()
  49. session.cookies['session_id'] = request.session.sid
  50. try:
  51. response = session.post("%s%s" % (base_url, path), params=params, verify=False)
  52. return response.status_code, response.headers, response.content
  53. except:
  54. _logger.info("Trying custom certificate")
  55. custom_cert = config.get("muk_custom_certificate", False)
  56. try:
  57. _logger.info("Using Certificate: {}".format(custom_cert))
  58. response = session.post("%s%s" % (base_url, path), params=params, verify=custom_cert)
  59. return response.status_code, response.headers, response.reason
  60. except:
  61. try:
  62. _logger.info("Custom Certificate didn't work")
  63. response = session.post("%s%s" % (base_url, path), params=params, verify=False)
  64. return response.status_code, response.headers, response.reason
  65. except Exception as e:
  66. _logger.exception("Request failed!")
  67. return 501, [], str(e)
  68. else:
  69. try:
  70. response = requests.get(url)
  71. return response.status_code, response.headers, response.content
  72. except requests.exceptions.RequestException as exception:
  73. try:
  74. return exception.response.status_code, exception.response.headers, exception.response.reason
  75. except Exception as e:
  76. _logger.exception("Request failed!")
  77. return 501, [], str(e)