kwhitley / itty-router

A little router.
MIT License
1.75k stars 78 forks source link

Invalid character in route '+' #56

Closed vivaladan closed 3 years ago

vivaladan commented 3 years ago

I'm having some difficulty routing a particular URL. I'm wondering if it contains some characters that is breaking the matching?

The url I'm trying to match on is: /v1/osm-v4+cmap-nautical-v3+public-places-latest/*

I've played around a bit and if I take the plus characters out + then it seems to match, otherwise it falls through.

Would someone be kind enough to confirm what the issue is here and whether there is a workaround? This would be a great addition to the docs.

kwhitley commented 3 years ago

Hey!

So to give you an idea of what's happening, here are the steps inside itty:

  1. your route --> new regex
  2. regex is matched against the incoming URL (from the handle/request)

Due to the tiny size of itty, we handle ULTRA limited regex, and perform zero regex cleaning on the routes. What's likely happening is that "+" is left untouched, meaning when it's actually going to read as a duplicate character in part 2 when it's matched... matching in effect to: /v1/osm-v44cmap-nautical-v33public-places-latest/*

This definitely falls under edge cases, which unfortunately requires manual routing: https://github.com/kwhitley/itty-router#manual-routes

Time is short (I'm helping with the evac efforts) but I'll see if I can get you started:

import { Router } from 'itty-router'
import { json } from 'itty-router-extras'

const router = Router()

// let's define a simple request handler
const handler = request => json({ success: true })

// and manually push a route onto the internal routes collection
router.routes.push(
  [
    'GET', // method: GET
    /^\/v1\/osm-v4\+cmap-nautical-v3\+public-places-latest\/*$/,  // note escaped slashes and plus signs
    [handler1, handler2], // array of request handlers
  ]
)
vivaladan commented 3 years ago

Magical, that makes a lot more sense now. Thanks also for the code to get me started, I really appriciate that. I'll close the issue now, because it's more my lack of understanding than the router itself. However might be worth noting edge cases such as invalid characters in the readme to save others this little problem.