def payload_http(formobj):
    """Parse and generate an http payload."""

    # the raw flowsynth we'll return
    synth = ""

    # we must have a request header.
    try:
        request_header = (
            fs_replace_badchars(
                unicode_safe(formobj.get("request_header")).strip("\r\n")
            )
            .replace("\r", "\\x0d\\x0a")
            .replace("\n", "\\x0d\\x0a")
        )
        request_body = (
            fs_replace_badchars(unicode_safe(formobj.get("request_body")).strip("\r\n"))
            .replace("\r", "\\x0d\\x0a")
            .replace("\n", "\\x0d\\x0a")
        )
        request_body_len = len(request_body) - (request_body.count("\\x") * 3)
    except Exception as e:
        logger.error("Problem parsing HTTP Wizard payload request content: %s" % e)
        return None

    # the start of the flowsynth
    synth = 'default > (content:"%s";' % request_header

    if "payload_http_request_contentlength" in formobj:
        # add or update request content length
        # doesn't add 'Content-Length: 0' if empty request body unless POST
        # will do inline update of Content-Length value if exists in submitted data
        if re.search(
            r'\\x0d\\x0acontent-length\\x3a(\\x20)*\d+("|\\x0d\\x0a)', synth.lower()
        ):
            synth = re.sub(
                r'(\\x0d\\x0acontent-length\\x3a(?:\\x20)*)\d+("|\\x0d\\x0a)',
                "\g<1>%d\g<2>" % request_body_len,
                synth,
                flags=re.I,
            )
        elif request_body != "" or request_header.lower().startswith("post\\x20"):
            synth = '%s content:"\\x0d\\x0aContent-Length\x3a\x20%s";' % (
                synth,
                request_body_len,
            )

    # add an 0d0a0d0a
    synth = '%s content:"\\x0d\\x0a\\x0d\\x0a";' % synth
    if request_body != "":
        # add http_client_body
        synth = '%s content:"%s"; );\n' % (synth, request_body)
    else:
        synth = "%s );\n" % synth

    if "payload_http_response" in formobj:
        # include http response
        try:
            response_header = (
                fs_replace_badchars(
                    unicode_safe(formobj.get("response_header")).strip("\r\n")
                )
                .replace("\r", "\\x0d\\x0a")
                .replace("\n", "\\x0d\\x0a")
            )
            response_body = (
                fs_replace_badchars(
                    unicode_safe(formobj.get("response_body")).strip("\r\n")
                )
                .replace("\r", "\\x0d\\x0a")
                .replace("\n", "\\x0d\\x0a")
            )
            response_body_len = len(response_body) - (response_body.count("\\x") * 3)
        except Exception as e:
            logger.error("Problem parsing HTTP Wizard payload response content: %s" % e)
            return None

        if "payload_http_response_contentlength" in formobj:
            # add or update response content length; include "Content-Length: 0" if body empty
            # will do inline update of Content-Length value if exists in submitted data
            if re.search(
                r"\\x0d\\x0acontent-length\\x3a(\\x20)*\d+($|\\x0d\\x0a)",
                response_header.lower(),
            ):
                response_header = re.sub(
                    r"(\\x0d\\x0acontent-length\\x3a(?:\\x20)*)\d+($|\\x0d\\x0a)",
                    "\g<1>%d\g<2>" % response_body_len,
                    response_header,
                    flags=re.I,
                )
                synth = '%sdefault < (content:"%s";' % (synth, response_header)
            else:
                synth = '%sdefault < (content:"%s";' % (synth, response_header)
                synth = '%s content:"\\x0d\\x0aContent-Length\x3a\x20%s";' % (
                    synth,
                    response_body_len,
                )
        else:
            synth = '%sdefault < (content:"%s";' % (synth, response_header)

        # add an 0d0a0d0a
        synth = '%s content:"\\x0d\\x0a\\x0d\\x0a";' % synth
        if response_body != "":
            synth = '%s content:"%s"; );\n' % (synth, response_body)
        else:
            synth = "%s );\n" % synth

    return synth
Parse and generate an http payload.
app/flowsynth.py