/
utils.go
174 lines (150 loc) · 3.83 KB
/
utils.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
// Copyright 2012 by sdm. All rights reserved.
// license that can be found in the LICENSE file.
package wk
import (
"errors"
"fmt"
"github.com/sdming/kiss/gotype"
"net/http"
"os"
"path"
"reflect"
"runtime"
"runtime/debug"
"strings"
"time"
)
func cleanFilePath(p string) string {
if os.PathSeparator == '\\' {
p = strings.Replace(p, `\`, `/`, -1)
}
return path.Clean(p)
}
func isFileExists(path string) bool {
info, err := os.Stat(path)
if err == nil && !info.IsDir() {
return true
}
return os.IsExist(err)
}
func isDirExists(path string) bool {
info, err := os.Stat(path)
if err == nil && info.IsDir() {
return true
}
return false
}
// cleanPath return the canonical path for p, eliminating . and .. elements.
func cleanPath(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
np := path.Clean(p)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if p[len(p)-1] == '/' && np != "/" {
np += "/"
}
return np
}
// methodNameN returns the name of the calling method, Caller(N)
func methodNameN(skip int) string {
pc, _, _, _ := runtime.Caller(skip)
f := runtime.FuncForPC(pc)
if f == nil {
return "unknown method"
}
return f.Name()
}
// webTime return formated time.Time
func webTime(t time.Time) string {
return t.Format(http.TimeFormat)
}
// safeCall
func safeCall(fn reflect.Value, args []reflect.Value) (result []reflect.Value, err error) {
defer func() {
if x := recover(); x != nil {
if e, ok := x.(error); ok {
err = e
} else {
err = errors.New(fmt.Sprintf("call method %s fail, %s ", fn.Type(), x))
}
if LogLevel >= LogError {
Logger.Println(fn, string(debug.Stack()))
}
}
}()
return fn.Call(args), nil
}
// func isBodyAllowed(w *response) bool {
// return w.status != StatusNotModified && w.req.Method != "HEAD"
// }
// http://doc.golang.org/src/pkg/net/http/fs.go
func checkLastModified(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
if modtime.IsZero() {
return false
}
// The Date-Modified header truncates sub-second precision, so
// use mtime < t+1s instead of mtime <= t to check for unmodified.
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
w.WriteHeader(http.StatusNotModified)
return true
}
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
return false
}
var htmlReplacer = strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
// """ is shorter than """.
`"`, """,
// "'" is shorter than "'" and apos was not in HTML until HTML5.
"'", "'",
)
// http://doc.golang.org/src/pkg/net/http/server.go
func htmlEscape(s string) string {
return htmlReplacer.Replace(s)
}
func formatXml(ctx *HttpContext, x interface{}) (HttpResult, bool) {
return &XmlResult{Data: x}, true
}
func formatJson(ctx *HttpContext, x interface{}) (HttpResult, bool) {
return &JsonResult{Data: x}, true
}
// convertResult convert reflect.Value to http result
func convertResult(ctx *HttpContext, v reflect.Value) HttpResult {
i := v.Interface()
if r, ok := i.(HttpResult); ok {
return r
}
if Formatters != nil {
for _, f := range Formatters {
if formatted, ok := f(ctx, v.Interface()); ok {
return formatted
}
}
}
kind := reflect.Indirect(v).Kind()
switch {
case gotype.IsSimple(kind):
return &DataResult{Data: i}
case gotype.IsStruct(kind) || gotype.IsCollect(kind):
accept := ctx.Accept()
switch {
case strings.Index(accept, "xml") > -1:
return &XmlResult{Data: v.Interface()}
case strings.Index(accept, "jsonp") > -1:
return &JsonpResult{Data: v.Interface()}
default:
return &JsonResult{Data: v.Interface()}
}
}
return &ContentResult{Data: i}
}