Exemple #1
0
func globalFilter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) {
	resp.SetContentType("text/plain", nil)

	if resp.Status() == http.StatusNotFound {
		resp.SetHeader("Location", path+"/options?from="+url.QueryEscape(req.URL().Path))
		resp.ReportMovedPermanently()
	} else if resp.Status() == http.StatusMethodNotAllowed {
		resp.WriteString("The pprof interface only support GET request\n")
	} else {
		chain(req, resp)
	}
}
Exemple #2
0
func (j JSONP) Filter(req zerver.Request, resp zerver.Response, chain zerver.FilterChain) {
	if req.Method() != "GET" {
		chain(req, resp)
		return
	}

	res, _ := req.ResourceMaster().Resource(resource.RES_JSON)
	if res == nil {
		resp.ReportNotAcceptable()
		return
	}

	callback := req.Param(string(j))
	if callback == "" {
		resp.ReportBadRequest()
		resp.Send("error", "no callback function")
		return
	}

	resp.SetContentType(resource.RES_JSON, res)
	_, err := resp.WriteString(callback)
	if err != nil {
		goto ERROR
	}
	_, err = resp.WriteString("(")
	if err != nil {
		goto ERROR
	}
	chain(req, resp)
	_, err = resp.WriteString(")")
	if err == nil {
		return
	}
ERROR:
	req.Logger().Warnln(err)
}
Exemple #3
0
func (h *Handler) Handle(req zerver.Request, resp zerver.Response) {
	req.Wrap(func(requ *http.Request, shouldClose bool) (r *http.Request, c bool) {
		r, c = requ, shouldClose

		err := requ.ParseMultipartForm(h.MaxMemory)
		if err != nil {
			handle.SendBadRequest(resp, err)
			return
		}

		if requ.MultipartForm == nil || requ.MultipartForm.File == nil {
			handle.SendErr(resp, h.ErrNoFile)
			return
		}

		files, has := requ.MultipartForm.File[h.FileKey]
		if !has || len(files) == 0 {
			handle.SendErr(resp, h.ErrNoFile)
			return
		}

		if len(h.Params) > 0 {
			for _, param := range h.Params {
				if vals := requ.MultipartForm.Value[param]; len(vals) != 0 {
					req.SetAttr(param, vals[0])
				}
			}
		}
		if h.PreChecker != nil {
			if err = h.PreChecker(req); err != nil {
				handle.SendErr(resp, err)
				return
			}
		}

		file := convertFileHandler(files[0])
		if !h.isSuffixSupported(filepath.Ext(file.Filename)) {
			handle.SendErr(resp, h.ErrNonImage)
			return
		}

		if file.Size() > h.MaxSize {
			handle.SendErr(resp, h.ErrTooLarge)
			return
		}

		fd, err := file.Open()
		if err != nil {
			handle.SendBadRequest(resp, err)
			return
		}

		defer fd.Close()

		h.logger.Debugf("upload file: %s, size: %s\n", fd.Filename(), bytesize.ToHuman(uint64(fd.Size())))
		path, err := h.SaveImage(fd, req)
		if err != nil {
			handle.SendErr(resp, err)
			return
		}

		if h.PostDo != nil {
			err := h.PostDo(req)
			if err != nil {
				h.logger.Warnln("PostDo", err)
			}
		}

		resp.SetContentType(resource.RES_JSON, nil)
		resp.Send(h.PathKey, path)
		return
	})
}