'Encode a response to a L{WebResponse}.
@raises EncodingError: When I can\'t figure out how to encode this
message.'

def encode(self, response):

    encode_as = response.whichEncoding()
    if (encode_as == ENCODE_KVFORM):
        wr = self.responseFactory(body=response.encodeToKVForm())
        if isinstance(response, Exception):
            wr.code = HTTP_ERROR
    elif (encode_as == ENCODE_URL):
        location = response.encodeToURL()
        wr = self.responseFactory(code=HTTP_REDIRECT, headers={'location': location})
    elif (encode_as == ENCODE_HTML_FORM):
        wr = self.responseFactory(code=HTTP_OK, body=response.toFormMarkup())
    else:
        raise EncodingError(response)
    return wr
