builder.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package sip
  2. import (
  3. "fmt"
  4. "github.com/ghettovoice/gosip/util"
  5. )
  6. type RequestBuilder struct {
  7. protocol string
  8. protocolVersion string
  9. transport string
  10. host string
  11. method RequestMethod
  12. cseq *CSeq
  13. recipient Uri
  14. body string
  15. callID *CallID
  16. via ViaHeader
  17. from *FromHeader
  18. to *ToHeader
  19. contact *ContactHeader
  20. expires *Expires
  21. userAgent *UserAgentHeader
  22. maxForwards *MaxForwards
  23. supported *SupportedHeader
  24. require *RequireHeader
  25. allow AllowHeader
  26. contentType *ContentType
  27. accept *Accept
  28. route *RouteHeader
  29. generic map[string]Header
  30. }
  31. func NewRequestBuilder() *RequestBuilder {
  32. callID := CallID(util.RandString(32))
  33. maxForwards := MaxForwards(70)
  34. userAgent := UserAgentHeader("GoSIP")
  35. rb := &RequestBuilder{
  36. protocol: "SIP",
  37. protocolVersion: "2.0",
  38. transport: "UDP",
  39. host: "localhost",
  40. cseq: &CSeq{SeqNo: 1},
  41. body: "",
  42. via: make(ViaHeader, 0),
  43. callID: &callID,
  44. userAgent: &userAgent,
  45. maxForwards: &maxForwards,
  46. generic: make(map[string]Header),
  47. }
  48. return rb
  49. }
  50. func (rb *RequestBuilder) SetTransport(transport string) *RequestBuilder {
  51. if transport == "" {
  52. rb.transport = "UDP"
  53. } else {
  54. rb.transport = transport
  55. }
  56. return rb
  57. }
  58. func (rb *RequestBuilder) SetHost(host string) *RequestBuilder {
  59. if host == "" {
  60. rb.host = "localhost"
  61. } else {
  62. rb.host = host
  63. }
  64. return rb
  65. }
  66. func (rb *RequestBuilder) SetMethod(method RequestMethod) *RequestBuilder {
  67. rb.method = method
  68. rb.cseq.MethodName = method
  69. return rb
  70. }
  71. func (rb *RequestBuilder) SetSeqNo(seqNo uint) *RequestBuilder {
  72. rb.cseq.SeqNo = uint32(seqNo)
  73. return rb
  74. }
  75. func (rb *RequestBuilder) SetRecipient(uri Uri) *RequestBuilder {
  76. rb.recipient = uri.Clone()
  77. return rb
  78. }
  79. func (rb *RequestBuilder) SetBody(body string) *RequestBuilder {
  80. rb.body = body
  81. return rb
  82. }
  83. func (rb *RequestBuilder) SetCallID(callID *CallID) *RequestBuilder {
  84. if callID != nil {
  85. rb.callID = callID
  86. }
  87. return rb
  88. }
  89. func (rb *RequestBuilder) AddVia(via *ViaHop) *RequestBuilder {
  90. if via.ProtocolName == "" {
  91. via.ProtocolName = rb.protocol
  92. }
  93. if via.ProtocolVersion == "" {
  94. via.ProtocolVersion = rb.protocolVersion
  95. }
  96. if via.Transport == "" {
  97. via.Transport = rb.transport
  98. }
  99. if via.Host == "" {
  100. via.Host = rb.host
  101. }
  102. if via.Params == nil {
  103. via.Params = NewParams()
  104. }
  105. rb.via = append(rb.via, via)
  106. return rb
  107. }
  108. func (rb *RequestBuilder) SetFrom(address *Address) *RequestBuilder {
  109. if address == nil {
  110. rb.from = nil
  111. } else {
  112. address = address.Clone()
  113. if address.Uri.Host() == "" {
  114. address.Uri.SetHost(rb.host)
  115. }
  116. rb.from = &FromHeader{
  117. DisplayName: address.DisplayName,
  118. Address: address.Uri,
  119. Params: address.Params,
  120. }
  121. }
  122. return rb
  123. }
  124. func (rb *RequestBuilder) SetTo(address *Address) *RequestBuilder {
  125. if address == nil {
  126. rb.to = nil
  127. } else {
  128. address = address.Clone()
  129. if address.Uri.Host() == "" {
  130. address.Uri.SetHost(rb.host)
  131. }
  132. rb.to = &ToHeader{
  133. DisplayName: address.DisplayName,
  134. Address: address.Uri,
  135. Params: address.Params,
  136. }
  137. }
  138. return rb
  139. }
  140. func (rb *RequestBuilder) SetContact(address *Address) *RequestBuilder {
  141. if address == nil {
  142. rb.contact = nil
  143. } else {
  144. address = address.Clone()
  145. if address.Uri.Host() == "" {
  146. address.Uri.SetHost(rb.host)
  147. }
  148. rb.contact = &ContactHeader{
  149. DisplayName: address.DisplayName,
  150. Address: address.Uri,
  151. Params: address.Params,
  152. }
  153. }
  154. return rb
  155. }
  156. func (rb *RequestBuilder) SetExpires(expires *Expires) *RequestBuilder {
  157. rb.expires = expires
  158. return rb
  159. }
  160. func (rb *RequestBuilder) SetUserAgent(userAgent *UserAgentHeader) *RequestBuilder {
  161. rb.userAgent = userAgent
  162. return rb
  163. }
  164. func (rb *RequestBuilder) SetMaxForwards(maxForwards *MaxForwards) *RequestBuilder {
  165. rb.maxForwards = maxForwards
  166. return rb
  167. }
  168. func (rb *RequestBuilder) SetAllow(methods []RequestMethod) *RequestBuilder {
  169. rb.allow = methods
  170. return rb
  171. }
  172. func (rb *RequestBuilder) SetSupported(options []string) *RequestBuilder {
  173. if len(options) == 0 {
  174. rb.supported = nil
  175. } else {
  176. rb.supported = &SupportedHeader{
  177. Options: options,
  178. }
  179. }
  180. return rb
  181. }
  182. func (rb *RequestBuilder) SetRequire(options []string) *RequestBuilder {
  183. if len(options) == 0 {
  184. rb.require = nil
  185. } else {
  186. rb.require = &RequireHeader{
  187. Options: options,
  188. }
  189. }
  190. return rb
  191. }
  192. func (rb *RequestBuilder) SetContentType(contentType *ContentType) *RequestBuilder {
  193. rb.contentType = contentType
  194. return rb
  195. }
  196. func (rb *RequestBuilder) SetAccept(accept *Accept) *RequestBuilder {
  197. rb.accept = accept
  198. return rb
  199. }
  200. func (rb *RequestBuilder) SetRoutes(routes []Uri) *RequestBuilder {
  201. if len(routes) == 0 {
  202. rb.route = nil
  203. } else {
  204. rb.route = &RouteHeader{
  205. Addresses: routes,
  206. }
  207. }
  208. return rb
  209. }
  210. func (rb *RequestBuilder) AddHeader(header Header) *RequestBuilder {
  211. rb.generic[header.Name()] = header
  212. return rb
  213. }
  214. func (rb *RequestBuilder) RemoveHeader(headerName string) *RequestBuilder {
  215. if _, ok := rb.generic[headerName]; ok {
  216. delete(rb.generic, headerName)
  217. }
  218. return rb
  219. }
  220. func (rb *RequestBuilder) Build() (Request, error) {
  221. if rb.method == "" {
  222. return nil, fmt.Errorf("undefined method name")
  223. }
  224. if rb.recipient == nil {
  225. return nil, fmt.Errorf("empty recipient")
  226. }
  227. if rb.from == nil {
  228. return nil, fmt.Errorf("empty 'From' header")
  229. }
  230. if rb.to == nil {
  231. return nil, fmt.Errorf("empty 'From' header")
  232. }
  233. hdrs := make([]Header, 0)
  234. if rb.route != nil {
  235. hdrs = append(hdrs, rb.route)
  236. }
  237. if len(rb.via) != 0 {
  238. via := make(ViaHeader, 0)
  239. for _, viaHop := range rb.via {
  240. via = append(via, viaHop)
  241. }
  242. hdrs = append(hdrs, via)
  243. }
  244. hdrs = append(hdrs, rb.cseq, rb.from, rb.to, rb.callID)
  245. if rb.contact != nil {
  246. hdrs = append(hdrs, rb.contact)
  247. }
  248. if rb.maxForwards != nil {
  249. hdrs = append(hdrs, rb.maxForwards)
  250. }
  251. if rb.expires != nil {
  252. hdrs = append(hdrs, rb.expires)
  253. }
  254. if rb.supported != nil {
  255. hdrs = append(hdrs, rb.supported)
  256. }
  257. if rb.allow != nil {
  258. hdrs = append(hdrs, rb.allow)
  259. }
  260. if rb.contentType != nil {
  261. hdrs = append(hdrs, rb.contentType)
  262. }
  263. if rb.accept != nil {
  264. hdrs = append(hdrs, rb.accept)
  265. }
  266. if rb.userAgent != nil {
  267. hdrs = append(hdrs, rb.userAgent)
  268. }
  269. for _, header := range rb.generic {
  270. hdrs = append(hdrs, header)
  271. }
  272. sipVersion := rb.protocol + "/" + rb.protocolVersion
  273. // basic request
  274. req := NewRequest("", rb.method, rb.recipient, sipVersion, hdrs, "", nil)
  275. req.SetBody(rb.body, true)
  276. return req, nil
  277. }