-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathserver.py
More file actions
172 lines (147 loc) · 6.01 KB
/
server.py
File metadata and controls
172 lines (147 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import tornado.ioloop
import tornado.web
import os
import json
import sys
import rtxcomplete
import traceback
import re
import setproctitle
root = os.path.dirname(os.path.abspath(__file__))
rtxcomplete.load()
SERVER_TCP_PORT = 4999
#### Sanitize the client-provided callback function name
def sanitize_callback(callback):
if callback is None or not isinstance(callback,str):
return 'autocomplete_callback'
match = re.match(r'([a-zA-Z0-9_]+).*$', callback)
if match:
callback = match.group(1)
else:
callback = 'autocomplete_callback'
return callback
class autoSearch(tornado.web.RequestHandler):
def get(self, arg,word=None):
try:
limit = self.get_argument("limit")
word = self.get_argument("word")
callback = sanitize_callback(self.get_argument("callback"))
result = rtxcomplete.prefix(word,limit)
result = callback+"("+json.dumps(result)+");"
self.write(result)
except:
print(sys.exc_info()[:])
traceback.print_tb(sys.exc_info()[-1])
#print sys.exc_info()[2]
self.write("error")
class fuzzySearch(tornado.web.RequestHandler):
def get(self, arg,word=None):
#print "matched fuzzy"
try:
limit = self.get_argument("limit")
word = self.get_argument("word")
callback = sanitize_callback(self.get_argument("callback"))
#print word
#cursor.execute("SELECT word FROM spell WHERE word MATCH \"" + word + "\" LIMIT " + limit)
#cursor.execute("SELECT word FROM spell WHERE word MATCH \"" + word + "*\" LIMIT " + limit)
result = rtxcomplete.fuzzy(word,limit);
#rows = cursor.fetchall()
#print type(rows)
result = callback+"("+json.dumps(result)+");"
#print arg, result
#if (len(rows) > 0):
self.write(result)
#else:
#self.write(callback+"("+json.dumps([["NO SUGGESTIONS"]])+");")
#self.write(json.dumps(rows))
except:
print(sys.exc_info()[:])
traceback.print_tb(sys.exc_info()[-1])
#print sys.exc_info()[:]
self.write("error")
class autofuzzySearch(tornado.web.RequestHandler):
def get(self, arg,word=None):
#print "matched autofuzzy"
try:
limit = self.get_argument("limit")
word = self.get_argument("word")
callback = sanitize_callback(self.get_argument("callback"))
#print word
#cursor.execute("SELECT word FROM spell WHERE word MATCH \"" + word + "\" LIMIT " + limit)
#cursor.execute("SELECT word FROM spell WHERE word MATCH \"" + word + "*\" LIMIT " + limit)
result = rtxcomplete.autofuzzy(word,limit);
#rows = cursor.fetchall()
#print type(rows)
result = callback+"("+json.dumps(result)+");"
#print arg, result
#if (len(rows) > 0):
self.write(result)
#else:
#self.write(callback+"("+json.dumps([["NO SUGGESTIONS"]])+");")
#self.write(json.dumps(rows))
except:
print(sys.exc_info()[:])
traceback.print_tb(sys.exc_info()[-1])
#print sys.exc_info()[:]
self.write("error")
class nodesLikeSearch(tornado.web.RequestHandler):
def get(self, arg,word=None):
try:
limit = self.get_argument("limit")
word = self.get_argument("word")
callback = sanitize_callback(self.get_argument("callback"))
result = rtxcomplete.get_nodes_like(word,limit);
result = callback+"("+json.dumps(result)+");"
self.write(result)
except:
print(sys.exc_info()[:])
traceback.print_tb(sys.exc_info()[-1])
self.write("error")
class defineSearch(tornado.web.RequestHandler):
def get(self, arg,word=None):
print("matched define search: not implemented")
self.write("")
def make_https_app():
return tornado.web.Application([
#(r"/", MainHandler),
(r"/autofuzzy(.*)", autofuzzySearch),
(r"/auto(.*)", autoSearch),
(r"/fuzzy(.*)", fuzzySearch),
(r"/define(.*)", defineSearch),
(r"/nodeslike(.*)", nodesLikeSearch),
(r"/(.*)", tornado.web.StaticFileHandler,
{"path": root, "default_filename": "rtxcomplete.html"}),
],
compress_response= True)
class redirect_handler(tornado.web.RequestHandler):
def prepare(self):
if self.request.protocol == 'http':
if self.request.host == "rtxcomplete.ixlab.org":
self.redirect('https://'+self.request.host, permanent=False)
def get(self):
self.write("Looks like you're trying to access rtxcomplete at the wrong host name.")
self.write("<br>Please make sure the address is correct: 'rtxcomplete.ixlab.org'")
def make_redirect_app():
return tornado.web.Application([
(r'/', redirect_handler)
])
if __name__ == "__main__":
print("root: " + root)
proc_title = setproctitle.getproctitle()
setproctitle.setproctitle(proc_title.replace('server.py',
"autocomplete/server.py" +
f" [port={SERVER_TCP_PORT}]"))
if True: #FW/EWD: clean this up later
http_app = make_https_app()
http_server = tornado.httpserver.HTTPServer(http_app)
http_server.listen(SERVER_TCP_PORT)
else:
redirect_app = make_redirect_app()
redirect_app.listen(80)
https_app = make_https_app()
https_server = tornado.httpserver.HTTPServer(https_app, ssl_options={
"certfile": "/etc/letsencrypt/live/rtxcomplete.ixlab.org/fullchain.pem",
"keyfile" : "/etc/letsencrypt/live/rtxcomplete.ixlab.org/privkey.pem",
})
https_server.listen(443)
tornado.ioloop.IOLoop.current().start()