Improved parameter validation for internet.connect, closes #533.

This commit is contained in:
Florian Nücke 2014-09-04 16:47:47 +02:00
parent 9ae5fbc512
commit 4f2b942f73

View File

@ -68,7 +68,7 @@ class InternetCard extends component.ManagedComponent {
if (connections.size >= Settings.get.maxConnections) { if (connections.size >= Settings.get.maxConnections) {
throw new IOException("too many open connections") throw new IOException("too many open connections")
} }
val uri = checkUri(address) val uri = checkUri(address, port)
val handle = newHandle() val handle = newHandle()
connections += handle -> new InternetCard.Socket(uri, port) connections += handle -> new InternetCard.Socket(uri, port)
result(handle) result(handle)
@ -173,10 +173,10 @@ class InternetCard extends component.ManagedComponent {
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //
private def checkUri(address: String): URI = { private def checkUri(address: String, port: Int): URI = {
try { try {
val parsed = new URI(address) val parsed = new URI(address)
if (parsed.getHost != null && parsed.getPort != -1) { if (parsed.getHost != null && (parsed.getPort > 0 || port > 0)) {
return parsed return parsed
} }
} }
@ -185,11 +185,14 @@ class InternetCard extends component.ManagedComponent {
} }
val simple = new URI("oc://" + address) val simple = new URI("oc://" + address)
if (simple.getHost != null && simple.getPort != -1) { if (simple.getHost != null) {
return simple if (simple.getPort > 0)
return simple
else if (port > 0)
return new URI(simple.toString + ":" + port)
} }
throw new IllegalArgumentException("address could not be parsed") throw new IllegalArgumentException("address could not be parsed or no valid port given")
} }
private def checkAddress(address: String) = { private def checkAddress(address: String) = {