Include version in User-Agent and Server headers

This commit is contained in:
Mark Haines 2014-12-22 10:16:02 +00:00
parent 420ccfc925
commit 24b5d01853
4 changed files with 35 additions and 6 deletions

View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from synapse import __version__
AGENT_NAME = ("Synapse/%s" % (__version__,)).encode("ascii")

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
from synapse.http.agent_name import AGENT_NAME
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
from twisted.web.client import ( from twisted.web.client import (
Agent, readBody, FileBodyProducer, PartialDownloadError Agent, readBody, FileBodyProducer, PartialDownloadError
@ -51,7 +52,8 @@ class SimpleHttpClient(object):
"POST", "POST",
uri.encode("ascii"), uri.encode("ascii"),
headers=Headers({ headers=Headers({
"Content-Type": ["application/x-www-form-urlencoded"] b"Content-Type": [b"application/x-www-form-urlencoded"],
b"User-Agent": AGENT_NAME,
}), }),
bodyProducer=FileBodyProducer(StringIO(query_bytes)) bodyProducer=FileBodyProducer(StringIO(query_bytes))
) )
@ -86,6 +88,9 @@ class SimpleHttpClient(object):
response = yield self.agent.request( response = yield self.agent.request(
"GET", "GET",
uri.encode("ascii"), uri.encode("ascii"),
headers=Headers({
b"User-Agent": AGENT_NAME,
})
) )
body = yield readBody(response) body = yield readBody(response)
@ -108,7 +113,8 @@ class CaptchaServerHttpClient(SimpleHttpClient):
url.encode("ascii"), url.encode("ascii"),
bodyProducer=FileBodyProducer(StringIO(query_bytes)), bodyProducer=FileBodyProducer(StringIO(query_bytes)),
headers=Headers({ headers=Headers({
"Content-Type": ["application/x-www-form-urlencoded"] b"Content-Type": [b"application/x-www-form-urlencoded"],
b"User-Agent": AGENT_NAME,
}) })
) )

View File

@ -20,6 +20,7 @@ from twisted.web.client import readBody, _AgentBase, _URI
from twisted.web.http_headers import Headers from twisted.web.http_headers import Headers
from twisted.web._newclient import ResponseDone from twisted.web._newclient import ResponseDone
from synapse.http.agent_name import AGENT_NAME
from synapse.http.endpoint import matrix_federation_endpoint from synapse.http.endpoint import matrix_federation_endpoint
from synapse.util.async import sleep from synapse.util.async import sleep
from synapse.util.logcontext import PreserveLoggingContext from synapse.util.logcontext import PreserveLoggingContext
@ -71,6 +72,7 @@ class MatrixFederationHttpClient(object):
requests. requests.
""" """
def __init__(self, hs): def __init__(self, hs):
self.hs = hs self.hs = hs
self.signing_key = hs.config.signing_key[0] self.signing_key = hs.config.signing_key[0]
@ -83,7 +85,7 @@ class MatrixFederationHttpClient(object):
query_bytes=b"", retry_on_dns_fail=True): query_bytes=b"", retry_on_dns_fail=True):
""" Creates and sends a request to the given url """ Creates and sends a request to the given url
""" """
headers_dict[b"User-Agent"] = [b"Synapse"] headers_dict[b"User-Agent"] = [AGENT_NAME]
headers_dict[b"Host"] = [destination] headers_dict[b"Host"] = [destination]
url_bytes = urlparse.urlunparse( url_bytes = urlparse.urlunparse(

View File

@ -14,14 +14,16 @@
# limitations under the License. # limitations under the License.
from syutil.jsonutil import ( from synapse.http.agent_name import AGENT_NAME
encode_canonical_json, encode_pretty_printed_json
)
from synapse.api.errors import ( from synapse.api.errors import (
cs_exception, SynapseError, CodeMessageException cs_exception, SynapseError, CodeMessageException
) )
from synapse.util.logcontext import LoggingContext from synapse.util.logcontext import LoggingContext
from syutil.jsonutil import (
encode_canonical_json, encode_pretty_printed_json
)
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
from twisted.web import server, resource from twisted.web import server, resource
from twisted.web.server import NOT_DONE_YET from twisted.web.server import NOT_DONE_YET
@ -230,6 +232,7 @@ def respond_with_json_bytes(request, code, json_bytes, send_cors=False,
request.setResponseCode(code, message=response_code_message) request.setResponseCode(code, message=response_code_message)
request.setHeader(b"Content-Type", b"application/json") request.setHeader(b"Content-Type", b"application/json")
request.setHeader(b"Server", AGENT_NAME)
if send_cors: if send_cors:
request.setHeader("Access-Control-Allow-Origin", "*") request.setHeader("Access-Control-Allow-Origin", "*")