Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
37 changes: 35 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
'use strict';

const http = require('http');
const { notFound, badRequest, hint } = require('./helpers/responses');
const { fileOutput } = require('./handlers/fileHandler');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
const rawUrl = req.url.split('?')[0];

let decodedUrl;

try {
decodedUrl = decodeURIComponent(rawUrl);
} catch (e) {
return badRequest(res);
}

if (decodedUrl.includes('//')) {
return notFound(res);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for .. anywhere in the URL, but the requirement states 'If the pathname contains ../'. The current implementation would block filenames like ..hidden or index..html even though they don't contain the path traversal sequence ../. Consider checking for ../ specifically instead.

if (decodedUrl.includes('..') || !decodedUrl.startsWith('/file')) {
return badRequest(res);
}

const urlArr = decodedUrl.split('/').filter(Boolean);

if (decodedUrl === '/file' || urlArr[0] !== 'file') {
return hint(res);
}

const filename = 'public/' + urlArr.slice(1).join('/');

fileOutput(res, filename);
Comment on lines +33 to +35
});

return server;
}

module.exports = {
Expand Down
35 changes: 35 additions & 0 deletions src/handlers/fileHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const fs = require('fs');
const path = require('path');
const { notFound } = require('../helpers/responses');

const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.txt': 'text/plain',
};

function fileOutput(res, filename) {
const stream = fs.createReadStream(filename);

stream.on('open', () => {
const ext = path.extname(filename).toLowerCase();
const contentType = mimeTypes[ext] || 'text/plain';

res.writeHead(200, { 'Content-Type': contentType });
stream.pipe(res);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition || decodedUrl === '/app.js' is not part of the requirements. The task only requires blocking paths containing ../. Consider removing this extra check.

});

Comment on lines +22 to +24
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition !decodedUrl.startsWith('/file') returns badRequest (400), but requirement #3 specifies that paths NOT starting with /file/ should return a hint message (200). For example, /anything should return hint, not 400. This check should only return badRequest for .. (path traversal), not for missing /file/ prefix.

stream.on('error', () => {
if (res.headersSent) {
return res.end();
}
notFound(res);
});
Comment on lines +28 to +30
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hint logic at line 28-30 is unreachable for paths like /anything because line 22 catches them first. According to requirements, /anything should return hint (200), not badRequest (400). The startsWith('/file') check needs to be moved after the .. check and return hint instead of badRequest.

}

module.exports = {
Comment on lines +28 to +33
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the URL is /file/, urlArr.slice(1) produces an empty array, resulting in filename = 'public/'. The code then tries to read a directory instead of public/index.html. According to the requirements, /file/ should return public/index.html.

fileOutput,
};
22 changes: 22 additions & 0 deletions src/helpers/responses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

function notFound(res) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}

function badRequest(res) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');
}

function hint(res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hint: /file/*filename*');
}

module.exports = {
notFound,
badRequest,
hint,
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line combines two separate conditions that should be handled independently. includes('..') correctly returns 400 (checklist item #5), but !decodedUrl.startsWith('/file') should return hint (200) per checklist item #3, not badRequest (400). Consider splitting: first check for .. returning 400, then check if path starts with /file for hint.

Loading