def _show_general_stats(self): """ Prints a page with some serice wide statistics. | **param** header_only (bool) """ stat = YuStats() template_filename = self._get_config_template("stats") text = read_template( template_filename, title=SERVER_NAME, header=SERVER_NAME, number_of_links=format_none(stat.links_all), number_of_redirects=format_none(stat.redirect_all), number_of_redirects_today=format_none(stat.redirect_today), number_of_redirects_this_week=format_none(stat.redirect_this_week), number_of_redirects_this_month=format_none(stat.redirect_this_month), number_of_redirects_this_year=format_none(stat.redirect_this_year), number_of_url_today=format_none(stat.links_today), number_of_url_this_week=format_none(stat.links_this_week), number_of_url_this_month=format_none(stat.links_this_month), number_of_url_this_year=format_none(stat.links_this_year), date_of_first_redirect=format_none(stat.date_of_first_redirect), ) if text: self._send_head(text, 200) if not self._header_only: try: self.wfile.write(text) except socket.error: # clients like to stop reading after they got a 404 pass else: self._send_internal_server_error()
def _send_404(self): """ Send HTTP status code 404 """ template_filename = self._get_config_template("404") text = read_template( template_filename, title="%s - 404" % SERVER_NAME, header="404 — Page not found", URL="Nothing" ) self._send_response(text, 404)
def _send_homepage(self, message=""): """ Sends the default startpage. |**param** message (str) Optional message that should appear on startpage """ template_filename = self._get_config_template("homepage") text = read_template(template_filename, title=SERVER_NAME, header=SERVER_NAME, msg=message) self._send_response(text, 200)
def _send_blocked_page(self, reason): """ Send a block page which tells the user that the URL has been blocked for some reason. | **param** reason Reason, why page has been blocked (str) """ template_filename = self._get_config_template("blocked") text = read_template(template_filename, title=SERVER_NAME, header=SERVER_NAME, comment=reason) self._send_response(text, 200)
def _send_404(self): """ Send HTTP status code 404 """ template_filename = self._get_config_template('404') text = read_template(template_filename, title='%s - 404' % SERVER_NAME, header='404 — Page not found', URL="Nothing") self._send_response(text, 404)
def _send_internal_server_error(self): """ Send HTTP status code 500 """ template_filename = self._get_config_template("500") text = read_template(template_filename, title="%s - Internal Error" % SERVER_NAME, header="Internal error") if not text: # fallback to hard-coded template text = TEMPLATE_500 self._send_head(text, 500) if not self._header_only: self.wfile.write(text)
def _send_homepage(self, message=''): """ Sends the default startpage. |**param** message (str) Optional message that should appear on startpage """ template_filename = self._get_config_template('homepage') text = read_template(template_filename, title=SERVER_NAME, header=SERVER_NAME, msg=message) self._send_response(text, 200)
def _send_blocked_page(self, reason): """ Send a block page which tells the user that the URL has been blocked for some reason. | **param** reason Reason, why page has been blocked (str) """ template_filename = self._get_config_template('blocked') text = read_template(template_filename, title=SERVER_NAME, header=SERVER_NAME, comment=reason) self._send_response(text, 200)
def _send_internal_server_error(self): """ Send HTTP status code 500 """ template_filename = self._get_config_template('500') text = read_template(template_filename, title='%s - Internal Error' % SERVER_NAME, header='Internal error') if not text: # fallback to hard-coded template text = TEMPLATE_500 self._send_head(text, 500) if not self._header_only: self.wfile.write(text)
def _send_database_problem(self): """ Send HTTP status code 500 due to a database connection error | **param** header_only (bool) """ template_filename = self._get_config_template("databaseerror") text = read_template(template_filename, title="%s - Datebase error" % SERVER_NAME, header="Database error") if not text: self._send_internal_server_error() return self._send_head(text, 500) if not self._header_only: self.wfile.write(text)
def _send_database_problem(self): """ Send HTTP status code 500 due to a database connection error | **param** header_only (bool) """ template_filename = self._get_config_template('databaseerror') text = read_template(template_filename, title='%s - Datebase error' % SERVER_NAME, header='Database error') if not text: self._send_internal_server_error() return self._send_head(text, 500) if not self._header_only: self.wfile.write(text)
def _send_return_page(self, shorthash): """ Send the result page for a new created short URL. | **param** shorthash - new shorthash for URL """ template_filename = self._get_config_template('return') if shorthash == '1337': messagetext = '<p>Hey, you are 1337!</p>' else: messagetext = '' text = read_template(template_filename, message=messagetext, title='%s - Short URL Result' % SERVER_NAME, header='new URL', path=shorthash, hostname=self.server.hostname) self._send_response(text, 200)
def _send_return_page(self, shorthash): """ Send the result page for a new created short URL. | **param** shorthash - new shorthash for URL """ template_filename = self._get_config_template("return") if shorthash == "1337": messagetext = "<p>Hey, you are 1337!</p>" else: messagetext = "" text = read_template( template_filename, message=messagetext, title="%s - Short URL Result" % SERVER_NAME, header="new URL", path=shorthash, hostname=self.server.hostname, ) self._send_response(text, 200)
def _show_link_stats(self, shorthash=None): """ Shows a page with some statistics for a short URL | **param** shorthash (string) """ # First doing some basis input validation as we don't want to # get f****d by the Jesus if shorthash == None or not shorthash.isalnum(): self._send_404() return else: blocked = self._db.is_hash_blocked(shorthash) if blocked: self._send_blocked_page(blocked[3]) return link_stats = YuLinkStats(shorthash) # Only proceed if there is a address behind the link, # else sending a 404 if link_stats.link_address: template_filename = self._get_config_template('statsLink') url = "/" + shorthash new_url = '<a href="%(url)s">%(result)s</a>' % \ {'result': link_stats.link_address, 'url': url} # FIXME: Check for None on timestamps and replace it with something like Unknown. text = read_template( template_filename, title='%s - Linkstats' % SERVER_NAME, header='Stats for Link', URL=new_url, CREATION_TIME=link_stats.creation_time, FIRST_REDIRECT=link_stats.first_redirect, LAST_REDIRECT=link_stats.last_redirect, NUMBER_OF_REDIRECTS=link_stats.number_of_redirects) self._send_response(text, 200) else: self._send_404() return
def _show_link_stats(self, shorthash=None): """ Shows a page with some statistics for a short URL | **param** shorthash (string) """ # First doing some basis input validation as we don't want to # get f****d by the Jesus if shorthash == None or not shorthash.isalnum(): self._send_404() return else: blocked = self._db.is_hash_blocked(shorthash) if blocked: self._send_blocked_page(blocked[3]) return link_stats = YuLinkStats(shorthash) # Only proceed if there is a address behind the link, # else sending a 404 if link_stats.link_address: template_filename = self._get_config_template("statsLink") url = "/" + shorthash new_url = '<a href="%(url)s">%(result)s</a>' % {"result": link_stats.link_address, "url": url} # FIXME: Check for None on timestamps and replace it with something like Unknown. text = read_template( template_filename, title="%s - Linkstats" % SERVER_NAME, header="Stats for Link", URL=new_url, CREATION_TIME=link_stats.creation_time, FIRST_REDIRECT=link_stats.first_redirect, LAST_REDIRECT=link_stats.last_redirect, NUMBER_OF_REDIRECTS=link_stats.number_of_redirects, ) self._send_response(text, 200) else: self._send_404() return
def _show_general_stats(self): """ Prints a page with some serice wide statistics. | **param** header_only (bool) """ stat = YuStats() template_filename = self._get_config_template('stats') text = read_template( template_filename, title=SERVER_NAME, header=SERVER_NAME, number_of_links=format_none(stat.links_all), number_of_redirects=format_none(stat.redirect_all), number_of_redirects_today=format_none(stat.redirect_today), number_of_redirects_this_week=format_none(stat.redirect_this_week), number_of_redirects_this_month=format_none( stat.redirect_this_month), number_of_redirects_this_year=format_none(stat.redirect_this_year), number_of_url_today=format_none(stat.links_today), number_of_url_this_week=format_none(stat.links_this_week), number_of_url_this_month=format_none(stat.links_this_month), number_of_url_this_year=format_none(stat.links_this_year), date_of_first_redirect=format_none(stat.date_of_first_redirect), ) if text: self._send_head(text, 200) if not self._header_only: try: self.wfile.write(text) except socket.error: # clients like to stop reading after they got a 404 pass else: self._send_internal_server_error()
def _handle_post_request(self): """ POST HTTP request entry point """ form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST'}) if self.path == '/URLRequest': # First we check, whether the formular has been filled by # something behaving like a bot if form.has_key('URL'): self._send_homepage( '<p class="warning">Please check your input</p>') return else: url = form['real_URL'].value if form.has_key( 'real_URL') else None tmp = self._insert_url_to_db(url) if tmp: try: blocked = self._db.is_hash_blocked(tmp) if tmp < 0: self._send_database_problem() return elif blocked: self._send_blocked_page(blocked[3]) return else: self._send_return_page(tmp) return except YuDatabaseError: self._send_database_problem() return else: # There was a general issue with URL self._send_homepage( '''<p class="warning">Please check your input.</p>''') return elif self.path == '/ContactUs': if form.has_key('URL'): # Here we might have a bot who likes to send the webmaster some spam # who most likely will be not amused about. template_filename = self._get_config_template( 'contactUsResult') text = read_template( template_filename, title='', header='Mail NOT sent', msg='There was an issue with your request. Are you a bot? ' '<a href="/ContactUs">Please try again</a>.') else: try: email = form['email'].value subj = form['subject'].value descr = form['request'].value if self._send_mail(subj, descr, email): template_filename = self._get_config_template( 'contactUsResult') text = read_template( template_filename, title='', header='Mail sent', msg= "Your request has been sent. You will receive an answer soon." ) else: self._send_internal_server_error() return except KeyError: template_filename = self._get_config_template( 'contactUsResult') text = read_template( template_filename, title='', header='Mail NOT sent', msg='It appers you did not fill out all needed fields.\ <a href="/ContactUs">Please try again</a>.') elif self.path == '/Show': short_url = form['ShortURL'].value if form.has_key( 'ShortURL') else None if short_url != None and short_url.find("yaturl.net") > -1: tmp = short_url.rfind("/") if tmp > -1 and short_url != "": tmp = tmp + 1 short_url = short_url[tmp:] if short_url != None and short_url.isalnum(): try: result = self._db.get_link_from_db(short_url) except YuDatabaseError: self._send_database_problem() return template_filename = self._get_config_template('showpage') if result: new_url = '<p><a href="%(result)s">%(result)s</a></p>' % \ {'result': result} else: new_url = '<p class="warning">No URL found for this string. Please double check your\ <a href="/ShowURL">input and try again</a></p>' stats = self._db.get_statistics_for_hash(short_url) text = read_template(template_filename, title=SERVER_NAME, header=SERVER_NAME, msg=new_url, stat=stats, statspage="/stats/" + short_url) else: self._send_404() return else: self._send_404() return self._send_response(text, 200)
def _handle_get_request(self): """ GET HTTP request entry point """ docroot = self._get_config_value('main', 'staticdocumentroot') local_path = sanitize_path(self.path) path = docroot + local_path try: # actually try deliver the requested file - First we try to send # every static content requested_file = open(path) text = requested_file.read() requested_file.close() except IOError: try: parsed_path = urlparse(self.path) params = dict( [p.split('=') for p in parsed_path[4].split('&')]) if params['addurl']: tmp = self._insert_url_to_db(params['addurl']) if tmp and tmp < 0: self._send_database_problem() return blocked = self._db.is_hash_blocked(tmp) if blocked: self._send_blocked_page(blocked[3]) return elif tmp: self._send_return_page(tmp) return else: # There was a general issue with URL self._send_homepage( '''<p class="warning">Please check your input.</p>''' ) return else: # There was a general issue with URL self._send_homepage( '''<p class="warning">Please check your input.</p>''') return except YuDatabaseError: self._send_database_problem() return except: if self.path in ('/', '/URLRequest'): self._send_homepage() return elif self.path.startswith('/stats') or self.path.endswith('+'): if self.path == '/stats': # Doing general statistics here # Let's hope this page is not getting to popular .... # Create a new stats objekt which is fetching data in background self._show_general_stats() return else: # Check whether we do have the + or the stats kind of URL if self.path.endswith('+'): # Well I guess this is the proof you can write # real ugly code in Python too. try: if self.path.startswith('/show/'): request_path = self.path[6:] elif self.path.startswith('/s/'): request_path = self.path[3:] elif self.path.startswith('/stats/'): request_path = self.path[7:] else: request_path = self.path[1:] self._show_link_stats( request_path[:request_path.rfind('+')]) return except Exception, e: # Oopps. Something went wrong. Most likely # a malformed link # TODO raise a (yet to be written) FileNotFoundException self._logger.error( u'An exception occurred: %s' % unicode(e), exc_info=True) self._send_404() return else: # Trying to understand for which link we shall print # out stats. splitted = self.path[1:].split('/') try: self._show_link_stats(splitted[1]) return except IndexError: # Something went wrong. Most likely there was a # malformed URL for accessing the stats. self._send_404() return # Any other page else: # First check, whether we want to have a real redirect # or just an info request_path = self.path if self.path.startswith('/show/'): request_path = self.path[5:] show = True elif self.path.startswith('/s/'): request_path = self.path[2:] show = True else: show = False # Assuming, if there is anything else than an # alphanumeric character after the starting /, it's # not a valid hash at all if request_path[1:].isalnum(): try: result = self._db.get_link_from_db( request_path[1:]) blocked = self._db.is_hash_blocked( request_path[1:]) except YuDatabaseError: self._send_database_problem() return if result and blocked == None: if show == True: template_filename = self._get_config_template( 'showpage') url = "/" + request_path[1:] new_url = '<p><a href="%(url)s">%(result)s</a></p>' % \ {'result': result, 'url': url} stats = self._db.get_statistics_for_hash( request_path[1:]) text = read_template(template_filename, title=SERVER_NAME, header=SERVER_NAME, msg=new_url, stat=stats, statspage="/stats/" + request_path[1:]) else: self._db.add_logentry_to_database( request_path[1:]) self._send_301(result) return elif blocked: self._send_blocked_page(blocked[3]) return else: self._send_404() return else: self._send_404() return
def _handle_post_request(self): """ POST HTTP request entry point """ form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={"REQUEST_METHOD": "POST"}) if self.path == "/URLRequest": # First we check, whether the formular has been filled by # something behaving like a bot if form.has_key("URL"): self._send_homepage('<p class="warning">Please check your input</p>') return else: url = form["real_URL"].value if form.has_key("real_URL") else None tmp = self._insert_url_to_db(url) if tmp: try: blocked = self._db.is_hash_blocked(tmp) if tmp < 0: self._send_database_problem() return elif blocked: self._send_blocked_page(blocked[3]) return else: self._send_return_page(tmp) return except YuDatabaseError: self._send_database_problem() return else: # There was a general issue with URL self._send_homepage("""<p class="warning">Please check your input.</p>""") return elif self.path == "/ContactUs": if form.has_key("URL"): # Here we might have a bot who likes to send the webmaster some spam # who most likely will be not amused about. template_filename = self._get_config_template("contactUsResult") text = read_template( template_filename, title="", header="Mail NOT sent", msg="There was an issue with your request. Are you a bot? " '<a href="/ContactUs">Please try again</a>.', ) else: try: email = form["email"].value subj = form["subject"].value descr = form["request"].value if self._send_mail(subj, descr, email): template_filename = self._get_config_template("contactUsResult") text = read_template( template_filename, title="", header="Mail sent", msg="Your request has been sent. You will receive an answer soon.", ) else: self._send_internal_server_error() return except KeyError: template_filename = self._get_config_template("contactUsResult") text = read_template( template_filename, title="", header="Mail NOT sent", msg='It appers you did not fill out all needed fields.\ <a href="/ContactUs">Please try again</a>.', ) elif self.path == "/Show": short_url = form["ShortURL"].value if form.has_key("ShortURL") else None if short_url != None and short_url.find("yaturl.net") > -1: tmp = short_url.rfind("/") if tmp > -1 and short_url != "": tmp = tmp + 1 short_url = short_url[tmp:] if short_url != None and short_url.isalnum(): try: result = self._db.get_link_from_db(short_url) except YuDatabaseError: self._send_database_problem() return template_filename = self._get_config_template("showpage") if result: new_url = '<p><a href="%(result)s">%(result)s</a></p>' % {"result": result} else: new_url = '<p class="warning">No URL found for this string. Please double check your\ <a href="/ShowURL">input and try again</a></p>' stats = self._db.get_statistics_for_hash(short_url) text = read_template( template_filename, title=SERVER_NAME, header=SERVER_NAME, msg=new_url, stat=stats, statspage="/stats/" + short_url, ) else: self._send_404() return else: self._send_404() return self._send_response(text, 200)
def _handle_get_request(self): """ GET HTTP request entry point """ docroot = self._get_config_value("main", "staticdocumentroot") local_path = sanitize_path(self.path) path = docroot + local_path try: # actually try deliver the requested file - First we try to send # every static content requested_file = open(path) text = requested_file.read() requested_file.close() except IOError: try: parsed_path = urlparse(self.path) params = dict([p.split("=") for p in parsed_path[4].split("&")]) if params["addurl"]: tmp = self._insert_url_to_db(params["addurl"]) if tmp and tmp < 0: self._send_database_problem() return blocked = self._db.is_hash_blocked(tmp) if blocked: self._send_blocked_page(blocked[3]) return elif tmp: self._send_return_page(tmp) return else: # There was a general issue with URL self._send_homepage("""<p class="warning">Please check your input.</p>""") return else: # There was a general issue with URL self._send_homepage("""<p class="warning">Please check your input.</p>""") return except YuDatabaseError: self._send_database_problem() return except: if self.path in ("/", "/URLRequest"): self._send_homepage() return elif self.path.startswith("/stats") or self.path.endswith("+"): if self.path == "/stats": # Doing general statistics here # Let's hope this page is not getting to popular .... # Create a new stats objekt which is fetching data in background self._show_general_stats() return else: # Check whether we do have the + or the stats kind of URL if self.path.endswith("+"): # Well I guess this is the proof you can write # real ugly code in Python too. try: if self.path.startswith("/show/"): request_path = self.path[6:] elif self.path.startswith("/s/"): request_path = self.path[3:] elif self.path.startswith("/stats/"): request_path = self.path[7:] else: request_path = self.path[1:] self._show_link_stats(request_path[: request_path.rfind("+")]) return except Exception, e: # Oopps. Something went wrong. Most likely # a malformed link # TODO raise a (yet to be written) FileNotFoundException self._logger.error(u"An exception occurred: %s" % unicode(e), exc_info=True) self._send_404() return else: # Trying to understand for which link we shall print # out stats. splitted = self.path[1:].split("/") try: self._show_link_stats(splitted[1]) return except IndexError: # Something went wrong. Most likely there was a # malformed URL for accessing the stats. self._send_404() return # Any other page else: # First check, whether we want to have a real redirect # or just an info request_path = self.path if self.path.startswith("/show/"): request_path = self.path[5:] show = True elif self.path.startswith("/s/"): request_path = self.path[2:] show = True else: show = False # Assuming, if there is anything else than an # alphanumeric character after the starting /, it's # not a valid hash at all if request_path[1:].isalnum(): try: result = self._db.get_link_from_db(request_path[1:]) blocked = self._db.is_hash_blocked(request_path[1:]) except YuDatabaseError: self._send_database_problem() return if result and blocked == None: if show == True: template_filename = self._get_config_template("showpage") url = "/" + request_path[1:] new_url = '<p><a href="%(url)s">%(result)s</a></p>' % {"result": result, "url": url} stats = self._db.get_statistics_for_hash(request_path[1:]) text = read_template( template_filename, title=SERVER_NAME, header=SERVER_NAME, msg=new_url, stat=stats, statspage="/stats/" + request_path[1:], ) else: self._db.add_logentry_to_database(request_path[1:]) self._send_301(result) return elif blocked: self._send_blocked_page(blocked[3]) return else: self._send_404() return else: self._send_404() return