Skip to content

Commit 0d8db6e

Browse files
committed
fix: should not replace literal + with in the query string (#943)
1 parent d783eed commit 0d8db6e

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

src/compose.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ export const composeHandler = ({
692692
}`
693693
} else {
694694
fnLiteral += `if(c.qi !== -1) {
695-
let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1))
695+
let url = '&' + decodeURIComponent(c.url.slice(c.qi + 1).replaceAll('+', ' '))
696696
697697
${destructured
698698
.map(
@@ -726,8 +726,8 @@ export const composeHandler = ({
726726
727727
let temp
728728
729-
if(memory === -1) temp = url.slice(start).replace(/\\+|%20/g, ' ')
730-
else temp = url.slice(start, memory).replace(/\\+|%20/g, ' ')
729+
if(memory === -1) temp = url.slice(start)
730+
else temp = url.slice(start, memory)
731731
732732
const charCode = temp.charCodeAt(0)
733733
if(charCode !== 91 && charCode !== 123)
@@ -755,10 +755,10 @@ export const composeHandler = ({
755755
a${index} = []
756756
757757
if(memory === -1) {
758-
a${index}.push(url.slice(start).replace(/\\+|%20/g, ' '))
758+
a${index}.push(url.slice(start))
759759
break
760760
}
761-
else a${index}.push(url.slice(start, memory).replace(/\\+|%20/g, ' '))
761+
else a${index}.push(url.slice(start, memory))
762762
763763
memory = url.indexOf('&${key}=', memory)
764764
if(memory === -1) break
@@ -772,8 +772,8 @@ export const composeHandler = ({
772772
const start = memory + ${key.length + 2}
773773
memory = url.indexOf('&', start)
774774
775-
if(memory === -1) a${index} = url.slice(start).replace(/\\+|%20/g, ' ')
776-
else a${index} = url.slice(start, memory).replace(/\\+|%20/g, ' ')
775+
if(memory === -1) a${index} = url.slice(start)
776+
else a${index} = url.slice(start, memory)
777777
778778
if (a${index} !== undefined) {
779779
try {
@@ -790,9 +790,9 @@ export const composeHandler = ({
790790
const start = memory + ${key.length + 2}
791791
memory = url.indexOf('&', start)
792792
793-
if(memory === -1) a${index} = url.slice(start).replace(/\\+|%20/g, ' ')
793+
if(memory === -1) a${index} = url.slice(start)
794794
else {
795-
a${index} = url.slice(start, memory).replace(/\\+|%20/g, ' ')
795+
a${index} = url.slice(start, memory)
796796
797797
${
798798
anyOf
@@ -811,8 +811,8 @@ export const composeHandler = ({
811811
deepMemory = url.indexOf('&', start)
812812
813813
let value
814-
if(deepMemory === -1) value = url.slice(start).replace(/\\+|%20/g, ' ')
815-
else value = url.slice(start, deepMemory).replace(/\\+|%20/g, ' ')
814+
if(deepMemory === -1) value = url.slice(start)
815+
else value = url.slice(start, deepMemory)
816816
817817
const vStart = value.charCodeAt(0)
818818
const vEnd = value.charCodeAt(value.length - 1)

test/validator/query.test.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -680,23 +680,43 @@ describe('Query Validator', () => {
680680
})
681681
})
682682

683-
it('parse + in query', async () => {
683+
it('parse query with space', async () => {
684684
const api = new Elysia().get('', ({ query }) => query, {
685685
query: t.Object({
686686
keyword: t.String()
687687
})
688688
})
689689

690690
const url = new URL('http://localhost:3000/')
691-
url.searchParams.append('keyword', 'hello world')
692-
console.log(url.href) //http://localhost:3000/?keyword=hello+world
691+
url.searchParams.append('keyword', 'with space')
692+
console.log(url.href) // http://localhost:3000/?keyword=with+space
693693

694694
const result = await api
695695
.handle(new Request(url.href))
696696
.then((response) => response.json())
697697

698698
expect(result).toEqual({
699-
keyword: 'hello world'
699+
keyword: 'with space'
700+
})
701+
})
702+
703+
it('parse query with +', async () => {
704+
const api = new Elysia().get('', ({ query }) => query, {
705+
query: t.Object({
706+
keyword: t.String()
707+
})
708+
})
709+
710+
const url = new URL("http://localhost:3000/");
711+
url.searchParams.append("keyword", "with+plus");
712+
console.log(url.href) // http://localhost:3000/?keyword=with%2Bplus
713+
714+
const result = await api
715+
.handle(new Request(url.href))
716+
.then((response) => response.json())
717+
718+
expect(result).toEqual({
719+
keyword: 'with+plus'
700720
})
701721
})
702722

0 commit comments

Comments
 (0)