Пример #1
0
func getEncodingFromAccept(produces []string, r *request.Request) (string, *response.Error) {
	var encoding string

	for _, acceptMime := range strings.Split(r.Header.Get(constants.HEADER_Accept), ",") {
		mime := strings.Trim(strings.Split(acceptMime, ";")[0], " ")
		if 0 == len(mime) || mime == "*/*" {
			if len(produces) == 0 {
				encoding = defaults.MimeType
				break
			} else {
				encoding = produces[0]
				break
			}
		} else {
			if util.ContainsString(produces, mime) {
				encoding = mime
				break
			}
		}
	}

	if len(encoding) == 0 {
		return encoding, response.NewError(406, "Request", "Encoding requested not valid.")
	}

	return encoding, nil
}
Пример #2
0
func CheckSchemes(schemes []string) MiddlewareFunc {
	return MiddlewareFunc(func(next Handler) Handler {
		return HandlerFunc(func(w *response.Response, r *request.Request) {

			var schemeAccepted bool
			if r.URL.Scheme == "" {
				r.URL.Scheme = constants.SCHEME_HTTP
			}

			schemeAccepted = util.ContainsString(schemes, r.URL.Scheme)
			if !schemeAccepted {
				return
			}

			next.ServeHTTP(w, r)
		})
	})
}