Properly handle absolute URLs when parsing UPnP service control URLs

Fixes #1187
This commit is contained in:
Caleb Callaway 2015-01-07 21:23:20 -08:00
parent 7d39d1a925
commit af96f7a0cd
2 changed files with 42 additions and 11 deletions

View File

@ -448,6 +448,13 @@ func getIGDServices(rootURL string, device upnpDevice, wanDeviceURN string, wanC
}
func replaceRawPath(u *url.URL, rp string) {
asURL, err := url.Parse(rp)
if err != nil {
return
} else if asURL.IsAbs() {
u.Path = asURL.Path
u.RawQuery = asURL.RawQuery
} else {
var p, q string
fs := strings.Split(rp, "?")
p = fs[0]
@ -462,6 +469,7 @@ func replaceRawPath(u *url.URL, rp string) {
}
u.RawQuery = q
}
}
func soapRequest(url, service, function, message string) ([]byte, error) {
tpl := `<?xml version="1.0" ?>

View File

@ -17,6 +17,7 @@ package upnp
import (
"encoding/xml"
"net/url"
"testing"
)
@ -40,3 +41,25 @@ func TestExternalIPParsing(t *testing.T) {
t.Error("Parse of SOAP request failed.")
}
}
func TestControlURLParsing(t *testing.T) {
rootURL := "http://192.168.243.1:80/igd.xml"
u, _ := url.Parse(rootURL)
subject := "/upnp?control=WANCommonIFC1"
expected := "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
replaceRawPath(u, subject)
if u.String() != expected {
t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
}
u, _ = url.Parse(rootURL)
subject = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
expected = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
replaceRawPath(u, subject)
if u.String() != expected {
t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
}
}