From b3ac91c4fb0ed7fde401221f4c96836a0e2b92ab Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Mon, 14 May 2012 15:05:53 -0700 Subject: [PATCH 1/2] Support beautification of HTTP 100 responses. Detect when the server sends an HTTP 100 (Continue) response header and kick our beautification loop into 'continuing' mode. While in this mode, we wait for another ^HTTP/ header to be received before responding to the `\r\n` break sequence signalling the end of the headers and the start of the body. This gives us output like: HTTP/1.1 100 (Continue) HTTP/1.1 200 OK Content-Length: 387 Content-Type: application/json; charset=UTF-8 { } This comes into play when uploading files using curlish. Previously, these types of HTTP conversations were not "beautified" because the header processing loop bailed out after the initial `HTTP/1.1 100 (Continue)\r\n\r\n` content was handled. --- curlish.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/curlish.py b/curlish.py index c89fbb6..940b39a 100755 --- a/curlish.py +++ b/curlish.py @@ -553,20 +553,25 @@ def _walk(obj, indentation, inline=False, w=sys.stdout.write): def beautify_curl_output(iterable, hide_headers, hide_jsonp=False): """Parses curl output and adds colors and reindents as necessary.""" + continuing = False json_body = False might_be_javascript = False has_colors = is_color_terminal() # Headers for line in iterable: - if has_colors and re.search(r'^HTTP/', line): - if re.search('HTTP/\d+.\d+ [45]\d+', line): - color = get_color('statusline_error') - else: - color = get_color('statusline_ok') - if not hide_headers: + if re.search(r'^HTTP/', line): + if re.search('HTTP/\d+.\d+ 100', line): + continuing = True + elif continuing: + continuing = False + if has_colors and not hide_headers: + if re.search('HTTP/\d+.\d+ [45]\d+', line): + color = get_color('statusline_error') + else: + color = get_color('statusline_ok') sys.stdout.write(color + line + ANSI_CODES['reset']) - continue + continue if re.search(r'^Content-Type:\s*(text/javascript|application/(.+?\+)?json)\s*(?i)', line): json_body = True if 'javascript' in line: @@ -583,7 +588,7 @@ def beautify_curl_output(iterable, hide_headers, hide_jsonp=False): else: sys.stdout.write(line) sys.stdout.flush() - if line == '\r\n': + if line == '\r\n' and not continuing: break # JSON Body. Do not reindent if we have headers and are piping From 1f5543057f28ab79ba9b7af76190a5f4663fab57 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Fri, 1 Jun 2012 10:44:30 -0700 Subject: [PATCH 2/2] Match multiple spaces before the HTTP status code. --- curlish.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/curlish.py b/curlish.py index 940b39a..bc9042b 100755 --- a/curlish.py +++ b/curlish.py @@ -561,12 +561,12 @@ def beautify_curl_output(iterable, hide_headers, hide_jsonp=False): # Headers for line in iterable: if re.search(r'^HTTP/', line): - if re.search('HTTP/\d+.\d+ 100', line): + if re.search('HTTP/\d+.\d+\s+100', line): continuing = True elif continuing: continuing = False if has_colors and not hide_headers: - if re.search('HTTP/\d+.\d+ [45]\d+', line): + if re.search('HTTP/\d+.\d+\s+[45]\d+', line): color = get_color('statusline_error') else: color = get_color('statusline_ok')