'Send www-authenticate headers to the client'

def render(self, request):

    def ensureBytes(s):
        return (s.encode('ascii') if isinstance(s, unicode) else s)
    def generateWWWAuthenticate(scheme, challenge):
        l = []
        for (k, v) in challenge.items():
            k = ensureBytes(k)
            v = ensureBytes(v)
            l.append(((k + '=') + quoteString(v)))
        return '    '.join([scheme, ',    '.join(l)])
    def quoteString(s):
        return (('"' + s.replace('\\', '\\\\').replace('"', '\\"')) + '"')
    request.setResponseCode(401)
    for fact in self._credentialFactories:
        challenge = fact.getChallenge(request)
        request.responseHeaders.addRawHeader('www-authenticate', generateWWWAuthenticate(fact.scheme, challenge))
    if (request.method == 'HEAD'):
        return ''
    return 'Unauthorized'
