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
63 changes: 63 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
/* eslint-disable no-console */
'use strict';

const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
const normalizedURL = new url.URL(req.url, `http://${req.headers.host}`);
const fileName = normalizedURL.pathname;

if (!fileName.startsWith('/file/')) {
if (fileName === '/file') {
res.statusCode = 200;
} else {
res.statusCode = 400;
}
res.setHeader('Content-Type', 'text/plain');
res.end('You need to set path starting with "/file/"');

return;
}

if (fileName.includes('//')) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');

res.end('You can`t type double slash(/)');

return;
}

const relativePath = fileName.replace('/file/', '');

const publicDir = path.resolve('./public');
const resolvedPath = path.resolve(`./public/${relativePath}`);

if (
!resolvedPath.startsWith(publicDir + path.sep) &&
resolvedPath !== publicDir
) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end("You can't go outside /file folder");

return;
Comment on lines +36 to +58
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 requirements specify that if pathname contains ../ it should return 400 status. Currently there's no explicit check for ../ substring. The path traversal check handles security, but the explicit requirement is to detect ../ in the pathname itself.

}

fs.readFile(`./public/${relativePath}`, (err, data) => {
if (err) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end(`Not Found`);

return;
}

res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
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 Content-Type is always set to text/plain. According to requirements, it should be text/html for .html files and text/css for .css files. Consider determining the content type based on the file extension.

res.end(data);
});
});

return server;
}

module.exports = {
Expand Down
Loading