Files
onvif/examples/event/renew/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

95 lines
2.6 KiB
Go

package main
import (
"encoding/xml"
"github.com/kerberos-io/onvif"
"github.com/kerberos-io/onvif/event"
"github.com/kerberos-io/onvif/xsd"
"io/ioutil"
"log"
)
// === Geovision ===
// Request:
// <tev:PullMessages>
// <tev:Timeout>PT20S</tev:Timeout>
// <tev:MessageLimit>10</tev:MessageLimit>
// </tev:PullMessages>
// Response:
// <tev:PullMessagesResponse>
// <tev:CurrentTime>2021-12-02T02:42:30Z</tev:CurrentTime>
// <tev:TerminationTime>2021-12-02T02:42:50Z</tev:TerminationTime>
// <wsnt:NotificationMessage> ... </wsnt:NotificationMessage>
// </tev:PullMessagesResponse>
//
// Test Summary:
// 1. the TerminationTime = CurrentTime+Timeout
// 2. even current time exceed the TerminationTime, the pull point still alive
// === BOSCH ===
// Request:
// <wsnt:Renew>
// <wsnt:TerminationTime>
// 2021-12-03T15:50:03Z
// </wsnt:TerminationTime>
// </wsnt:Renew>
// Response:
// <wsnt:RenewResponse>
// <wsnt:TerminationTime>
// 2021-12-03T15:50:03Z
// </wsnt:TerminationTime>
// <wsnt:CurrentTime>
// 2021-12-02T03:36:04Z
// </wsnt:CurrentTime>
// </wsnt:RenewResponse>
//
// Test Summary:
// 1. the response's TerminationTime equal request's TerminationTime
// 2. But the subscription still live for one minute
// === Hikvision ===
// Request:
// <wsnt:Renew><wsnt:TerminationTime>2021-12-02T18:30:53Z</wsnt:TerminationTime></wsnt:Renew>
// Response:
// <wsnt:RenewResponse>
// <wsnt:TerminationTime>2021-12-02T18:30:53Z</wsnt:TerminationTime>
// <wsnt:CurrentTime>2021-12-02T06:31:57Z</wsnt:CurrentTime>
// </wsnt:RenewResponse>
//
// Test Summary:
// 1. the subscription's termination time will update if the request's TerminationTime greater than the curren time
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)
}
terminationTime := xsd.String("PT120S")
renew := event.Renew{
TerminationTime: terminationTime,
}
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:80/onvif/Events/SubManager__0" // Hikvision
requestBody, err := xml.Marshal(renew)
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)
}