0

I'm working on an application that does some basic network management. Due to constraints outside of the scope of this application, there's a need to encode a cidr block (eg 127.0.0.1/32) into the url. For instance, an endpoint route might look something like this post "/home/network/:user/:cidr". In most cases, one could easily send the cidr as a string in the payload but that is not an option here.

I know that . is a valid url character and that / can be encoded with %2F. Naively combining these and attempting to post against that endpoint like so: /home/network/janedoe/127.0.0.1%2F32 404ed which makes me think Sinatra isn't able to properly parse that url parameter. I've also tried using the URI module to encode the cidr string with no luck so far.

Perhaps there's an easier way to think about encoding the cidr block into the url that makes for easy extraction in the endpoint.

RangerRanger
  • 2,455
  • 2
  • 16
  • 36
  • Do you need to properly encode it, or could you just substitute it instead to something like `-`? Could you accept the `/` and just treat this as a pair of parameters? – tadman May 30 '19 at 15:43
  • @tadman I have a strong preference to learn of a way to properly encode it but if all else fails then it would be an option to sub and reconstitute a valid cidr in the endpoint. – RangerRanger May 30 '19 at 15:49
  • There's no proper way other than `%2F`, but that's also the ugly way. Often you need to work around limitations in URI encoding by using substitution to keep the presentation clean. – tadman May 30 '19 at 15:53
  • "Does Ruby or Sinatra have any tooling to assist here?" Ew. Don't ask us to find or recommend tools or code as that's off-topic. Instead show us where you looked and why that didn't help. As for the rest, you can use URL encoding or convert the IP to an integer (as long as it's not IPv6 which would be a bigger ewwwww.) I'd recommend showing code demonstrating what you tried and explain why it didn't work. We can write tutorials or examples but that's not the SO way. – the Tin Man May 30 '19 at 21:59
  • URI/URL encoding can help, but if you don't need to send the IP/CIDR in clear text then I'd recommend using Base64 to encode. Strip the resulting trailing newline, send it, then decode it on the far end. You don't have to re-add the trailing newline for it to work. – the Tin Man May 30 '19 at 22:09
  • 1
    Base64 still needs to be url encoded. https://stackoverflow.com/a/1374794/3784008 – anothermh May 31 '19 at 00:38

0 Answers0