| 7 | search = compile(r'([^\\/]+)$').search |
| 8 | |
| 9 | def open(): |
| 10 | global next, last |
| 11 | try: |
| 12 | next = '--' + parse_header(env['CONTENT_TYPE'])[1]['boundary'] |
| 13 | except: |
| 14 | raise IOError |
| 15 | |
| 16 | last = next + '--' |
| 17 | |
| 18 | filename = None |
| 19 | while not filename: |
| 20 | while True: |
| 21 | l = stdin.readline(65536) |
| 22 | if not l: |
| 23 | raise IOError |
| 24 | |
| 25 | if l[:2] == '--': |
| 26 | sl = l.strip() |
| 27 | if sl == next: |
| 28 | break |
| 29 | if sl == last: |
| 30 | raise IOError |
| 31 | |
| 32 | for i in xrange(10): |
| 33 | l = stdin.readline(65536).strip() |
| 34 | if not l: |
| 35 | break |
| 36 | try: |
| 37 | filename = parse_header(l.split(':', 1)[1])[1]['filename'] |
| 38 | break |
| 39 | except: |
| 40 | continue |
| 41 | |
| 42 | m = search(filename) |
| 43 | if not m: |
| 44 | raise IOError |
| 45 | filename = m.groups()[0] |
| 46 | |
| 47 | while stdin.readline(65536).strip(): |
| 48 | pass |
| 49 | |
| 50 | def _readline(): |
| 51 | el = '' |
| 52 | while True: |
| 53 | l = stdin.readline(65536) |
| 54 | if not l: |
| 55 | raise IOError |
| 56 | |
| 57 | if l[:2] == '--' and el: |
| 58 | sl = l.strip() |
| 59 | if sl == next or sl == last: |
| 60 | break |
| 61 | |
| 62 | ol, el = el, l[-2:] == '\r\n' and '\r\n' or ( |
| 63 | l[-1] == '\n' and '\n' or '') |
| 64 | |
| 65 | p = len(el) |
| 66 | if p == 0: |