Commit | Line | Data |
---|---|---|
c06db49a FT |
1 | class protoerr(Exception): |
2 | pass | |
3 | ||
4 | def readns(sk): | |
5 | hln = 0 | |
6 | while True: | |
7 | c = sk.read(1) | |
173e0e9e | 8 | if c == ':': |
c06db49a | 9 | break |
173e0e9e FT |
10 | elif c >= '0' or c <= '9': |
11 | hln = (hln * 10) + (ord(c) - ord('0')) | |
c06db49a | 12 | else: |
173e0e9e | 13 | raise protoerr, "Invalid netstring length byte: " + c |
c06db49a | 14 | ret = sk.read(hln) |
173e0e9e FT |
15 | if sk.read(1) != ',': |
16 | raise protoerr, "Non-terminated netstring" | |
c06db49a FT |
17 | return ret |
18 | ||
19 | def readhead(sk): | |
173e0e9e | 20 | parts = readns(sk).split('\0')[:-1] |
c06db49a | 21 | if len(parts) % 2 != 0: |
173e0e9e | 22 | raise protoerr, "Malformed headers" |
c06db49a FT |
23 | ret = {} |
24 | i = 0 | |
25 | while i < len(parts): | |
26 | ret[parts[i]] = parts[i + 1] | |
27 | i += 2 | |
28 | return ret |