试探http1.1

用Node建立服务端(service)和客户端(clinent),在客户端自定义http的内容,看看有什么好玩的

 2022-04-09

const http = require("http");
let n = 0;

const server = http
    .createServer((req, res) => {
        console.log("someone", ++n);
        console.log(1, req.headers);
        req.on("data", (d) => {
            console.log(2, d);
        });
        res.setHeader("Content-Type", "text/html");
        res.setHeader("X-Foo", "bar");
        res.writeHead(200, { "Content-Type": "text/plain" });
        res.end("ok");
    })
    .listen(8000);

1.如果在请求头里去掉空行(\r\n)会怎样

const net = require("net");
const client = net.createConnection({
        port: 8000,
        host: "127.0.0.1",
    },
    () => {
        //请求头
        client.write("GET / HTTP/1.1\r\n");
        client.write("Content-Type:text/plain\r\n");
        // client.write("Content-Length:8\r\n");
        // client.write("Host: 127.0.0.1\r\n");
        // client.write("\r\n");
        //这后面是请求体
        client.write("1234");
    }
);
client.on("data", (data) => {
    console.log(data.toString());
    client.end();
});
client.on("end", () => {
    console.log("disconnected from server");
});

 结果:客户端无响应

 

2.如果响应头格式写错会怎样

const net = require("net");
const client = net.createConnection({
        port: 8000,
        host: "127.0.0.1",
    },
    () => {
        //请求头
        client.write("GET / HTTP/1.1\r\n");
        client.write("Content-Type:text/plain\r\n");
        client.write("Content-Length:8");
        client.write("Host: 127.0.0.1\r\n");
        client.write("\r\n");
        // 这后面是请求体
        client.write("1234");
    }
);
client.on("data", (data) => {
    console.log(data.toString());
    client.end();
});
client.on("end", () => {
    console.log("disconnected from server");
});

 结果:客户端报错

 

 

3.Content-Length字段和实际的请求体长度不一致会怎样

const net = require("net");
const client = net.createConnection({
        port: 8000,
        host: "127.0.0.1",
    },
    () => {
        //请求头
        client.write("POST / HTTP/1.1\r\n");
        client.write("Content-Type:text/plain\r\n");
        client.write("Content-Length:3\r\n");
        client.write("Host: 127.0.0.1\r\n");
        client.write("\r\n");
        // 这后面是请求体
        client.write("0123456789");
    }
);
client.on("data", (data) => {
    console.log(data.toString());
    client.end();
});
client.on("end", () => {
    console.log("disconnected from server");
});

结果:服务端收到到了响应,但响应体的长度只有3byte,与我胡乱设置的content-length一样

 

 4.如果请求不带content-length字段会怎样

const net = require("net");
const client = net.createConnection({
        port: 8000,
        host: "127.0.0.1",
    },
    () => {
        //请求头
        client.write("POST / HTTP/1.1\r\n");
        client.write("Content-Type:text/plain\r\n");
        // client.write("Content-Length:3\r\n");
        client.write("Host: 127.0.0.1\r\n");
        client.write("\r\n");
        // 这后面是请求体
        client.write("0123456789");
    }
);
client.on("data", (data) => {
    console.log(data.toString());
    client.end();
});
client.on("end", () => {
    console.log("disconnected from server");
});

结果:客户端根本不触发'data'事件

 

 5GET请求可以想POST请求一样附带响应体吗

const net = require("net");
const client = net.createConnection({
        port: 8000,
        host: "127.0.0.1",
    },
    () => {
        //请求头
        client.write("GET / HTTP/1.1\r\n");
        client.write("Content-Type:text/plain\r\n");
      client.write("Content-Length:3\r\n");
        client.write("Host: 127.0.0.1\r\n");
        client.write("\r\n");
        // 这后面是请求体
        client.write("0123456789");
    }
);
client.on("data", (data) => {
    console.log(data.toString());
    client.end();
});
client.on("end", () => {
    console.log("disconnected from server");
});

结果:服务端接收到了GET的响应体,且格式与POST请求一致

 

posted @ 2022-04-09 15:14  犬齿间的玉米  阅读(34)  评论(0)    收藏  举报