MCPcopy Create free account
hub / github.com/honojs/node-server / buildUrl

Function buildUrl

src/url.ts:14–42  ·  view source on GitHub ↗
(scheme: string, host: string, incomingUrl: string)

Source from the content-addressed store, hash-verified

12const reValidHost = /^[a-z0-9._-]+(?::(?:[1-5]\d{3,4}|[6-9]\d{3}))?$/
13
14export const buildUrl = (scheme: string, host: string, incomingUrl: string) => {
15 const url = `${scheme}://${host}${incomingUrl}`
16
17 if (!reValidHost.test(host)) {
18 const urlObj = new URL(url)
19
20 // if suspicious, check by host. host header sometimes contains port.
21 if (
22 urlObj.hostname.length !== host.length &&
23 urlObj.hostname !== (host.includes(':') ? host.replace(/:\d+$/, '') : host).toLowerCase()
24 ) {
25 throw new RequestError('Invalid host header')
26 }
27 return urlObj.href
28 } else if (incomingUrl.length === 0) {
29 return url + '/'
30 } else {
31 if (incomingUrl.charCodeAt(0) !== 0x2f) {
32 // '/'
33 throw new RequestError('Invalid URL')
34 }
35
36 if (!reValidRequestUrl.test(incomingUrl) || reDotSegment.test(incomingUrl)) {
37 return new URL(url).href
38 }
39
40 return url
41 }
42}

Callers 2

url.test.tsFile · 0.90
newRequestFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…