-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathrequest.go
139 lines (113 loc) · 2.99 KB
/
request.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package canopus
import (
"strconv"
"strings"
)
// Creates a New Request Instance
func NewRequest(messageType uint8, messageMethod CoapCode) Request {
return NewRequestWithMessageId(messageType, messageMethod, GenerateMessageID())
}
func NewRequestWithMessageId(messageType uint8, messageMethod CoapCode, messageID uint16) Request {
msg := NewMessage(messageType, messageMethod, messageID)
return &CoapRequest{
msg: msg,
}
}
func NewConfirmableGetRequest() Request {
return &CoapRequest{
msg: NewMessage(MessageConfirmable, Get, GenerateMessageID()),
}
}
func NewConfirmablePostRequest() Request {
return &CoapRequest{
msg: NewMessage(MessageConfirmable, Post, GenerateMessageID()),
}
}
func NewConfirmablePutRequest() Request {
return &CoapRequest{
msg: NewMessage(MessageConfirmable, Put, GenerateMessageID()),
}
}
func NewConfirmableDeleteRequest() Request {
return &CoapRequest{
msg: NewMessage(MessageConfirmable, Delete, GenerateMessageID()),
}
}
// Creates a new request messages from a CoAP Message
func NewRequestFromMessage(msg Message) Request {
return &CoapRequest{
msg: msg,
}
}
func NewClientRequestFromMessage(msg Message, attrs map[string]string, session Session) Request {
return &CoapRequest{
msg: msg,
attrs: attrs,
session: session,
}
}
// Wraps a CoAP Message as a Request
// Provides various methods which proxies the Message object methods
type CoapRequest struct {
msg Message
attrs map[string]string
session Session
server *CoapServer
}
func (c *CoapRequest) SetProxyURI(uri string) {
c.msg.AddOption(OptionProxyURI, uri)
}
func (c *CoapRequest) SetMediaType(mt MediaType) {
c.msg.AddOption(OptionContentFormat, mt)
}
func (c *CoapRequest) GetSession() Session {
return c.session
}
func (c *CoapRequest) GetAttributes() map[string]string {
return c.attrs
}
func (c *CoapRequest) GetAttribute(o string) string {
return c.attrs[o]
}
func (c *CoapRequest) GetAttributeAsInt(o string) int {
attr := c.GetAttribute(o)
i, _ := strconv.Atoi(attr)
return i
}
func (c *CoapRequest) GetMessage() Message {
return c.msg
}
func (c *CoapRequest) SetStringPayload(s string) {
c.msg.(*CoapMessage).SetPayload(NewPlainTextPayload(s))
}
func (c *CoapRequest) SetPayload(b []byte) {
c.msg.(*CoapMessage).SetPayload(NewBytesPayload(b))
}
func (c *CoapRequest) SetRequestURI(uri string) {
c.msg.AddOptions(NewPathOptions(uri))
}
func (c *CoapRequest) SetConfirmable(con bool) {
if con {
c.msg.(*CoapMessage).SetMessageType(MessageConfirmable)
} else {
c.msg.(*CoapMessage).SetMessageType(MessageNonConfirmable)
}
}
func (c *CoapRequest) SetToken(t string) {
c.msg.(*CoapMessage).SetToken([]byte(t))
}
func (c *CoapRequest) GetURIQuery(q string) string {
qs := c.GetMessage().GetOptionsAsString(OptionURIQuery)
for _, o := range qs {
ps := strings.Split(o, "=")
if len(ps) == 2 {
if ps[0] == q {
return ps[1]
}
}
}
return ""
}
func (c *CoapRequest) SetURIQuery(k string, v string) {
c.GetMessage().AddOption(OptionURIQuery, k+"="+v)
}