def checkContentTypeIgnoreCase(request, expectedContentType): """Performs Content-Type validation of the incoming request, ignoring case and any <code>charset</code> parameter. @see #checkCharacterEncodingIgnoreCase(HttpServletRequest, String) @param request the incoming request @param expectedContentType the expected Content-Type for the incoming request @throws ServletException if the request's content type is not <code>null</code> and does not, ignoring case, equal <code>expectedContentType</code>, """ assert expectedContentType is not None contentType = CONTENT_TYPE(request.environ()) contentTypeIsOkay = False if contentType is not None: contentType = contentType.lower() # NOTE:We use startsWith because some servlet engines, do # not remove the charset component but others do. if contentType.startswith(expectedContentType.lower()): contentTypeIsOkay = True if not contentTypeIsOkay: raise ServletException('Content-Type was \'' + ( '(null)' if contentType is None else contentType) + '\'. Expected \'' + expectedContentType + '\'.')
def checkContentTypeIgnoreCase(request, expectedContentType): """Performs Content-Type validation of the incoming request, ignoring case and any <code>charset</code> parameter. @see #checkCharacterEncodingIgnoreCase(HttpServletRequest, String) @param request the incoming request @param expectedContentType the expected Content-Type for the incoming request @throws ServletException if the request's content type is not <code>null</code> and does not, ignoring case, equal <code>expectedContentType</code>, """ assert expectedContentType is not None contentType = CONTENT_TYPE(request.environ()) contentTypeIsOkay = False if contentType is not None: contentType = contentType.lower() # NOTE:We use startsWith because some servlet engines, do # not remove the charset component but others do. if contentType.startswith(expectedContentType.lower()): contentTypeIsOkay = True if not contentTypeIsOkay: raise ServletException('Content-Type was \'' + ('(null)' if contentType is None else contentType) + '\'. Expected \'' + expectedContentType + '\'.')
def challenge(self, environ, status, app_headers, forget_headers): log.debug ("APP CHALLENGE %s %s %s %s", environ.keys(), status, app_headers, forget_headers) content_type = CONTENT_TYPE(environ) log.debug ("APP CONTENT %s", content_type) if any (content_type.startswith(v) for v in ('application/json', 'application/xml')): return Response(status=401, content_type=content_type, body="") return None
def bisque_request_classifier(environ): request_method = REQUEST_METHOD(environ) content_type = CONTENT_TYPE(environ) if request_method == "POST" and content_type.startswith( "application/json"): return "json" if content_type.startswith("application/xml"): return "xml" return default_request_classifier(environ)
def auth_form(environ, start_response): towrite = "Challenging this" content_length = CONTENT_LENGTH.tuples(str(len(towrite))) content_type = CONTENT_TYPE.tuples('text/html') headers = content_length + content_type + forget_headers start_response('200 OK', headers) return [towrite]
def my_request_classifier(environ): """ Returns one of the classifiers 'dav', 'xmlpost', or 'browser', depending on the imperative logic below""" request_method = REQUEST_METHOD(environ) if request_method in _DAV_METHODS: return 'dav' useragent = USER_AGENT(environ) if useragent: for agent in _DAV_USERAGENTS: if useragent.find(agent) != -1: return 'dav' if request_method == 'POST': if CONTENT_TYPE(environ) == 'text/xml': return 'xmlpost' elif CONTENT_TYPE(environ) == "application/soap+xml": return 'soap' return 'browser'
def bungeni_request_classifier(environ): """ Returns one of the classifiers "api", "xmlpost", or "browser", depending on the imperative logic below""" if PATH_INFO(environ).startswith("/api"): return "api" request_method = REQUEST_METHOD(environ) if request_method == "POST": if CONTENT_TYPE(environ) == "text/xml": return "xmlpost" return "browser"
def bungeni_request_classifier(environ): """ Returns one of the classifiers 'api', 'xmlpost', or 'browser', depending on the imperative logic below""" if PATH_INFO(environ).startswith("/api"): return 'api' request_method = REQUEST_METHOD(environ) if request_method == 'POST': if CONTENT_TYPE(environ) == 'text/xml': return 'xmlpost' return 'browser'
def auth_resp(environ, start_response): import json resp = {"success": True} resp_str = json.dumps(resp) content_length = CONTENT_LENGTH.tuples(str(len(resp_str))) content_type = CONTENT_TYPE.tuples('application/json') headers = content_length + content_type start_response('200 OK', headers) return [resp_str]
def identify(self, environ): request = Request(environ) # first test for logout as we then don't need the rest if request.path == self.logout_path: headers = self.forget(environ, {}) raise HTTPFound (location="/", headers = headers) if request.path == self.login_path: content_type = CONTENT_TYPE(environ) if content_type.startswith('application/json'): login_data = json.load (request.body_file) elif content_type.startswith('application/xml'): xml = etree.parse (request.body_file).getroot() login_data = dict (username = xml.find ("tag[@name='username']").get('value'), password = xml.find ("tag[@name='password']").get('value')) else: return None log.debug ("found user %s", login_data.get('username', None)) return { 'login': login_data.get('username', None), 'password': login_data.get('password', None) } return None
def auth_resp(environ, start_response): import json resp = { "success": True } resp_str = json.dumps(resp) content_length = CONTENT_LENGTH.tuples(str(len(resp_str))) content_type = CONTENT_TYPE.tuples('application/json') headers = content_length + content_type start_response('200 OK', headers) return [resp_str]
def auth_form(environ, start_response): content_length = CONTENT_LENGTH.tuples(str(len(form))) content_type = CONTENT_TYPE.tuples('text/html') headers = content_length + content_type + forget_headers start_response('200 OK', headers) return [form]
def getContentType(self, request): return CONTENT_TYPE(request.environ())