- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在不同端口上本地运行的两个 WSGI 服务器。一个服务器返回一个包含 javascript 的 html 页面,该 javascript 使用 jQuery 向另一个 WSGI 服务器发出跨域 ajax 请求。
origin_server.py
在 http://localhost:9010
提供 html。
#!/usr/bin/env python
from wsgiref.simple_server import make_server
def origin_html(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
start_response(status, response_headers)
f = open('./index.html', 'rb')
return [f.read()]
httpd = make_server('localhost', 9010, origin_html)
httpd.serve_forever()
cors_server.py
提供 javascript 将请求的跨域资源。
#!/usr/bin/env python
from wsgiref.simple_server import make_server
import json
def cors_json(environ, start_response):
status = '200 OK'
response_headers = [
('Content-Type', 'application/json'),
('Access-Control-Allow-Origin', '*')
]
start_response(status, response_headers)
return [json.dumps({'foo': 'bar'})]
httpd = make_server('localhost', 9011, cors_json)
httpd.serve_forever()
index.html
origin_sever.py
提供的页面。
<!DOCTYPE html>
<html>
<head>
<script
type="text/javascript"
src="https://code.jquery.com/jquery-2.1.3.min.js"
>
</script>
<script type="text/javascript">
$(document).ready(function () {
console.log('Doing the thing.');
$.get('http://localhost:9011').done(function (data) {
console.log('Got the thing', data);
});
});
</script>
</head>
<body>
Beep boop.
</body>
</html>
github 上面的代码在这里:https://github.com/thatjpk/local-wsgi-cors-ajax
引用版本:
鉴于上述情况,我可以打开两个终端,在一个终端中启动 origin_server.py
,在另一个终端中启动 cors_server.py
,然后打开浏览器选项卡并指向它到位于 http://localhost:9010/
的原始服务器。这会加载页面,并执行跨域请求。
理想情况下,延迟应该都非常低,因为它都是本地的。但是,我一直看到跨源请求的延迟超过十 秒。奇怪的是,我只在 Chrome 非隐身窗口中看到过这个。同一台机器上的 Firefox、curl、httpie 和 Chrome 隐身标签页都在不到 10 毫秒的时间内完成跨域请求,而非隐身 Chrome 标签页则需要三个数量级的时间。
如果我从等式中剔除 CORS,并且只是将 http://localhost:9011
放在 Chrome 地址栏中,json 会立即加载到 View 中。这显然只有在请求来自 javascript 时才会发生。
如果尝试向 cors_server.py
发出另一个请求,而 Chrome 中对它的请求处于“待处理”状态,则第二个请求将等到第一个请求完成)。这意味着第一个请求在执行到达 cors_server.py
中的处理程序之前会以某种方式占用 wsgi 服务器很长时间。
当请求永远完成时,肯定会有更多事情发生。我使用 tcpdump -i lo0 dst port 9011 or src port 9011
收集了以下信息。
这是来自 httpie (http get localhost:9011
) 的请求,可以快速完成。
17:33:57.176028 IP localhost.64723 > localhost.9011: Flags [S], seq 2244357563, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910553864 ecr 0,sackOK,eol], length 0
17:33:57.176089 IP localhost.9011 > localhost.64723: Flags [S.], seq 1494086712, ack 2244357564, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910553864 ecr 910553864,sackOK,eol], length 0
17:33:57.176103 IP localhost.64723 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176113 IP localhost.9011 > localhost.64723: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176145 IP localhost.64723 > localhost.9011: Flags [P.], seq 1:136, ack 1, win 12759, options [nop,nop,TS val 910553864 ecr 910553864], length 135
17:33:57.176158 IP localhost.9011 > localhost.64723: Flags [.], ack 136, win 12755, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176760 IP localhost.9011 > localhost.64723: Flags [P.], seq 1:18, ack 136, win 12755, options [nop,nop,TS val 910553864 ecr 910553864], length 17
17:33:57.176781 IP localhost.64723 > localhost.9011: Flags [.], ack 18, win 12758, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176811 IP localhost.9011 > localhost.64723: Flags [P.], seq 18:55, ack 136, win 12755, options [nop,nop,TS val 910553864 ecr 910553864], length 37
17:33:57.176823 IP localhost.64723 > localhost.9011: Flags [.], ack 55, win 12757, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176834 IP localhost.9011 > localhost.64723: Flags [P.], seq 55:92, ack 136, win 12755, options [nop,nop,TS val 910553864 ecr 910553864], length 37
17:33:57.176844 IP localhost.64723 > localhost.9011: Flags [.], ack 92, win 12756, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176857 IP localhost.9011 > localhost.64723: Flags [P.], seq 92:178, ack 136, win 12755, options [nop,nop,TS val 910553864 ecr 910553864], length 86
17:33:57.176866 IP localhost.64723 > localhost.9011: Flags [.], ack 178, win 12753, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176878 IP localhost.9011 > localhost.64723: Flags [P.], seq 178:192, ack 136, win 12755, options [nop,nop,TS val 910553864 ecr 910553864], length 14
17:33:57.176886 IP localhost.64723 > localhost.9011: Flags [.], ack 192, win 12753, options [nop,nop,TS val 910553864 ecr 910553864], length 0
17:33:57.176961 IP localhost.9011 > localhost.64723: Flags [F.], seq 192, ack 136, win 12755, options [nop,nop,TS val 910553865 ecr 910553864], length 0
17:33:57.176975 IP localhost.64723 > localhost.9011: Flags [.], ack 193, win 12753, options [nop,nop,TS val 910553865 ecr 910553865], length 0
17:33:57.176980 IP localhost.9011 > localhost.64723: Flags [.], ack 136, win 12755, options [nop,nop,TS val 910553865 ecr 910553865], length 0
17:33:57.182034 IP localhost.64723 > localhost.9011: Flags [F.], seq 136, ack 193, win 12753, options [nop,nop,TS val 910553870 ecr 910553865], length 0
17:33:57.182095 IP localhost.9011 > localhost.64723: Flags [.], ack 137, win 12755, options [nop,nop,TS val 910553870 ecr 910553870], length 0
这是来自 Firefox 的请求,可以快速完成。
18:12:57.416741 IP localhost.65089 > localhost.9011: Flags [S], seq 2202457533, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 912890306 ecr 0,sackOK,eol], length 0
18:12:57.416794 IP localhost.9011 > localhost.65089: Flags [S.], seq 1807873905, ack 2202457534, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 912890306 ecr 912890306,sackOK,eol], length 0
18:12:57.416806 IP localhost.65089 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.416818 IP localhost.9011 > localhost.65089: Flags [.], ack 1, win 12759, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.416883 IP localhost.65089 > localhost.9011: Flags [P.], seq 1:346, ack 1, win 12759, options [nop,nop,TS val 912890306 ecr 912890306], length 345
18:12:57.416899 IP localhost.9011 > localhost.65089: Flags [.], ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417478 IP localhost.9011 > localhost.65089: Flags [P.], seq 1:18, ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 17
18:12:57.417492 IP localhost.65089 > localhost.9011: Flags [.], ack 18, win 12758, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417516 IP localhost.9011 > localhost.65089: Flags [P.], seq 18:55, ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 37
18:12:57.417538 IP localhost.65089 > localhost.9011: Flags [.], ack 55, win 12757, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417548 IP localhost.9011 > localhost.65089: Flags [P.], seq 55:92, ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 37
18:12:57.417555 IP localhost.65089 > localhost.9011: Flags [.], ack 92, win 12756, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417562 IP localhost.9011 > localhost.65089: Flags [P.], seq 92:192, ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 100
18:12:57.417569 IP localhost.65089 > localhost.9011: Flags [.], ack 192, win 12753, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417618 IP localhost.9011 > localhost.65089: Flags [F.], seq 192, ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417629 IP localhost.65089 > localhost.9011: Flags [.], ack 193, win 12753, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417634 IP localhost.9011 > localhost.65089: Flags [.], ack 346, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417638 IP localhost.65089 > localhost.9011: Flags [F.], seq 346, ack 193, win 12753, options [nop,nop,TS val 912890306 ecr 912890306], length 0
18:12:57.417647 IP localhost.9011 > localhost.65089: Flags [.], ack 347, win 12748, options [nop,nop,TS val 912890306 ecr 912890306], length 0
这是来自 Chrome 隐身标签的请求,可快速完成。
17:28:10.096905 IP6 localhost.64691 > localhost.9011: Flags [S], seq 2861078667, win 65535, options [mss 16324,nop,wscale 5,nop,nop,TS val 910207638 ecr 0,sackOK,eol], length 0
17:28:10.096926 IP6 localhost.9011 > localhost.64691: Flags [R.], seq 0, ack 2861078668, win 0, length 0
17:28:10.097010 IP localhost.64692 > localhost.9011: Flags [S], seq 3615577823, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910207638 ecr 0,sackOK,eol], length 0
17:28:10.097056 IP localhost.9011 > localhost.64692: Flags [S.], seq 3533709271, ack 3615577824, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910207638 ecr 910207638,sackOK,eol], length 0
17:28:10.097066 IP localhost.64692 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910207638 ecr 910207638], length 0
17:28:10.097075 IP localhost.9011 > localhost.64692: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910207638 ecr 910207638], length 0
17:28:10.097274 IP localhost.64692 > localhost.9011: Flags [P.], seq 1:373, ack 1, win 12759, options [nop,nop,TS val 910207638 ecr 910207638], length 372
17:28:10.097293 IP localhost.9011 > localhost.64692: Flags [.], ack 373, win 12747, options [nop,nop,TS val 910207638 ecr 910207638], length 0
17:28:10.098108 IP localhost.9011 > localhost.64692: Flags [P.], seq 1:18, ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207638], length 17
17:28:10.098131 IP localhost.64692 > localhost.9011: Flags [.], ack 18, win 12758, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098166 IP localhost.9011 > localhost.64692: Flags [P.], seq 18:55, ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207639], length 37
17:28:10.098184 IP localhost.64692 > localhost.9011: Flags [.], ack 55, win 12757, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098212 IP localhost.9011 > localhost.64692: Flags [P.], seq 55:92, ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207639], length 37
17:28:10.098232 IP localhost.64692 > localhost.9011: Flags [.], ack 92, win 12756, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098246 IP localhost.9011 > localhost.64692: Flags [P.], seq 92:178, ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207639], length 86
17:28:10.098267 IP localhost.64692 > localhost.9011: Flags [.], ack 178, win 12753, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098277 IP localhost.9011 > localhost.64692: Flags [P.], seq 178:192, ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207639], length 14
17:28:10.098283 IP localhost.64692 > localhost.9011: Flags [.], ack 192, win 12753, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098352 IP localhost.9011 > localhost.64692: Flags [F.], seq 192, ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098369 IP localhost.64692 > localhost.9011: Flags [.], ack 193, win 12753, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.098380 IP localhost.9011 > localhost.64692: Flags [.], ack 373, win 12747, options [nop,nop,TS val 910207639 ecr 910207639], length 0
17:28:10.099183 IP localhost.64692 > localhost.9011: Flags [F.], seq 373, ack 193, win 12753, options [nop,nop,TS val 910207640 ecr 910207639], length 0
17:28:10.099217 IP localhost.9011 > localhost.64692: Flags [.], ack 374, win 12747, options [nop,nop,TS val 910207640 ecr 910207640], length 0
这是来自常规 Chrome 标签页的请求,耗时约 18 秒。
17:28:27.147218 IP6 localhost.64699 > localhost.9011: Flags [S], seq 1481186887, win 65535, options [mss 16324,nop,wscale 5,nop,nop,TS val 910224643 ecr 0,sackOK,eol], length 0
17:28:27.147230 IP6 localhost.9011 > localhost.64699: Flags [R.], seq 0, ack 1481186888, win 0, length 0
17:28:27.147277 IP6 localhost.64700 > localhost.9011: Flags [S], seq 1928824698, win 65535, options [mss 16324,nop,wscale 5,nop,nop,TS val 910224643 ecr 0,sackOK,eol], length 0
17:28:27.147287 IP6 localhost.9011 > localhost.64700: Flags [R.], seq 0, ack 1928824699, win 0, length 0
17:28:27.147331 IP6 localhost.64701 > localhost.9011: Flags [S], seq 63463797, win 65535, options [mss 16324,nop,wscale 5,nop,nop,TS val 910224643 ecr 0,sackOK,eol], length 0
17:28:27.147340 IP6 localhost.9011 > localhost.64701: Flags [R.], seq 0, ack 63463798, win 0, length 0
17:28:27.147578 IP localhost.64704 > localhost.9011: Flags [S], seq 2185396531, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224643 ecr 0,sackOK,eol], length 0
17:28:27.147602 IP localhost.64705 > localhost.9011: Flags [S], seq 2307844059, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224643 ecr 0,sackOK,eol], length 0
17:28:27.147617 IP localhost.9011 > localhost.64704: Flags [S.], seq 4199967470, ack 2185396532, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224643 ecr 910224643,sackOK,eol], length 0
17:28:27.147627 IP localhost.64706 > localhost.9011: Flags [S], seq 324888549, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224643 ecr 0,sackOK,eol], length 0
17:28:27.147638 IP localhost.9011 > localhost.64705: Flags [S.], seq 1097272416, ack 2307844060, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224643 ecr 910224643,sackOK,eol], length 0
17:28:27.147643 IP localhost.64704 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224643 ecr 910224643], length 0
17:28:27.147665 IP localhost.9011 > localhost.64706: Flags [S.], seq 837656804, ack 324888550, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224643 ecr 910224643,sackOK,eol], length 0
17:28:27.147669 IP localhost.64705 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224643 ecr 910224643], length 0
17:28:27.147675 IP localhost.9011 > localhost.64704: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224643 ecr 910224643], length 0
17:28:27.147681 IP localhost.64706 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224643 ecr 910224643], length 0
17:28:27.147686 IP localhost.9011 > localhost.64705: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224643 ecr 910224643], length 0
17:28:27.147691 IP localhost.9011 > localhost.64706: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224643 ecr 910224643], length 0
17:28:27.243730 IP6 localhost.64708 > localhost.9011: Flags [S], seq 4213763930, win 65535, options [mss 16324,nop,wscale 5,nop,nop,TS val 910224733 ecr 0,sackOK,eol], length 0
17:28:27.243748 IP6 localhost.9011 > localhost.64708: Flags [R.], seq 0, ack 4213763931, win 0, length 0
17:28:27.243855 IP localhost.64709 > localhost.9011: Flags [S], seq 1721904940, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224733 ecr 0,sackOK,eol], length 0
17:28:27.243910 IP localhost.9011 > localhost.64709: Flags [S.], seq 2521348026, ack 1721904941, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 910224733 ecr 910224733,sackOK,eol], length 0
17:28:27.243925 IP localhost.64709 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224733 ecr 910224733], length 0
17:28:27.243934 IP localhost.9011 > localhost.64709: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910224733 ecr 910224733], length 0
17:28:27.244093 IP localhost.64709 > localhost.9011: Flags [P.], seq 1:390, ack 1, win 12759, options [nop,nop,TS val 910224733 ecr 910224733], length 389
17:28:27.244108 IP localhost.9011 > localhost.64709: Flags [.], ack 390, win 12747, options [nop,nop,TS val 910224733 ecr 910224733], length 0
17:28:45.693569 IP localhost.64704 > localhost.9011: Flags [F.], seq 1, ack 1, win 12759, options [nop,nop,TS val 910243145 ecr 910224643], length 0
17:28:45.693596 IP localhost.64705 > localhost.9011: Flags [F.], seq 1, ack 1, win 12759, options [nop,nop,TS val 910243145 ecr 910224643], length 0
17:28:45.693605 IP localhost.64706 > localhost.9011: Flags [F.], seq 1, ack 1, win 12759, options [nop,nop,TS val 910243145 ecr 910224643], length 0
17:28:45.693627 IP localhost.9011 > localhost.64704: Flags [.], ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693636 IP localhost.9011 > localhost.64705: Flags [.], ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693645 IP localhost.9011 > localhost.64706: Flags [.], ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693655 IP localhost.64704 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693661 IP localhost.64705 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693666 IP localhost.64706 > localhost.9011: Flags [.], ack 1, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693737 IP localhost.9011 > localhost.64704: Flags [F.], seq 1, ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693766 IP localhost.64704 > localhost.9011: Flags [.], ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693862 IP localhost.9011 > localhost.64705: Flags [F.], seq 1, ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693909 IP localhost.64705 > localhost.9011: Flags [.], ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693935 IP localhost.9011 > localhost.64706: Flags [F.], seq 1, ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.693949 IP localhost.64706 > localhost.9011: Flags [.], ack 2, win 12759, options [nop,nop,TS val 910243145 ecr 910243145], length 0
17:28:45.694861 IP localhost.9011 > localhost.64709: Flags [P.], seq 1:18, ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910224733], length 17
17:28:45.694886 IP localhost.64709 > localhost.9011: Flags [.], ack 18, win 12758, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.694937 IP localhost.9011 > localhost.64709: Flags [P.], seq 18:55, ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910243146], length 37
17:28:45.694958 IP localhost.64709 > localhost.9011: Flags [.], ack 55, win 12757, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.694971 IP localhost.9011 > localhost.64709: Flags [P.], seq 55:92, ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910243146], length 37
17:28:45.694982 IP localhost.64709 > localhost.9011: Flags [.], ack 92, win 12756, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.694997 IP localhost.9011 > localhost.64709: Flags [P.], seq 92:178, ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910243146], length 86
17:28:45.695009 IP localhost.64709 > localhost.9011: Flags [.], ack 178, win 12753, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.695016 IP localhost.9011 > localhost.64709: Flags [P.], seq 178:192, ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910243146], length 14
17:28:45.695020 IP localhost.64709 > localhost.9011: Flags [.], ack 192, win 12753, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.695087 IP localhost.9011 > localhost.64709: Flags [F.], seq 192, ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.695111 IP localhost.64709 > localhost.9011: Flags [.], ack 193, win 12753, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.695120 IP localhost.9011 > localhost.64709: Flags [.], ack 390, win 12747, options [nop,nop,TS val 910243146 ecr 910243146], length 0
17:28:45.696046 IP localhost.64709 > localhost.9011: Flags [F.], seq 390, ack 193, win 12753, options [nop,nop,TS val 910243147 ecr 910243146], length 0
17:28:45.696069 IP localhost.9011 > localhost.64709: Flags [.], ack 391, win 12747, options [nop,nop,TS val 910243147 ecr 910243147], length 0
看起来 Chrome 会先尝试 IPv6,wsgiref 不喜欢。因此服务器发送的连接重置是有意义的。
在非隐身 Chrome 的情况下,客户端似乎进行了多次连接尝试。服务器重置前几次尝试,然后接受几次,然后客户端仅使用其中一个连接实际发送请求。我假设因为 wsgiref.simple_server
是单线程的,所以之前的连接必须超时才能处理实际请求;因此延迟。
有趣的是,即使是隐身 Chrome 仍然会遇到重置,但只是重试一次,然后按预期工作。 Firefox 和 httpie 都只建立一个可以顺利运行的连接。
最佳答案
tl;dr:Chrome 的优化过于激进。 11 点的新闻。
事实证明,这是因为 Chrome 为加速页面加载、API 交互等所做的“优化”。
Chrome 有一个名为“预测网络操作以提高页面加载性能”的设置。 (它在高级设置中的隐私下。)启用此功能后,Chrome 将抢先打开与托管页面资源的服务器的 TCP 连接,甚至在客户端代码到达执行点之前就会告诉浏览器发出 HTTP 请求.
在这种情况下,Chrome 看到我将向 CORS 服务器发出 AJAX 请求,并打开多个到服务器的 TCP 连接,为我稍后可能发出的其他 HTTP 请求做准备(即使我的 javascript 只会永远向服务器发出一个请求)。它显然在 实际承载 javascript 发出的一个 HTTP 请求的连接之前打开了这些额外连接。这意味着对于单线程服务器,那些额外的连接必须在承载请求的连接得到服务之前超时。这就是延迟的根源。
“预测网络操作以提高页面加载性能”设置在默认情况下处于启用状态,并且在隐身时处于非事件状态(大概是因为它会将用户的行为泄露给服务器,而这些服务器可能会或可能不会最终收到请求)。禁用它会阻止打开这些额外的连接,并且 Chrome 在这种情况下的行为与 Firefox 相同。
这让我想知道 Chrome 在互联网上的生产服务器上打开了多少 TCP 连接,这些连接要么未被使用,要么在发送请求之前保持打开状态的时间过长。未使用的连接甚至不会显示在访问日志中,因为没有发出 HTTP 请求,因此很难衡量 Chrome 用户创建了多少额外负载。在我看来,这种行为是有害的。首先,因为它试图提高客户端性能(这是令人钦佩的),但这样做是以牺牲服务器运算符(operator)为代价的(事实并非如此)。其次,因为(在隐身模式下禁用它可以避免)对隐私有(private)负面影响。
关于javascript - CORS AJAX 请求到 Chrome 中本地 WSGI 服务器的延迟过高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204466/
我尝试访问此 API click here for more info POST https://api.line.me/v2/oauth/accessToken 但总是得到错误: XMLHttpRe
我在掌握 CORS 概念时遇到问题... 我认为同源策略保护应用程序不会对“不受信任的域”进行 ajax 调用。所以, mydomain.com 向 发出 ajax 调用somedomain.com
一个技术性很强的问题,可能只有了解浏览器内部结构的人才能回答...... 浏览器缓存 CORS 预检响应的准确程度如何(假设在对 OPTIONS 预检请求的响应中返回了 Access-Control-
我一直在阅读 CORS以及它是如何工作的,但我发现很多事情令人困惑。例如,有很多关于事情的细节,比如 User Joe is using browser BrowserX to get data fr
在 OWASP site 上看到这个矛盾,我感到很困惑。 CORS 备忘单: 使用 Access-Control-Allow-Credentials: true 响应 header 时要特别小心。将允
我们无法在飞行前恢复使用 cors:任何帮助都非常感谢 ==========错误========================== About to connect() to localhost p
跨域请求字体文件时,您必须确保允许请求域使用 CORS header 访问字体文件: 访问控制允许来源 访问控制允许凭据 然而,这在请求图像时不是必需的,无论是对于 img元素或 background
CORS 规范没有说明服务器应如何响应无效的 CORS 请求。例如,如果请求 Origin 无效,则 CORS spec states :“终止这组步骤。请求超出了本规范的范围。”其他错误情况也有类似
我在理解同源策略和“解决”它的不同方法时遇到了一些麻烦。 很明显,同源策略是作为一种安全措施而存在的,因此来自服务器/域的一个脚本无法访问来自另一个服务器/域的数据。 也很明显,有时能够打破此规则很有
我正在尝试使用 cloudformation 在 API Gateway 中部署 API。这些方法需要启用 CORS,我遵循此处的模板 Enable CORS for API Gateway in C
我正在构建一个使用 CORS 的 REST 应用程序。每个 REST 调用都是不同的,我发现获取预检 OPTIONS 调用会产生很大的开销。有没有办法缓存并应用预检选项结果,以便对同一域的任何后续调用
我正在将我的 WebApi 升级到 MVC6。 在 WebApi 中,我可以拦截每个 HTTP 请求,如果是预检,我可以用浏览器可以接受的 header 进行响应。 我正在尝试找出如何在 MVC6 W
假设一个 CORS 预检请求进来了,但它为一个或多个 Access-Control-Request-* 指定了一个不受支持的值。标题。服务器应该如何将其传达回浏览器? 一些例子: 浏览器发送带有 Ac
问题中的一切。 附加信息: 使用 Win 10,GraphDB 免费,9.1.1 • RDF4J 3.0.1 • Connectors 12.0.2 我在控制台 => 设置中添加了 graphdb.w
我正在尝试通过 jQuery 调用 Sonar 网络服务之一。由于调用是跨域进行的,因此调用在 Chrome 上失败并出现以下错误: 请求的资源上不存在“Access-Control-Allow-Or
我想使用 NestJs api,但我在每个 fetch 中都收到相同的错误消息: Access to fetch at 'http://localhost:3000/articles' from or
我不确定这是否属于这里,但我在开发我的 svelte 应用程序时遇到了问题。 在开发过程中,它目前在独立服务器上运行(遵循使用 rollup 和 sirv 的指南)并针对不同端口上的后端 API。 稍
如果在服务器上正确设置 CORS 以仅允许某些来源访问服务器,这是否足以防止 XSRF 攻击? 最佳答案 更具体地说,很容易错误地认为如果 evil.com 由于 CORS 无法向 good.com
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我是一名优秀的程序员,十分优秀!