def send_response()

in Modules/Azure/Scaffolding/Python/WebRole/wfastcgi.py [0:0]


def send_response(id, resp_type, content, streaming = True):
    """sends a response w/ the given id, type, and content to the server.
If the content is streaming then an empty record is sent at the end to 
terminate the stream"""
    offset = 0
    while 1:
        if id < 256:
            id_0 = 0
            id_1 = id
        else:
            id_0 = id >> 8
            id_1 = id & 0xff
    
        # content len, padding len, content
        len_remaining = len(content) - offset
        if len_remaining > 65535:
            len_0 = 0xff
            len_1 = 0xff
            content_str = content[offset:offset+65535]
            offset += 65535
        else:
            len_0 = len_remaining >> 8
            len_1 = len_remaining & 0xff
            content_str = content[offset:]
            offset += len_remaining

        data = '%c%c%c%c%c%c%c%c%s' % (
                FCGI_VERSION_1,     # version
                resp_type,          # type
                id_0,               # requestIdB1
                id_1,               # requestIdB0
                len_0,              # contentLengthB1
                len_1,              # contentLengthB0
                0,                  # paddingLength
                0,                  # reserved
                content_str)

        os.write(stdout, data)
        if len_remaining == 0 or not streaming:
            break