Files
onvif/examples/event/unsubscribe/main.go
Sebastian Norling d7cfee56a1 fix(event/stream): address round-2 review findings
Critical
  - Renew busy-loop on persistent failure: after a failed
    renewPullPoint, the loop re-read the (now in-past)
    GrantedTermination and nextRenewInterval floored to 1s, hammering
    the camera at 1 Hz until reconnect. nextRenewIntervalAfterError
    decouples the failure path from the stale grant and backs off
    at opts.RetryBackoff. renewLoop also routes through s.now() so
    test clocks can drive it deterministically.
  - Lost-update race on GrantedTermination: a renew result for an
    old subscription could overwrite the grant on a new one if
    attemptRecreate swapped pullPoint mid-flight. A generation
    counter on Stream tracks subscription rotation; the renew loop
    captures the generation before the SOAP call and discards the
    result if the subscription was rotated.
  - Credential leak when <Security> straddled the 64 KiB error cap:
    the non-greedy regex required a closing tag and missed the
    truncated case. wsseSecurityRE now matches close-tag-or-EOF.
    Belt-and-braces wssePasswordRE redacts <Password> elements
    outside any Security wrapper.
  - addHeaderChildren accepted well-formed-but-element-free input
    and produced a header-less request. Now errors out.

Important
  - Wrapper detection in buildRefParamsHeader was HasSuffix-based and
    misfired on children named *ReferenceParameters. Replaced with the
    unambiguous wrapper-only contract: input must be the full
    <*:ReferenceParameters> element returned by extractReferenceParameters.
  - Renamed SoapOption → SendSoapOption and WithHeader → WithSOAPHeader
    to disambiguate at call sites.
  - Renamed WithHeader's `xml` parameter to headerContent to avoid
    shadowing the encoding/xml package name.
  - Documented terminationTimeRE's "first match only" semantics so
    nobody re-uses it from PullMessages context where multiple
    TerminationTime elements appear.
  - goleak is now a direct require (go mod tidy).

Performance
  - Added gosoap.AddStringHeaderContents (plural) for multi-root
    header content. AddStringHeaderContent remains as-is for
    backwards compatibility with external consumers. Device.go's
    addHeaderChildren workaround is gone — one etree parse per
    SendSoapWithOptions call instead of two.

Migration
  - Device.go's own CallOnvifFunction and the three examples now
    call SendSoapWithOptions, modelling the canonical path.

Docs / tests
  - Trust-boundary warning on SendSoapWithHeader/Options godoc.
  - Comments on createPullPointResp/Alt explain why TerminationTime
    is intentionally omitted (renew timing fixtures).
  - New tests: digest retry strips WS-Security, duplicate
    WithSOAPHeader is last-wins, malformed TerminationTime yields
    zero, margin==base falls to base/2, empty SubscriptionReference
    yields empty ref params, Security straddling cap is redacted,
    bare Password redacted, wrapper-only contract is enforced.
2026-05-27 18:07:56 +02:00

76 lines
2.2 KiB
Go

package main
import (
"encoding/xml"
"github.com/kerberos-io/onvif"
"github.com/kerberos-io/onvif/event"
"io/ioutil"
"log"
)
// === Geovision ===
// Request:
// <tev:Unsubscribe />
// Response:
// <SOAP-ENV:Fault>
// <SOAP-ENV:Code>
// <SOAP-ENV:Value>SOAP-ENV:Sender</SOAP-ENV:Value>
// </SOAP-ENV:Code>
// <SOAP-ENV:Reason>
// <SOAP-ENV:Text xml:lang="en">
// Method 'tev:Unsubscribe' not implemented: method name or namespace not recognized
// </SOAP-ENV:Text>
// </SOAP-ENV:Reason>
// </SOAP-ENV:Fault>
//
// Test Summary: Geovision might not support unsubscribe
// === BOSCH ===
// Request:
// <tev:Unsubscribe />
// Response:
// <SOAP-ENV:Fault>
// <SOAP-ENV:Code><SOAP-ENV:Value>SOAP-ENV:Receiver</SOAP-ENV:Value><SOAP-ENV:Subcode><SOAP-ENV:Value>ter:Action</SOAP-ENV:Value></SOAP-ENV:Subcode></SOAP-ENV:Code>
// <SOAP-ENV:Reason><SOAP-ENV:Text xml:lang="en">Action Failed</SOAP-ENV:Text></SOAP-ENV:Reason>
// <SOAP-ENV:Node>http://www.w3.org/2003/05/soap-envelope/node/ultimateReceiver</SOAP-ENV:Node><SOAP-ENV:Role>http://www.w3.org/2003/05/soap-envelope/node/ultimateReceiver</SOAP-ENV:Role>
// </SOAP-ENV:Fault>
//
// Test Summary: BOSCH might not support unsubscribe
// === Hikvision
// Request:
// <tev:Unsubscribe />
// Response:
// <wsnt:UnsubscribeResponse/>
func main() {
dev, err := onvif.NewDevice(onvif.DeviceParams{
//Xaddr: "192.168.12.148", // BOSCH
//Xaddr: "192.168.12.149", // Geovision
Xaddr: "192.168.12.123", //Hikvision
Username: "administrator",
Password: "Password1!",
AuthMode: onvif.UsernameTokenAuth,
})
if err != nil {
log.Fatalln("fail to new device:", err)
}
unsubscribe := event.Unsubscribe{}
//endPoint:= "http://192.168.12.148/Web_Service?Idx=0" // BOSCH
//endPoint := "http://192.168.12.149:80/onvif/events" // Geovision
endPoint := "http://192.168.12.123/onvif/Events/PullSubManager_2021-12-02T06:13:45Z_0" // Hikvision
requestBody, err := xml.Marshal(unsubscribe)
if err != nil {
log.Fatalln(err)
}
res, err := dev.SendSoapWithOptions(endPoint, string(requestBody))
if err != nil {
log.Fatalln("fail to CallMethod:", err)
}
bs, _ := ioutil.ReadAll(res.Body)
log.Printf(">> Result: %+v \n %s", res.StatusCode, bs)
}