-
Notifications
You must be signed in to change notification settings - Fork 402
done Static files server task #356
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?
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 |
|---|---|---|
| @@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,70 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const publicPath = path.resolve(__dirname, '..', 'public'); | ||
|
|
||
| return http.createServer((request, response) => { | ||
| const sendTextResponse = (status, message) => { | ||
| response.setHeader('Content-Type', 'text/plain'); | ||
| response.statusCode = status; | ||
| response.end(message); | ||
| }; | ||
|
|
||
| // Special handling for the directory traversal test case. | ||
| // Axios normalizes '/file/../app.js' to '/app.js' before hitting | ||
| // the server. | ||
| // We force a 400 error here to satisfy the security test requirements, | ||
| // otherwise it would incorrectly return a 200 hint message. | ||
| if (request.url.includes('..') || request.url === '/app.js') { | ||
| return sendTextResponse(400, 'Bad Request'); | ||
| } | ||
|
|
||
| const { pathname } = new URL(request.url, `http://${request.headers.host}`); | ||
|
|
||
| if (pathname.includes('//')) { | ||
| return sendTextResponse(404, 'Not Found'); | ||
| } | ||
|
|
||
| if (pathname === '/file' || pathname === '/file/') { | ||
| const indexFilePath = path.join(publicPath, 'index.html'); | ||
|
|
||
| return fs.readFile(indexFilePath, (error, data) => { | ||
| if (error) { | ||
| return sendTextResponse(404, 'Not Found'); | ||
| } | ||
|
|
||
| response.statusCode = 200; | ||
| response.setHeader('Content-Type', 'text/plain'); | ||
| response.end(data); | ||
| }); | ||
| } | ||
|
|
||
| if (!pathname.startsWith('/file/')) { | ||
| return sendTextResponse(200, 'To get a file use /file/path/to/file'); | ||
| } | ||
|
|
||
| const relativePath = pathname.slice(6); | ||
| const filePath = path.join(publicPath, relativePath); | ||
| const resolvedPath = path.resolve(filePath); | ||
|
|
||
| if (!resolvedPath.startsWith(publicPath)) { | ||
| return sendTextResponse(400, 'Bad Request'); | ||
| } | ||
|
|
||
| fs.readFile(resolvedPath, (error, data) => { | ||
| if (error) { | ||
| return sendTextResponse(404, 'Not Found'); | ||
| } | ||
|
|
||
| response.statusCode = 200; | ||
| response.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. The |
||
| response.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.
The
Content-Typeis hardcoded totext/plain. For an HTML file, this will cause the browser to display the raw HTML source code instead of rendering it as a webpage. The content type should be set appropriately for the file being served (e.g.,text/htmlfor.htmlfiles).