-
Notifications
You must be signed in to change notification settings - Fork 1
/
strip_prefix.go
120 lines (104 loc) · 3.34 KB
/
strip_prefix.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
//
// Go-FTL
//
// Copyright (C) Philip Schlump, 2014-2015-2016
//
// Do not remove the following lines - used in auto-update.
// Version: 0.5.9
// BuildNo: 1811
// FileId: 1200
//
//
// Package header remove prefix - usually prior to proxy or file server
//
// Copyright (C) Philip Schlump, 2016
//
package mid
import (
"fmt"
"net/http"
"os"
"regexp"
"strings"
JsonX "github.com/pschlump/JSONx"
"github.com/pschlump/Go-FTL/server/cfg"
"github.com/pschlump/Go-FTL/server/goftlmux"
"github.com/pschlump/Go-FTL/server/lib"
)
// --------------------------------------------------------------------------------------------------------------------------
//func init() {
//
// // normally identical
// initNext := func(next http.Handler, gCfg *cfg.ServerGlobalConfigType, ppCfg interface{}, serverName string, pNo int) (rv http.Handler, err error) {
// pCfg, ok := ppCfg.(*StripPrefixType)
// if ok {
// pCfg.SetNext(next)
// rv = pCfg
// } else {
// err = FtlConfigError
// }
// return
// }
//
// // normally identical
// createEmptyType := func() interface{} { return &StripPrefixType{} }
//
// cfg.RegInitItem2("strip_prefix", initNext, createEmptyType, nil, `{
// "Paths": { "type":["string","filepath"], "isarray":true, "required":true },
// "Prefix": { "type":[ "string" ], "required":true },
// "LineNo": { "type":[ "int" ], "default":"1" }
// }`)
//}
//
//// normally identical
//func (hdlr *StripPrefixType) SetNext(next http.Handler) {
// hdlr.Next = next
//}
func init() {
CreateEmpty := func(name string) GoFTLMiddleWare {
x := &StripPrefixType{}
meta := make(map[string]JsonX.MetaInfo)
JsonX.SetDefaults(&x, meta, "", "", "") // xyzzy - report errors in 'meta'
return x
}
RegInitItem3("strip_prefix", CreateEmpty, `{
"Paths": { "type":["string","filepath"], "isarray":true, "required":true },
"Prefix": { "type":[ "string" ], "required":true },
"LineNo": { "type":[ "int" ], "default":"1" }
}`)
}
func (hdlr *StripPrefixType) InitializeWithConfigData(next http.Handler, gCfg *cfg.ServerGlobalConfigType, serverName string, pNo, callNo int) (err error) {
hdlr.Next = next
//hdlr.CallNo = callNo // 0 if 1st init
return
}
func (hdlr *StripPrefixType) PreValidate(gCfg *cfg.ServerGlobalConfigType, cfgData map[string]interface{}, serverName string, pNo, callNo int) (err error) {
return
}
var _ GoFTLMiddleWare = (*StripPrefixType)(nil)
// --------------------------------------------------------------------------------------------------------------------------
type StripPrefixType struct {
Next http.Handler //
Paths []string //
Prefix string // regular expression
matchre *regexp.Regexp
}
func NewStripPrefixServer(n http.Handler, p []string, h, v string) *StripPrefixType {
return &StripPrefixType{Next: n, Paths: p, Prefix: h}
}
func (hdlr StripPrefixType) ServeHTTP(www http.ResponseWriter, req *http.Request) {
if lib.PathsMatch(hdlr.Paths, req.URL.Path) {
if rw, ok := www.(*goftlmux.MidBuffer); ok {
req.URL.Path = strings.TrimPrefix(req.URL.Path, hdlr.Prefix)
req.RequestURI = strings.TrimPrefix(req.RequestURI, hdlr.Prefix)
hdlr.Next.ServeHTTP(rw, req)
} else {
fmt.Fprintf(os.Stderr, "%s\n", ErrNonMidBufferWriter)
www.WriteHeader(http.StatusInternalServerError)
return
}
} else {
hdlr.Next.ServeHTTP(www, req)
}
}
/* vim: set noai ts=4 sw=4: */