# -*- coding: utf-8 -*-1# This program is free software; you can redistribute it and/or modify2# it under the terms of the GNU General Public License as published by3# the Free Software Foundation; either version 2 of the License, or4# (at your option) any later version.5#6# This program is distributed in the hope that it will be useful,7# but WITHOUT ANY WARRANTY; without even the implied warranty of8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9# GNU General Public License for more details.10#11# You should have received a copy of the GNU General Public License12# along with this program; if not, write to the Free Software13# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,14# MA 02110-1301, USA.15#16# Author: Mauro Soria1718from __future__ import annotations1920from lib.core.exceptions import InvalidRawRequest21from lib.core.logger import logger22from lib.parse.headers import HeadersParser23from lib.utils.file import File242526def parse_raw(raw_file: str) -> tuple[list[str], str, dict[str, str], str | None]:27with File(raw_file) as fd:28raw_content = fd.read()2930try:31head, body = raw_content.split("\n\n", 1)32except ValueError:33try:34head, body = raw_content.split("\r\n\r\n", 1)35except ValueError:36head = raw_content.strip("\n")37body = None3839try:40method, path = head.splitlines()[0].split()[:2]41headers = HeadersParser("\n".join(head.splitlines()[1:]))42host = headers.get("host")43except KeyError:44raise InvalidRawRequest("Can't find the Host header in the raw request")45except Exception as e:46logger.exception(e)47raise InvalidRawRequest("The raw request is formatively invalid")4849return [host + path], method, dict(headers), body505152