Improve websocket logging

This commit is contained in:
Aldo Cortesi 2015-06-05 12:04:40 +12:00
parent 0a7da6a9b1
commit d23691f98c
5 changed files with 40 additions and 32 deletions

View File

@ -2,7 +2,7 @@ import time
import pyparsing as pp import pyparsing as pp
from . import base, http, websockets, writer, exceptions from . import http, websockets, writer, exceptions
from exceptions import * from exceptions import *
from base import Settings from base import Settings

View File

@ -93,6 +93,7 @@ COMPONENTS = (
class WebsocketFrame(message.Message): class WebsocketFrame(message.Message):
components = COMPONENTS components = COMPONENTS
logattrs = ["body"] logattrs = ["body"]
# Used for nested frames
unique_name = "body" unique_name = "body"
@property @property

View File

@ -347,8 +347,8 @@ class Pathoc(tcp.TCPClient):
""" """
Performs a single request. Performs a single request.
r: A language.http.Request object, or a string representing one r: A language.message.Messsage object, or a string representing
request. one.
Returns Response if we have a non-ignored response. Returns Response if we have a non-ignored response.

View File

@ -5,6 +5,7 @@ import sys
import threading import threading
import urllib import urllib
import re import re
import time
from netlib import tcp, http, wsgi, certutils, websockets from netlib import tcp, http, wsgi, certutils, websockets
@ -106,42 +107,52 @@ class PathodHandler(tcp.BaseHandler):
lw = self.wfile if self.server.logresp else None lw = self.wfile if self.server.logresp else None
while True: while True:
with log.Log(self.logfp, self.server.hexdump, lr, lw) as lg: with log.Log(self.logfp, self.server.hexdump, lr, lw) as lg:
started = time.time()
try: try:
frm = websockets.Frame.from_file(self.rfile) frm = websockets.Frame.from_file(self.rfile)
except tcp.NetLibIncomplete, e: except tcp.NetLibIncomplete, e:
lg("Error reading websocket frame: %s"%e) lg("Error reading websocket frame: %s"%e)
break break
ended = time.time()
lg(frm.human_readable()) lg(frm.human_readable())
retlog = dict( retlog = dict(
type="wsframe", type = "inbound",
frame=dict( protocol = "websockets",
), started = started,
cipher=None, duration = ended - started,
) frame = dict(
ld = language.websockets.NESTED_LEADER ),
if frm.payload.startswith(ld): cipher=None,
nest = frm.payload[len(ld):] )
try: if self.ssl_established:
wf = language.parse_websocket_frame(nest) retlog["cipher"] = self.get_current_cipher()
except language.exceptions.ParseException, v: self.addlog(retlog)
lg( ld = language.websockets.NESTED_LEADER
"Parse error in reflected frame specifcation:" if frm.payload.startswith(ld):
" %s" % v.msg nest = frm.payload[len(ld):]
) try:
break wf = language.parse_websocket_frame(nest)
except language.exceptions.ParseException, v:
lg(
"Parse error in reflected frame specifcation:"
" %s" % v.msg
)
break
with log.Log(self.logfp, self.server.hexdump, lr, lw) as lg:
frame_log = language.serve( frame_log = language.serve(
wf, wf,
self.wfile, self.wfile,
self.settings self.settings
) )
self.addlog(retlog) lg("crafting websocket spec: %s" % frame_log["spec"])
self.addlog(frame_log)
return self.handle_websocket, None return self.handle_websocket, None
def handle_http_connect(self, connect, lg): def handle_http_connect(self, connect, lg):
""" """
Handle a CONNECT request. Handle a CONNECT request.
""" """
headers = http.read_headers(self.rfile) http.read_headers(self.rfile)
self.wfile.write( self.wfile.write(
'HTTP/1.1 200 Connection established\r\n' + 'HTTP/1.1 200 Connection established\r\n' +
('Proxy-agent: %s\r\n' % version.NAMEVERSION) + ('Proxy-agent: %s\r\n' % version.NAMEVERSION) +
@ -237,6 +248,7 @@ class PathodHandler(tcp.BaseHandler):
retlog = dict( retlog = dict(
type="crafted", type="crafted",
protocol="http",
request=dict( request=dict(
path=path, path=path,
method=method, method=method,

View File

@ -1,8 +1,7 @@
import cStringIO import cStringIO
from libpathod import pathod, version from libpathod import pathod, version
from netlib import tcp, http from netlib import tcp, http
import time import pprint
import sys
import tutils import tutils
@ -211,14 +210,10 @@ class CommonTests(tutils.DaemonTests):
assert r.status_code == 101 assert r.status_code == 101
def test_websocket_frame(self): def test_websocket_frame(self):
r = self.pathoc(["ws:/p/", "wf:b@10"], ws_read_limit=0) r = self.pathoc(["ws:/p/", "wf:f'wf'"], ws_read_limit=1)
print >> sys.stderr, r #print r
print >> sys.stderr, self.d.log() #pprint.pprint(r)
assert self.d.last_log()["type"] == "wsframe" #pprint.pprint(self.d.log())
def test_websocket_reflected_frame(self):
r = self.pathoc(["ws:/p/", "wf:f'wf'"], ws_read_limit=0)
assert r
class TestDaemon(CommonTests): class TestDaemon(CommonTests):