/
server.go
504 lines (461 loc) · 13 KB
/
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package main
import (
"fmt"
"image/jpeg"
"io/fs"
"io/ioutil"
"log"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/stuartdd2/JsonParser4go/parser"
)
type UserData struct {
userName string
locations map[string]string
}
type TNServer struct {
port int
server *http.Server
thumbNailSize int
getRoutes map[string]func([]string, *TNServer, http.ResponseWriter, *http.Request) *TNResp
srcPath string
verbose bool
startTime int64
users map[string]*UserData
}
type TNResp struct {
returnCode int
mimeType string
resp []byte
}
const (
MEDIA_JSON = "application/json"
PATH_SEP = string(os.PathSeparator)
URL_SEP = "/"
NL = "\n"
)
var (
CLOSED = &TNResp{returnCode: http.StatusOK, mimeType: MEDIA_JSON, resp: []byte(`{"message":"Server-Closed"}` + NL)}
THUMB_FILE_TYPES = map[string]string{
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
}
THUMB_FILE_TYPE = ".jpg"
USER_PATH = parser.NewDotPath("resources.users")
)
func UMT(tag, ent string, uri []string, err error) *TNResp {
logServer(fmt.Sprintf("UnsupportedMediaType:%s: ent:%s", tag, ent), strings.Join(uri, URL_SEP), err)
return &TNResp{returnCode: http.StatusUnsupportedMediaType, mimeType: MEDIA_JSON, resp: []byte(fmt.Sprintf("{\"message\":\"Unsupported Media Type\", \"Item\": \"%s\"\"}", ent))}
}
func ISE(tag, ent string, uri []string, err error) *TNResp {
logServer(fmt.Sprintf("InternalServerError:%s: ent:%s", tag, ent), strings.Join(uri, URL_SEP), err)
return &TNResp{returnCode: http.StatusInternalServerError, mimeType: MEDIA_JSON, resp: []byte(fmt.Sprintf("{\"message\":\"Internal Server Error\", \"Item\": \"%s\"\"}", ent))}
}
func NF(tag string, uri []string, err error) *TNResp {
logServer(fmt.Sprintf("NotFound:%s:", tag), strings.Join(uri, URL_SEP), err)
return &TNResp{returnCode: http.StatusNotFound, mimeType: MEDIA_JSON, resp: []byte(fmt.Sprintf("{\"message\":\"Not Found\", \"URI\": \"%s\"\"}", strings.Join(uri, URL_SEP)))}
}
func BR(tag, ent string, uri []string, err error) *TNResp {
logServer(fmt.Sprintf("BadRequest:%s: ent:%s", tag, ent), strings.Join(uri, URL_SEP), err)
return &TNResp{returnCode: http.StatusBadRequest, mimeType: MEDIA_JSON, resp: []byte(fmt.Sprintf("{\"message\":\"Bad Request\", \"Value\": \"%s\"\"}", ent))}
}
func (s *TNResp) String() string {
return fmt.Sprintf("{\"RESPONSE\":{\"rc\":\"%d\",\"mime\":\"%s\",\"len\":\"%d\",\"resp\":\"%s\" }}", s.returnCode, s.mimeType, len(s.resp), EncodeString(s.resp, 50, s.mimeType))
}
/*
Backspace is replaced with \b.
Form feed is replaced with \f.
Newline is replaced with \n.
Carriage return is replaced with \r.
Tab is replaced with \t.
Double quote is replaced with \"
Backslash is replaced with \\
*/
func EncodeString(b []byte, max int, mt string) string {
image := strings.Contains(mt, "image")
var sb strings.Builder
for i, c := range b {
if image {
sb.WriteRune('[')
if c < 16 {
sb.WriteRune('0')
}
sb.WriteString(strconv.FormatInt(int64(c), 16))
sb.WriteRune(']')
} else {
switch c {
case 8:
sb.WriteRune('\\')
sb.WriteRune('b')
case 12:
sb.WriteRune('\\')
sb.WriteRune('f')
case 10:
sb.WriteRune('\\')
sb.WriteRune('n')
case 13:
sb.WriteRune('\\')
sb.WriteRune('r')
case 11:
sb.WriteRune('\\')
sb.WriteRune('t')
case 34:
sb.WriteRune('\\')
sb.WriteRune('"')
case 92:
sb.WriteRune('\\')
sb.WriteRune('\\')
default:
if c >= 32 && c <= 127 {
sb.WriteByte(c)
}
}
}
if i > 25 && image {
break
} else {
if i > max {
break
}
}
}
return sb.String()
}
func NewTnServer(port int, srcPath, configPath string, sizeInt int, verbose bool) (*TNServer, error) {
absFileName, err := filepath.Abs(configPath)
if err != nil {
return nil, err
}
j, err := ioutil.ReadFile(absFileName)
if err != nil {
return nil, err
}
if strings.TrimSpace(string(j)) == "" {
return nil, fmt.Errorf("file '%s' is empty", absFileName)
}
configData, err := parser.Parse(j)
if err != nil {
return nil, err
}
if verbose {
log.Printf("{\"CONFIG\":{\"file\":\"%s\",\"info\":\"Parsed config ok\"}}", absFileName)
}
userDataNode, err := parser.Find(configData, USER_PATH)
if err != nil {
return nil, err
}
userDataObj, ok := userDataNode.(*parser.JsonObject)
if !ok {
return nil, fmt.Errorf("config data [%s] node %s is not a json object", absFileName, USER_PATH.String())
}
userMap := make(map[string]*UserData)
for _, ud := range userDataObj.GetValues() {
name := ud.GetName()
udObj, ok := ud.(*parser.JsonObject)
if !ok {
return nil, fmt.Errorf("user data node %s.%s is not an object node", USER_PATH, name)
}
locations := make(map[string]string)
for _, udv := range udObj.GetValues() {
udvStr, ok := udv.(*parser.JsonString)
if ok {
locations[udvStr.GetName()] = udvStr.GetValue()
}
}
userMap[ud.GetName()] = &UserData{userName: name, locations: locations}
if verbose {
n := ud.(parser.NodeC).GetNodeWithName("name")
if n != nil {
log.Printf("{\"CONFIG\":{\"user\":\"%s\",\"info\":\"%s\"}}", ud.GetName(), n)
} else {
log.Printf("{\"CONFIG\":{\"user\":\"%s\",\"info\":\"undefined\"}}", ud.GetName())
}
}
}
routes := make(map[string]func([]string, *TNServer, http.ResponseWriter, *http.Request) *TNResp)
tns := &TNServer{port: port, srcPath: srcPath, getRoutes: routes, thumbNailSize: sizeInt, verbose: verbose, users: userMap, startTime: time.Now().Unix()}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.RawPath
if p == "" {
p = r.URL.Path
}
rq := strings.Split(p, URL_SEP)
if len(rq) > 1 && rq[0] == "" {
rq = rq[1:]
}
if r.Method == http.MethodGet {
if verbose {
log.Printf("{\"REQUEST\":{\"path\":\"%s\", \"query\":\"%s\"}}", p, r.URL.RawQuery)
}
fn, found := tns.getRoutes[rq[0]]
var resp *TNResp
if found && len(rq) > 1 {
resp = fn(rq[1:], tns, w, r)
}
if resp == nil {
resp = NF("GET", rq, nil)
}
writeResp(tns, w, resp)
} else {
writeResp(tns, w, NF("UNSUPPORTED", rq, nil))
}
}),
}
tns.AddGetHandler("control", controlHandler)
tns.AddGetHandler("files", fileHandler)
tns.AddGetHandler("paths", pathHandler)
tns.server = srv
if verbose {
log.Printf("{\"SERVER\":{\"port\":\"%d\",\"info\":\"Configured\"}}", port)
}
return tns, nil
}
//
// /paths/user/{user}/loc/{loc} to filepath
//
func locationFromPath(uri []string, tns *TNServer) (string, *TNResp) {
user := dataFromPathElement(uri, "user")
if user == "" {
return "", NF("PATH", uri, nil)
}
loc := dataFromPathElement(uri, "loc")
if loc == "" {
return "", NF("PATH", uri, nil)
}
location := filepath.Join(tns.srcPath, tns.users[user].locations[loc])
_, err := os.Stat(location)
if err != nil {
return "", ISE("PATH", location, uri, err)
}
return location, nil
}
func filePathFromPath(uri []string, location string, tns *TNServer, pathRequired bool) (string, bool, *TNResp) {
path := dataFromPathElement(uri, "path")
if path == "" {
if pathRequired {
return "", false, NF("PATH", uri, nil)
}
}
unEscapedPath, err := url.PathUnescape(path)
if err != nil {
return "", false, BR("PATH", path, uri, err)
}
if unEscapedPath == "." {
unEscapedPath = ""
}
fullPath := ""
name := dataFromPathElement(uri, "name")
if name == "" {
fullPath = filepath.Join(location, unEscapedPath)
} else {
unEscapedName, err := url.PathUnescape(name)
if err != nil {
return "", false, BR("PATH", name, uri, err)
}
fullPath = filepath.Join(location, unEscapedPath, unEscapedName)
}
if !strings.HasPrefix(fullPath, location) {
return "", false, BR("PATH", "invalid-path", uri, nil)
}
fil, err := os.Stat(fullPath)
if err != nil {
return "", false, NF("PATH", uri, nil)
}
return fullPath, fil.IsDir(), nil
}
//
// paths/user/{user}/loc/{loc}
//
func pathHandler(uri []string, tns *TNServer, w http.ResponseWriter, r *http.Request) *TNResp {
location, resp := locationFromPath(uri, tns)
if resp != nil {
return resp
}
path, isDir, resp := filePathFromPath(uri, location, tns, false)
if resp != nil {
return resp
}
if !isDir {
return BR("PATH", "not-dir", uri, nil)
}
var sb strings.Builder
count := 0
sb.WriteString("[")
fsys := os.DirFS(path)
fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
if d != nil {
if d.IsDir() && p != "." {
fp := filepath.Join(path, p)
if len(filesOfInterest(fp, queryAllFile(r))) > 0 {
sb.WriteString(fmt.Sprintf("\n \"%s\",", url.PathEscape(p+PATH_SEP)))
count++
}
}
}
return nil
})
s := sb.String()
if count > 0 {
s = s[:len(s)-1]
}
return &TNResp{returnCode: http.StatusOK, mimeType: MEDIA_JSON, resp: []byte(s + "\n]")}
}
//
//
// files/user/{user}/loc/{loc}/path/{path}
//
func fileHandler(uri []string, tns *TNServer, w http.ResponseWriter, r *http.Request) *TNResp {
location, resp := locationFromPath(uri, tns)
if resp != nil {
return resp
}
if len(uri) < 5 {
return NF("FILE", uri, nil)
}
path, isDir, resp := filePathFromPath(uri, location, tns, true)
if resp != nil {
return resp
}
if isDir {
return returnFileList(path, queryAllFile(r))
}
thumbnail, thumbNailSize := queryThumbnail(r, tns.thumbNailSize)
return returnFileContent(path, uri, thumbnail, thumbNailSize, tns)
}
func returnFileList(path string, all bool) *TNResp {
list := filesOfInterest(path, all)
var sb strings.Builder
count := 0
sb.WriteString("[")
for _, f := range list {
sb.WriteString(fmt.Sprintf("\n \"%s\",", url.PathEscape(f)))
count++
}
s := sb.String()
if count > 0 {
s = s[:len(s)-1]
}
return &TNResp{returnCode: http.StatusOK, mimeType: MEDIA_JSON, resp: []byte(s + "\n]")}
}
func returnFileContent(srcFile string, uri []string, thumbnail bool, thumbNailSize int, tns *TNServer) *TNResp {
ext := filepath.Ext(srcFile)
_, fName := filepath.Split(srcFile)
if thumbnail {
_, ok := THUMB_FILE_TYPES[ext]
if !ok {
return UMT("FILE", fName, uri, nil)
}
pic := NewPicture(srcFile, thumbnail)
if pic.err != nil {
return NF("IMAGE", uri, pic.err)
}
dstImage, err := createThumbImage(pic, "", thumbNailSize, tns.verbose, true, len(tns.srcPath))
if err != nil {
return ISE("THUMB", pic.GetFileName(), uri, err)
}
w := NewEncodedWriter(500)
err = jpeg.Encode(w, dstImage, &jpeg.Options{Quality: jpeg.DefaultQuality})
if err != nil {
return ISE("ENCODE", pic.GetFileName(), uri, err)
}
return &TNResp{returnCode: http.StatusOK, mimeType: THUMB_FILE_TYPES[THUMB_FILE_TYPE], resp: w.Bytes()}
}
mediaType := mime.TypeByExtension(ext)
buf, err := ioutil.ReadFile(srcFile)
if err != nil {
return ISE("FILE", fName, uri, err)
}
return &TNResp{returnCode: http.StatusOK, mimeType: mediaType, resp: buf}
}
func controlHandler(uri []string, tns *TNServer, w http.ResponseWriter, r *http.Request) *TNResp {
if uri[0] == "close" {
go func() {
time.Sleep(2 * time.Second)
tns.Close()
}()
if tns.verbose {
log.Printf("{\"SERVER\":{\"port\":\"%d\",\"info\":\"Stopping\"}}", tns.port)
}
return CLOSED
}
if uri[0] == "time" {
return &TNResp{returnCode: http.StatusOK, mimeType: MEDIA_JSON, resp: []byte(fmt.Sprintf("{\"upSeconds\": \"%d\"}", time.Now().Unix()-tns.startTime))}
}
return NF("CNTL", uri, nil)
}
func writeResp(tns *TNServer, w http.ResponseWriter, resp *TNResp) {
if tns.verbose {
log.Print(resp)
}
w.WriteHeader(resp.returnCode)
w.Header().Set("Content-Type", resp.mimeType)
w.Header().Add("Content-Length", fmt.Sprintf("%d", len(resp.resp)))
_, _ = w.Write(resp.resp)
}
func (s *TNServer) AddGetHandler(uri string, handle func([]string, *TNServer, http.ResponseWriter, *http.Request) *TNResp) {
s.getRoutes[uri] = handle
}
func (s *TNServer) Close() {
if s.verbose {
log.Printf("{\"SERVER\":{\"port\":\"%d\",\"info\":\"Stopped\"}}", s.port)
}
s.server.Close()
}
func (s *TNServer) Run() error {
if s.verbose {
log.Printf("{\"SERVER\":{\"port\":\"%d\",\"info\":\"Started\"}}", s.port)
}
return s.server.ListenAndServe()
}
func queryThumbnail(r *http.Request, defSize int) (bool, int) {
tnRaw := r.URL.Query().Get("thumbnail")
tn := strings.TrimSpace(tnRaw)
if tn == "" {
return false, 0
}
i, err := strconv.Atoi(tn)
if err != nil {
return true, defSize
}
return true, i
}
func queryAllFile(r *http.Request) bool {
tnRaw := r.URL.Query().Get("allfiles")
tn := strings.TrimSpace(tnRaw)
return tn != ""
}
func dataFromPathElement(uri []string, name string) string {
for i, s := range uri {
if s == name && len(uri) > i+1 {
return uri[i+1]
}
}
return ""
}
func filesOfInterest(path string, all bool) []string {
list := make([]string, 0)
files, err := ioutil.ReadDir(path)
if err != nil {
return list
}
for _, f := range files {
if !strings.HasPrefix(f.Name(), ".") {
if !f.IsDir() {
mt := mime.TypeByExtension(strings.ToLower(filepath.Ext(f.Name())))
if all || strings.HasPrefix(mt, "image/") {
list = append(list, f.Name())
}
}
}
}
return list
}