o a!@sddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZGd d d eZGd d d eZdS)N)urlparse)settings)PermissionDenied) mail_managers)HttpResponsePermanentRedirect) is_valid_path)MiddlewareMixin)escape_leading_slashesc@s4eZdZdZeZddZddZddZdd Z d S) CommonMiddlewarea  "Common" middleware for taking care of some basic operations: - Forbid access to User-Agents in settings.DISALLOWED_USER_AGENTS - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings, append missing slashes and/or prepends missing "www."s. - If APPEND_SLASH is set and the initial URL doesn't end with a slash, and it is not found in urlpatterns, form a new URL by appending a slash at the end. If this new URL is found in urlpatterns, return an HTTP redirect to this new URL; otherwise process the initial URL as usual. This behavior can be customized by subclassing CommonMiddleware and overriding the response_redirect_class attribute. cCs|jd}|durtjD] }||rtdq |}tjo'|o'|d }|r1d|j |fnd}| |r>| |}n| }|sJ|| krS||7}| |SdS)z Check for denied User-Agents and rewrite the URL based on settings.APPEND_SLASH and settings.PREPEND_WWW HTTP_USER_AGENTNzForbidden user agentzwww.z %s://www.%s)METAgetrDISALLOWED_USER_AGENTSsearchrget_host PREPEND_WWW startswithschemeshould_redirect_with_slashget_full_path_with_slash get_full_pathresponse_redirect_class)selfrequest user_agentuser_agent_regexhost must_prepend redirect_urlpathr!:/usr/lib/python3/dist-packages/django/middleware/common.pyprocess_request"s      z CommonMiddleware.process_requestcCsTtjr(|jds(t|dd}t|j|s(td|j|}|r(|j}t|ddSdS)z Return True if settings.APPEND_SLASH is True and appending a slash to the request path turns an invalid path into a valid one. /urlconfNz%s/should_append_slashTF)r APPEND_SLASH path_infoendswithgetattrrfunc)rrr%matchviewr!r!r"r?s   z+CommonMiddleware.should_redirect_with_slashcCsD|jdd}t|}tjr |jdvr td|j||d|S)z Return the full path of the request with a trailing slash appended. Raise a RuntimeError if settings.DEBUG is True and request.method is POST, PUT, or PATCH. T)force_append_slash)POSTPUTPATCHa"You called this URL via %(method)s, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining %(method)s data. Change your form to point to %(url)s (note the trailing slash), or set APPEND_SLASH=False in your Django settings.)methodurl)rr rDEBUGr2 RuntimeErrorr)rrnew_pathr!r!r"rMs   z)CommonMiddleware.get_full_path_with_slashcCsL|jdkr||r|||S|js$|ds$tt|j|j d<|S)z When the status code of the response is 404, it may redirect to a path with an appended slash if should_redirect_with_slash() returns True. zContent-Length) status_coderrr streaming has_headerstrlencontentheaders)rrresponser!r!r"process_responseds z!CommonMiddleware.process_responseN) __name__ __module__ __qualname____doc__rrr#rrr@r!r!r!r"r s r c@s$eZdZddZddZddZdS)BrokenLinkEmailsMiddlewarecCs|jdkrFtjsF|}|}|jdd}|||||sF|jdd}|jdd}td| ||r7dnd|fd ||||fd d |S) z=Send broken link emails for relevant 404 NOT FOUND responses.r7 HTTP_REFERERr r z REMOTE_ADDRzBroken %slink on %sz INTERNAL z=Referrer: %s Requested URL: %s User agent: %s IP address: %s T) fail_silently) r8rr4rrr ris_ignorable_requestris_internal_request)rrr?domainr refereruaipr!r!r"r@xs$  z+BrokenLinkEmailsMiddleware.process_responsecCsttdt||S)ze Return True if the referring URL is the same domain as the current request. z ^https?://%s/)boolrer,escape)rrKrLr!r!r"rJsz.BrokenLinkEmailsMiddleware.is_internal_requestcs|sdStjrdr|ddkrdS|||s"d|vr"dSt|}|jd|fvr4|jkr4dStfddtjDS) z Return True if the given request *shouldn't* notify the site managers according to project settings or in situations outlined by the inline comments. Tr$N?r c3s|]}|VqdS)N)r).0patternurir!r" szBBrokenLinkEmailsMiddleware.is_ignorable_request..) rr'r)rJrnetlocr anyIGNORABLE_404_URLS)rrrWrKrLparsed_refererr!rVr"rIs z/BrokenLinkEmailsMiddleware.is_ignorable_requestN)rArBrCr@rJrIr!r!r!r"rEvs rE)rP urllib.parser django.confrdjango.core.exceptionsrdjango.core.mailr django.httpr django.urlsrdjango.utils.deprecationrdjango.utils.httpr r rEr!r!r!r"s        i