6

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

mpounsett
  • 1,174
  • 1
  • 10
  • 30
Andre Pastore
  • 2,841
  • 4
  • 33
  • 44

3 Answers3

5

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)
mpounsett
  • 1,174
  • 1
  • 10
  • 30
  • 1
    the [format for `server` is not arbitrary](http://tools.ietf.org/html/rfc2616#section-14.38): it should be `token/token token/token ..` e.g., `MyVeryOwnServer/1.0` no spaces/tabs inside a token are allowed. – jfs Dec 11 '13 at 23:50
  • You're right. Sorry, I was concentrating on the question and didn't pay attention (as I should) to other standards. – mpounsett Dec 13 '13 at 23:06
4

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

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.

AmaJayJB
  • 1,453
  • 2
  • 14
  • 21