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

Some handy features (non-UTF8 charset decoding, webirc header support) #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions daemon/orbited2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def __init__(self,
hostname=None,
port=None,
host_header=None,
webirc=None,
ipv6entry=None,
protocol='ws/hixie76'
):
ConfigBase.__init__(self)
Expand Down Expand Up @@ -104,3 +106,5 @@ def __init__(self,
self.port = port
self.host_header = host_header
self.protocol = protocol
self.webirc = webirc
self.ipv6entry = ipv6entry
20 changes: 17 additions & 3 deletions daemon/orbited2/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def ensure_allowed_and_get_protocol_class(rules, conn):
for rule in rules:
if rule.hostname == conn.hostname and rule.port == conn.port:
if (rule.host_header == '*' or rule.host_header == conn.host_header):
conn.myRule = rule
if rule.protocol == 'ws/hixie75':
return WebSocket75Protocol
if rule.protocol == 'ws/hixie76':
Expand Down Expand Up @@ -176,7 +177,10 @@ def __init__(self, browser_conn, handshake, environ, rules):
self.port = handshake['port']
self.path = handshake.get('path', '/')
self.host_header = environ.get('HTTP_HOST', '')
self.origin = environ.get('HTTP_ORIGIN', '')
if environ.get('HTTP_X_FORWARDED_FOR', ''):
self.origin = environ.get('HTTP_X_FORWARDED_FOR', '')
else:
self.origin = environ.get('HTTP_ORIGIN', '')

self._msgs = collections.deque()
self._sendlock = eventlet.semaphore.Semaphore()
Expand All @@ -190,7 +194,11 @@ def connect(self):
self.sock = eventlet.connect((self.hostname, self.port))
self.closed = False
self.proto.handshake(self.sock)

v4origin = self.origin.split(":")[-1]
if self.myRule.webirc:
self.send("WEBIRC %s cgiirc %s %s\n" % (self.myRule.webirc, v4origin, v4origin))
if self.myRule.ipv6entry:
self.send(self.myRule.ipv6entry % self.origin)
eventlet.spawn(self._run)


Expand All @@ -199,6 +207,12 @@ def _run(self):
msg = self.wait()
if msg is None:
break

# CP1252 incompatibility handling: Convert input to unicode.
try:
msg = unicode(msg, encoding='utf-8', errors='strict')
except UnicodeDecodeError:
msg = unicode(msg, encoding='cp1252', errors='replace')
# print 'RECV<-Server', repr(msg)
self._browser_conn.send_frame(msg)
self._browser_conn.close()
Expand Down Expand Up @@ -404,4 +418,4 @@ def handshake(self, sock):
raise Exception("Invalid server handshake (wrong 16 byte security key)")
self._buf = buf[16:]
def close(self, sock):
sock.sendall(self.pack_message(""))
sock.sendall(self.pack_message(""))