wsgi: Support optional headers w/ "100 Continue" responses

Since WSGI is all about a single response to a single
request, in order for servers to process a PUT as a
single HTTP request and at the same time pass **hints**
back to the client (for example, a payload type that the
server can accept, or to inform the client that the server
expects to receive payload metadata at the end of the
payload transfer, etc), "100 Continue" response headers
is pretty much only place to add these hints.

This is consistent with **RFC2616, section 10.1**:

10.1 Informational 1xx

This class of status code indicates a provisional response,
consisting only of the **Status-Line** and **optional
headers**, and is terminated by an empty line.

Openstack Swift has an immediate use case for this feature
where the object server will use an
"Accept-Payload-Footer: 1" like header to hint the proxy
server to send a payload footer after it has finished
transmitting the payload (for sending metadata for an
erasure coded or encrypted payload, enforcing end-to-end
Etag checks, etc).

Addresses eventlet issue/enhancement #126

Signed-off-by: Tushar Gohad <tushar.gohad@intel.com>
This commit is contained in:
Tushar Gohad
2014-08-22 21:52:30 -07:00
parent 5e553771d0
commit e18b30b119
2 changed files with 91 additions and 10 deletions

View File

@@ -83,13 +83,33 @@ class Input(object):
self.chunked_input = chunked_input
self.chunk_length = -1
# (optional) headers to send with a "100 Continue" response. Set by
# calling set_hundred_continue_respose_headers() on env['wsgi.input']
self.hundred_continue_headers = None
def _send_hundred_continue_response(self):
towrite = []
# 100 Continue status line
towrite.append(self.wfile_line)
# Optional headers
if self.hundred_continue_headers is not None:
# 100 Continue headers
for header in self.hundred_continue_headers:
towrite.append('%s: %s\r\n' % header)
# Blank line
towrite.append('\r\n')
self.wfile.writelines(towrite)
self.wfile = None
self.wfile_line = None
def _do_read(self, reader, length=None):
if self.wfile is not None:
# 100 Continue
self.wfile.write(self.wfile_line)
self.wfile = None
self.wfile_line = None
# 100 Continue response
self._send_hundred_continue_response()
if length is None and self.content_length is not None:
length = self.content_length - self.position
if length and length > self.content_length - self.position:
@@ -105,10 +125,8 @@ class Input(object):
def _chunked_read(self, rfile, length=None, use_readline=False):
if self.wfile is not None:
# 100 Continue
self.wfile.write(self.wfile_line)
self.wfile = None
self.wfile_line = None
# 100 Continue response
self._send_hundred_continue_response()
try:
if length == 0:
return ""
@@ -175,6 +193,18 @@ class Input(object):
def get_socket(self):
return self.rfile._sock
def set_hundred_continue_response_headers(self, headers,
capitalize_response_headers=True):
# Response headers capitalization (default)
# CONTent-TYpe: TExt/PlaiN -> Content-Type: TExt/PlaiN
# Per HTTP RFC standard, header name is case-insensitive.
# Please, fix your client to ignore header case if possible.
if capitalize_response_headers:
headers = [
('-'.join([x.capitalize() for x in key.split('-')]), value)
for key, value in headers]
self.hundred_continue_headers = headers
class HeaderLineTooLong(Exception):
pass
@@ -526,7 +556,7 @@ class HttpProtocol(BaseHTTPServer.BaseHTTPRequestHandler):
if env.get('HTTP_EXPECT') == '100-continue':
wfile = self.wfile
wfile_line = 'HTTP/1.1 100 Continue\r\n\r\n'
wfile_line = 'HTTP/1.1 100 Continue\r\n'
else:
wfile = None
wfile_line = None

View File

@@ -774,6 +774,57 @@ class TestHttpd(_TestBase):
fd.close()
sock.close()
def test_024a_expect_100_continue_with_headers(self):
def wsgi_app(environ, start_response):
if int(environ['CONTENT_LENGTH']) > 1024:
start_response('417 Expectation Failed', [('Content-Length', '7')])
return ['failure']
else:
environ['wsgi.input'].set_hundred_continue_response_headers(
[('Hundred-Continue-Header-1', 'H1'),
('Hundred-Continue-Header-2', 'H2'),
('Hundred-Continue-Header-k', 'Hk')])
text = environ['wsgi.input'].read()
start_response('200 OK', [('Content-Length', str(len(text)))])
return [text]
self.site.application = wsgi_app
sock = eventlet.connect(('localhost', self.port))
fd = sock.makefile('rw')
fd.write(b'PUT / HTTP/1.1\r\nHost: localhost\r\nContent-length: 1025\r\nExpect: 100-continue\r\n\r\n')
fd.flush()
result = read_http(sock)
self.assertEqual(result.status, 'HTTP/1.1 417 Expectation Failed')
self.assertEqual(result.body, 'failure')
fd.write(
b'PUT / HTTP/1.1\r\nHost: localhost\r\nContent-length: 7\r\nExpect: 100-continue\r\n\r\ntesting')
fd.flush()
header_lines = []
while True:
line = fd.readline()
if line == '\r\n':
break
else:
header_lines.append(line.strip())
assert header_lines[0].startswith('HTTP/1.1 100 Continue')
headers = dict((k, v) for k, v in (h.split(': ', 1) for h in header_lines[1:]))
assert 'Hundred-Continue-Header-1' in headers
assert 'Hundred-Continue-Header-2' in headers
assert 'Hundred-Continue-Header-K' in headers
self.assertEqual('H1', headers['Hundred-Continue-Header-1'])
self.assertEqual('H2', headers['Hundred-Continue-Header-2'])
self.assertEqual('Hk', headers['Hundred-Continue-Header-K'])
header_lines = []
while True:
line = fd.readline()
if line == '\r\n':
break
else:
header_lines.append(line)
assert header_lines[0].startswith('HTTP/1.1 200 OK')
self.assertEqual(fd.read(7), 'testing')
fd.close()
sock.close()
def test_025_accept_errors(self):
debug.hub_exceptions(True)
listener = greensocket.socket()