Skip to content

Commit

Permalink
Better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Sep 28, 2022
1 parent 250be8b commit 1a47268
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 280 deletions.
6 changes: 3 additions & 3 deletions parser/parse_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (

func parseContentLength(headerName string, headerText string) (
header sip.Header, err error) {
var contentLength sip.ContentLength
var contentLength sip.ContentLengthHeader
var value uint64
value, err = strconv.ParseUint(strings.TrimSpace(headerText), 10, 32)
contentLength = sip.ContentLength(value)
contentLength = sip.ContentLengthHeader(value)
return &contentLength, err
}

func parseContentType(headerName string, headerText string) (headers sip.Header, err error) {
// var contentType sip.ContentType
headerText = strings.TrimSpace(headerText)
contentType := sip.ContentType(headerText)
contentType := sip.ContentTypeHeader(headerText)
return &contentType, nil
}
2 changes: 1 addition & 1 deletion parser/parse_cseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Parse a string representation of a CSeq header, returning a slice of at most one CSeq.
func parseCSeq(headerName string, headerText string) (
headers sip.Header, err error) {
var cseq sip.CSeq
var cseq sip.CSeqHeader
ind := strings.IndexAny(headerText, abnfWs)
if ind < 1 || len(headerText)-ind < 2 {
err = fmt.Errorf(
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func parseCallId(headerName string, headerText string) (
return
}

var callId = sip.CallID(headerText)
var callId = sip.CallIDHeader(headerText)

return &callId, nil
}
2 changes: 1 addition & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestParseHeaders(t *testing.T) {
"Contact: sip:[email protected]:5060": "Contact: <sip:[email protected]:5060>",
"Contact: SIPP <sip:[email protected]:5060>": "Contact: \"SIPP\" <sip:[email protected]:5060>",
"Contact: <sip:127.0.0.2:5060;transport=UDP>": "Contact: <sip:127.0.0.2:5060;transport=UDP>",
"m: <sip:[email protected]:50267;transport=TCP;ob>;reg-id=1;+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000eb83488d>\"": "Contact: <sip:[email protected]:50267;transport=TCP;ob>;reg-id=1;+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000eb83488d>\"",
// "m: <sip:[email protected]:50267;transport=TCP;ob>;reg-id=1;+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000eb83488d>\"": "Contact: <sip:[email protected]:50267;transport=TCP;ob>;reg-id=1;+sip.instance=\"<urn:uuid:00000000-0000-0000-0000-0000eb83488d>\"",
} {
h, err := parser.ParseHeader(header)
require.Nil(t, err)
Expand Down
8 changes: 7 additions & 1 deletion sip/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
DialogStateEnded
)

// DialogStateString maps state to string
func DialogStateString(state int) string {
switch state {
case DialogStateEstablished:
Expand All @@ -24,11 +25,16 @@ func DialogStateString(state int) string {
}
}

// Dialog is data structure represanting dialog
type Dialog struct {
ID string
// ID created by FROM tag, TO tag and Callid
ID string
// State of dialog. Check more for DialogState... constants
State int
}

// StateString returns string version of state
// established, confirmed, ended
func (d *Dialog) StateString() string {
return DialogStateString(d.State)
}
161 changes: 9 additions & 152 deletions sip/header_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,158 +27,16 @@ type HeaderKV struct {
K string
V string
}
type HeaderParamsOrdered []HeaderKV

// Create an empty set of parameters.
func NewOrderedParams() HeaderParamsOrdered {
return HeaderParamsOrdered{}
}

// Returns the entire parameter map.
func (hp HeaderParamsOrdered) Items() map[string]string {
m := make(map[string]string, len(hp))
for _, v := range hp {
m[v.K] = v.V
}
return m
}

// Returns a slice of keys, in order.
func (hp HeaderParamsOrdered) Keys() []string {
s := make([]string, len(hp))
for i, v := range hp {
s[i] = v.V
}
return s
}

// Returns the requested parameter value.
func (hp HeaderParamsOrdered) Get(key string) (string, bool) {
for _, v := range hp {
if v.K == key {
return v.V, true
}
}
return "", false
}

// Put a new parameter.
func (hp HeaderParamsOrdered) Add(key string, val string) Params {
hp = append(hp, HeaderKV{key, val})
return hp
}

func (hp HeaderParamsOrdered) Remove(key string) Params {
for i, v := range hp {
if v.K == key {
hp = append(hp[:i], hp[i+1:]...)
}
}
return hp
}

func (hp HeaderParamsOrdered) Has(key string) bool {
for _, v := range hp {
if v.K == key {
return true
}
}

return false
}

// Copy a list of params.
func (hp HeaderParamsOrdered) Clone() Params {
dup := make(HeaderParamsOrdered, len(hp))

for _, v := range hp {
dup.Add(v.K, v.V)
}

return dup
}

// Render params to a string.
// Note that this does not escape special characters, this should already have been done before calling this method.
func (hp HeaderParamsOrdered) ToString(sep uint8) string {
if hp == nil || len(hp) == 0 {
return ""
}

sepstr := string(sep)
var buffer strings.Builder

for _, v := range hp {
buffer.WriteString(sepstr)
buffer.WriteString(v.K)
val := v.V
// This could be removed
if strings.ContainsAny(val, abnfWs) {
buffer.WriteString("=\"")
buffer.WriteString(val)
buffer.WriteString("\"")
} else {
buffer.WriteString("=")
buffer.WriteString(val)
}
}

return buffer.String()[1:]
}

func (hp HeaderParamsOrdered) ToStringWrite(sep uint8, buffer io.StringWriter) {

}

// String returns params joined with '&' char.
func (hp HeaderParamsOrdered) String() string {
return hp.ToString('&')
}

// Returns number of params.
func (hp HeaderParamsOrdered) Length() int {
return len(hp)
}

// Check if two maps of parameters are equal in the sense of having the same keys with the same values.
// This does not rely on any ordering of the keys of the map in memory.
func (hp HeaderParamsOrdered) Equals(other interface{}) bool {
q, ok := other.(HeaderParamsOrdered)
if !ok {
return false
}

hplen := hp.Length()
qlen := q.Length()
if hplen != qlen {
return false
}

if hplen == 0 && qlen == 0 {
return true
}

for key, pVal := range hp.Items() {
qVal, ok := q.Get(key)
if !ok {
return false
}
if pVal != qVal {
return false
}
}

return true
}

// HeaderParams are key value params. They do not provide order by default due to performance reasons
type HeaderParams map[string]string

// Create an empty set of parameters.
func NewParams() HeaderParams {
return HeaderParams{}
}

// Returns the entire parameter map.
// Items returns the entire parameter map.
func (hp HeaderParams) Items() map[string]string {
m := make(map[string]string, len(hp))
for k, v := range hp {
Expand All @@ -187,23 +45,21 @@ func (hp HeaderParams) Items() map[string]string {
return m
}

// Returns a slice of keys, in order.
// Keys return a slice of keys, in order.
func (hp HeaderParams) Keys() []string {
s := make([]string, len(hp))
i := 0
for k, _ := range hp {
for k := range hp {
s[i] = k
}
return s
}

// Returns the requested parameter value.
func (hp HeaderParams) Get(key string) (string, bool) {
v, ok := hp[key]
return v, ok
}

// Put a new parameter.
func (hp HeaderParams) Add(key string, val string) Params {
hp[key] = val
return hp
Expand All @@ -219,7 +75,7 @@ func (hp HeaderParams) Has(key string) bool {
return exists
}

// Copy a list of params.
// Clone returns underneath map copied
func (hp HeaderParams) Clone() Params {
return hp.clone()
}
Expand All @@ -234,7 +90,7 @@ func (hp HeaderParams) clone() HeaderParams {
return dup
}

// Render params to a string.
// ToString renders params to a string.
// Note that this does not escape special characters, this should already have been done before calling this method.
func (hp HeaderParams) ToString(sep uint8) string {
if hp == nil || len(hp) == 0 {
Expand Down Expand Up @@ -263,6 +119,7 @@ func (hp HeaderParams) ToString(sep uint8) string {
return buffer.String()[1:]
}

// ToStringWrite is same as ToString but it stores to defined buffer instead returning string
func (hp HeaderParams) ToStringWrite(sep uint8, buffer io.StringWriter) {
if hp == nil || len(hp) == 0 {
return
Expand Down Expand Up @@ -298,12 +155,12 @@ func (hp HeaderParams) String() string {
return hp.ToString('&')
}

// Returns number of params.
// Length returns number of params.
func (hp HeaderParams) Length() int {
return len(hp)
}

// Check if two maps of parameters are equal in the sense of having the same keys with the same values.
// Equals check if two maps of parameters are equal in the sense of having the same keys with the same values.
// This does not rely on any ordering of the keys of the map in memory.
func (hp HeaderParams) Equals(other interface{}) bool {
q, ok := other.(HeaderParams)
Expand Down
8 changes: 0 additions & 8 deletions sip/header_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ func BenchmarkHeaderParams(b *testing.B) {
// }
// })

// Our version must be faster than GOSIP
b.Run("SLICE", func(b *testing.B) {
for i := 0; i < b.N; i++ {
hp := Params(NewOrderedParams())
testParams(b, hp)
}
})

// Our version must be faster than GOSIP
b.Run("MAP", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down
Loading

0 comments on commit 1a47268

Please sign in to comment.