# Copyright 2024 Open2Bizz # License LGPL-3 import logging import requests from odoo import http from odoo.http import request, Response _logger = logging.getLogger(__name__) MAILHOG_BASE = "http://localhost:8025" class MailcatcherProxy(http.Controller): @http.route(['/mailcatcher', '/mailcatcher/'], type='http', auth='user', website=False, csrf=False) def mailcatcher_proxy(self, subpath='', **kw): """Proxy requests to the local MailHog instance.""" target_url = f"{MAILHOG_BASE}/{subpath}" if request.httprequest.query_string: target_url += '?' + request.httprequest.query_string.decode('utf-8') try: headers = { k: v for k, v in request.httprequest.headers if k.lower() not in ('host', 'cookie', 'authorization') } resp = requests.request( method=request.httprequest.method, url=target_url, headers=headers, data=request.httprequest.get_data(), timeout=30, allow_redirects=False, ) except requests.exceptions.ConnectionError: _logger.warning("Could not connect to MailHog at %s", MAILHOG_BASE) return Response("MailHog is not available", status=502) excluded_headers = { 'content-encoding', 'content-length', 'transfer-encoding', 'connection', } response_headers = [ (k, v) for k, v in resp.headers.items() if k.lower() not in excluded_headers ] return Response( resp.content, status=resp.status_code, headers=response_headers, )