-
Notifications
You must be signed in to change notification settings - Fork 44
/
response.go
111 lines (97 loc) · 2.99 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2020 Percipia
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Contributor(s):
* Andrew Querol <[email protected]>
*/
package eslgo
import (
"fmt"
"io"
"net/textproto"
"net/url"
"strconv"
"strings"
)
const (
TypeEventPlain = `text/event-plain`
TypeEventJSON = `text/event-json`
TypeEventXML = `text/event-xml`
TypeReply = `command/reply`
TypeAPIResponse = `api/response`
TypeAuthRequest = `auth/request`
TypeDisconnect = `text/disconnect-notice`
)
// RawResponse This struct contains all response data from FreeSWITCH
type RawResponse struct {
Headers textproto.MIMEHeader
Body []byte
}
func (c *Conn) readResponse() (*RawResponse, error) {
header, err := c.header.ReadMIMEHeader()
if err != nil {
return nil, err
}
response := &RawResponse{
Headers: header,
}
if contentLength := header.Get("Content-Length"); len(contentLength) > 0 {
length, err := strconv.Atoi(contentLength)
if err != nil {
return response, err
}
response.Body = make([]byte, length)
_, err = io.ReadFull(c.reader, response.Body)
if err != nil {
return response, err
}
}
return response, nil
}
// IsOk Helper to check response status, uses the Reply-Text header primarily. Calls GetReply internally
func (r RawResponse) IsOk() bool {
return strings.HasPrefix(r.GetReply(), "+OK")
}
// GetReply Helper to get the Reply text from FreeSWITCH, uses the Reply-Text header primarily.
// Also will use the body if the Reply-Text header does not exist, this can be the case for TypeAPIResponse
func (r RawResponse) GetReply() string {
if r.HasHeader("Reply-Text") {
return r.GetHeader("Reply-Text")
}
return string(r.Body)
}
// ChannelUUID Helper to get the channel UUID. Calls GetHeader internally
func (r RawResponse) ChannelUUID() string {
return r.GetHeader("Unique-ID")
}
// HasHeader Helper to check if the RawResponse has a header
func (r RawResponse) HasHeader(header string) bool {
_, ok := r.Headers[textproto.CanonicalMIMEHeaderKey(header)]
return ok
}
// GetVariable Helper function to get "Variable_" headers. Calls GetHeader internally
func (r RawResponse) GetVariable(variable string) string {
return r.GetHeader(fmt.Sprintf("Variable_%s", variable))
}
// GetHeader Helper function that calls RawResponse.Headers.Get. Result gets passed through url.PathUnescape
func (r RawResponse) GetHeader(header string) string {
value, _ := url.PathUnescape(r.Headers.Get(header))
return value
}
// String Implement the Stringer interface for pretty printing
func (r RawResponse) String() string {
var builder strings.Builder
for key, values := range r.Headers {
builder.WriteString(fmt.Sprintf("%s: %#v\n", key, values))
}
builder.Write(r.Body)
return builder.String()
}
// GoString Implement the GoStringer interface for pretty printing (%#v)
func (r RawResponse) GoString() string {
return r.String()
}