headers.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. package sip
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "github.com/ghettovoice/gosip/util"
  8. )
  9. // SIP Headers structs
  10. // Originally forked from github.com/stefankopieczek/gossip by @StefanKopieczek
  11. // with a tiny changes
  12. // Whitespace recognised by SIP protocol.
  13. const abnfWs = " \t"
  14. // Header is a single SIP header.
  15. type Header interface {
  16. // Name returns header name.
  17. Name() string
  18. Value() string
  19. // Clone returns copy of header struct.
  20. Clone() Header
  21. String() string
  22. Equals(other interface{}) bool
  23. }
  24. // A URI from any schema (e.g. sip:, tel:, callto:)
  25. type Uri interface {
  26. // Determine if the two URIs are equal according to the rules in RFC 3261 s. 19.1.4.
  27. Equals(other interface{}) bool
  28. String() string
  29. Clone() Uri
  30. IsEncrypted() bool
  31. SetEncrypted(flag bool)
  32. User() MaybeString
  33. SetUser(user MaybeString)
  34. Password() MaybeString
  35. SetPassword(pass MaybeString)
  36. Host() string
  37. SetHost(host string)
  38. Port() *Port
  39. SetPort(port *Port)
  40. UriParams() Params
  41. SetUriParams(params Params)
  42. Headers() Params
  43. SetHeaders(params Params)
  44. // Return true if and only if the URI is the special wildcard URI '*'; that is, if it is
  45. // a WildcardUri struct.
  46. IsWildcard() bool
  47. }
  48. // A URI from a schema suitable for inclusion in a Contact: header.
  49. // The only such URIs are sip/sips URIs and the special wildcard URI '*'.
  50. // hold this interface to not break other code
  51. type ContactUri interface {
  52. Uri
  53. }
  54. // Generic list of parameters on a header.
  55. type Params interface {
  56. Get(key string) (MaybeString, bool)
  57. Add(key string, val MaybeString) Params
  58. Remove(key string) Params
  59. Clone() Params
  60. Equals(params interface{}) bool
  61. ToString(sep uint8) string
  62. String() string
  63. Length() int
  64. Items() map[string]MaybeString
  65. Keys() []string
  66. Has(key string) bool
  67. }
  68. // IMPLEMENTATION
  69. // Params implementation.
  70. type headerParams struct {
  71. mu sync.RWMutex
  72. params map[string]MaybeString
  73. paramOrder []string
  74. }
  75. // Create an empty set of parameters.
  76. func NewParams() Params {
  77. return &headerParams{
  78. params: make(map[string]MaybeString),
  79. paramOrder: []string{},
  80. }
  81. }
  82. // Returns the entire parameter map.
  83. func (params *headerParams) Items() map[string]MaybeString {
  84. params.mu.RLock()
  85. defer params.mu.RUnlock()
  86. return params.params
  87. }
  88. // Returns a slice of keys, in order.
  89. func (params *headerParams) Keys() []string {
  90. params.mu.RLock()
  91. defer params.mu.RUnlock()
  92. return params.paramOrder
  93. }
  94. // Returns the requested parameter value.
  95. func (params *headerParams) Get(key string) (MaybeString, bool) {
  96. params.mu.RLock()
  97. v, ok := params.params[key]
  98. params.mu.RUnlock()
  99. return v, ok
  100. }
  101. // Put a new parameter.
  102. func (params *headerParams) Add(key string, val MaybeString) Params {
  103. params.mu.Lock()
  104. // Add param to order list if new.
  105. if _, ok := params.params[key]; !ok {
  106. params.paramOrder = append(params.paramOrder, key)
  107. }
  108. // Set param value.
  109. params.params[key] = val
  110. params.mu.Unlock()
  111. // Return the params so calls can be chained.
  112. return params
  113. }
  114. func (params *headerParams) Remove(key string) Params {
  115. params.mu.Lock()
  116. if _, ok := params.params[key]; ok {
  117. for k, v := range params.paramOrder {
  118. if v == key {
  119. params.paramOrder = append(params.paramOrder[:k], params.paramOrder[k+1:]...)
  120. break
  121. }
  122. }
  123. delete(params.params, key)
  124. }
  125. params.mu.Unlock()
  126. // Return the params so calls can be chained.
  127. return params
  128. }
  129. func (params *headerParams) Has(key string) bool {
  130. params.mu.RLock()
  131. _, ok := params.params[key]
  132. params.mu.RUnlock()
  133. return ok
  134. }
  135. // Copy a list of params.
  136. func (params *headerParams) Clone() Params {
  137. if params == nil {
  138. var dup *headerParams
  139. return dup
  140. }
  141. dup := NewParams()
  142. for _, key := range params.Keys() {
  143. if val, ok := params.Get(key); ok {
  144. dup.Add(key, val)
  145. }
  146. }
  147. return dup
  148. }
  149. // Render params to a string.
  150. // Note that this does not escape special characters, this should already have been done before calling this method.
  151. func (params *headerParams) ToString(sep uint8) string {
  152. if params == nil {
  153. return ""
  154. }
  155. var buffer bytes.Buffer
  156. first := true
  157. for _, key := range params.Keys() {
  158. val, ok := params.Get(key)
  159. if !ok {
  160. continue
  161. }
  162. if !first {
  163. buffer.WriteString(fmt.Sprintf("%c", sep))
  164. }
  165. first = false
  166. buffer.WriteString(fmt.Sprintf("%s", Escape(key, EncodeQueryComponent)))
  167. if val, ok := val.(String); ok {
  168. valStr := val.String()
  169. if valStr[0] == '"' && valStr[len(valStr)-1] == '"' { // already escaped header param value
  170. buffer.WriteString(fmt.Sprintf("=%s", valStr))
  171. } else if strings.ContainsAny(valStr, abnfWs) {
  172. buffer.WriteString(fmt.Sprintf("=\"%s\"", Escape(valStr, EncodeQueryComponent)))
  173. } else {
  174. buffer.WriteString(fmt.Sprintf("=%s", Escape(valStr, EncodeQueryComponent)))
  175. }
  176. }
  177. }
  178. return buffer.String()
  179. }
  180. // String returns params joined with '&' char.
  181. func (params *headerParams) String() string {
  182. if params == nil {
  183. return ""
  184. }
  185. return params.ToString('&')
  186. }
  187. // Returns number of params.
  188. func (params *headerParams) Length() int {
  189. params.mu.RLock()
  190. defer params.mu.RUnlock()
  191. return len(params.params)
  192. }
  193. // Check if two maps of parameters are equal in the sense of having the same keys with the same values.
  194. // This does not rely on any ordering of the keys of the map in memory.
  195. func (params *headerParams) Equals(other interface{}) bool {
  196. q, ok := other.(*headerParams)
  197. if !ok {
  198. return false
  199. }
  200. if params == q {
  201. return true
  202. }
  203. if params == nil && q != nil || params != nil && q == nil {
  204. return false
  205. }
  206. if params.Length() == 0 && q.Length() == 0 {
  207. return true
  208. }
  209. if params.Length() != q.Length() {
  210. return false
  211. }
  212. for key, pVal := range params.Items() {
  213. qVal, ok := q.Get(key)
  214. if !ok {
  215. return false
  216. }
  217. if pVal != qVal {
  218. return false
  219. }
  220. }
  221. return true
  222. }
  223. func cloneWithNil(params Params) Params {
  224. if params == nil {
  225. return NewParams()
  226. }
  227. return params.Clone()
  228. }
  229. // SipUri
  230. // A SIP or SIPS URI, including all params and URI header params.
  231. // noinspection GoNameStartsWithPackageName
  232. type SipUri struct {
  233. // True if and only if the URI is a SIPS URI.
  234. FIsEncrypted bool
  235. // The user part of the URI: the 'joe' in sip:joe@bloggs.com
  236. // This is a pointer, so that URIs without a user part can have 'nil'.
  237. FUser MaybeString
  238. // The password field of the URI. This is represented in the URI as joe:hunter2@bloggs.com.
  239. // Note that if a URI has a password field, it *must* have a user field as well.
  240. // This is a pointer, so that URIs without a password field can have 'nil'.
  241. // Note that RFC 3261 strongly recommends against the use of password fields in SIP URIs,
  242. // as they are fundamentally insecure.
  243. FPassword MaybeString
  244. // The host part of the URI. This can be a domain, or a string representation of an IP address.
  245. FHost string
  246. // The port part of the URI. This is optional, and so is represented here as a pointer type.
  247. FPort *Port
  248. // Any parameters associated with the URI.
  249. // These are used to provide information about requests that may be constructed from the URI.
  250. // (For more details, see RFC 3261 section 19.1.1).
  251. // These appear as a semicolon-separated list of key=value pairs following the host[:port] part.
  252. FUriParams Params
  253. // Any headers to be included on requests constructed from this URI.
  254. // These appear as a '&'-separated list at the end of the URI, introduced by '?'.
  255. // Although the values of the map are MaybeStrings, they will never be NoString in practice as the parser
  256. // guarantees to not return blank values for header elements in SIP URIs.
  257. // You should not set the values of headers to NoString.
  258. FHeaders Params
  259. }
  260. func (uri *SipUri) IsEncrypted() bool {
  261. return uri.FIsEncrypted
  262. }
  263. func (uri *SipUri) SetEncrypted(flag bool) {
  264. uri.FIsEncrypted = flag
  265. }
  266. func (uri *SipUri) User() MaybeString {
  267. return uri.FUser
  268. }
  269. func (uri *SipUri) SetUser(user MaybeString) {
  270. uri.FUser = user
  271. }
  272. func (uri *SipUri) Password() MaybeString {
  273. return uri.FPassword
  274. }
  275. func (uri *SipUri) SetPassword(pass MaybeString) {
  276. uri.FPassword = pass
  277. }
  278. func (uri *SipUri) Host() string {
  279. return uri.FHost
  280. }
  281. func (uri *SipUri) SetHost(host string) {
  282. uri.FHost = host
  283. }
  284. func (uri *SipUri) Port() *Port {
  285. return uri.FPort
  286. }
  287. func (uri *SipUri) SetPort(port *Port) {
  288. uri.FPort = port
  289. }
  290. func (uri *SipUri) UriParams() Params {
  291. return uri.FUriParams
  292. }
  293. func (uri *SipUri) SetUriParams(params Params) {
  294. uri.FUriParams = params
  295. }
  296. func (uri *SipUri) Headers() Params {
  297. return uri.FHeaders
  298. }
  299. func (uri *SipUri) SetHeaders(params Params) {
  300. uri.FHeaders = params
  301. }
  302. func (uri *SipUri) IsWildcard() bool {
  303. return false
  304. }
  305. // Determine if the SIP URI is equal to the specified URI according to the rules laid down in RFC 3261 s. 19.1.4.
  306. // TODO: The Equals method is not currently RFC-compliant; fix this!
  307. func (uri *SipUri) Equals(val interface{}) bool {
  308. otherPtr, ok := val.(*SipUri)
  309. if !ok {
  310. return false
  311. }
  312. if uri == otherPtr {
  313. return true
  314. }
  315. if uri == nil && otherPtr != nil || uri != nil && otherPtr == nil {
  316. return false
  317. }
  318. other := *otherPtr
  319. result := uri.FIsEncrypted == other.FIsEncrypted &&
  320. uri.FUser == other.FUser &&
  321. uri.FPassword == other.FPassword &&
  322. uri.FHost == other.FHost &&
  323. util.Uint16PtrEq((*uint16)(uri.FPort), (*uint16)(other.FPort))
  324. if !result {
  325. return false
  326. }
  327. if uri.FUriParams != otherPtr.FUriParams {
  328. if uri.FUriParams == nil {
  329. result = result && otherPtr.FUriParams != nil
  330. } else {
  331. result = result && uri.FUriParams.Equals(otherPtr.FUriParams)
  332. }
  333. }
  334. if uri.FHeaders != otherPtr.FHeaders {
  335. if uri.FHeaders == nil {
  336. result = result && otherPtr.FHeaders != nil
  337. } else {
  338. result = result && uri.FHeaders.Equals(otherPtr.FHeaders)
  339. }
  340. }
  341. return result
  342. }
  343. // Generates the string representation of a SipUri struct.
  344. func (uri *SipUri) String() string {
  345. var buffer bytes.Buffer
  346. // Compulsory protocol identifier.
  347. if uri.FIsEncrypted {
  348. buffer.WriteString("sips")
  349. buffer.WriteString(":")
  350. } else {
  351. buffer.WriteString("sip")
  352. buffer.WriteString(":")
  353. }
  354. // Optional userinfo part.
  355. if user, ok := uri.FUser.(String); ok && user.String() != "" {
  356. buffer.WriteString(Escape(uri.FUser.String(), EncodeUserPassword))
  357. if pass, ok := uri.FPassword.(String); ok && pass.String() != "" {
  358. buffer.WriteString(":")
  359. buffer.WriteString(Escape(pass.String(), EncodeUserPassword))
  360. }
  361. buffer.WriteString("@")
  362. }
  363. // Compulsory hostname.
  364. buffer.WriteString(Escape(uri.FHost, EncodeHost))
  365. // Optional port number.
  366. if uri.FPort != nil {
  367. buffer.WriteString(fmt.Sprintf(":%d", *uri.FPort))
  368. }
  369. if (uri.FUriParams != nil) && uri.FUriParams.Length() > 0 {
  370. buffer.WriteString(";")
  371. buffer.WriteString(uri.FUriParams.ToString(';'))
  372. }
  373. if (uri.FHeaders != nil) && uri.FHeaders.Length() > 0 {
  374. buffer.WriteString("?")
  375. buffer.WriteString(uri.FHeaders.ToString('&'))
  376. }
  377. return buffer.String()
  378. }
  379. // Clone the Sip URI.
  380. func (uri *SipUri) Clone() Uri {
  381. var newUri *SipUri
  382. if uri == nil {
  383. return newUri
  384. }
  385. newUri = &SipUri{
  386. FIsEncrypted: uri.FIsEncrypted,
  387. FUser: uri.FUser,
  388. FPassword: uri.FPassword,
  389. FHost: uri.FHost,
  390. FUriParams: cloneWithNil(uri.FUriParams),
  391. FHeaders: cloneWithNil(uri.FHeaders),
  392. }
  393. if uri.FPort != nil {
  394. newUri.FPort = uri.FPort.Clone()
  395. }
  396. return newUri
  397. }
  398. // The special wildcard URI used in Contact: headers in REGISTER requests when expiring all registrations.
  399. type WildcardUri struct{}
  400. func (uri WildcardUri) IsEncrypted() bool { return false }
  401. func (uri WildcardUri) SetEncrypted(flag bool) {}
  402. func (uri WildcardUri) User() MaybeString { return nil }
  403. func (uri WildcardUri) SetUser(user MaybeString) {}
  404. func (uri WildcardUri) Password() MaybeString { return nil }
  405. func (uri WildcardUri) SetPassword(pass MaybeString) {}
  406. func (uri WildcardUri) Host() string { return "" }
  407. func (uri WildcardUri) SetHost(host string) {}
  408. func (uri WildcardUri) Port() *Port { return nil }
  409. func (uri WildcardUri) SetPort(port *Port) {}
  410. func (uri WildcardUri) UriParams() Params { return nil }
  411. func (uri WildcardUri) SetUriParams(params Params) {}
  412. func (uri WildcardUri) Headers() Params { return nil }
  413. func (uri WildcardUri) SetHeaders(params Params) {}
  414. // Copy the wildcard URI. Not hard!
  415. func (uri WildcardUri) Clone() Uri { return &WildcardUri{} }
  416. // Always returns 'true'.
  417. func (uri WildcardUri) IsWildcard() bool {
  418. return true
  419. }
  420. // Always returns '*' - the representation of a wildcard URI in a SIP message.
  421. func (uri WildcardUri) String() string {
  422. return "*"
  423. }
  424. // Determines if this wildcard URI equals the specified other URI.
  425. // This is true if and only if the other URI is also a wildcard URI.
  426. func (uri WildcardUri) Equals(other interface{}) bool {
  427. switch other.(type) {
  428. case WildcardUri:
  429. return true
  430. default:
  431. return false
  432. }
  433. }
  434. // Encapsulates a header that gossip does not natively support.
  435. // This allows header data that is not understood to be parsed by gossip and relayed to the parent application.
  436. type GenericHeader struct {
  437. // The name of the header.
  438. HeaderName string
  439. // The contents of the header, including any parameters.
  440. // This is transparent data that is not natively understood by gossip.
  441. Contents string
  442. }
  443. // Convert the header to a flat string representation.
  444. func (header *GenericHeader) String() string {
  445. return header.HeaderName + ": " + header.Contents
  446. }
  447. // Pull out the header name.
  448. func (header *GenericHeader) Name() string {
  449. return header.HeaderName
  450. }
  451. func (header *GenericHeader) Value() string {
  452. return header.Contents
  453. }
  454. // Copy the header.
  455. func (header *GenericHeader) Clone() Header {
  456. if header == nil {
  457. var newHeader *GenericHeader
  458. return newHeader
  459. }
  460. return &GenericHeader{
  461. HeaderName: header.HeaderName,
  462. Contents: header.Contents,
  463. }
  464. }
  465. func (header *GenericHeader) Equals(other interface{}) bool {
  466. if h, ok := other.(*GenericHeader); ok {
  467. if header == h {
  468. return true
  469. }
  470. if header == nil && h != nil || header != nil && h == nil {
  471. return false
  472. }
  473. return header.HeaderName == h.HeaderName &&
  474. header.Contents == h.Contents
  475. }
  476. return false
  477. }
  478. // ToHeader introduces SIP 'To' header
  479. type ToHeader struct {
  480. // The display name from the header, may be omitted.
  481. DisplayName MaybeString
  482. Address Uri
  483. // Any parameters present in the header.
  484. Params Params
  485. }
  486. func (to *ToHeader) String() string {
  487. return fmt.Sprintf("%s: %s", to.Name(), to.Value())
  488. }
  489. func (to *ToHeader) Name() string { return "To" }
  490. func (to *ToHeader) Value() string {
  491. var buffer bytes.Buffer
  492. if displayName, ok := to.DisplayName.(String); ok && displayName.String() != "" {
  493. buffer.WriteString(fmt.Sprintf("\"%s\" ", displayName))
  494. }
  495. buffer.WriteString(fmt.Sprintf("<%s>", to.Address))
  496. if to.Params != nil && to.Params.Length() > 0 {
  497. buffer.WriteString(";")
  498. buffer.WriteString(to.Params.ToString(';'))
  499. }
  500. return buffer.String()
  501. }
  502. // Copy the header.
  503. func (to *ToHeader) Clone() Header {
  504. var newTo *ToHeader
  505. if to == nil {
  506. return newTo
  507. }
  508. newTo = &ToHeader{
  509. DisplayName: to.DisplayName,
  510. }
  511. if to.Address != nil {
  512. newTo.Address = to.Address.Clone()
  513. }
  514. if to.Params != nil {
  515. newTo.Params = to.Params.Clone()
  516. }
  517. return newTo
  518. }
  519. func (to *ToHeader) Equals(other interface{}) bool {
  520. if h, ok := other.(*ToHeader); ok {
  521. if to == h {
  522. return true
  523. }
  524. if to == nil && h != nil || to != nil && h == nil {
  525. return false
  526. }
  527. res := true
  528. if to.DisplayName != h.DisplayName {
  529. if to.DisplayName == nil {
  530. res = res && h.DisplayName == nil
  531. } else {
  532. res = res && to.DisplayName.Equals(h.DisplayName)
  533. }
  534. }
  535. if to.Address != h.Address {
  536. if to.Address == nil {
  537. res = res && h.Address == nil
  538. } else {
  539. res = res && to.Address.Equals(h.Address)
  540. }
  541. }
  542. if to.Params != h.Params {
  543. if to.Params == nil {
  544. res = res && h.Params == nil
  545. } else {
  546. res = res && to.Params.Equals(h.Params)
  547. }
  548. }
  549. return res
  550. }
  551. return false
  552. }
  553. type FromHeader struct {
  554. // The display name from the header, may be omitted.
  555. DisplayName MaybeString
  556. Address Uri
  557. // Any parameters present in the header.
  558. Params Params
  559. }
  560. func (from *FromHeader) String() string {
  561. return fmt.Sprintf("%s: %s", from.Name(), from.Value())
  562. }
  563. func (from *FromHeader) Name() string { return "From" }
  564. func (from *FromHeader) Value() string {
  565. var buffer bytes.Buffer
  566. if displayName, ok := from.DisplayName.(String); ok && displayName.String() != "" {
  567. buffer.WriteString(fmt.Sprintf("\"%s\" ", displayName))
  568. }
  569. buffer.WriteString(fmt.Sprintf("<%s>", from.Address))
  570. if from.Params != nil && from.Params.Length() > 0 {
  571. buffer.WriteString(";")
  572. buffer.WriteString(from.Params.ToString(';'))
  573. }
  574. return buffer.String()
  575. }
  576. // Copy the header.
  577. func (from *FromHeader) Clone() Header {
  578. var newFrom *FromHeader
  579. if from == nil {
  580. return newFrom
  581. }
  582. newFrom = &FromHeader{
  583. DisplayName: from.DisplayName,
  584. }
  585. if from.Address != nil {
  586. newFrom.Address = from.Address.Clone()
  587. }
  588. if from.Params != nil {
  589. newFrom.Params = from.Params.Clone()
  590. }
  591. return newFrom
  592. }
  593. func (from *FromHeader) Equals(other interface{}) bool {
  594. if h, ok := other.(*FromHeader); ok {
  595. if from == h {
  596. return true
  597. }
  598. if from == nil && h != nil || from != nil && h == nil {
  599. return false
  600. }
  601. res := true
  602. if from.DisplayName != h.DisplayName {
  603. if from.DisplayName == nil {
  604. res = res && h.DisplayName == nil
  605. } else {
  606. res = res && from.DisplayName.Equals(h.DisplayName)
  607. }
  608. }
  609. if from.Address != h.Address {
  610. if from.Address == nil {
  611. res = res && h.Address == nil
  612. } else {
  613. res = res && from.Address.Equals(h.Address)
  614. }
  615. }
  616. if from.Params != h.Params {
  617. if from.Params == nil {
  618. res = res && h.Params == nil
  619. } else {
  620. res = res && from.Params.Equals(h.Params)
  621. }
  622. }
  623. return res
  624. }
  625. return false
  626. }
  627. type ContactHeader struct {
  628. // The display name from the header, may be omitted.
  629. DisplayName MaybeString
  630. Address ContactUri
  631. // Any parameters present in the header.
  632. Params Params
  633. }
  634. func (contact *ContactHeader) String() string {
  635. return fmt.Sprintf("%s: %s", contact.Name(), contact.Value())
  636. }
  637. func (contact *ContactHeader) Name() string { return "Contact" }
  638. func (contact *ContactHeader) Value() string {
  639. var buffer bytes.Buffer
  640. if displayName, ok := contact.DisplayName.(String); ok && displayName.String() != "" {
  641. buffer.WriteString(fmt.Sprintf("\"%s\" ", displayName))
  642. }
  643. switch contact.Address.(type) {
  644. case *WildcardUri:
  645. // Treat the Wildcard URI separately as it must not be contained in < > angle brackets.
  646. buffer.WriteString("*")
  647. default:
  648. buffer.WriteString(fmt.Sprintf("<%s>", contact.Address.String()))
  649. }
  650. if (contact.Params != nil) && (contact.Params.Length() > 0) {
  651. buffer.WriteString(";")
  652. buffer.WriteString(contact.Params.ToString(';'))
  653. }
  654. return buffer.String()
  655. }
  656. // Copy the header.
  657. func (contact *ContactHeader) Clone() Header {
  658. var newCnt *ContactHeader
  659. if contact == nil {
  660. return newCnt
  661. }
  662. newCnt = &ContactHeader{
  663. DisplayName: contact.DisplayName,
  664. }
  665. if contact.Address != nil {
  666. newCnt.Address = contact.Address.Clone()
  667. }
  668. if contact.Params != nil {
  669. newCnt.Params = contact.Params.Clone()
  670. }
  671. return newCnt
  672. }
  673. func (contact *ContactHeader) Equals(other interface{}) bool {
  674. if h, ok := other.(*ContactHeader); ok {
  675. if contact == h {
  676. return true
  677. }
  678. if contact == nil && h != nil || contact != nil && h == nil {
  679. return false
  680. }
  681. res := true
  682. if contact.DisplayName != h.DisplayName {
  683. if contact.DisplayName == nil {
  684. res = res && h.DisplayName == nil
  685. } else {
  686. res = res && contact.DisplayName.Equals(h.DisplayName)
  687. }
  688. }
  689. if contact.Address != h.Address {
  690. if contact.Address == nil {
  691. res = res && h.Address == nil
  692. } else {
  693. res = res && contact.Address.Equals(h.Address)
  694. }
  695. }
  696. if contact.Params != h.Params {
  697. if contact.Params == nil {
  698. res = res && h.Params == nil
  699. } else {
  700. res = res && contact.Params.Equals(h.Params)
  701. }
  702. }
  703. return res
  704. }
  705. return false
  706. }
  707. // CallID - 'Call-ID' header.
  708. type CallID string
  709. func (callId CallID) String() string {
  710. return fmt.Sprintf("%s: %s", callId.Name(), callId.Value())
  711. }
  712. func (callId *CallID) Name() string { return "Call-ID" }
  713. func (callId CallID) Value() string { return string(callId) }
  714. func (callId *CallID) Clone() Header {
  715. return callId
  716. }
  717. func (callId *CallID) Equals(other interface{}) bool {
  718. if h, ok := other.(CallID); ok {
  719. if callId == nil {
  720. return false
  721. }
  722. return *callId == h
  723. }
  724. if h, ok := other.(*CallID); ok {
  725. if callId == h {
  726. return true
  727. }
  728. if callId == nil && h != nil || callId != nil && h == nil {
  729. return false
  730. }
  731. return *callId == *h
  732. }
  733. return false
  734. }
  735. type CSeq struct {
  736. SeqNo uint32
  737. MethodName RequestMethod
  738. }
  739. func (cseq *CSeq) String() string {
  740. return fmt.Sprintf("%s: %s", cseq.Name(), cseq.Value())
  741. }
  742. func (cseq *CSeq) Name() string { return "CSeq" }
  743. func (cseq *CSeq) Value() string {
  744. return fmt.Sprintf("%d %s", cseq.SeqNo, cseq.MethodName)
  745. }
  746. func (cseq *CSeq) Clone() Header {
  747. if cseq == nil {
  748. var newCSeq *CSeq
  749. return newCSeq
  750. }
  751. return &CSeq{
  752. SeqNo: cseq.SeqNo,
  753. MethodName: cseq.MethodName,
  754. }
  755. }
  756. func (cseq *CSeq) Equals(other interface{}) bool {
  757. if h, ok := other.(*CSeq); ok {
  758. if cseq == h {
  759. return true
  760. }
  761. if cseq == nil && h != nil || cseq != nil && h == nil {
  762. return false
  763. }
  764. return cseq.SeqNo == h.SeqNo &&
  765. cseq.MethodName == h.MethodName
  766. }
  767. return false
  768. }
  769. type MaxForwards uint32
  770. func (maxForwards MaxForwards) String() string {
  771. return fmt.Sprintf("%s: %s", maxForwards.Name(), maxForwards.Value())
  772. }
  773. func (maxForwards *MaxForwards) Name() string { return "Max-Forwards" }
  774. func (maxForwards MaxForwards) Value() string { return fmt.Sprintf("%d", maxForwards) }
  775. func (maxForwards *MaxForwards) Clone() Header { return maxForwards }
  776. func (maxForwards *MaxForwards) Equals(other interface{}) bool {
  777. if h, ok := other.(MaxForwards); ok {
  778. if maxForwards == nil {
  779. return false
  780. }
  781. return *maxForwards == h
  782. }
  783. if h, ok := other.(*MaxForwards); ok {
  784. if maxForwards == h {
  785. return true
  786. }
  787. if maxForwards == nil && h != nil || maxForwards != nil && h == nil {
  788. return false
  789. }
  790. return *maxForwards == *h
  791. }
  792. return false
  793. }
  794. type Expires uint32
  795. func (expires *Expires) String() string {
  796. return fmt.Sprintf("%s: %s", expires.Name(), expires.Value())
  797. }
  798. func (expires *Expires) Name() string { return "Expires" }
  799. func (expires Expires) Value() string { return fmt.Sprintf("%d", expires) }
  800. func (expires *Expires) Clone() Header { return expires }
  801. func (expires *Expires) Equals(other interface{}) bool {
  802. if h, ok := other.(Expires); ok {
  803. if expires == nil {
  804. return false
  805. }
  806. return *expires == h
  807. }
  808. if h, ok := other.(*Expires); ok {
  809. if expires == h {
  810. return true
  811. }
  812. if expires == nil && h != nil || expires != nil && h == nil {
  813. return false
  814. }
  815. return *expires == *h
  816. }
  817. return false
  818. }
  819. type ContentLength uint32
  820. func (contentLength ContentLength) String() string {
  821. return fmt.Sprintf("%s: %s", contentLength.Name(), contentLength.Value())
  822. }
  823. func (contentLength *ContentLength) Name() string { return "Content-Length" }
  824. func (contentLength ContentLength) Value() string { return fmt.Sprintf("%d", contentLength) }
  825. func (contentLength *ContentLength) Clone() Header { return contentLength }
  826. func (contentLength *ContentLength) Equals(other interface{}) bool {
  827. if h, ok := other.(ContentLength); ok {
  828. if contentLength == nil {
  829. return false
  830. }
  831. return *contentLength == h
  832. }
  833. if h, ok := other.(*ContentLength); ok {
  834. if contentLength == h {
  835. return true
  836. }
  837. if contentLength == nil && h != nil || contentLength != nil && h == nil {
  838. return false
  839. }
  840. return *contentLength == *h
  841. }
  842. return false
  843. }
  844. type ViaHeader []*ViaHop
  845. func (via ViaHeader) String() string {
  846. return fmt.Sprintf("%s: %s", via.Name(), via.Value())
  847. }
  848. func (via ViaHeader) Name() string { return "Via" }
  849. func (via ViaHeader) Value() string {
  850. var buffer bytes.Buffer
  851. for idx, hop := range via {
  852. buffer.WriteString(hop.String())
  853. if idx != len(via)-1 {
  854. buffer.WriteString(", ")
  855. }
  856. }
  857. return buffer.String()
  858. }
  859. func (via ViaHeader) Clone() Header {
  860. if via == nil {
  861. var newVie ViaHeader
  862. return newVie
  863. }
  864. dup := make([]*ViaHop, 0, len(via))
  865. for _, hop := range via {
  866. dup = append(dup, hop.Clone())
  867. }
  868. return ViaHeader(dup)
  869. }
  870. func (via ViaHeader) Equals(other interface{}) bool {
  871. if h, ok := other.(ViaHeader); ok {
  872. if len(via) != len(h) {
  873. return false
  874. }
  875. for i, hop := range via {
  876. if !hop.Equals(h[i]) {
  877. return false
  878. }
  879. }
  880. return true
  881. }
  882. return false
  883. }
  884. // A single component in a Via header.
  885. // Via headers are composed of several segments of the same structure, added by successive nodes in a routing chain.
  886. type ViaHop struct {
  887. // E.g. 'SIP'.
  888. ProtocolName string
  889. // E.g. '2.0'.
  890. ProtocolVersion string
  891. Transport string
  892. Host string
  893. // The port for this via hop. This is stored as a pointer type, since it is an optional field.
  894. Port *Port
  895. Params Params
  896. }
  897. func (hop *ViaHop) SentBy() string {
  898. var buf bytes.Buffer
  899. buf.WriteString(hop.Host)
  900. if hop.Port != nil {
  901. buf.WriteString(fmt.Sprintf(":%d", *hop.Port))
  902. }
  903. return buf.String()
  904. }
  905. func (hop *ViaHop) String() string {
  906. var buffer bytes.Buffer
  907. buffer.WriteString(
  908. fmt.Sprintf(
  909. "%s/%s/%s %s",
  910. hop.ProtocolName,
  911. hop.ProtocolVersion,
  912. hop.Transport,
  913. hop.Host,
  914. ),
  915. )
  916. if hop.Port != nil {
  917. buffer.WriteString(fmt.Sprintf(":%d", *hop.Port))
  918. }
  919. if hop.Params != nil && hop.Params.Length() > 0 {
  920. buffer.WriteString(";")
  921. buffer.WriteString(hop.Params.ToString(';'))
  922. }
  923. return buffer.String()
  924. }
  925. // Return an exact copy of this ViaHop.
  926. func (hop *ViaHop) Clone() *ViaHop {
  927. var newHop *ViaHop
  928. if hop == nil {
  929. return newHop
  930. }
  931. newHop = &ViaHop{
  932. ProtocolName: hop.ProtocolName,
  933. ProtocolVersion: hop.ProtocolVersion,
  934. Transport: hop.Transport,
  935. Host: hop.Host,
  936. }
  937. if hop.Port != nil {
  938. newHop.Port = hop.Port.Clone()
  939. }
  940. if hop.Params != nil {
  941. newHop.Params = hop.Params.Clone()
  942. }
  943. return newHop
  944. }
  945. func (hop *ViaHop) Equals(other interface{}) bool {
  946. if h, ok := other.(*ViaHop); ok {
  947. if hop == h {
  948. return true
  949. }
  950. if hop == nil && h != nil || hop != nil && h == nil {
  951. return false
  952. }
  953. res := hop.ProtocolName == h.ProtocolName &&
  954. hop.ProtocolVersion == h.ProtocolVersion &&
  955. hop.Transport == h.Transport &&
  956. hop.Host == h.Host &&
  957. util.Uint16PtrEq((*uint16)(hop.Port), (*uint16)(h.Port))
  958. if hop.Params != h.Params {
  959. if hop.Params == nil {
  960. res = res && h.Params == nil
  961. } else {
  962. res = res && hop.Params.Equals(h.Params)
  963. }
  964. }
  965. return res
  966. }
  967. return false
  968. }
  969. type RequireHeader struct {
  970. Options []string
  971. }
  972. func (require *RequireHeader) String() string {
  973. return fmt.Sprintf("%s: %s", require.Name(), require.Value())
  974. }
  975. func (require *RequireHeader) Name() string { return "Require" }
  976. func (require *RequireHeader) Value() string {
  977. return strings.Join(require.Options, ", ")
  978. }
  979. func (require *RequireHeader) Clone() Header {
  980. if require == nil {
  981. var newRequire *RequireHeader
  982. return newRequire
  983. }
  984. dup := make([]string, len(require.Options))
  985. copy(dup, require.Options)
  986. return &RequireHeader{dup}
  987. }
  988. func (require *RequireHeader) Equals(other interface{}) bool {
  989. if h, ok := other.(*RequireHeader); ok {
  990. if require == h {
  991. return true
  992. }
  993. if require == nil && h != nil || require != nil && h == nil {
  994. return false
  995. }
  996. if len(require.Options) != len(h.Options) {
  997. return false
  998. }
  999. for i, opt := range require.Options {
  1000. if opt != h.Options[i] {
  1001. return false
  1002. }
  1003. }
  1004. return true
  1005. }
  1006. return false
  1007. }
  1008. type SupportedHeader struct {
  1009. Options []string
  1010. }
  1011. func (support *SupportedHeader) String() string {
  1012. return fmt.Sprintf("%s: %s", support.Name(), support.Value())
  1013. }
  1014. func (support *SupportedHeader) Name() string { return "Supported" }
  1015. func (support *SupportedHeader) Value() string {
  1016. return strings.Join(support.Options, ", ")
  1017. }
  1018. func (support *SupportedHeader) Clone() Header {
  1019. if support == nil {
  1020. var newSupport *SupportedHeader
  1021. return newSupport
  1022. }
  1023. dup := make([]string, len(support.Options))
  1024. copy(dup, support.Options)
  1025. return &SupportedHeader{dup}
  1026. }
  1027. func (support *SupportedHeader) Equals(other interface{}) bool {
  1028. if h, ok := other.(*SupportedHeader); ok {
  1029. if support == h {
  1030. return true
  1031. }
  1032. if support == nil && h != nil || support != nil && h == nil {
  1033. return false
  1034. }
  1035. if len(support.Options) != len(h.Options) {
  1036. return false
  1037. }
  1038. for i, opt := range support.Options {
  1039. if opt != h.Options[i] {
  1040. return false
  1041. }
  1042. }
  1043. return true
  1044. }
  1045. return false
  1046. }
  1047. type ProxyRequireHeader struct {
  1048. Options []string
  1049. }
  1050. func (proxyRequire *ProxyRequireHeader) String() string {
  1051. return fmt.Sprintf("%s: %s", proxyRequire.Name(), proxyRequire.Value())
  1052. }
  1053. func (proxyRequire *ProxyRequireHeader) Name() string { return "Proxy-Require" }
  1054. func (proxyRequire *ProxyRequireHeader) Value() string {
  1055. return strings.Join(proxyRequire.Options, ", ")
  1056. }
  1057. func (proxyRequire *ProxyRequireHeader) Clone() Header {
  1058. if proxyRequire == nil {
  1059. var newProxy *ProxyRequireHeader
  1060. return newProxy
  1061. }
  1062. dup := make([]string, len(proxyRequire.Options))
  1063. copy(dup, proxyRequire.Options)
  1064. return &ProxyRequireHeader{dup}
  1065. }
  1066. func (proxyRequire *ProxyRequireHeader) Equals(other interface{}) bool {
  1067. if h, ok := other.(*ProxyRequireHeader); ok {
  1068. if proxyRequire == h {
  1069. return true
  1070. }
  1071. if proxyRequire == nil && h != nil || proxyRequire != nil && h == nil {
  1072. return false
  1073. }
  1074. if len(proxyRequire.Options) != len(h.Options) {
  1075. return false
  1076. }
  1077. for i, opt := range proxyRequire.Options {
  1078. if opt != h.Options[i] {
  1079. return false
  1080. }
  1081. }
  1082. return true
  1083. }
  1084. return false
  1085. }
  1086. // 'Unsupported:' is a SIP header type - this doesn't indicate that the
  1087. // header itself is not supported by gossip!
  1088. type UnsupportedHeader struct {
  1089. Options []string
  1090. }
  1091. func (unsupported *UnsupportedHeader) String() string {
  1092. return fmt.Sprintf("%s: %s", unsupported.Name(), unsupported.Value())
  1093. }
  1094. func (unsupported *UnsupportedHeader) Name() string { return "Unsupported" }
  1095. func (unsupported *UnsupportedHeader) Value() string {
  1096. return strings.Join(unsupported.Options, ", ")
  1097. }
  1098. func (unsupported *UnsupportedHeader) Clone() Header {
  1099. if unsupported == nil {
  1100. var newUnsup *UnsupportedHeader
  1101. return newUnsup
  1102. }
  1103. dup := make([]string, len(unsupported.Options))
  1104. copy(dup, unsupported.Options)
  1105. return &UnsupportedHeader{dup}
  1106. }
  1107. func (unsupported *UnsupportedHeader) Equals(other interface{}) bool {
  1108. if h, ok := other.(*UnsupportedHeader); ok {
  1109. if unsupported == h {
  1110. return true
  1111. }
  1112. if unsupported == nil && h != nil || unsupported != nil && h == nil {
  1113. return false
  1114. }
  1115. if len(unsupported.Options) != len(h.Options) {
  1116. return false
  1117. }
  1118. for i, opt := range unsupported.Options {
  1119. if opt != h.Options[i] {
  1120. return false
  1121. }
  1122. }
  1123. return true
  1124. }
  1125. return false
  1126. }
  1127. type UserAgentHeader string
  1128. func (ua *UserAgentHeader) String() string {
  1129. return fmt.Sprintf("%s: %s", ua.Name(), ua.Value())
  1130. }
  1131. func (ua *UserAgentHeader) Name() string { return "User-Agent" }
  1132. func (ua UserAgentHeader) Value() string { return string(ua) }
  1133. func (ua *UserAgentHeader) Clone() Header { return ua }
  1134. func (ua *UserAgentHeader) Equals(other interface{}) bool {
  1135. if h, ok := other.(UserAgentHeader); ok {
  1136. if ua == nil {
  1137. return false
  1138. }
  1139. return *ua == h
  1140. }
  1141. if h, ok := other.(*UserAgentHeader); ok {
  1142. if ua == h {
  1143. return true
  1144. }
  1145. if ua == nil && h != nil || ua != nil && h == nil {
  1146. return false
  1147. }
  1148. return *ua == *h
  1149. }
  1150. return false
  1151. }
  1152. type AllowHeader []RequestMethod
  1153. func (allow AllowHeader) String() string {
  1154. return fmt.Sprintf("%s: %s", allow.Name(), allow.Value())
  1155. }
  1156. func (allow AllowHeader) Name() string { return "Allow" }
  1157. func (allow AllowHeader) Value() string {
  1158. parts := make([]string, 0)
  1159. for _, method := range allow {
  1160. parts = append(parts, string(method))
  1161. }
  1162. return strings.Join(parts, ", ")
  1163. }
  1164. func (allow AllowHeader) Clone() Header {
  1165. if allow == nil {
  1166. var newAllow AllowHeader
  1167. return newAllow
  1168. }
  1169. newAllow := make(AllowHeader, len(allow))
  1170. copy(newAllow, allow)
  1171. return newAllow
  1172. }
  1173. func (allow AllowHeader) Equals(other interface{}) bool {
  1174. if h, ok := other.(AllowHeader); ok {
  1175. if len(allow) != len(h) {
  1176. return false
  1177. }
  1178. for i, v := range allow {
  1179. if v != h[i] {
  1180. return false
  1181. }
  1182. }
  1183. return true
  1184. }
  1185. return false
  1186. }
  1187. type ContentType string
  1188. func (ct *ContentType) String() string { return fmt.Sprintf("%s: %s", ct.Name(), ct.Value()) }
  1189. func (ct *ContentType) Name() string { return "Content-Type" }
  1190. func (ct ContentType) Value() string { return string(ct) }
  1191. func (ct *ContentType) Clone() Header { return ct }
  1192. func (ct *ContentType) Equals(other interface{}) bool {
  1193. if h, ok := other.(ContentType); ok {
  1194. if ct == nil {
  1195. return false
  1196. }
  1197. return *ct == h
  1198. }
  1199. if h, ok := other.(*ContentType); ok {
  1200. if ct == h {
  1201. return true
  1202. }
  1203. if ct == nil && h != nil || ct != nil && h == nil {
  1204. return false
  1205. }
  1206. return *ct == *h
  1207. }
  1208. return false
  1209. }
  1210. type Accept string
  1211. func (ct *Accept) String() string { return fmt.Sprintf("%s: %s", ct.Name(), ct.Value()) }
  1212. func (ct *Accept) Name() string { return "Accept" }
  1213. func (ct Accept) Value() string { return string(ct) }
  1214. func (ct *Accept) Clone() Header { return ct }
  1215. func (ct *Accept) Equals(other interface{}) bool {
  1216. if h, ok := other.(Accept); ok {
  1217. if ct == nil {
  1218. return false
  1219. }
  1220. return *ct == h
  1221. }
  1222. if h, ok := other.(*Accept); ok {
  1223. if ct == h {
  1224. return true
  1225. }
  1226. if ct == nil && h != nil || ct != nil && h == nil {
  1227. return false
  1228. }
  1229. return *ct == *h
  1230. }
  1231. return false
  1232. }
  1233. type RouteHeader struct {
  1234. Addresses []Uri
  1235. }
  1236. func (route *RouteHeader) Name() string { return "Route" }
  1237. func (route *RouteHeader) Value() string {
  1238. var addrs []string
  1239. for _, uri := range route.Addresses {
  1240. addrs = append(addrs, "<"+uri.String()+">")
  1241. }
  1242. return strings.Join(addrs, ", ")
  1243. }
  1244. func (route *RouteHeader) String() string {
  1245. return fmt.Sprintf("%s: %s", route.Name(), route.Value())
  1246. }
  1247. func (route *RouteHeader) Clone() Header {
  1248. var newRoute *RouteHeader
  1249. if route == nil {
  1250. return newRoute
  1251. }
  1252. newRoute = &RouteHeader{
  1253. Addresses: make([]Uri, len(route.Addresses)),
  1254. }
  1255. for i, uri := range route.Addresses {
  1256. newRoute.Addresses[i] = uri.Clone()
  1257. }
  1258. return newRoute
  1259. }
  1260. func (route *RouteHeader) Equals(other interface{}) bool {
  1261. if h, ok := other.(*RouteHeader); ok {
  1262. if route == h {
  1263. return true
  1264. }
  1265. if route == nil && h != nil || route != nil && h == nil {
  1266. return false
  1267. }
  1268. for i, uri := range route.Addresses {
  1269. if !uri.Equals(h.Addresses[i]) {
  1270. return false
  1271. }
  1272. }
  1273. return true
  1274. }
  1275. return false
  1276. }
  1277. type RecordRouteHeader struct {
  1278. Addresses []Uri
  1279. }
  1280. func (route *RecordRouteHeader) Name() string { return "Record-Route" }
  1281. func (route *RecordRouteHeader) Value() string {
  1282. var addrs []string
  1283. for _, uri := range route.Addresses {
  1284. addrs = append(addrs, "<"+uri.String()+">")
  1285. }
  1286. return strings.Join(addrs, ", ")
  1287. }
  1288. func (route *RecordRouteHeader) String() string {
  1289. return fmt.Sprintf("%s: %s", route.Name(), route.Value())
  1290. }
  1291. func (route *RecordRouteHeader) Clone() Header {
  1292. var newRoute *RecordRouteHeader
  1293. if route == nil {
  1294. return newRoute
  1295. }
  1296. newRoute = &RecordRouteHeader{
  1297. Addresses: make([]Uri, len(route.Addresses)),
  1298. }
  1299. for i, uri := range route.Addresses {
  1300. newRoute.Addresses[i] = uri.Clone()
  1301. }
  1302. return newRoute
  1303. }
  1304. func (route *RecordRouteHeader) Equals(other interface{}) bool {
  1305. if h, ok := other.(*RecordRouteHeader); ok {
  1306. if route == h {
  1307. return true
  1308. }
  1309. if route == nil && h != nil || route != nil && h == nil {
  1310. return false
  1311. }
  1312. for i, uri := range route.Addresses {
  1313. if !uri.Equals(h.Addresses[i]) {
  1314. return false
  1315. }
  1316. }
  1317. return true
  1318. }
  1319. return false
  1320. }