def application(environ, start_response): """Main WSGI handler. Process requests and calls proper functions.""" global routes fname = environ['PATH_INFO'] config = configparser.RawConfigParser() here = os.path.dirname(__file__) config.read(os.path.join(here, 'routing.cfg')) verbo = config.get('Service', 'verbosity') baseURL = config.get('Service', 'baseURL') # Warning is the default value verboNum = getattr(logging, verbo.upper(), 30) logging.info('Verbosity configured with %s' % verboNum) logging.basicConfig(level=verboNum) # Among others, this will filter wrong function names, # but also the favicon.ico request, for instance. if fname is None: raise WIClientError('Method name not recognized!') # return send_html_response(status, 'Error! ' + status, start_response) if len(environ['QUERY_STRING']) > 1000: return send_error_response("414 Request URI too large", "maximum URI length is 1000 characters", start_response) try: outForm = 'xml' if environ['REQUEST_METHOD'] == 'GET': form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) if 'format' in form: outForm = form['format'].value.lower() elif environ['REQUEST_METHOD'] == 'POST': try: length = int(environ.get('CONTENT_LENGTH', '0')) except ValueError: length = 0 # If there is a body to read if length: form = environ['wsgi.input'].read(length).decode() else: form = environ['wsgi.input'].read().decode() for line in form.splitlines(): if not len(line): continue if '=' not in line: break k, v = line.split('=') if k.strip() == 'format': outForm = v.strip() else: raise Exception except ValueError as e: if str(e) == "Maximum content length exceeded": # Add some user-friendliness (this message triggers an alert # box on the client) return send_error_response("400 Bad Request", "maximum request size exceeded", start_response) return send_error_response("400 Bad Request", str(e), start_response) # Check whether the function called is implemented implementedFunctions = ['query', 'application.wadl', 'localconfig', 'globalconfig', 'version', 'info', '', 'virtualnets', 'endpoints'] if routes is None: # Add routing cache here, to be accessible to all modules routesFile = os.path.join(here, 'data', 'routing.xml') configFile = os.path.join(here, 'routing.cfg') routes = RoutingCache(routesFile, configFile) fname = environ['PATH_INFO'].split('/')[-1] if fname not in implementedFunctions: return send_error_response("400 Bad Request", 'Function "%s" not implemented.' % fname, start_response) if fname == '': here = os.path.dirname(__file__) helpFile = os.path.join(here, 'help.html') with open(helpFile, 'r') as helpHandle: iterObj = helpHandle.read() status = '200 OK' return send_html_response(status, iterObj, start_response) elif fname == 'application.wadl': here = os.path.dirname(__file__) appWadl = os.path.join(here, 'application.wadl') with open(appWadl, 'r') \ as appFile: tomorrow = datetime.date.today() + datetime.timedelta(days=1) iterObj = appFile.read() % (baseURL, tomorrow) status = '200 OK' return send_xml_response(status, iterObj, start_response) elif fname == 'query': makeQuery = globals()['makeQuery%s' % environ['REQUEST_METHOD']] try: iterObj = makeQuery(form) iterObj = applyFormat(iterObj, outForm) status = '200 OK' if outForm == 'xml': return send_xml_response(status, iterObj, start_response) elif outForm == 'json': return send_json_response(status, iterObj, start_response) else: return send_plain_response(status, iterObj, start_response) except WIError as w: return send_error_response(w.status, w.body, start_response) elif fname == 'endpoints': result = routes.endpoints() return send_plain_response('200 OK', result, start_response) elif fname == 'localconfig': result = routes.localConfig() if outForm == 'xml': return send_xml_response('200 OK', result, start_response) elif fname == 'globalconfig': result = routes.globalConfig() if outForm == 'fdsn': return send_json_response('200 OK', result, start_response) # Only FDSN format is supported for the time being text = 'Only format=FDSN is supported' return send_error_response("400 Bad Request", text, start_response) elif fname == 'virtualnets': result = routes.virtualNets() return send_json_response('200 OK', result, start_response) elif fname == 'version': text = "1.2.1-b2" return send_plain_response('200 OK', text, start_response) elif fname == 'info': config = configparser.RawConfigParser() here = os.path.dirname(__file__) config.read(os.path.join(here, 'routing.cfg')) text = config.get('Service', 'info') return send_plain_response('200 OK', text, start_response) raise Exception('This point should have never been reached!')
def setUp(cls): "Setting up test" if hasattr(cls, 'rc'): return cls.rc = RoutingCache('../data/routing.xml.sample')