From a372a96d9f5ebcc976e5ab7c42febf5437c7f6d7 Mon Sep 17 00:00:00 2001 From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk> Date: Fri, 2 Nov 2018 13:29:19 +0000 Subject: [PATCH] Updates CLMC to always set the HTTP Accept header to application/json - otherwise, error messages might be sent as HTML --- src/service/clmcservice/__init__.py | 2 ++ src/service/clmcservice/tweens.py | 36 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/service/clmcservice/tweens.py diff --git a/src/service/clmcservice/__init__.py b/src/service/clmcservice/__init__.py index 70c6e76..3d56991 100644 --- a/src/service/clmcservice/__init__.py +++ b/src/service/clmcservice/__init__.py @@ -60,6 +60,8 @@ def main(global_config, **settings): config = Configurator(settings=settings) + config.add_tween('clmcservice.tweens.accept_header_tween_factory') + # add routes of the WHOAMI API config.add_route('whoami_endpoints', '/whoami/endpoints') config.add_route('whoami_endpoints_instance', 'whoami/endpoints/instance') diff --git a/src/service/clmcservice/tweens.py b/src/service/clmcservice/tweens.py new file mode 100644 index 0000000..a0f9eba --- /dev/null +++ b/src/service/clmcservice/tweens.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 +""" +// © University of Southampton IT Innovation Centre, 2018 +// +// Copyright in this software belongs to University of Southampton +// IT Innovation Centre of Gamma House, Enterprise Road, +// Chilworth Science Park, Southampton, SO16 7NS, UK. +// +// This software may not be used, sold, licensed, transferred, copied +// or reproduced in whole or in part in any manner or form or in or +// on any media by any person other than in accordance with the terms +// of the Licence Agreement supplied with the software, or otherwise +// without the prior written consent of the copyright owners. +// +// This software is distributed WITHOUT ANY WARRANTY, without even the +// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE, except where stated in the Licence Agreement supplied with +// the software. +// +// Created By : Nikolay Stanchev +// Created Date : 02-11-2018 +// Created for Project : FLAME +""" + + +def accept_header_tween_factory(handler, registry): + """ + Since clmc-service is currently based on JSON only, make sure an application/json header is passed with the request + """ + + def accept_header_tween(request): + request.accept = 'application/json' + response = handler(request) + return response + + return accept_header_tween -- GitLab