2014-09-17 19:14:55 +00:00
|
|
|
from __future__ import absolute_import, print_function
|
2014-09-13 23:30:00 +00:00
|
|
|
import tornado.ioloop
|
2014-09-13 23:46:01 +00:00
|
|
|
import tornado.httpserver
|
2014-09-17 19:14:55 +00:00
|
|
|
from .. import controller, flow
|
|
|
|
from . import app
|
2014-09-13 23:30:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Stop(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class WebState(flow.State):
|
|
|
|
def __init__(self):
|
|
|
|
flow.State.__init__(self)
|
|
|
|
|
|
|
|
|
|
|
|
class Options(object):
|
|
|
|
attributes = [
|
|
|
|
"app",
|
|
|
|
"app_domain",
|
|
|
|
"app_ip",
|
|
|
|
"anticache",
|
|
|
|
"anticomp",
|
|
|
|
"client_replay",
|
|
|
|
"eventlog",
|
|
|
|
"keepserving",
|
|
|
|
"kill",
|
|
|
|
"intercept",
|
|
|
|
"no_server",
|
|
|
|
"refresh_server_playback",
|
|
|
|
"rfile",
|
|
|
|
"scripts",
|
|
|
|
"showhost",
|
|
|
|
"replacements",
|
|
|
|
"rheaders",
|
|
|
|
"setheaders",
|
|
|
|
"server_replay",
|
|
|
|
"stickycookie",
|
|
|
|
"stickyauth",
|
|
|
|
"stream_large_bodies",
|
|
|
|
"verbosity",
|
|
|
|
"wfile",
|
|
|
|
"nopop",
|
2014-09-14 00:22:28 +00:00
|
|
|
|
|
|
|
"wdebug",
|
|
|
|
"wport",
|
|
|
|
"wiface",
|
2014-09-13 23:30:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
for i in self.attributes:
|
|
|
|
if not hasattr(self, i):
|
|
|
|
setattr(self, i, None)
|
|
|
|
|
|
|
|
|
|
|
|
class WebMaster(flow.FlowMaster):
|
|
|
|
def __init__(self, server, options):
|
2014-09-14 00:22:28 +00:00
|
|
|
self.options = options
|
2014-09-16 21:40:25 +00:00
|
|
|
self.app = app.Application(self.options.wdebug)
|
2014-09-17 19:14:55 +00:00
|
|
|
super(WebMaster, self).__init__(server, WebState())
|
2014-09-13 23:30:00 +00:00
|
|
|
|
2014-09-17 15:30:19 +00:00
|
|
|
self.last_log_id = 0
|
|
|
|
|
2014-09-13 23:30:00 +00:00
|
|
|
def tick(self):
|
|
|
|
flow.FlowMaster.tick(self, self.masterq, timeout=0)
|
|
|
|
|
|
|
|
def run(self): # pragma: no cover
|
|
|
|
self.server.start_slave(
|
|
|
|
controller.Slave,
|
|
|
|
controller.Channel(self.masterq, self.should_exit)
|
|
|
|
)
|
|
|
|
iol = tornado.ioloop.IOLoop.instance()
|
2014-09-14 00:22:28 +00:00
|
|
|
|
2014-09-16 21:40:25 +00:00
|
|
|
http_server = tornado.httpserver.HTTPServer(self.app)
|
2014-09-14 00:22:28 +00:00
|
|
|
http_server.listen(self.options.wport)
|
|
|
|
|
2014-09-13 23:30:00 +00:00
|
|
|
tornado.ioloop.PeriodicCallback(self.tick, 5).start()
|
|
|
|
try:
|
|
|
|
iol.start()
|
|
|
|
except (Stop, KeyboardInterrupt):
|
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def handle_request(self, f):
|
2014-09-17 15:30:19 +00:00
|
|
|
app.ClientConnection.broadcast("add_flow", f.get_state(True))
|
2014-09-13 23:30:00 +00:00
|
|
|
flow.FlowMaster.handle_request(self, f)
|
|
|
|
if f:
|
|
|
|
f.reply()
|
|
|
|
return f
|
|
|
|
|
|
|
|
def handle_response(self, f):
|
2014-09-17 15:30:19 +00:00
|
|
|
app.ClientConnection.broadcast("update_flow", f.get_state(True))
|
2014-09-13 23:30:00 +00:00
|
|
|
flow.FlowMaster.handle_response(self, f)
|
|
|
|
if f:
|
|
|
|
f.reply()
|
|
|
|
return f
|
|
|
|
|
|
|
|
def handle_error(self, f):
|
2014-09-17 15:30:19 +00:00
|
|
|
app.ClientConnection.broadcast("update_flow", f.get_state(True))
|
2014-09-13 23:30:00 +00:00
|
|
|
flow.FlowMaster.handle_error(self, f)
|
|
|
|
return f
|
2014-09-17 01:58:56 +00:00
|
|
|
|
|
|
|
def handle_log(self, l):
|
2014-09-17 15:30:19 +00:00
|
|
|
self.last_log_id += 1
|
2014-09-17 01:58:56 +00:00
|
|
|
app.ClientConnection.broadcast(
|
2014-09-17 13:22:42 +00:00
|
|
|
"add_event", {
|
2014-09-17 15:30:19 +00:00
|
|
|
"id": self.last_log_id,
|
2014-09-17 01:58:56 +00:00
|
|
|
"message": l.msg,
|
|
|
|
"level": l.level
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.add_event(l.msg, l.level)
|
|
|
|
l.reply()
|
|
|
|
|