|
|
@ -44,6 +44,7 @@ type HttpServerCfg struct {
|
|
|
|
disableMIME bool
|
|
|
|
disableMIME bool
|
|
|
|
ctx context.Context
|
|
|
|
ctx context.Context
|
|
|
|
hooks []ServerHook
|
|
|
|
hooks []ServerHook
|
|
|
|
|
|
|
|
httpDebug bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ServerHook struct {
|
|
|
|
type ServerHook struct {
|
|
|
@ -477,6 +478,37 @@ func (h *HttpServer) SetUpload(w http.ResponseWriter, r *http.Request, path stri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (h *HttpServer) debugMode(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
|
|
|
html := `<html><head><meta charset="utf-8"><title>B612 Http Server</title></head><body><h1 "style="text-align: center;">Debug Mode</h1><hr >%s</body></html>`
|
|
|
|
|
|
|
|
resp := "<h2>Url</h2>"
|
|
|
|
|
|
|
|
resp += "<p>" + r.Method + " " + r.URL.Path + "</p>"
|
|
|
|
|
|
|
|
resp += "<p> query: " + r.URL.RawQuery + "</p>"
|
|
|
|
|
|
|
|
resp += "<p> fragment: " + r.URL.Fragment + "</p>"
|
|
|
|
|
|
|
|
resp += "<p> FullUrl: " + r.URL.String() + "</p>"
|
|
|
|
|
|
|
|
resp += "<h2>Query</h2>"
|
|
|
|
|
|
|
|
for k, v := range r.URL.Query() {
|
|
|
|
|
|
|
|
resp += fmt.Sprintf("<p>%s:%s</p>", k, v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
resp += "<h2>Header</h2>"
|
|
|
|
|
|
|
|
for key, val := range r.Header {
|
|
|
|
|
|
|
|
for _, v := range val {
|
|
|
|
|
|
|
|
resp += fmt.Sprintf("<p>%s:%s</p>", key, v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
resp += "<h2>Cookie</h2>"
|
|
|
|
|
|
|
|
for _, c := range r.Cookies() {
|
|
|
|
|
|
|
|
resp += fmt.Sprintf("<p>%s:%s</p>", c.Name, c.Value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
resp += "<h2>RemoteAddr</h2>"
|
|
|
|
|
|
|
|
resp += "<p>" + r.RemoteAddr + "</p>"
|
|
|
|
|
|
|
|
resp += "<h2>Proto</h2>"
|
|
|
|
|
|
|
|
resp += "<p>" + r.Proto + "</p>"
|
|
|
|
|
|
|
|
w.Write([]byte(fmt.Sprintf(html, resp)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log := starlog.Std.NewFlag()
|
|
|
|
log := starlog.Std.NewFlag()
|
|
|
|
log.SetShowFuncName(false)
|
|
|
|
log.SetShowFuncName(false)
|
|
|
@ -487,11 +519,16 @@ func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path := r.URL.Path
|
|
|
|
path := r.URL.Path
|
|
|
|
|
|
|
|
ua := r.Header.Get("User-Agent")
|
|
|
|
|
|
|
|
if h.httpDebug {
|
|
|
|
|
|
|
|
log.Infof("debug mode:%s %s From %s %s\n", r.Method, path, r.RemoteAddr, ua)
|
|
|
|
|
|
|
|
h.debugMode(w, r)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
if h.uploadFolder != "" && path == "/recv" && len(r.URL.Query()["upload"]) != 0 {
|
|
|
|
if h.uploadFolder != "" && path == "/recv" && len(r.URL.Query()["upload"]) != 0 {
|
|
|
|
h.uploadFile(w, r)
|
|
|
|
h.uploadFile(w, r)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ua := r.Header.Get("User-Agent")
|
|
|
|
|
|
|
|
fullpath := filepath.Clean(filepath.Join(h.envPath, path))
|
|
|
|
fullpath := filepath.Clean(filepath.Join(h.envPath, path))
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -532,7 +569,7 @@ func (h *HttpServer) Listen(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Infof("%s %s From %s %s %.2fs\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds())
|
|
|
|
log.Infof("%s %s From %s %s %.2fs\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds())
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
log.Errorf("Invalid %s %s From %s %s %.2fs %v\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds())
|
|
|
|
log.Errorf("Invalid %s %s From %s %s %.2fs\n", r.Method, path, r.RemoteAddr, ua, time.Since(now).Seconds())
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|