- HTTP 100
The purpose of the 100 (Continue) status (see section 10.1.1) is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.------w3
這段話的大概意思是,服務(wù)器根據(jù)客戶端的請求頭判斷是否接受客戶端的請求。如果接受請求則響應(yīng)100狀態(tài)碼,服務(wù)端根據(jù)是否存在 Expect: 100-continue 請求頭判斷是否是Expect請求(有部分web服務(wù)器不能正確的處理Expect請求)
- curl 驗證(curl 請求如果請求體大于1024自己會自動帶上 Expect: 100-continue請求頭)
curl -v -d '大于1024字節(jié)' 'http://127.0.0.1:12345/'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 12345 (#0)
> POST / HTTP/1.1
> Host: 127.0.0.1:12345
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Length: 1047(字節(jié)數(shù))
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
>
< HTTP/1.1 100 Continue(100 狀態(tài)碼)
* We are completely uploaded and fine (響應(yīng)嗎是100,發(fā)送請求體)
< HTTP/1.1 200 OK
< Date: Sun, 03 Dec 2017 10:51:15 GMT
< Connection: keep-alive
< Content-Length: 7
curl -v -d '等于1024字節(jié)' 'http://127.0.0.1:12345/'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 12345 (#0)
> POST / HTTP/1.1
> Host: 127.0.0.1:12345
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Length: 1024(等于102字節(jié)無響應(yīng)嗎)
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 1024 out of 1024 bytes
< HTTP/1.1 200 OK
< Date: Sun, 03 Dec 2017 10:53:15 GMT
< Connection: keep-alive
< Content-Length: 7
- 也可以偽造直接在請求頭上帶上 Expect: 100-continue
curl -v -H 'Expect:100-continue' 'http://127.0.0.1:12345/'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 12345 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:12345
> User-Agent: curl/7.55.1
> Accept: */*
> Expect:100-continue
>
< HTTP/1.1 100 Continue (100狀態(tài)碼)
< HTTP/1.1 200 OK
< Date: Sun, 03 Dec 2017 11:04:49 GMT
< Connection: keep-alive
< Content-Length: 7
- Node http 中100 狀態(tài)碼的使用
Node http.server中存在一個checkContinue事件,在請求頭中帶有Expect: 100-continue時會觸發(fā)事件(觸發(fā)checkContinue事件之后不會再觸發(fā)request事件),如果Excpet的值為空,則不會觸發(fā)checkContinue事件,直接觸發(fā)request事件,如果Excpet的值為其它值觸發(fā)checkExpectation事件
const http = require("http");
const server = http.createServer()
.on('request', (request, response) => {
console.log(request.headers);
response.end('Hello Node');
})
.on('checkContinue', (request, response) => {
// 如果接受請求響應(yīng)100狀態(tài)碼,觸發(fā)request
response.writeContinue();
server.emit('request', request, response);
// 不接受請求響應(yīng)417,請求結(jié)束
//response.statusCode = 417;
//response.end();
})
.on('checkExpectation', (request, response) => {
// 如果請求頭Expect的只不為空且不是100-continue 觸發(fā)
response.statusCode = 500;
response.end();
})
.listen(12345,'127.0.0.1');
Node http.ClientRequest 中有一個continue事件,當(dāng)http響應(yīng)碼是100時會觸發(fā)
const http = require('http');
let data = '1'.repeat(1200);
let options = {
'protocol':'http:',
'hostname':'127.0.0.1',
'port':'12345',
'method':'POST',
'path':'',
'headers':{
// 帶上Expect請求頭,和curl有區(qū)別,在請求體超過1024的時候不會自動帶上Expect請求頭
'Expect': '100-continue',
// 帶上Connection:Close 否則連接不會斷開,只能在,response事件中調(diào)用 response.req.destroy()
//'Connection': 'Close',
'Content-Length':data.length
}
};
let client = http.request(options);
client.on('response', function(response){
console.log(response.headers);
console.log(response.statusCode);
/* console.log(response.statusMessage);
console.log(Object.keys(response));
console.log(response.connection === response.socket); */
//response.req.destroy();
}).on('continue', function(){
// 第一個響應(yīng)碼是 100 觸發(fā) 發(fā)送請求體,這樣做的好處是防止了無效的網(wǎng)絡(luò)數(shù)據(jù)傳輸,
// 如果請求頭和請求體一起發(fā)送,服務(wù)器如果拒絕處理,那請求體就白白發(fā)送了。在傳輸大文件時很有用
// 服務(wù)器響應(yīng)數(shù)據(jù)之后觸發(fā) response 事件
// 如果第一個響應(yīng)碼不是100觸發(fā) respnse 事件
client.write(data);
client.end();
}).on('error', function(error){
console.log(error)
});
- 結(jié)束
如果服務(wù)能夠正確處理Expect: 100-continue 請求頭,合理的響應(yīng)100狀態(tài)碼能夠防止無效的網(wǎng)絡(luò)數(shù)據(jù)傳輸。