Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
match requests with origin params
  • Loading branch information
gzzhanghao committed Nov 15, 2018
1 parent 5c4b340 commit 63fb485
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
16 changes: 14 additions & 2 deletions src/RequestHandler.js
@@ -1,7 +1,8 @@
import url from 'url'
import chokidar from 'chokidar'
import { gray, red, green } from 'chalk'

import { log } from './utils'
import { log, parseHost } from './utils'
import buildRoutes from './routes/buildRoutes'

export default class RequestHandler {
Expand All @@ -24,7 +25,7 @@ export default class RequestHandler {

async handleRequest(req) {
for (const { match, handle } of this.routes) {
if (!(req.params = match(req))) {
if (!(req.params = match(getRouteParams(req.raw)))) {
continue
}
let res = handle
Expand Down Expand Up @@ -57,3 +58,14 @@ export default class RequestHandler {
}
}
}

function getRouteParams(req) {
const secure = req.socket.encrypted
const [hostname, port] = parseHost(req.headers['host'], secure ? 443 : 80)
let protocol = secure ? 'https:' : 'http:'
if ((req.headers['upgrade'] || '').toUpperCase() === 'WEBSOCKET') {
protocol = secure ? 'wss:' : 'ws:'
}
const { pathname, query } = url.parse(req.url, true, true)
return { method: req.method, secure, protocol, hostname, port, pathname, query }
}
4 changes: 2 additions & 2 deletions src/routes/buildPattern.js
Expand Up @@ -29,8 +29,8 @@ function protocol(schema) {
if (schema == null) {
return () => true
}
const accepts = schema.split('|')
return target => accepts.includes(target)
const accepts = schema.toUpperCase().split('|')
return target => accepts.includes(target.replace(/:$/, '').toUpperCase())
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/routes/parseURL.js
Expand Up @@ -10,7 +10,7 @@
* - Pathname must have leading slash
*/

const protocol = '([^\\/:]+:?)'
const protocol = '(([^\\/:]+):?)'
const username = '([^:@\\/\\?#]+)'
const password = '([^@\\/\\?#]+)'
const hostname = '([^:\\/\\?#]+)'
Expand All @@ -26,6 +26,6 @@ const path = `((${pathname}?${search}?)${hash}?)`
const parser = new RegExp(`^((${protocol}?//)?(${auth}@)?${host})?${path}?$`)

export default function parseURL(url) {
const [,,,protocol,,auth,username,,password,host,hostname,,port,,path,pathname,search,hash] = url.match(parser)
const [,,,,protocol,,auth,username,,password,host,hostname,,port,,path,pathname,search,hash] = url.match(parser)
return { protocol, auth, username, password, host, hostname, port, path, pathname, search, hash }
}
17 changes: 10 additions & 7 deletions src/utils.js
@@ -1,10 +1,13 @@
const IPV6_REGEX = /^(.*?)(:(\d*))?$/

export const log = console.log // eslint-disable-line no-console

export function defer() {
const result = {}
result.promise = new Promise((resolve, reject) => {
result.resolve = resolve
result.reject = reject
})
return result
export function parseHost(value, defaultPort) {
const match = value.match(IPV6_REGEX)
let hostname = match[1]
if (match[1][0] === '[') {
hostname = match[1].slice(1, -1)
}
const port = match[3] ? +match[3] : defaultPort
return [hostname, port]
}
12 changes: 3 additions & 9 deletions src/wrapper/Request.js
@@ -1,8 +1,7 @@
import qs from 'querystring'
import net from 'net'
import url from 'url'

const IPV6_REGEX = /^(.*?)(:(\d*))?$/
import { parseHost } from '../utils'

export default class Request {

Expand Down Expand Up @@ -33,7 +32,7 @@ export default class Request {
this.raw = req

this.method = req.method.toUpperCase()
this.headers = req.headers
this.headers = { ...req.headers }
this.secure = req.socket.encrypted
this.host = req.headers['host'] || ''
this.path = req.url
Expand All @@ -56,12 +55,7 @@ export default class Request {
}

set host(value) {
const match = value.match(IPV6_REGEX)
this.hostname = match[1]
if (match[1][0] === '[') {
this.hostname = match[1].slice(1, -1)
}
this.port = match[3] ? +match[3] : (this.secure ? 443 : 80)
[this.hostname, this.port] = parseHost(value, this.secure ? 443 : 80)
}

get path() {
Expand Down

0 comments on commit 63fb485

Please sign in to comment.