4.7.2.3. Configuring policies for HTTP headers

Both request and response headers can be modified by the proxy during the transfer. New header lines can be inserted, entries can be modified or deleted. To change headers in the requests and responses use the request_header hash or the response_header hash, respectively.

Similarly to the request hash, these hashes are indexed by the header name (like "User-Agent") and contain an actiontuple describing the action to take.

By default, the proxy modifies only the "Host", "Connection", "Proxy-Connection" and "Transfer-Encoding" headers. "Host" headers need to be changed when the proxy modifies the URL; "(Proxy-)Connection" is changed when the proxy turns connection keep-alive on/off; "Transfer-Enconding" is changed to enable chunked encoding.

ActionDescription
HTTP_HDR_ABORT Terminate the connection.
HTTP_HDR_ACCEPT Accept the header.
HTTP_HDR_DROP Remove the header.
HTTP_HDR_POLICY Call the function specified to make a decision about the event. The function receives three parameters: self, hdr_name, and hdr_value.
HTTP_HDR_CHANGE_NAME Rename the header to the name specified in the second argument.
HTTP_HDR_CHANGE_VALUE Change the value of the header to the value specified in the second argument.
HTTP_HDR_CHANGE_BOTH Change both the name and value of the header to the values specified in the second and third arguments, respectively.
HTTP_HDR_INSERT Insert a new header defined in the second argument.
HTTP_HDR_REPLACE Remove all existing occurrences of a header and replace them with the one specified in the second argument.

Table 4.12.  Action codes for HTTP headers

Example 4.10. Header filtering in HTTP

The following example hides the browser used by the client by replacing the value of the User-Agent header to Lynx in all requests. The use of cookies is disabled as well.

class MyHttp(HttpProxy):
        def config(self):
                HttpProxy.config(self)
                self.request_header["User-Agent"] = (HTTP_HDR_CHANGE_VALUE, "Lynx 2.4.1")
                self.request_header["Cookie"] = (HTTP_HDR_POLICY, self.processCookies)
                self.response_header["Set-Cookie"] = (HTTP_HDR_DROP,)

        def processCookies(self, name, value):
                # You could change the current header in self.current_header_name
                # or self.current_header_value, the current request url is
                # in self.request_url
                return HTTP_HDR_DROP