-
Notifications
You must be signed in to change notification settings - Fork 3
/
coap.go
151 lines (136 loc) · 5.98 KB
/
coap.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
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2021 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lb
import (
"bytes"
"net/http"
"github.com/matrix-org/go-coap/v2/message"
"github.com/matrix-org/go-coap/v2/message/codes"
coapmux "github.com/matrix-org/go-coap/v2/mux"
)
const ctxValAccessToken = "ctxValAccessToken"
// The CoAP Option ID corresponding to the access_token for Matrix requests
var OptionIDAccessToken = message.OptionID(256)
var methodCodes = map[codes.Code]string{
codes.POST: "POST",
codes.PUT: "PUT",
codes.GET: "GET",
codes.DELETE: "DELETE",
}
var methodToCodes = map[string]codes.Code{}
func init() {
for k, v := range methodCodes {
methodToCodes[v] = k
}
for k, v := range statusCodes {
responseCodes[v] = k
}
for k, v := range contentTypeToContentFormat {
contentFormatToContentType[v] = k
}
}
// https://tools.ietf.org/html/rfc8075#section-7
//
// +-------------------------------+----------------------------+------+
// | CoAP Response Code | HTTP Status Code | Note |
// +-------------------------------+----------------------------+------+
// | 2.01 Created | 201 Created | 1 |
// | 2.02 Deleted | 200 OK | 2 |
// | | 204 No Content | 2 |
// | 2.03 Valid | 304 Not Modified | 3 |
// | | 200 OK | 4 |
// | 2.04 Changed | 200 OK | 2 |
// | | 204 No Content | 2 |
// | 2.05 Content | 200 OK | |
// | 2.31 Continue | N/A | 10 |
// | 4.00 Bad Request | 400 Bad Request | |
// | 4.01 Unauthorized | 403 Forbidden | 5 |
// | 4.02 Bad Option | 400 Bad Request | 6 |
// | | 500 Internal Server Error | 6 |
// | 4.03 Forbidden | 403 Forbidden | |
// | 4.04 Not Found | 404 Not Found | |
// | 4.05 Method Not Allowed | 400 Bad Request | 7 |
// | | 405 Method Not Allowed | 7 |
// | 4.06 Not Acceptable | 406 Not Acceptable | |
// | 4.08 Request Entity Incomplt. | N/A | 10 |
// | 4.12 Precondition Failed | 412 Precondition Failed | |
// | 4.13 Request Ent. Too Large | 413 Payload Too Large | 11 |
// | 4.15 Unsupported Content-Fmt. | 415 Unsupported Media Type | |
// | 5.00 Internal Server Error | 500 Internal Server Error | |
// | 5.01 Not Implemented | 501 Not Implemented | |
// | 5.02 Bad Gateway | 502 Bad Gateway | |
// | 5.03 Service Unavailable | 503 Service Unavailable | 8 |
// | 5.04 Gateway Timeout | 504 Gateway Timeout | |
// | 5.05 Proxying Not Supported | 502 Bad Gateway | 9 |
// +-------------------------------+----------------------------+------+
//
// Table 2: CoAP-HTTP Response Code Mappings
var statusCodes = map[int]codes.Code{
http.StatusOK: codes.Content, // 200
http.StatusBadRequest: codes.BadRequest, // 400
http.StatusUnauthorized: codes.Unauthorized, // 401
http.StatusForbidden: codes.Forbidden, // 403
http.StatusNotFound: codes.NotFound, // 404
http.StatusMethodNotAllowed: codes.MethodNotAllowed, // 405
http.StatusRequestEntityTooLarge: codes.RequestEntityTooLarge, // 413
http.StatusInternalServerError: codes.InternalServerError, // 500
http.StatusBadGateway: codes.BadGateway, // 502
http.StatusGatewayTimeout: codes.GatewayTimeout, // 504
}
var responseCodes = map[codes.Code]int{}
var contentTypeToContentFormat = map[string]message.MediaType{
"application/json": message.AppJSON,
"application/cbor": message.AppCBOR,
"application/octet-stream": message.AppOctets,
"text/plain": message.TextPlain,
}
var contentFormatToContentType = map[message.MediaType]string{}
// coapResponseWriter is a http.ResponseWriter which actually writes CoAP instead (lossy)
type coapResponseWriter struct {
coapmux.ResponseWriter
headers http.Header
body *bytes.Reader
logger Logger
statusCode int
}
func (w *coapResponseWriter) Header() http.Header {
return w.headers
}
func (w *coapResponseWriter) log(format string, v ...interface{}) {
if w.logger == nil {
return
}
w.logger.Printf(format, v...)
}
func (w *coapResponseWriter) Write(b []byte) (int, error) {
w.body = bytes.NewReader(b)
code, ok := statusCodes[w.statusCode]
if !ok {
w.log("cannot map HTTP status %d to CoAP code, using codes.Empty", w.statusCode)
code = codes.Empty
}
// check content-type header for media type
// TODO: Parse mime type correctly and use the registry at https://tools.ietf.org/html/rfc7252#section-12.3
cType := w.headers.Get("Content-Type")
contentFormat, ok := contentTypeToContentFormat[cType]
if !ok {
contentFormat = message.AppOctets
}
// TODO: convert HTTP headers to options?
w.ResponseWriter.SetResponse(code, contentFormat, w.body)
return len(b), nil
}
func (w *coapResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
}