forked from venliong/webby
/
webby.go
287 lines (240 loc) · 5.96 KB
/
webby.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
package webby
import (
"bufio"
html "html/template"
"io"
"net"
"net/http"
"net/http/cgi"
"net/http/fcgi"
"os"
"time"
)
// Debug Mode
var DEBUG = false
func init() {
HtmlFuncBoot.Register(func(w *Web) {
w.HtmlFunc["Debug"] = func() bool {
return DEBUG
}
w.HtmlFunc["NotDebug"] = func() bool {
return !DEBUG
}
})
}
var RootView RouteHandler = FuncToRouteHandler(func(w *Web) {
appMiddlewares := AppMiddlewares.Init(w)
appMiddlewares.Pre()
defer appMiddlewares.Post()
if w.CutOut() {
return
}
Boot.Load(w)
if w.CutOut() {
return
}
Route.Load(w)
})
type web interface {
http.ResponseWriter
}
type webPrivate struct {
path string
curpath string
reswrite io.Writer
cut bool
firstWrite bool
cmd map[string]func(interface{}) interface{}
html *htmlDefault
session *SessionAdv
}
// The Framework Structure, it's implement the interfaces of 'net/http.ResponseWriter',
// 'net/http.Hijacker', 'net/http.Flusher' and 'net/http.Handler'
type Web struct {
// Error Code
Status int
// Server Environment Variables
Env http.Header
// Request
Req *http.Request
// Meta, useful for storing login credentail
Meta map[string]interface{}
// Well same as meta, but for string data type only! Useful for storing user country code!
Str map[string]string
// Used by router for storing data of named group in RegExpRule
Param Param
// Function to load in html template system.
HtmlFunc html.FuncMap
// For holding session!
Session interface{}
// Errors
Errors *Errors
// Time Location
TimeLoc *time.Location
// Time Format
TimeFormat string
web
pri *webPrivate
}
// HTTP Handler
func (_ Web) ServeHTTP(res http.ResponseWriter, req *http.Request) {
w := &Web{
web: res.(web),
Status: http.StatusOK,
Env: req.Header,
Req: req,
Meta: map[string]interface{}{},
Str: map[string]string{},
Param: Param{},
HtmlFunc: html.FuncMap{},
Session: nil,
TimeLoc: DefaultTimeLoc,
TimeFormat: DefaultTimeFormat,
Errors: &Errors{
E403: Error403,
E404: Error404,
E500: Error500,
},
pri: &webPrivate{
path: req.URL.Path,
curpath: "",
cut: false,
firstWrite: true,
cmd: map[string]func(interface{}) interface{}{},
},
}
w.initWriter()
w.initTrueHost()
w.initTrueRemoteAddr()
w.initTruePath()
w.initSecure()
w.initSession()
defer w.recover()
defer w.closeCompression()
defer PostBoot.Load(w)
w.debugStart()
defer w.debugEnd()
HtmlFuncBoot.Load(w)
if w.CutOut() {
return
}
mainMiddleware := MainMiddlewares.Init(w)
mainMiddleware.Pre()
defer mainMiddleware.Post()
if w.CutOut() {
return
}
MainBoot.Load(w)
if w.CutOut() {
return
}
w.RouteDealer(RootView)
if w.CutOut() {
return
}
w.Error500()
}
// Header returns the header map that will be sent by WriteHeader.
// Changing the header after a call to WriteHeader (or Write) has
// no effect.
func (w *Web) Header() http.Header {
return w.web.Header()
}
// Write writes the data to the connection as part of an HTTP reply.
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
// before writing the data. If the Header does not contain a
// Content-Type line, Write adds a Content-Type set to the result of passing
// the initial 512 bytes of written data to DetectContentType.
func (w *Web) Write(data []byte) (int, error) {
w.pri.cut = true
if w.pri.firstWrite {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(data))
}
w.pri.firstWrite = false
w.WriteHeader(w.Status)
}
return w.pri.reswrite.Write(data)
}
// WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
// Thus explicit calls to WriteHeader are mainly used to
// send error codes.
//
// Note: Use Status property to set error code! As this disable compression!
func (w *Web) WriteHeader(num int) {
w.pri.cut = true
if w.pri.firstWrite {
w.pri.firstWrite = false
}
w.web.WriteHeader(num)
}
// Hijack lets the caller take over the connection.
// After a call to Hijack(), the HTTP server library
// will not do anything else with the connection.
// It becomes the caller's responsibility to manage
// and close the connection.
func (w *Web) Hijack() (net.Conn, *bufio.ReadWriter, error) {
w.pri.cut = true
hj, ok := w.web.(http.Hijacker)
if ok {
return hj.Hijack()
}
return nil, nil, ErrorStr("Connection is not Hijackable")
}
// Flush sends any buffered data to the client.
func (w *Web) Flush() {
fl, ok := w.web.(http.Flusher)
if ok {
fl.Flush()
}
}
// true if output was sent to client, otherwise false!
func (w *Web) CutOut() bool {
return w.pri.cut
}
func (w *Web) debuginfo(a string) {
if !DEBUG {
return
}
ErrPrintf("--\r\n %s %s, %s, %s, %s, ?%s IP:%s \r\n--\r\n",
a, w.Req.Proto, w.Req.Method,
w.Req.Host, w.Req.URL.Path,
w.Req.URL.RawQuery, w.Req.RemoteAddr)
}
func (w *Web) debugStart() {
w.debuginfo("START")
}
func (w *Web) debugEnd() {
w.debuginfo("END ")
}
var _web = Web{}
func secure(res http.ResponseWriter, req *http.Request) {
req.Header.Set("X-Secure-Mode", "1")
_web.ServeHTTP(res, req)
}
func nonsecure(res http.ResponseWriter, req *http.Request) {
req.Header.Del("X-Secure-Mode")
_web.ServeHTTP(res, req)
}
// Start Http Server
func StartHttp(addr string) error {
return http.ListenAndServe(addr, http.HandlerFunc(nonsecure))
}
// Start Http Server with TLS
func StartHttpTLS(addr string, certFile string, keyFile string) error {
return http.ListenAndServeTLS(addr, certFile, keyFile, http.HandlerFunc(secure))
}
// Start FastCGI Server
func StartFastCGI(l net.Listener) error {
if l == nil {
os.Stderr = nil
}
return fcgi.Serve(l, http.HandlerFunc(nonsecure))
}
// Start CGI, disables Stderr completely. (Due to the way how IIS handlers Stderr)
func StartCGI() error {
os.Stderr = nil
return cgi.Serve(http.HandlerFunc(nonsecure))
}