def api(request): print request print "a", request.is_ajax() if ("url" not in request.POST or "customURL" not in request.POST or ("csrfmiddlewaretoken" not in request.POST and "isFromExtension" not in request.POST)): print request.POST raise Http404 print "a" # Need client IP for flood prevention so need this header. # It's a required header to be standard-compliant. if "REMOTE_ADDR" not in request.META: return HttpResponse("Oops, seems like you're using a non-standard compliant browser!") isFromExtension = False if "isFromExtension" in request.POST: isFromExtension = True ips = models.IP.objects.filter(ip=request.META["REMOTE_ADDR"]) print request.POST url = str(request.POST["url"]) print "url", url customURL = request.POST["customURL"] # If we have a custom URL, then validate it. if customURL != "": p = re.compile("[^a-zA-Z-_0-9]") results = p.search(customURL) # If not valid, return error message. if results != None: return HttpResponse("Custom URLs can only contain alphanumeric symbols, - and _", content_type="text/plain") if len(customURL) <4 or len(customURL) > 32: return HttpResponse("Custom URLs must be between 4 and 32 characters long!", content_type="text/plain") print "a" validator = URLValidator() try: # Validate url print "url", url validator(url) print "url2", url # First check if we have a custom url. # If so, we override the preexisting checks. if customURL != "": # Check if customURL is already in use or not existingCheck = models.Url.objects.filter(hashOfUrl=customURL) if len(existingCheck) > 0: # Check if that full url already exists, eg the exact same parameters if existingCheck[0].fullUrl == url: # Already existed with exact same shortening so might as well return it return HttpResponse(existingCheck[0].shortenedUrl, content_type="text/plain") # Otherwise we have a conflicting customURL to different redirects return HttpResponse("That custom URL is already in use!", content_type="text/plain") # Else, we're good. returnUrl = HOST_URL+customURL # Store IP to prevent floods if len(ips)==0: ipObj = models.IP(ip=request.META["REMOTE_ADDR"]) else: ipObj = ips[0] # 2016-01-12 16:22:22.129932+00:00 now = datetime.datetime.now(tz=pytz.utc) # If requests are too quick if (formatTime(now)-formatTime(ipObj.lastUsed))<=datetime.timedelta(milliseconds=1000): return HttpResponse("Slow down! You're making too many requests!",content_type="text/plain") ipObj.lastUsed = now ipObj.save() urlObj = models.Url(fullUrl=url, hashOfUrl=customURL, shortenedUrl=returnUrl, hits=0,isCustom=True, madeByExtension=isFromExtension) urlObj.save() return HttpResponse(returnUrl, content_type="text/plain") print "b" check = models.Url.objects.filter(fullUrl=url) if len(check) >0: for url in check: if url.isCustom == False: returnUrl = check[0].shortenedUrl print "already exists",returnUrl return HttpResponse(returnUrl, content_type="text/plain") print "aaaaaa", repr(url) hashed = str(url) collission = True while collission: hashed = hasher.returnShortenedURL(hashed) print "hashing" if len(models.Url.objects.filter(hashOfUrl=hashed)) == 0: collission = False returnUrl = HOST_URL+hashed print "done hashing" print repr(url); print str(url), hashed, returnUrl, isFromExtension urlObj = models.Url(fullUrl=str(url), hashOfUrl=hashed, shortenedUrl=returnUrl, hits=0,isCustom=False, madeByExtension=isFromExtension) print "a" print urlObj try: urlObj.save() except: print "AAAAAAAA" return HttpResponse("Oops something broke! Try again in a bit!", content_type="text/plain") print "asdf" if len(ips)==0: ipObj = models.IP(ip=request.META["REMOTE_ADDR"]) else: print "f" ipObj = ips[0] # 2016-01-12 16:22:22.129932+00:00 now = datetime.datetime.now(tz=pytz.utc) # If requests are too quick if (formatTime(now)-formatTime(ipObj.lastUsed))<=datetime.timedelta(milliseconds=1000): return HttpResponse("Slow down! You're making too many requests!",content_type="text/plain") ipObj.lastUsed = now ipObj.save() return HttpResponse(returnUrl, content_type="text/plain") except ValidationError, e: print e return HttpResponse("Please enter a valid and full url!", content_type="text/plain")
def api(request): if ("url" not in request.POST or "csrfmiddlewaretoken" not in request.POST or "customURL" not in request.POST): print request.POST raise Http404 url = request.POST["url"] customURL = request.POST["customURL"] # If we have a custom URL, then validate it. if customURL != "": p = re.compile("[^a-zA-Z-_0-9]") results = p.search(customURL) # If not valid, return error message. if results != None: return HttpResponse( "Custom URLs can only contain alphanumeric symbols, - and _", content_type="text/plain") if len(customURL) < 4 or len(customURL) > 32: return HttpResponse( "Custom URLs must be between 4 and 32 characters long!", content_type="text/plain") validator = URLValidator() try: # Validate url validator(url) # First check if we have a custom url. # If so, we override the preexisting checks. if customURL != "": # Check if customURL is already in use or not existingCheck = models.Url.objects.filter(hashOfUrl=customURL) if len(existingCheck) > 0: # Check if that full url already exists, eg the exact same parameters if existingCheck[0].fullUrl == url: # Already existed with exact same shortening so might as well return it return HttpResponse(existingCheck[0].shortenedUrl, content_type="text/plain") # Otherwise we have a conflicting customURL to different redirects return HttpResponse("That custom URL is already in use!", content_type="text/plain") # Else, we're good. returnUrl = HOST_URL + customURL urlObj = models.Url(fullUrl=url, hashOfUrl=customURL, shortenedUrl=returnUrl, hits=0) urlObj.save() return HttpResponse(returnUrl, content_type="text/plain") # check = models.Url.objects.filter(fullUrl=url) # if len(check) >0: # returnUrl = check[0].shortenedUrl # print "already exists",returnUrl # return HttpResponse(returnUrl, content_type="text/plain") hashed = url collission = True while collission: hashed = hasher.returnShortenedURL(hashed) if len(models.Url.objects.filter(hashOfUrl=hashed)) == 0: collission = False returnUrl = HOST_URL + hashed urlObj = models.Url(fullUrl=url, hashOfUrl=hashed, shortenedUrl=returnUrl, hits=0) urlObj.save() return HttpResponse(returnUrl, content_type="text/plain") except ValidationError, e: print e return HttpResponse("Please enter a valid and full url!", content_type="text/plain")
def api(request): if ("url" not in request.POST or "csrfmiddlewaretoken" not in request.POST or "customURL" not in request.POST): print request.POST raise Http404 url = request.POST["url"] customURL = request.POST["customURL"] # If we have a custom URL, then validate it. if customURL != "": p = re.compile("[^a-zA-Z-_0-9]") results = p.search(customURL) # If not valid, return error message. if results != None: return HttpResponse("Custom URLs can only contain alphanumeric symbols, - and _", content_type="text/plain") if len(customURL) <4 or len(customURL) > 32: return HttpResponse("Custom URLs must be between 4 and 32 characters long!", content_type="text/plain") validator = URLValidator() try: # Validate url validator(url) # First check if we have a custom url. # If so, we override the preexisting checks. if customURL != "": # Check if customURL is already in use or not existingCheck = models.Url.objects.filter(hashOfUrl=customURL) if len(existingCheck) > 0: # Check if that full url already exists, eg the exact same parameters if existingCheck[0].fullUrl == url: # Already existed with exact same shortening so might as well return it return HttpResponse(existingCheck[0].shortenedUrl, content_type="text/plain") # Otherwise we have a conflicting customURL to different redirects return HttpResponse("That custom URL is already in use!", content_type="text/plain") # Else, we're good. returnUrl = HOST_URL+customURL urlObj = models.Url(fullUrl=url,hashOfUrl=customURL,shortenedUrl=returnUrl,hits=0) urlObj.save() return HttpResponse(returnUrl, content_type="text/plain") # check = models.Url.objects.filter(fullUrl=url) # if len(check) >0: # returnUrl = check[0].shortenedUrl # print "already exists",returnUrl # return HttpResponse(returnUrl, content_type="text/plain") hashed = url collission = True while collission: hashed = hasher.returnShortenedURL(hashed) if len(models.Url.objects.filter(hashOfUrl=hashed)) == 0: collission = False returnUrl = HOST_URL+hashed urlObj = models.Url(fullUrl=url,hashOfUrl=hashed,shortenedUrl=returnUrl,hits=0) urlObj.save() return HttpResponse(returnUrl, content_type="text/plain") except ValidationError, e: print e return HttpResponse("Please enter a valid and full url!", content_type="text/plain")