Skip to content

Commit a5eea65

Browse files
committed
#57: Not support Unicode name
1 parent 3fa0a22 commit a5eea65

3 files changed

Lines changed: 117 additions & 2 deletions

File tree

src/web_service.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"database/sql"
2222
"errors"
23+
"net/url"
2324
"strings"
2425
"time"
2526

@@ -201,6 +202,18 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
201202

202203
isListResultSet := body.ResultFormat != nil && strings.EqualFold(*body.ResultFormat, "list")
203204

205+
// Fix for Issue #57: URL-decode the database ID parameter.
206+
// The databaseId parameter is the URL-encoded form passed from the route registration
207+
// (e.g., "%E6%95%B0%E6%8D%AE%E5%BA%93"). We decode it to get the human-readable ID
208+
// (e.g., "数据库") to look it up in the dbs map which stores human-readable IDs.
209+
// The parameter comes in URL-encoded (e.g., "%E6%95%B0%E6%8D%AE%E5%BA%93")
210+
// and must be decoded to match the human-readable ID stored in the
211+
// dbs map (e.g., "数据库").
212+
databaseId, err := url.PathUnescape(databaseId)
213+
if err != nil {
214+
return newWSErrorf(-1, fiber.StatusBadRequest, "invalid URL path encoding: %s", err.Error())
215+
}
216+
204217
db, found := dbs[databaseId]
205218
if !found {
206219
return newWSErrorf(-1, fiber.StatusNotFound, "database with ID '%s' not found", databaseId)

src/ws4sqlite.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"database/sql"
2222
"fmt"
23+
"net/url"
2324
"os"
2425
"strings"
2526
"sync"
@@ -309,10 +310,16 @@ func launch(cfg config, disableKeepAlive4Tests bool) {
309310

310311
handlers = append(handlers, handler(db.Id))
311312

312-
app.Post(fmt.Sprintf("/%s", db.Id), handlers...)
313+
// Fix for Issue #57: Support Unicode database names in HTTP routes
314+
// URL-encode the database ID for route registration to handle Unicode characters.
315+
// When clients send requests with Unicode (e.g., "数据库"), browsers/curl will
316+
// URL-encode it (e.g., "%E6%95%B0%E6%8D%AE%E5%BA%93"). We must register routes
317+
// with the encoded form so Fiber can match incoming requests.
318+
encodedId := url.PathEscape(db.Id)
319+
app.Post(fmt.Sprintf("/%s", encodedId), handlers...)
313320

314321
if db.CORSOrigin != "" {
315-
app.Options(fmt.Sprintf("/%s", db.Id), handlers...)
322+
app.Options(fmt.Sprintf("/%s", encodedId), handlers...)
316323
}
317324
}
318325

src/ws4sqlite_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,3 +1564,98 @@ func TestFileServerWithOverlap(t *testing.T) {
15641564

15651565
Shutdown()
15661566
}
1567+
1568+
func TestUnicodeDatabaseName(t *testing.T) {
1569+
// Test for Issue #57: Unicode database names should work properly
1570+
os.Remove("../test/数据库.db")
1571+
1572+
cfg := config{
1573+
Bindhost: "0.0.0.0",
1574+
Port: 12321,
1575+
Databases: []db{
1576+
{
1577+
Id: "数据库",
1578+
Path: "../test/数据库.db",
1579+
},
1580+
},
1581+
}
1582+
go launch(cfg, true)
1583+
1584+
time.Sleep(time.Second)
1585+
1586+
if !fileExists("../test/数据库.db") {
1587+
t.Error("Unicode db file not created")
1588+
return
1589+
}
1590+
1591+
// Create a table
1592+
req := request{
1593+
Transaction: []requestItem{
1594+
{
1595+
Statement: "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)",
1596+
},
1597+
},
1598+
}
1599+
1600+
code, _, res := call("数据库", req, t)
1601+
1602+
if code != 200 {
1603+
t.Error("Create table failed with code", code)
1604+
return
1605+
}
1606+
1607+
if !res.Results[0].Success {
1608+
t.Error("Create table did not succeed")
1609+
return
1610+
}
1611+
1612+
// Insert data
1613+
req = request{
1614+
Transaction: []requestItem{
1615+
{
1616+
Statement: "INSERT INTO test (name) VALUES ('test data')",
1617+
},
1618+
},
1619+
}
1620+
1621+
code, _, res = call("数据库", req, t)
1622+
1623+
if code != 200 {
1624+
t.Error("Insert failed with code", code)
1625+
return
1626+
}
1627+
1628+
if !res.Results[0].Success {
1629+
t.Error("Insert did not succeed")
1630+
return
1631+
}
1632+
1633+
// Query data
1634+
req = request{
1635+
Transaction: []requestItem{
1636+
{
1637+
Query: "SELECT * FROM test",
1638+
},
1639+
},
1640+
}
1641+
1642+
code, _, res = call("数据库", req, t)
1643+
1644+
if code != 200 {
1645+
t.Error("Query failed with code", code)
1646+
return
1647+
}
1648+
1649+
if !res.Results[0].Success {
1650+
t.Error("Query did not succeed")
1651+
return
1652+
}
1653+
1654+
if len(res.Results[0].ResultSet) != 1 {
1655+
t.Error("Expected 1 row, got", len(res.Results[0].ResultSet))
1656+
return
1657+
}
1658+
1659+
Shutdown()
1660+
os.Remove("../test/数据库.db")
1661+
}

0 commit comments

Comments
 (0)