-
Notifications
You must be signed in to change notification settings - Fork 2
/
piping_server.go
284 lines (265 loc) · 9.49 KB
/
piping_server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package piping_server
import (
"fmt"
"github.com/nwtgck/go-piping-server/syncmap"
"github.com/nwtgck/go-piping-server/version"
"io"
"log"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"strconv"
"sync/atomic"
)
const (
reservedPathIndex = "/"
reservedPathNoScript = "/noscript"
reservedPathVersion = "/version"
reservedPathHelp = "/help"
reservedPathFaviconIco = "/favicon.ico"
reservedPathRobotsTxt = "/robots.txt"
)
var reservedPaths = [...]string{
reservedPathIndex,
reservedPathVersion,
reservedPathHelp,
reservedPathFaviconIco,
reservedPathRobotsTxt,
}
const noscriptPathQueryParameterName = "path"
type pipe struct {
receiverResWriterCh chan http.ResponseWriter
sendFinishedCh chan struct{}
isSenderConnected uint32 // NOTE: for atomic operation
isTransferring uint32 // NOTE: for atomic operation
}
type PipingServer struct {
pathToPipe syncmap.SyncMap[string, *pipe]
logger *log.Logger
}
func isReservedPath(path string) bool {
for _, p := range reservedPaths {
if p == path {
return true
}
}
return false
}
func NewServer(logger *log.Logger) *PipingServer {
return &PipingServer{
pathToPipe: syncmap.SyncMap[string, *pipe]{},
logger: logger,
}
}
func (s *PipingServer) getPipe(path string) *pipe {
pi := &pipe{
receiverResWriterCh: make(chan http.ResponseWriter, 1),
sendFinishedCh: make(chan struct{}),
isSenderConnected: 0,
}
pi, _ = s.pathToPipe.LoadOrStore(path, pi)
return pi
}
func transferHeaderIfExists(w http.ResponseWriter, reqHeader textproto.MIMEHeader, header string) {
values := reqHeader.Values(header)
if len(values) == 1 {
w.Header().Add(header, values[0])
}
}
func getTransferHeaderAndBody(req *http.Request) (textproto.MIMEHeader, io.ReadCloser) {
mediaType, params, mediaTypeParseErr := mime.ParseMediaType(req.Header.Get("Content-Type"))
// If multipart upload
if mediaTypeParseErr == nil && mediaType == "multipart/form-data" {
multipartReader := multipart.NewReader(req.Body, params["boundary"])
part, err := multipartReader.NextPart()
if err != nil {
// Return normal if multipart error
return textproto.MIMEHeader(req.Header), req.Body
}
return part.Header, part
}
return textproto.MIMEHeader(req.Header), req.Body
}
func (s *PipingServer) Handler(resWriter http.ResponseWriter, req *http.Request) {
s.logger.Printf("%s %s %s", req.Method, req.URL, req.Proto)
path := req.URL.Path
if req.Method == "GET" || req.Method == "HEAD" {
switch path {
case reservedPathIndex:
indexPageBytes := []byte(indexPage)
resWriter.Header().Set("Content-Type", "text/html")
resWriter.Header().Set("Content-Length", strconv.Itoa(len(indexPageBytes)))
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.Write(indexPageBytes)
return
case reservedPathNoScript:
noScriptHtmlBytes := []byte(noScriptHtml(req.URL.Query().Get(noscriptPathQueryParameterName)))
resWriter.Header().Set("Content-Type", "text/html")
resWriter.Header().Set("Content-Length", strconv.Itoa(len(noScriptHtmlBytes)))
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.Write(noScriptHtmlBytes)
return
case reservedPathVersion:
versionBytes := []byte(fmt.Sprintf("%s in Go\n", version.Version))
resWriter.Header().Set("Content-Type", "text/plain")
resWriter.Header().Set("Content-Length", strconv.Itoa(len(versionBytes)))
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.Write(versionBytes)
return
case reservedPathHelp:
protocol := "http"
if req.TLS != nil {
protocol = "https"
}
url := fmt.Sprintf(protocol+"://%s", req.Host)
helpPageBytes := []byte(helpPage(url))
resWriter.Header().Set("Content-Type", "text/plain")
resWriter.Header().Set("Content-Length", strconv.Itoa(len(helpPageBytes)))
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.Write(helpPageBytes)
return
case reservedPathFaviconIco:
resWriter.Header().Set("Content-Length", "0")
resWriter.WriteHeader(204)
return
case reservedPathRobotsTxt:
resWriter.Header().Set("Content-Length", "0")
resWriter.WriteHeader(404)
return
}
}
// TODO: should close if either sender or receiver closes
switch req.Method {
case "GET":
// If the receiver requests Service Worker registration
// (from: https://speakerdeck.com/masatokinugawa/pwa-study-sw?slide=32)
if req.Header.Get("Service-Worker") == "script" {
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.WriteHeader(400)
resWriter.Write([]byte("[ERROR] Service Worker registration is rejected.\n"))
return
}
pi := s.getPipe(path)
// If already get the path or transferring
if len(pi.receiverResWriterCh) != 0 || atomic.LoadUint32(&pi.isTransferring) == 1 {
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.WriteHeader(400)
resWriter.Write([]byte("[ERROR] The number of receivers has reached limits.\n"))
return
}
pi.receiverResWriterCh <- resWriter
// Wait for finish
<-pi.sendFinishedCh
case "POST", "PUT":
// If reserved path
if isReservedPath(path) {
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.WriteHeader(400)
resWriter.Write([]byte(fmt.Sprintf("[ERROR] Cannot send to the reserved path '%s'. (e.g. '/mypath123')\n", path)))
return
}
// Notify that Content-Range is not supported
// In the future, resumable upload using Content-Range might be supported
// ref: https://github.com/httpwg/http-core/pull/653
if len(req.Header.Values("Content-Range")) != 0 {
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.WriteHeader(400)
resWriter.Write([]byte(fmt.Sprintf("[ERROR] Content-Range is not supported for now in %s\n", req.Method)))
return
}
pi := s.getPipe(path)
// If a sender is already connected
if !atomic.CompareAndSwapUint32(&pi.isSenderConnected, 0, 1) {
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.WriteHeader(400)
reqContentLength := req.ContentLength
// NOTE: `req.ContentLength = 0` is a workaround for full duplex
// Replace with https://github.com/golang/go/blob/457fd1d52d17fc8e73d4890150eadab3128de64d/src/net/http/responsecontroller.go#L119-L141 in the future
req.ContentLength = 0
if f, ok := resWriter.(http.Flusher); ok {
f.Flush()
}
req.ContentLength = reqContentLength
resWriter.Write([]byte(fmt.Sprintf("[ERROR] Another sender has been connected on '%s'.\n", path)))
return
}
contentLength := req.ContentLength
// NOTE: `req.ContentLength = 0` is a workaround for full duplex
// Replace with https://github.com/golang/go/blob/457fd1d52d17fc8e73d4890150eadab3128de64d/src/net/http/responsecontroller.go#L119-L141 in the future
req.ContentLength = 0
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.WriteHeader(200)
if f, ok := resWriter.(http.Flusher); ok {
f.Flush()
}
req.ContentLength = contentLength
resWriteFlusher := NewWriteFlusherIfPossible(resWriter)
if _, err := resWriteFlusher.Write([]byte("[INFO] Waiting for 1 receiver(s)...\n")); err != nil {
return
}
receiverResWriter := <-pi.receiverResWriterCh
if _, err := resWriteFlusher.Write([]byte("[INFO] A receiver was connected.\n")); err != nil {
return
}
if _, err := resWriteFlusher.Write([]byte("[INFO] Start sending to 1 receiver(s)!\n")); err != nil {
return
}
atomic.StoreUint32(&pi.isTransferring, 1)
transferHeader, transferBody := getTransferHeaderAndBody(req)
receiverResWriter.Header()["Content-Type"] = nil // not to sniff
transferHeaderIfExists(receiverResWriter, transferHeader, "Content-Type")
transferHeaderIfExists(receiverResWriter, transferHeader, "Content-Length")
transferHeaderIfExists(receiverResWriter, transferHeader, "Content-Disposition")
xPipingValues := req.Header.Values("X-Piping")
if len(xPipingValues) != 0 {
receiverResWriter.Header()["X-Piping"] = xPipingValues
}
receiverResWriter.Header().Set("Access-Control-Allow-Origin", "*")
if len(xPipingValues) != 0 {
receiverResWriter.Header().Set("Access-Control-Expose-Headers", "X-Piping")
}
receiverResWriter.Header().Set("X-Robots-Tag", "none")
receiverResWriteFlusher := NewWriteFlusherIfPossible(receiverResWriter)
if _, err := io.Copy(receiverResWriteFlusher, transferBody); err != nil {
return
}
if _, err := resWriteFlusher.Write([]byte("[INFO] Sent successfully!\n")); err != nil {
return
}
pi.sendFinishedCh <- struct{}{}
s.pathToPipe.Delete(path)
case "OPTIONS":
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, OPTIONS")
resWriter.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Disposition, X-Piping")
resWriter.Header().Set("Access-Control-Max-Age", "86400")
resWriter.Header().Set("Content-Length", "0")
resWriter.WriteHeader(200)
return
default:
resWriter.WriteHeader(405)
resWriter.Header().Set("Access-Control-Allow-Origin", "*")
resWriter.Write([]byte(fmt.Sprintf("[ERROR] Unsupported method: %s.\n", req.Method)))
return
}
s.logger.Printf("Transferring %s has finished in %s method.\n", req.URL.Path, req.Method)
}
type WriteFlusher struct {
writer io.Writer
flusher http.Flusher
}
func NewWriteFlusherIfPossible(w http.ResponseWriter) io.Writer {
if f, ok := w.(http.Flusher); ok {
return &WriteFlusher{writer: w, flusher: f}
}
return w
}
func (f *WriteFlusher) Write(p []byte) (int, error) {
n, err := f.writer.Write(p)
if err != nil {
return n, err
}
f.flusher.Flush()
return n, err
}