Commit 40738c6e authored by Tim Neutkens's avatar Tim Neutkens Committed by Guillermo Rauch

Throw Error when url.parse without true is parsed (#1282)

* Throw Error when url.parse without true is parsed

This is a bit more descriptive when this mistake is made by the user.

* Parse when needed

* Parse querystring if it is not provided
parent 7c8cd4bc
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { parse } from 'url' import { parse as parseUrl } from 'url'
import { parse as parseQs } from 'querystring'
import fs from 'mz/fs' import fs from 'mz/fs'
import http, { STATUS_CODES } from 'http' import http, { STATUS_CODES } from 'http'
import { import {
...@@ -33,12 +34,14 @@ export default class Server { ...@@ -33,12 +34,14 @@ export default class Server {
getRequestHandler () { getRequestHandler () {
return (req, res, parsedUrl) => { return (req, res, parsedUrl) => {
// Parse url if parsedUrl not provided
if (!parsedUrl) { if (!parsedUrl) {
parsedUrl = parse(req.url, true) parsedUrl = parseUrl(req.url, true)
} }
if (!parsedUrl.query) { // Parse the querystring ourselves if the user doesn't handle querystring parsing
throw new Error('Please provide a parsed url to `handle` as third parameter. See https://github.com/zeit/next.js#custom-server-and-routing for an example.') if (typeof parsedUrl.query === 'string') {
parsedUrl.query = parseQs(parsedUrl.query)
} }
return this.run(req, res, parsedUrl) return this.run(req, res, parsedUrl)
...@@ -222,7 +225,7 @@ export default class Server { ...@@ -222,7 +225,7 @@ export default class Server {
} }
} }
async render404 (req, res, parsedUrl = parse(req.url, true)) { async render404 (req, res, parsedUrl = parseUrl(req.url, true)) {
const { pathname, query } = parsedUrl const { pathname, query } = parsedUrl
res.statusCode = 404 res.statusCode = 404
return this.renderError(null, req, res, pathname, query) return this.renderError(null, req, res, pathname, query)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment