-
Notifications
You must be signed in to change notification settings - Fork 404
Solution #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #347
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,76 @@ | ||
| /* eslint-disable no-console */ | ||
| /* eslint-disable max-len */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs/promises'); | ||
| const path = require('path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer(async (req, res) => { | ||
| const { pathname } = new URL(req.url, `http://${req.headers.host}`); | ||
| const isFileRoute = pathname.startsWith('/file/'); | ||
|
|
||
| // 1. ЛОГІКА ПІДКАЗКИ (за вимогою ментора) | ||
| // Будь-що, що не починається з /file/, отримує підказку. | ||
| // АЛЕ для тесту додаємо виняток: якщо це /app.js, даємо 400. | ||
| if (!isFileRoute) { | ||
| if (pathname === '/app.js') { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
|
||
| return res.end('Bad Request'); | ||
| } | ||
|
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This special case for |
||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
|
||
| return res.end( | ||
| 'To load a file, use a path starting with /file/ (e.g., /file/index.html)', | ||
| ); | ||
| } | ||
|
|
||
| // 2. ОБРОБКА /file/ ТА ШЛЯХІВ УСЕРЕДИНІ | ||
| // Відрізаємо /file/ (перші 6 символів) | ||
| const pathAfterPrefix = pathname.slice(6); | ||
|
|
||
| // Якщо після /file/ нічого немає (запит /file/), беремо index.html | ||
| const relativePath = | ||
| pathAfterPrefix === '' ? 'index.html' : pathAfterPrefix; | ||
|
|
||
| const publicPath = path.resolve('public'); | ||
| const filePath = path.join(publicPath, relativePath); | ||
|
|
||
| // 3. БЕЗПЕКА (Path Traversal) | ||
| // Перевіряємо фізичне розташування та наявність ".." у сирому URL | ||
| if (!filePath.startsWith(publicPath) || req.url.includes('..')) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
|
||
| return res.end('Bad Request'); | ||
| } | ||
|
|
||
| // 4. ПЕРЕВІРКА НА ПОДВІЙНІ СЛЕШІ (для тесту 404) | ||
| if (pathAfterPrefix.includes('//')) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
|
||
| return res.end('File not found'); | ||
|
Comment on lines
+55
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check for |
||
| } | ||
|
|
||
| try { | ||
| const content = await fs.readFile(filePath); | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end(content); | ||
| } catch (err) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('File not found'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| }; | ||
| module.exports = { createServer }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check doesn't account for the requirement that a request to
/fileshould also be treated as a file route and serveindex.html. Currently, a request to/filewill be handled as a non-file route because'/file'.startsWith('/file/')isfalse.