Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pase Error when Unicode in Response #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions handler.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#------------------------------------------------------------

class BaseHandler(tornado.web.RequestHandler):

def get_current_user(self):
return []

Expand All @@ -38,7 +38,7 @@ def get_current_user(self):
class MainHandler(BaseHandler):
def initialize(self):
return

def get(self):
self.render(
'templates/index.html',
Expand All @@ -51,13 +51,13 @@ def get(self):
class ViewAboutHandler(BaseHandler):
def initialize(self):
return

def get(self):
self.render(
'templates/about.html',
)


#------------------------------------------------------------
# /parse/ajax
#------------------------------------------------------------
Expand All @@ -76,21 +76,21 @@ def find_str(self, s, char):
return index
index += 1
return -1

def findEntireLine(self, contents, str):
lineNum = 0
for item in contents.split("\n"):
if str in item:
linkPos = self.find_str(item, str)
return item,lineNum,linkPos
lineNum = lineNum+1

def parseForLinks(self, contents):
discoveredLinks = []
outputLinks = []
# ugh lol
regex = "(`|'|\")([\/]([_a-zA-Z0-9\-\_]+))+"
links = re.finditer(regex, contents)
links = re.finditer(regex, contents)
for link in links:
linkStr = link.group(0)
# discoveredLinks list to avoid dupes and complex dupe checks
Expand All @@ -114,25 +114,25 @@ def getFormattedTimestamp(self):
def formatHTMLOutput(self, html):
output = output + html
return output

def beautifyJS(self, content):
return jsbeautifier.beautify(content)

def isLongLine(self, line):
if len(line)>1000:
return True
return False

def fileRoutine(self, url, content):
html = ""

# beautify the JS for cleaner parsing
# note: this can be slow against large JS files and can lead to failure
prettyContent = self.beautifyJS(content)

# parse all the links out
parsedLinks = self.parseForLinks(prettyContent)

# if we have results, start building HTML
if parsedLinks:
print "Discovered {} links in {}".format(len(parsedLinks), url)
Expand All @@ -153,43 +153,44 @@ def fileRoutine(self, url, content):
html = html+'<div class="link">{}: {}</div>'.format(link["lineNum"], highlightedLine)
html = html+'</div>'
return html

def fetchURL(self, url):
sc = safeurl.SafeURL()
res = sc.execute(url)
return res

def parseLinks(self, url):
html = ""
file = self.fetchURL(url)
html = html + self.fileRoutine(url, file)
return html

def get(self):

error = False
errorMsg = ""

url = self.get_argument("url")

if error == False:

data = self.parseLinks(url)
data = unicode(data,errors='ignore')

# set content-type
self.set_header('Content-Type', 'application/json')

# output
self.write(json_encode({
"url": url,
"output": data,
}))

else:

self.write("error")


#------------------------------------------------------------
# Main
#------------------------------------------------------------
Expand Down