Skip to content

Commit

Permalink
completed the http server
Browse files Browse the repository at this point in the history
  • Loading branch information
kripa-sindhu-007 committed Jun 26, 2024
1 parent 8973df5 commit e71742a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const net = require("net");
const fs = require("fs");
const zlib=require("zlib");
const zlib = require("zlib");

// You can use print statements as follows for debugging, they'll be visible when running tests.
console.log("Logs from your program will appear here!");
Expand All @@ -17,22 +17,24 @@ const server = net.createServer((socket) => {
const content = url.split("/echo/")[1].trim();
const acceptEncodingHeaderRegex = /Accept-Encoding:\s.*\r\n/g;
const acceptEncodingMatch = acceptEncodingHeaderRegex.exec(request);
const acceptedEncoding = acceptEncodingMatch?.[0].split(":")[1].trim().split(',').map(e => e.trim());
console.log(acceptedEncoding)
if (acceptedEncoding?.includes('gzip')) {
if(content){
const body =zlib.gzipSync(content);
const acceptedEncoding = acceptEncodingMatch?.[0]
.split(":")[1]
.trim()
.split(",")
.map((e) => e.trim());
console.log(acceptedEncoding);
if (acceptedEncoding?.includes("gzip")) {
if (content) {
const body = zlib.gzipSync(content);
socket.write(
`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${body.length}\r\nContent-Encoding: gzip\r\n\r\n`
);
socket.write(body);
}else{
} else {
socket.write(
`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${content.length}\r\nContent-Encoding: gzip\r\n\r\n${content}`
);

}

} else {
socket.write(
`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${content.length}\r\n\r\n${content}`
Expand Down

0 comments on commit e71742a

Please sign in to comment.