Andre Pastore
Andre Pastore

Reputation: 2897

Remove/Rewrite HTTP header 'Server: TwistedWeb'

There is some way to remove HTTP Header 'Server: TwistedWeb/13.1.0' from responses from a Twisted based web application?

Upvotes: 6

Views: 3794

Answers (3)

AmaJayJB
AmaJayJB

Reputation: 1453

I know this is an old question, but if you would like to remove the server http header. I am talking about the

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

This is set by Twisted Web automagically if you don't specify a value. You can remove it by using the inner Headers class. For example,

request.responseHeaders.removeHeader('Server')

This will remove the Server Http Header.

Upvotes: 1

mpounsett
mpounsett

Reputation: 1194

You can rewrite any header by calling the request.setHeader method.

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

Upvotes: 5

jfs
jfs

Reputation: 414139

The change applies to any resources on your site; you could put it in your Site class. You want that 404 or 500 errors would also return correct header; so you should set it as earlier as possible but not before it is set by twisted itself (in order 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()

Default Server http header is specified in t.w.server.version.

Upvotes: 4

Related Questions