68 '''need to read all data from request, httplib is half-duplex''' |
68 '''need to read all data from request, httplib is half-duplex''' |
69 length = int(self.env.get('CONTENT_LENGTH') or 0) |
69 length = int(self.env.get('CONTENT_LENGTH') or 0) |
70 for s in util.filechunkiter(self.inp, limit=length): |
70 for s in util.filechunkiter(self.inp, limit=length): |
71 pass |
71 pass |
72 |
72 |
73 def respond(self, status, type, filename=None, length=None): |
73 def respond(self, status, type, filename=None, body=None): |
74 if self._start_response is not None: |
74 if self._start_response is not None: |
75 self.headers.append(('Content-Type', type)) |
75 self.headers.append(('Content-Type', type)) |
76 if filename: |
76 if filename: |
77 filename = (filename.split('/')[-1] |
77 filename = (filename.split('/')[-1] |
78 .replace('\\', '\\\\').replace('"', '\\"')) |
78 .replace('\\', '\\\\').replace('"', '\\"')) |
79 self.headers.append(('Content-Disposition', |
79 self.headers.append(('Content-Disposition', |
80 'inline; filename="%s"' % filename)) |
80 'inline; filename="%s"' % filename)) |
81 if length is not None: |
81 if body is not None: |
82 self.headers.append(('Content-Length', str(length))) |
82 self.headers.append(('Content-Length', str(len(body)))) |
83 |
83 |
84 for k, v in self.headers: |
84 for k, v in self.headers: |
85 if not isinstance(v, str): |
85 if not isinstance(v, str): |
86 raise TypeError('header value must be string: %r' % (v,)) |
86 raise TypeError('header value must be string: %r' % (v,)) |
87 |
87 |
101 status = statusmessage(status) |
101 status = statusmessage(status) |
102 |
102 |
103 self.server_write = self._start_response(status, self.headers) |
103 self.server_write = self._start_response(status, self.headers) |
104 self._start_response = None |
104 self._start_response = None |
105 self.headers = [] |
105 self.headers = [] |
|
106 if body is not None: |
|
107 self.write(body) |
|
108 self.server_write = None |
106 |
109 |
107 def write(self, thing): |
110 def write(self, thing): |
108 if thing: |
111 if thing: |
109 try: |
112 try: |
110 self.server_write(thing) |
113 self.server_write(thing) |