Delete / rewrite HTTP header 'Server: TwistedWeb'

Is there a way to remove the HTTP Header 'Server: TwistedWeb / 13.1.0' from the responses of a Twisted based web application?

+4
source share
3 answers

You can rewrite any title by calling request.setHeader.

class RootPage(Resource):
    def getChild(self, name, request):
        request.setHeader('server', 'MyVeryOwnServer/1.0')
        return OtherResource(name)
+4
source

This change applies to any resources on your site; you can put it in a class Site. You want 404 or 500 errors to also return the correct header; therefore, you should install it as soon as possible, but not before it is installed using the most twisted one (to overwrite it):

#!/usr/bin/env python
import sys
from twisted.web import server, resource
from twisted.internet import reactor
from twisted.python import log

class Site(server.Site):
    def getResourceFor(self, request):
        request.setHeader('server', 'Server/1.9E377')
        return server.Site.getResourceFor(self, request)

# example from http://twistedmatrix.com/
class HelloResource(resource.Resource):
    isLeaf = True
    numberRequests = 0

    def render_GET(self, request):
        self.numberRequests += 1
        request.setHeader("content-type", "text/plain")
        return "I am request #" + str(self.numberRequests) + "\n"

log.startLogging(sys.stderr)
reactor.listenTCP(8080, Site(HelloResource()))
reactor.run()

HTTP t.w.server.version.

+3

I know this is an old question, but if you want to remove the HTTP server header. I'm talking about

request.setHeader('Server', 'SomeServer')

This is set by Twisted Web automatically if you do not specify a value. You can remove it using the inner header class. For example,

request.responseHeaders.removeHeader('Server')

This will remove the Http server header.

0
source

All Articles