Skip to content

Commit a5b218d

Browse files
committed
make server more resilient to bad requests
1 parent add0b21 commit a5b218d

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

build/client.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,27 +3462,35 @@ var treeToHtml = require('../treeToHtml')
34623462
var requestDetails = url.parse(window.location.href, true)
34633463
var source = requestDetails.query.source
34643464

3465+
var mdRegex = new RegExp(/\.h?md(\#[-_\w]*)?/)
3466+
34653467
if (source) {
34663468
xhr({
34673469
uri: '/api/render?source=' + source,
34683470
headers: {
34693471
"Content-Type": "application/json"
34703472
}
3471-
}, function (err, resp, body) {
3472-
console.log(body)
3473-
var results = JSON.parse(body)
3474-
3475-
var fullRenderedMarkdown = treeToHtml(results)
3476-
dom('body main .container.target').replace('#loading', "<div class='markdown-body'>{body}</div>", {body: fullRenderedMarkdown} )
3477-
})
3473+
}, renderResponse)
34783474
}
34793475
else {
3480-
34813476
dom('#loading').style({'display': 'none'})
34823477
dom('#how-to').removeClass('hide')
34833478
//dom('body main .container.target').replace('#loading', '<div></div>')
34843479
}
34853480

3481+
function renderResponse (err, resp, body) {
3482+
if (resp.statusCode == 400) {
3483+
var insertContent = body
3484+
}
3485+
else {
3486+
console.log(body)
3487+
var results = JSON.parse(body)
3488+
var insertContent = treeToHtml(results)
3489+
}
3490+
3491+
dom('body main .container.target').replace('#loading', "<div class='markdown-body'>{insert}</div>", {insert: insertContent} )
3492+
}
3493+
34863494

34873495
},{"../treeToHtml":52,"domquery":6,"url":5,"xhr":44}],52:[function(require,module,exports){
34883496
module.exports = function treeToHtml ( tree ) {

hypermarkdown_badge_small.png

2.44 KB
Loading

server.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ router.addRoute('/', rootRequestResponse)
1717
function rootRequestResponse(req, res, match) {
1818
var referer = req.headers.referer || ''
1919
//linked from an md file?
20-
if (referer.match(/\.h?md(\#[-_\w]*)?/)) {
20+
if (referer.match(mdRegex)) {
2121
res.writeHead(302, {'Location': 'http://hypermarkdown.herokuapp.com/?source=' + referer, })
2222
res.end()
2323
}
@@ -32,7 +32,7 @@ function buildHypermarkdownTree(req, res, match) {
3232
var requestDetails = url.parse(req.url, true)
3333
var source = requestDetails.query.source
3434

35-
if (source) {
35+
if (source && source.match(mdRegex) ) {
3636
var target = make_raw(source)
3737

3838
build(target, function(err, tree) {
@@ -44,8 +44,8 @@ function buildHypermarkdownTree(req, res, match) {
4444
})
4545
}
4646
else {
47-
res.writeHead(200, {'content-type': 'text/plain'})
48-
res.write('use format /api/render?source=address_to_md_file')
47+
res.writeHead(400, {'content-type': 'text/plain'})
48+
res.write("You need to provide a link to a markdown file. <br />Check you're using the format <strong>/?source=address_to_file.md</strong> or <strong>/api/render/?source=address_to_file.md</strong> if you're using the API")
4949
res.end()
5050
}
5151
}
@@ -77,6 +77,8 @@ function make_raw( url ) {
7777
return url
7878
}
7979

80+
var mdRegex = new RegExp(/\.h?md(\#[-_\w]*)?/)
81+
8082

8183
function startServer() {
8284
var port = process.env.PORT || 5000

source/client.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ var treeToHtml = require('../treeToHtml')
77
var requestDetails = url.parse(window.location.href, true)
88
var source = requestDetails.query.source
99

10+
var mdRegex = new RegExp(/\.h?md(\#[-_\w]*)?/)
11+
1012
if (source) {
1113
xhr({
1214
uri: '/api/render?source=' + source,
1315
headers: {
1416
"Content-Type": "application/json"
1517
}
16-
}, function (err, resp, body) {
17-
console.log(body)
18-
var results = JSON.parse(body)
19-
20-
var fullRenderedMarkdown = treeToHtml(results)
21-
dom('body main .container.target').replace('#loading', "<div class='markdown-body'>{body}</div>", {body: fullRenderedMarkdown} )
22-
})
18+
}, renderResponse)
2319
}
2420
else {
25-
2621
dom('#loading').style({'display': 'none'})
2722
dom('#how-to').removeClass('hide')
2823
//dom('body main .container.target').replace('#loading', '<div></div>')
2924
}
3025

26+
function renderResponse (err, resp, body) {
27+
if (resp.statusCode == 400) {
28+
var insertContent = body
29+
}
30+
else {
31+
console.log(body)
32+
var results = JSON.parse(body)
33+
var insertContent = treeToHtml(results)
34+
}
35+
36+
dom('body main .container.target').replace('#loading', "<div class='markdown-body'>{insert}</div>", {insert: insertContent} )
37+
}
38+

styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ main {
4545
background: #ffffff;
4646
padding: 0 20px;
4747
margin: 0 20px;
48+
padding-top: 10px;
4849
}
4950

5051

0 commit comments

Comments
 (0)