Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 276
Andre Pastore Avatar asked Nov 30 '13 18:11

Andre Pastore


2 Answers

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)
like image 82
mpounsett Avatar answered Sep 21 '22 14:09

mpounsett


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.

like image 32
jfs Avatar answered Sep 18 '22 14:09

jfs