Ejemplo n.º 1
0
func NewGzipResponseWriterLevelFile(w http.ResponseWriter, r *http.Request, level int, file *os.File) *GzipResponseWriter {
	if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
		if level < gzip.DefaultCompression || level > gzip.BestCompression {
			level = gzip.DefaultCompression
		}

		var gz *gzip.Writer

		if file != nil {
			gz, _ = gzip.NewWriterLevel(io.MultiWriter(w, file), level)
		} else {
			gz, _ = gzip.NewWriterLevel(w, level)
		}

		resp := &GzipResponseWriter{
			ResponseWriter: w,
			gzip:           gz,
		}

		header := w.Header()
		header.Set("Content-Encoding", "gzip")
		if vary, exists := header["Vary"]; !exists || !validate.IsIn("Accept-Encoding", vary...) {
			header.Add("Vary", "Accept-Encoding")
		}

		return resp
	}

	return &GzipResponseWriter{w, nil}
}
Ejemplo n.º 2
0
func (this *Context) XMethod() string {
	xmeth := this.Request.Method

	if xmeth == "POST" {
		tmpxmeth := this.Request.Header.Get(REQMETHOD_X_METHOD_NAME)

		if tmpxmeth == "" {
			tmpxmeth = strings.ToUpper(this.PostValue(REQMETHOD_X_METHOD_NAME))
		}

		if validate.IsIn(tmpxmeth, "GET", "POST", "PUT", "PATCH", "DELETE") {
			xmeth = tmpxmeth
		}
	}

	return xmeth
}
Ejemplo n.º 3
0
func (this *Context) ParseRequestData() {
	if !this.parseState.Start() {
		return
	}

	var (
		r             = this.Request
		w             = this.Response
		maxsize int64 = 10485760 //10MB
	)

	if validate.IsIn(r.Method, "POST", "PUT", "PATCH") {
		ctype := strings.ToLower(r.Header.Get("Content-Type"))
		is100 := strings.ToLower(r.Header.Get("Expect")) == "100-continue"

		if !strings.HasPrefix(ctype, "multipart") {
			if is100 && r.ContentLength > maxsize {
				goto expectFailed
			}
		} else if this.MaxFileSize > 0 {
			if maxsize = this.MaxFileSize; is100 && r.ContentLength > maxsize {
				goto expectFailed
			}

			r.Body = http.MaxBytesReader(w, r.Body, maxsize)
		}

		go this.parseRequestData(ctype, r.Body)
	} else {
		var err error

		if this.Request.Form == nil {
			this.Request.Form, err = url.ParseQuery(this.Request.URL.RawQuery)
		}

		this.parseState.End(err)
	}

	return

expectFailed:
	e := ExpectationError{size: r.ContentLength, maxsize: maxsize}
	this.parseState.End(e)
	w.Header().Set("Connection", "close")
	panic(e)
}
Ejemplo n.º 4
0
// TODO: refactor this
func setFuncMap(sunctxt *web.Context, vw mvc.View) {
	if fview, ok := vw.(mvc.TmplView); ok {
		fview.SetViewFunc("URLQ", sunctxt.URL)
		fview.SetViewFunc("URL", func(s string) string {
			return sunctxt.URL(s)
		})
		fview.SetViewFunc("Request", func() *http.Request {
			return sunctxt.Request
		})
		fview.SetViewFunc("QueryStr", sunctxt.QueryStr)
		fview.SetViewFunc("TimeNow", sunctxt.StartTime)
		fview.SetViewFunc("Nl2br", func(s string) template.HTML {
			s = strings.Replace(s, "\r\n", "\n", -1)
			s = strings.Replace(s, "\r", "\n", -1)
			return template.HTML(strings.Replace(template.HTMLEscapeString(s), "\n", "<br>\n", -1))
		})
		fview.SetViewFunc("SelectOption", func(selected ...string) template.HTMLAttr {
			if len(selected) == 2 && selected[0] == selected[1] {
				return " selected "
			}
			return ""
		})
		fview.SetViewFunc("SelectMultiOption", func(value []string, selected string) template.HTMLAttr {
			if validate.IsIn(selected, value...) {
				return " selected "
			}
			return ""
		})
		fview.SetViewFunc("CheckOption", func(selected ...string) template.HTMLAttr {
			if len(selected) == 2 && selected[0] == selected[1] {
				return " checked "
			}
			return ""
		})
		fview.SetViewFunc("RawHtml", func(s string) template.HTML {
			return template.HTML(s)
		})
		fview.SetViewFunc("User", func() interface{} {
			if sunctxt.Session != nil {
				return sunctxt.Session.AuthUser()
			}
			return nil
		})
		fview.SetViewFunc("Session", func() web.SessionManager {
			if sunctxt.Session != nil {
				return sunctxt.Session
			}
			return nil
		})
		fview.SetViewFunc("Json", func(i interface{}) template.HTML {
			b, _ := json.Marshal(i)
			return template.HTML(b)
		})
		fview.SetViewFunc("Implode", func(join string, slice []string) template.HTML {
			buf := bytes.Buffer{}
			for _, s := range slice {
				buf.WriteString(template.HTMLEscapeString(s))
				buf.WriteString(join)
			}
			if len(slice) > 0 {
				buf.Truncate(buf.Len() - len(join))
			}
			return template.HTML(buf.String())
		})
		fview.SetViewFunc("Flashes", sunctxt.AllFlashes)
		fview.SetViewFunc("CropText", func(s string, l int) string {
			if len(s) > l {
				s = s[:l-3] + "..."
			}
			return s
		})
		fview.SetViewFunc("IRange", func(i ...int) (arr []int) {
			count := len(i)
			switch count {
			case 0:
				arr = make([]int, 0)
			case 1:
				arr = make([]int, i[0]+1)
				for k := range arr {
					arr[k] = k
				}
			case 2:
				arr = make([]int, int(math.Abs(float64(i[1]-i[0])))+1)
				if i[0] > i[1] {
					for k := range arr {
						arr[k] = i[0]
						i[0]--
					}
				} else {
					for k := range arr {
						arr[k] = i[0]
						i[0]++
					}
				}
			case 3:
				if i[0] > i[1] {
					i[1] = i[1] - 1
				} else {
					i[1] = i[1] + 1
				}
				size := (float64(i[1]) - float64(i[0])) / float64(i[2])
				if size < 0 {
					return
				}
				arr = make([]int, int(math.Floor(size+0.5)))
				for k := range arr {
					arr[k] = i[0]
					i[0] = i[0] + i[2]
				}
			}
			return
		})
		fview.SetViewFunc("Limit", func(slice interface{}, limit int) interface{} {
			refslice := reflect.ValueOf(slice)
			if (refslice.Kind() == reflect.Slice || refslice.Kind() == reflect.Array) &&
				refslice.Cap() > limit {
				slice = refslice.Slice(0, limit).Interface()
			}
			return slice
		})
		fview.SetViewFunc("Add", func(num1 int, num2 int) int {
			return num1 + num2
		})
		fview.SetViewFunc("Sub", func(num1 int, num2 int) int {
			return num1 - num2
		})
	}
}
Ejemplo n.º 5
0
func (this *HTMLHead) AddScriptBatch(name string) {
	if arr, exists := this.scriptbatch[name]; exists && !validate.IsIn(name, this.addedscript...) {
		this.AddScript(arr...)
		this.addedscript = append(this.addedscript, name)
	}
}
Ejemplo n.º 6
0
func (this *HTMLHead) AddCssBatch(name string) {
	if arr, exists := this.cssbatch[name]; exists && !validate.IsIn(name, this.addedcss...) {
		this.AddCss(arr...)
		this.addedcss = append(this.addedcss, name)
	}
}