-
Notifications
You must be signed in to change notification settings - Fork 410
add solution #349
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?
add solution #349
Changes from 1 commit
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,8 +1,71 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const { url } = req; | ||
|
|
||
| if (!url.startsWith('/file')) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Use /file/<filename> to load files'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!url.startsWith('/file/')) { | ||
| res.statusCode = 200; | ||
|
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. A |
||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
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. When serving |
||
|
|
||
| return res.end('Use /file/<filename> to load files'); | ||
| } | ||
|
|
||
| const publicDir = path.resolve(__dirname, '..', 'public'); | ||
| let filePath = url.slice(6); | ||
|
|
||
| if (filePath === '' || filePath === '/') { | ||
| filePath = 'index.html'; | ||
| } | ||
|
|
||
| if (filePath.includes('..')) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (filePath.includes('//')) { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const fullPath = path.join(publicDir, filePath); | ||
|
|
||
| if (!fullPath.startsWith(publicDir)) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Bad Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.readFile(fullPath, (err, data) => { | ||
| if (err) { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('File not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200); | ||
| res.end(data); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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 condition incorrectly blocks requests to
/file. According to the requirements,/fileshould be treated the same as/file/and serveindex.html. This check should be adjusted to allow the/filepath to be handled by the file-serving logic below.