Relax requirement for a content-type on .well-known (#4511)
This commit is contained in:
parent
d02c5ccb11
commit
cc2d650ef7
|
@ -0,0 +1 @@
|
||||||
|
Implement MSC1708 (.well-known routing for server-server federation)
|
|
@ -12,7 +12,6 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
import cgi
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -263,37 +262,31 @@ class MatrixFederationAgent(object):
|
||||||
# FIXME: add a cache
|
# FIXME: add a cache
|
||||||
|
|
||||||
uri = b"https://%s/.well-known/matrix/server" % (server_name, )
|
uri = b"https://%s/.well-known/matrix/server" % (server_name, )
|
||||||
logger.info("Fetching %s", uri.decode("ascii"))
|
uri_str = uri.decode("ascii")
|
||||||
|
logger.info("Fetching %s", uri_str)
|
||||||
try:
|
try:
|
||||||
response = yield make_deferred_yieldable(
|
response = yield make_deferred_yieldable(
|
||||||
self._well_known_agent.request(b"GET", uri),
|
self._well_known_agent.request(b"GET", uri),
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(
|
logger.info("Connection error fetching %s: %s", uri_str, e)
|
||||||
"Connection error fetching %s: %s",
|
|
||||||
uri.decode("ascii"), e,
|
|
||||||
)
|
|
||||||
defer.returnValue(None)
|
defer.returnValue(None)
|
||||||
|
|
||||||
body = yield make_deferred_yieldable(readBody(response))
|
body = yield make_deferred_yieldable(readBody(response))
|
||||||
|
|
||||||
if response.code != 200:
|
if response.code != 200:
|
||||||
logger.info(
|
logger.info("Error response %i from %s", response.code, uri_str)
|
||||||
"Error response %i from %s: %s",
|
|
||||||
response.code, uri.decode("ascii"), body,
|
|
||||||
)
|
|
||||||
defer.returnValue(None)
|
defer.returnValue(None)
|
||||||
|
|
||||||
content_types = response.headers.getRawHeaders(u'content-type')
|
try:
|
||||||
if content_types is None:
|
parsed_body = json.loads(body.decode('utf-8'))
|
||||||
raise Exception("no content-type header on .well-known response")
|
logger.info("Response from .well-known: %s", parsed_body)
|
||||||
content_type, _opts = cgi.parse_header(content_types[-1])
|
if not isinstance(parsed_body, dict):
|
||||||
if content_type != 'application/json':
|
raise Exception("not a dict")
|
||||||
raise Exception("content-type not application/json on .well-known response")
|
if "m.server" not in parsed_body:
|
||||||
parsed_body = json.loads(body.decode('utf-8'))
|
raise Exception("Missing key 'm.server'")
|
||||||
logger.info("Response from .well-known: %s", parsed_body)
|
except Exception as e:
|
||||||
if not isinstance(parsed_body, dict) or "m.server" not in parsed_body:
|
raise Exception("invalid .well-known response from %s: %s" % (uri_str, e,))
|
||||||
raise Exception("invalid .well-known response")
|
|
||||||
defer.returnValue(parsed_body["m.server"].encode("ascii"))
|
defer.returnValue(parsed_body["m.server"].encode("ascii"))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -146,7 +146,6 @@ class MatrixFederationAgentTests(TestCase):
|
||||||
[b'testserv'],
|
[b'testserv'],
|
||||||
)
|
)
|
||||||
# send back a response
|
# send back a response
|
||||||
request.responseHeaders.setRawHeaders(b'Content-Type', [b'application/json'])
|
|
||||||
request.write(b'{ "m.server": "%s" }' % (target_server,))
|
request.write(b'{ "m.server": "%s" }' % (target_server,))
|
||||||
request.finish()
|
request.finish()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue