[HTTP] 协议底层文本格式
2019-07-27 本文已影响0人
爱上落入尘世间的你
平时使用http都是直接使用各种库区发起和接收http请求, 对于http底层格式的探究不多, 这次详细了解了一些http这个文本格式协议的运行流程
1. client.request.get
GET / HTTP/1.1
Accept: application/json
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_202-release)
Accept-Encoding: gzip,deflate
or
GET /?id=99 HTTP/1.1
Accept: application/json
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_202-release)
Accept-Encoding: gzip,deflate
2. client.request.post(normal)
POST / HTTP/1.1
Content-Type: application/json
Content-Length: 2
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_202-release)
Accept-Encoding: gzip,deflate
{}
3. client.request.post(x-www-form-urlencoded)
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 25
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_202-release)
Accept-Encoding: gzip,deflate
id=99&content=new-element
4. client.request.post(multipart/form-data)
POST / HTTP/1.1
Content-Length: 555
Content-Type: multipart/form-data; boundary=trJB5E5VXSMIf6OyGa7vnSQl2X-81n3
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_202-release)
Accept-Encoding: gzip,deflate
--trJB5E5VXSMIf6OyGa7vnSQl2X-81n3
Content-Disposition: form-data; name=".gitignore"; filename=".gitignore"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
.idea/
node_modules/
package-lock.json
--trJB5E5VXSMIf6OyGa7vnSQl2X-81n3
Content-Disposition: form-data; name="README.md"; filename="README.md"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
# simple-nodejs-proxy
a simple forward proxy implemented in node.js, and docker image is provided !
--trJB5E5VXSMIf6OyGa7vnSQl2X-81n3--
5. server.response(not chunked)
HTTP/1.1 200 OK
content-length: 11
hello world
6. server.response(chunked)
HTTP/1.1 200 OK
Transfer-Encoding: chunked
b
01234567890
5
12345
0
server的代码(node.js)
const net = require('net')
net.createServer(function (sock) {
sock.on('data', function (data) {
console.log(data.toString())
sock.write('HTTP/1.1 200 OK\r\n');
sock.write('Transfer-Encoding: chunked\r\n');
sock.write('\r\n');
sock.write('b\r\n');
sock.write('01234567890\r\n');
sock.write('5\r\n');
sock.write('12345\r\n');
sock.write('0\r\n');
sock.write('\r\n');
})
sock.on('error', function(err)
{
console.log(err)
})
}).listen(9000, '0.0.0.0')