-
Notifications
You must be signed in to change notification settings - Fork 415
solution #365
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 #365
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 |
|---|---|---|
| @@ -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,80 @@ | ||
| '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 server = http.createServer((req, res) => { | ||
| const rawUrl = req.url; | ||
|
|
||
| if (rawUrl.includes('//')) { | ||
| res.statusCode = 404; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const urlPath = rawUrl.split('?')[0]; | ||
|
|
||
| if (urlPath.startsWith('/file/')) { | ||
| const relativeRaw = urlPath.slice('/file/'.length); | ||
|
|
||
| let relative; | ||
|
|
||
| try { | ||
| relative = decodeURIComponent(relativeRaw); | ||
|
Comment on lines
+21
to
+26
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 the request is |
||
| } catch { | ||
| res.statusCode = 400; | ||
| res.end('Bad request'); | ||
|
Comment on lines
+27
to
+30
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. In the |
||
|
|
||
| return; | ||
| } | ||
|
|
||
| const publicDir = path.resolve(__dirname, '../public'); | ||
| const filePath = path.resolve(publicDir, relative); | ||
|
|
||
| const relativePath = path.relative(publicDir, filePath); | ||
|
|
||
| if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) { | ||
| res.statusCode = 400; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Bad request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.readFile(filePath, (err, data) => { | ||
| if (err) { | ||
| res.statusCode = 404; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 200; | ||
| res.end(data); | ||
|
Comment on lines
+63
to
+73
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. On successful file reads you set |
||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (urlPath !== '/file') { | ||
| res.statusCode = 400; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Bad request'); | ||
|
|
||
| return; | ||
|
Comment on lines
+79
to
+84
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 branch handles requests not equal to
Comment on lines
+79
to
+84
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 block returns |
||
| } | ||
|
Comment on lines
+78
to
+85
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 block returns a 400 for any request whose pathname is not exactly |
||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('content-type', 'text/plain'); | ||
| res.end('Use /file/<filename> to serve a file from the public folder'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| 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.
You check for duplicated slashes using
rawUrl.includes('//'). The requirement says to check the pathname for//— do this after extractingurlPath(or checkurlPath) so query strings won't cause false positives.