Composer.py 1.56 KB
"""
@author Georg Hopp

"""

import Message

class Composer(object):
    """
    compose to HTTP
    =====================================================================
    """
    def composeStartLine(self, message):
        """
        compose a HTTP message StartLine... currently this does no check for
        the validity of the StartLine.

        returns str The composed HTTP start line (either Request or Status)

        @message: HttpMessage The message that should be composed.
        """
        return message.getStartLine() + '\r\n'

    def composeHeaders(self, message):
        """
        this creates header lines for each key/value[n] pair.

        returns str All headers composed to an HTTP string.

        @message: HttpMessage The message to compose the header from.
        """
        headers = message.getHeaders()
        return '\r\n'.join([':'.join(h) for h in headers]) + '\r\n'

    def composeStartLineHeaders(self, message):
        """
        Compose the start line and the headers.

        returns str The start line and the headers as HTTP string.

        @message: HttpMessage The message to be composed.
        """
        return self.composeStartLine(message) + self.composeHeaders(message)

    def compose(self, message):
        """
        Compose the whole message to an HTTP string.

        returns str The whole message as an HTTP string.

        @message: HttpMessage The message to be composed.
        """
        return self.composeStartLineHeaders(message) + "\r\n" + message.getBody()

# vim: set ft=python et ts=8 sw=4 sts=4: