Commit c7d7f9f8 authored by Andre Blanke's avatar Andre Blanke
Browse files

Support URLs without a scheme as implied HTTP URLs

parent e7cc4216
......@@ -55,13 +55,22 @@ class UrlShorteningController @Autowired constructor(private val repository: Url
fun shortenUrl(@RequestParam url: String,
@Pattern(regexp = "[a-zA-Z0-9]+")
@RequestParam(required = false) id: String?): ResponseEntity<String> {
fun URI.withScheme(scheme: String): URI =
URI(scheme, this.userInfo, this.host, this.port, this.path, this.query, this.fragment)
/*
* We use a URI instead of a URL here because Java has the ability to open InputStreams from URLs using
* URL.openStream. Because that ability exists, URLStreamHandlers are used for certain protocols and the
* creation of a URL object will fail with a MalformedURLException if no URLStreamHandler could be found
* for the URL's protocol.
*/
val uri = URI(URLDecoder.decode(url, StandardCharsets.UTF_8))
var uri = URI(URLDecoder.decode(url, StandardCharsets.UTF_8))
/* Support URLs without a scheme as implied HTTP URLs. */
if (uri.scheme == null)
uri = uri.withScheme("http")
if (!allowedUrlProtocols.contains(uri.scheme)) {
throw ResponseStatusException(
HttpStatus.FORBIDDEN,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment