Commit 3afb367149c91fd56df47d066ab21c2963f62ccc

Authored by Georg Hopp
1 parent 5977b250

handle binary data

@@ -3,12 +3,16 @@ @@ -3,12 +3,16 @@
3 import time 3 import time
4 import random 4 import random
5 import mmap 5 import mmap
6 -import sys, getopt  
7 -from struct import pack  
8 -from collections import deque  
9 6
10 from os.path import dirname, realpath 7 from os.path import dirname, realpath
  8 +import sys, getopt
  9 +reload(sys)
  10 +from sys import path, setdefaultencoding
11 path.append(dirname(realpath(__file__)) + '/lib') 11 path.append(dirname(realpath(__file__)) + '/lib')
  12 +setdefaultencoding('utf-8')
  13 +
  14 +from struct import pack
  15 +from collections import deque
12 16
13 from Server import Server 17 from Server import Server
14 18
@@ -136,7 +140,7 @@ def main(): @@ -136,7 +140,7 @@ def main():
136 140
137 server = Server( 141 server = Server(
138 Application( 142 Application(
139 - args[0], int(args[1], hosturi, binddn, basedn, password)) 143 + args[0], int(args[1]), hosturi, binddn, basedn, password))
140 server.bindTcp(args[0], int(args[1]), Http()) 144 server.bindTcp(args[0], int(args[1]), Http())
141 server.start(1.0) 145 server.start(1.0)
142 146
@@ -7,9 +7,9 @@ from struct import pack @@ -7,9 +7,9 @@ from struct import pack
7 from collections import deque 7 from collections import deque
8 8
9 from os.path import dirname, realpath 9 from os.path import dirname, realpath
10 -import sys 10 +import sys, getopt
11 reload(sys) 11 reload(sys)
12 -from sys import argv, path, setdefaultencoding 12 +from sys import path, setdefaultencoding
13 path.append(dirname(realpath(__file__)) + '/lib') 13 path.append(dirname(realpath(__file__)) + '/lib')
14 setdefaultencoding('utf-8') 14 setdefaultencoding('utf-8')
15 import re 15 import re
@@ -44,7 +44,12 @@ class Application(EventHandler): @@ -44,7 +44,12 @@ class Application(EventHandler):
44 44
45 @property 45 @property
46 def _body(self): 46 def _body(self):
47 - return self._template.render(ldaptree=self._ldaptree).encode('utf8') 47 + try:
  48 + return self._template.render(ldaptree=self._ldaptree).encode('utf8')
  49 + except UnicodeDecodeError as e:
  50 + print e.object
  51 + raise TypeError('failed')
  52 +
48 53
49 def _handle_data(self, event): 54 def _handle_data(self, event):
50 protocol = event.subject.getProtocol() 55 protocol = event.subject.getProtocol()
@@ -114,7 +119,7 @@ def main(): @@ -114,7 +119,7 @@ def main():
114 119
115 server = Server( 120 server = Server(
116 Application( 121 Application(
117 - args[0], int(args[1], hosturi, binddn, basedn, password)) 122 + args[0], int(args[1]), hosturi, binddn, basedn, password))
118 server.bindTcp(args[0], int(args[1]), Http()) 123 server.bindTcp(args[0], int(args[1]), Http())
119 server.start(1.0) 124 server.start(1.0)
120 125
@@ -73,16 +73,20 @@ def main(): @@ -73,16 +73,20 @@ def main():
73 73
74 info = LdapTree(hosturi, binddn, basedn, password, use_gssapi) 74 info = LdapTree(hosturi, binddn, basedn, password, use_gssapi)
75 75
76 - if not creategraph:  
77 - if outfile:  
78 - info.text(outfile)  
79 - else:  
80 - print info.text()  
81 - else:  
82 - if outfile:  
83 - info.graph(outfile) 76 + try:
  77 + if not creategraph:
  78 + if outfile:
  79 + info.text(outfile)
  80 + else:
  81 + print info.text()
84 else: 82 else:
85 - print info.graph() 83 + if outfile:
  84 + info.graph(outfile)
  85 + else:
  86 + print info.graph()
  87 + except UnicodeDecodeError as e:
  88 + print e.object
  89 + raise TypeError('failed')
86 90
87 if __name__ == "__main__": 91 if __name__ == "__main__":
88 main() 92 main()
@@ -92,13 +92,36 @@ class LdapTree(object): @@ -92,13 +92,36 @@ class LdapTree(object):
92 92
93 return thislen 93 return thislen
94 94
  95 + def _encode(self, data):
  96 + if type(data) is str:
  97 + try:
  98 + unicode(data, 'utf-8')
  99 + except UnicodeDecodeError:
  100 + data = data.encode('base64')
  101 + return data
  102 +
95 @property 103 @property
96 def all(self): 104 def all(self):
97 if self._data == None: 105 if self._data == None:
98 self._data = {} 106 self._data = {}
99 result = self._ldap.search_s(self._basedn, ldap.SCOPE_SUBTREE) 107 result = self._ldap.search_s(self._basedn, ldap.SCOPE_SUBTREE)
100 for entry in result: 108 for entry in result:
101 - self._data[entry[0]] = entry[1:][0] 109 + if entry[1] is None:
  110 + self._data[entry[0]] = None
  111 + elif type(entry[1]) is str:
  112 + self._data[entry[0]] = self._encode(entry[1])
  113 + elif type(entry[1]) is list:
  114 + self._data[entry[0]] = [self._encode(v) for v in entry[1]]
  115 + elif type(entry[1]) is dict:
  116 + self._data[entry[0]] = {}
  117 + for k in entry[1].keys():
  118 + if type(entry[1][k]) is str:
  119 + self._data[entry[0]][k] = self._encode(entry[1])
  120 + else:
  121 + self._data[entry[0]][k] = [
  122 + self._encode(v) for v in entry[1][k]]
  123 + else:
  124 + raise TypeError("unsupported ldap type")
102 125
103 return self._data 126 return self._data
104 127
@@ -107,12 +130,13 @@ class LdapTree(object): @@ -107,12 +130,13 @@ class LdapTree(object):
107 retval = {} 130 retval = {}
108 for d in self.all.keys(): 131 for d in self.all.keys():
109 current = retval 132 current = retval
110 - for k in reversed(d.split(',')):  
111 - try:  
112 - current = current[k]  
113 - except:  
114 - current[k] = {}  
115 - current = current[k] 133 + if d:
  134 + for k in reversed(d.split(',')):
  135 + try:
  136 + current = current[k]
  137 + except:
  138 + current[k] = {}
  139 + current = current[k]
116 return retval 140 return retval
117 141
118 @property 142 @property
@@ -72,6 +72,9 @@ @@ -72,6 +72,9 @@
72 onclick="toggle(this, 'childs')">dn: {{ d[2]|e }}</span> 72 onclick="toggle(this, 'childs')">dn: {{ d[2]|e }}</span>
73 <button onclick="toggle(this, 'attributes')">[Attributes]</button> 73 <button onclick="toggle(this, 'attributes')">[Attributes]</button>
74 <ul class="attributes"> 74 <ul class="attributes">
  75 + {% if ldaptree.node(d[2]) is string -%}
  76 + <li>{{ ldaptree.node(d[2])|e }}</li>
  77 + {% else -%}
75 {% for k in ldaptree.node(d[2]).keys() -%} 78 {% for k in ldaptree.node(d[2]).keys() -%}
76 {% if ldaptree.node(d[2]) is string -%} 79 {% if ldaptree.node(d[2]) is string -%}
77 <li>{{ k }}: {{ ldaptree.node(d[2])[k]|e }}</li> 80 <li>{{ k }}: {{ ldaptree.node(d[2])[k]|e }}</li>
@@ -81,6 +84,7 @@ @@ -81,6 +84,7 @@
81 {% endfor -%} 84 {% endfor -%}
82 {% endif -%} 85 {% endif -%}
83 {% endfor -%} 86 {% endfor -%}
  87 + {% endif -%}
84 </ul> 88 </ul>
85 {% endfor -%} 89 {% endfor -%}
86 </ul> 90 </ul>
Please register or login to post a comment