<p dir="auto">Here is a small test program to demonstrate the bug:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import requests

url = &quot;http://lohas.pixnet.net/blog&quot;
r = requests.get(url)
iter_lines = [line for line in r.iter_lines(chunk_size=7, decode_unicode=False)]
split_lines = r.content.splitlines()
for index, (iline, sline) in enumerate(zip(iter_lines, split_lines)):
	if iline != sline:
		print(&quot;line {} is broken&quot;.format(index))
		print(iline, len(iline))
		print(sline, len(sline))
		break"><pre class="notranslate"><code class="notranslate">import requests

url = "http://lohas.pixnet.net/blog"
r = requests.get(url)
iter_lines = [line for line in r.iter_lines(chunk_size=7, decode_unicode=False)]
split_lines = r.content.splitlines()
for index, (iline, sline) in enumerate(zip(iter_lines, split_lines)):
	if iline != sline:
		print("line {} is broken".format(index))
		print(iline, len(iline))
		print(sline, len(sline))
		break
</code></pre></div>
<p dir="auto">Expected behavior:<br>
r.iter_lines() should give the same result as r.content.splitlines()</p>
<p dir="auto">Actual behavior:<br>
The test program generates the following output:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="line 275 is broken
b'' 0
b'\t\t\t&lt;tbody&gt;' 10"><pre class="notranslate"><code class="notranslate">line 275 is broken
b'' 0
b'\t\t\t&lt;tbody&gt;' 10
</code></pre></div>
<p dir="auto">Changing chunk_size can break different lines.</p>
<p dir="auto">I'm using a proxy that requires authentication to send request that receives 302 response with Location header. I would like python.requests to follow this redirect and make request via proxy with specified credentials. But it seems like this doesn't happen, if I provide credentials in HTTPProxyAuth they will work ok for 200 responses but will fail for 302. See below code sample:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
import requests
from requests.auth import HTTPProxyAuth

sess = requests.Session()
url1 = 'http://httpbin.org/'
url2 = 'http://httpbin.org/redirect/2'
auth = HTTPProxyAuth('frank', 'hunter2')
proxies = {
    &quot;http&quot;: &quot;http://localhost:9000&quot;
}
response1 = sess.get(url1, proxies=proxies, auth=auth)
response1.raise_for_status()
response2 = sess.get(url2, proxies=proxies, auth=auth)
response2.raise_for_status()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">requests</span>
<span class="pl-k">from</span> <span class="pl-s1">requests</span>.<span class="pl-s1">auth</span> <span class="pl-k">import</span> <span class="pl-v">HTTPProxyAuth</span>

<span class="pl-s1">sess</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-v">Session</span>()
<span class="pl-s1">url1</span> <span class="pl-c1">=</span> <span class="pl-s">'http://httpbin.org/'</span>
<span class="pl-s1">url2</span> <span class="pl-c1">=</span> <span class="pl-s">'http://httpbin.org/redirect/2'</span>
<span class="pl-s1">auth</span> <span class="pl-c1">=</span> <span class="pl-v">HTTPProxyAuth</span>(<span class="pl-s">'frank'</span>, <span class="pl-s">'hunter2'</span>)
<span class="pl-s1">proxies</span> <span class="pl-c1">=</span> {
    <span class="pl-s">"http"</span>: <span class="pl-s">"http://localhost:9000"</span>
}
<span class="pl-s1">response1</span> <span class="pl-c1">=</span> <span class="pl-s1">sess</span>.<span class="pl-en">get</span>(<span class="pl-s1">url1</span>, <span class="pl-s1">proxies</span><span class="pl-c1">=</span><span class="pl-s1">proxies</span>, <span class="pl-s1">auth</span><span class="pl-c1">=</span><span class="pl-s1">auth</span>)
<span class="pl-s1">response1</span>.<span class="pl-en">raise_for_status</span>()
<span class="pl-s1">response2</span> <span class="pl-c1">=</span> <span class="pl-s1">sess</span>.<span class="pl-en">get</span>(<span class="pl-s1">url2</span>, <span class="pl-s1">proxies</span><span class="pl-c1">=</span><span class="pl-s1">proxies</span>, <span class="pl-s1">auth</span><span class="pl-c1">=</span><span class="pl-s1">auth</span>)
<span class="pl-s1">response2</span>.<span class="pl-en">raise_for_status</span>()</pre></div>
<p dir="auto">Now launch MITM proxy on localhost</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt; mitmproxy -p 9000 --singleuser=frank:hunter2"><pre class="notranslate"><code class="notranslate">&gt; mitmproxy -p 9000 --singleuser=frank:hunter2
</code></pre></div>
<p dir="auto">This fails with 407 for me, and proxy logs only two requests</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    response2.raise_for_status()
  File &quot;----------&quot;, line 862, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 407 Client Error: Proxy Authentication Required for url: http://httpbin.org/relative-redirect/1"><pre class="notranslate"><code class="notranslate">    response2.raise_for_status()
  File "----------", line 862, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 407 Client Error: Proxy Authentication Required for url: http://httpbin.org/relative-redirect/1
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt; GET http://httpbin.org/
       ← 200 text/html 11.87kB 3.57MB/s
   GET http://httpbin.org/redirect/2
       ← 302 text/html 247B 76.59kB/s
"><pre class="notranslate"><code class="notranslate">&gt;&gt; GET http://httpbin.org/
       ← 200 text/html 11.87kB 3.57MB/s
   GET http://httpbin.org/redirect/2
       ← 302 text/html 247B 76.59kB/s

</code></pre></div>
<p dir="auto">it does not log request to <code class="notranslate">Location</code>.</p>
<p dir="auto">I see that putting credentials in proxies dictionary somehow fixes this issue when I use MITM proxy but it doesn't fix it for my production proxy (can't share code or proxy details here, need to check closer why it doesn't work for my proxy). I guess some details in setup of proxies might vary.</p>
<p dir="auto">Is this a bug? I see some issues for proxy auth but they are mostly about HTTPS, not sure if someone reported this thing I describe here. Should this be fixed?</p>
<p dir="auto">EDIT:</p>
<p dir="auto">It looks like this always fails if proxy password is empty string.</p>
<p dir="auto">change auth to</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="auth = HTTPProxyAuth('frank', '')

proxies = {
    &quot;http&quot;: &quot;http://frank:@localhost:9000&quot;
}"><pre class="notranslate"><span class="pl-s1">auth</span> <span class="pl-c1">=</span> <span class="pl-v">HTTPProxyAuth</span>(<span class="pl-s">'frank'</span>, <span class="pl-s">''</span>)

<span class="pl-s1">proxies</span> <span class="pl-c1">=</span> {
    <span class="pl-s">"http"</span>: <span class="pl-s">"http://frank:@localhost:9000"</span>
}</pre></div>
<p dir="auto">will now always fail on redirect.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="auth = HTTPProxyAuth('frank', 'hunter2')
proxies = {
    &quot;http&quot;: &quot;http://frank:hunter2@localhost:9000&quot;
}"><pre class="notranslate"><span class="pl-s1">auth</span> <span class="pl-c1">=</span> <span class="pl-v">HTTPProxyAuth</span>(<span class="pl-s">'frank'</span>, <span class="pl-s">'hunter2'</span>)
<span class="pl-s1">proxies</span> <span class="pl-c1">=</span> {
    <span class="pl-s">"http"</span>: <span class="pl-s">"http://frank:hunter2@localhost:9000"</span>
}</pre></div>
<p dir="auto">works fine on redirects, but seems somewhat duplicated.</p>
<p dir="auto">I noticed this on Ubuntu 14.04, requests 2.11.1, python 2.7.6, mitmproxy 0.10.1</p>