Esempio n. 1
0
def is_valid_email(e):    
    ##if not hasattr(RegularExpressions, "emailMatcher"):
    if not rx.exists("emailMatcher"):
        # See RFC 2821, RFC 2822, RFC 3696
        
        # lots of legitimate chars in localname, but can't start or end with a .
        # Not all emailers support all of these characters
        localnameChars = r"[-.a-zA-Z0-9!#$%*/?|^{}`~'""+=_]"
        localnameTerminalChars = r"[-a-zA-Z0-9!#$%*/?|^{}`~'""+=_]"
        localnameRE = r"(%s|%s%s{0,62}%s)" % (localnameTerminalChars, localnameTerminalChars, localnameChars, localnameTerminalChars)
        
        # Domain name can't start or end with a . or -
        domainChars = r"[-.a-zA-Z0-9]"
        domainTerminalChars = r"[a-zA-Z0-9]"
        hostnameRE = r"(%s|%s%s*%s)" % (domainTerminalChars, domainTerminalChars, domainChars, domainTerminalChars)
        
        tldChars = r"[a-zA-Z]"
        tldIntlChars = r"[a-zA-Z0-9]"
        tldRE = r"(%s{2,10}|\.xn--%s+)" % (tldChars, tldIntlChars)
        
        emailRE = r"^(?P<localname>%s)@(?P<hostname>%s)\.(?P<tld>%s)$" % (localnameRE, hostnameRE, tldRE)
        
        rx.define("emailMatcher", emailRE)
    
    matches = rx.match("emailMatcher", e)
    if matches is None:
        return False
    
    localname = matches.group("localname")
    hostname = matches.group("hostname")
    
    if localname.find("..") != -1 or hostname.find("..") != -1:
        return False

    return True
Esempio n. 2
0
def default_handler(request):
    """Default handler for Django and html pages"""

    if not rx.exists(kiwiPathMatcher):
        rx.define(kiwiPathMatcher, r"^(/)?(?P<name>.+?)(?P<suffix>(%s)%s)$" % (kiwioptions.WEB_ALLOWED_SUFFIXES, "?" if kiwioptions.WEB_PREFERRED_SUFFIX == "" else ""))

    # Separate the path into a name and suffix
    path = request.path.replace("\\", "/")
    m = rx.match(kiwiPathMatcher, path)
    if m is None:
        return error404(request)

    templateName = m.group("name")
    givenSuffix = m.group("suffix")

    # For GET requests when there is a preferred suffix, do a 301 to it if necessary
    if kiwioptions.WEB_PREFERRED_SUFFIX is not None and (request.method == "GET") and (givenSuffix != kiwioptions.WEB_PREFERRED_SUFFIX):
        newPath = "/" + templateName + kiwioptions.WEB_PREFERRED_SUFFIX
        return HttpResponsePermanentRedirect(newPath)
    
    # The urlpatterns should have filtered out any URLs with .'s, but make sure
    if templateName.find(".") > 0:
        return error404(request)
    
    return render_template_to_response(request, templateName)