文章目录
HTTP协议原理
1. HTTP浏览器输入URL后HTTP请求返回的完整过程
2. HTTP2新功能
- 所有数据以二进制传输
- 同一个连接里面发送多个请求,不再需要按照顺序来
- 头信息压缩以及推送等提高效率的功能
3. URI、URL、URN
URI : Uniform Resource Identifier 统一资源标志符
用来唯一标识互联网上的信息资源,包括URL和URN
URL: Uniform Resource Locator 统一资源定位符
URN: 永久统一资源定位符
4. 浏览器缓存
5. Cookie和Session
- Cookie
- 通过set-cookie设置
- 下次请求会自动带上
- 键值对,可设置多个
- max-age和expires设置过期时间
- Secure 只在https的时候发送
- HttpOnly 无法通过document.cookie访问
const http = require('http')
const fs = require('fs')
http.createServer(function (request, response) {
console.log('request come', request.url)
if (request.url === '/') {
const html = fs.readFileSync('test.html', 'utf8')
response.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': ['id=123; max-age=2', 'abc=456;domain=test.com'] //设置cookie
})
response.end(html)
}
}).listen(8887)
console.log('server listening on 8887')
6. HTTP长连接
const http = require('http')
const fs = require('fs')
http.createServer(function (request, response) {
console.log('request come', request.url)
const html = fs.readFileSync('test.html', 'utf8')
const img = fs.readFileSync('test.jpg')
if (request.url === '/') {
response.writeHead(200, {
'Content-Type': 'text/html',
})
response.end(html)
} else {
response.writeHead(200, {
'Content-Type': 'image/jpg',
// 'Connection': 'keep-alive'
'Connection': 'close'
})
response.end(img)
}
}).listen(8888)
console.log('server listening on 8888')
7. CSP
Content-Security-Policy 内容安全策略
作用:限制资源获取
限制方式 :default-src限制全局
根据资源类型限制:connect-src、img-src、 font-src等等。。。
8.HTTPS
HTTPS加密传输过程
9. HTTP2
优势:
- 信道复用
- 分帧传输:每一帧都有上下文的联系,也就是说通过HTTP发送的数据并不需要一定要按照严格的顺序发送;同一个连接上并发地发送不同的请求
- Server Push