diff --git a/HISTORY.md b/HISTORY.md index 7096ff60..32fb6460 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,8 +8,18 @@ ## 6.1.4 -> 6.2.0 (√Νεxτ) +_Update: 12 March 2017_ -_Last update: 28 Feb 2017_ +- Enhance Custom http errors with gzip and static files handler, as requested/reported [here](http://support.iris-go.com/d/17-fallback-handler-for-non-matched-routes/9). +- Enhance per-party custom http errors (now it works on any wildcard path too). +- Add a third parameter on `app.OnError(...)` for custom http errors with regexp validation, see [status_test.go](https://github.com/kataras/iris/blob/v6/status_test.go) for an example. +- Add a `context.ParamIntWildcard(...)` to skip the first slash, useful for wildcarded paths' parameters. + + +> Prepare for nice things, tomorrow is Iris' first birthday! + + +_Update: 28 Feb 2017_ > Note: I want you to know that I spent more than 200 hours (16 days of ~10-15 hours per-day, do the math) for this release, two days to write these changes, please read the sections before think that you have an issue and post a new question, thanks! diff --git a/context.go b/context.go index ab6c4d78..d90b5b01 100644 --- a/context.go +++ b/context.go @@ -164,8 +164,12 @@ var _ ContextPool = &contextPool{} func (c *contextPool) Acquire(w http.ResponseWriter, r *http.Request) *Context { ctx := c.pool.Get().(*Context) + ctx.Middleware = nil + ctx.session = nil ctx.ResponseWriter = acquireResponseWriter(w) ctx.Request = r + ctx.values = ctx.values[0:0] + return ctx } @@ -173,13 +177,8 @@ func (c *contextPool) Release(ctx *Context) { // flush the body (on recorder) or just the status code (on basic response writer) // when all finished ctx.ResponseWriter.flushResponse() - - ctx.Middleware = nil - ctx.session = nil - ctx.Request = nil ///TODO: ctx.ResponseWriter.releaseMe() - ctx.values.Reset() c.pool.Put(ctx) } @@ -810,8 +809,8 @@ func (ctx *Context) WriteGzip(b []byte) (int, error) { ctx.ResponseWriter.Header().Add(varyHeader, acceptEncodingHeader) gzipWriter := acquireGzipWriter(ctx.ResponseWriter) + defer releaseGzipWriter(gzipWriter) n, err := gzipWriter.Write(b) - releaseGzipWriter(gzipWriter) if err == nil { ctx.SetHeader(contentEncodingHeader, "gzip") @@ -1307,6 +1306,28 @@ func (ctx *Context) ParamInt(key string) (int, error) { return ctx.GetInt(key) } +// ParamIntWildcard removes the first slash if found and +// returns the int representation of the key's wildcard path parameter's value. +// +// Returns -1 with an error if the parameter couldn't be found. +func (ctx *Context) ParamIntWildcard(key string) (int, error) { + v := ctx.Get(key) + if v != nil { + if vint, ok := v.(int); ok { + return vint, nil + } else if vstring, sok := v.(string); sok { + if len(vstring) > 1 { + if vstring[0] == '/' { + vstring = vstring[1:] + } + } + return strconv.Atoi(vstring) + } + } + + return -1, errIntParse.Format(v) +} + // ParamInt64 returns the int64 representation of the key's path named parameter's value func (ctx *Context) ParamInt64(key string) (int64, error) { return strconv.ParseInt(ctx.Param(key), 10, 64) diff --git a/fs.go b/fs.go index e54b7f05..221053f9 100644 --- a/fs.go +++ b/fs.go @@ -1,12 +1,23 @@ package iris import ( + "fmt" + "io" "mime" + "mime/multipart" "net/http" + "net/textproto" + "net/url" "os" + "path" "path/filepath" + "sort" + "strconv" "strings" "sync" + "time" + + "github.com/kataras/go-errors" ) // StaticHandlerBuilder is the web file system's Handler builder @@ -149,26 +160,55 @@ func (w *fsHandler) Build() HandlerFunc { w.once.Do(func() { w.filesystem = w.directory - // set the filesystem to itself in order to be recognised of listing property (can be change at runtime too) - fileserver := http.FileServer(w) - fsHandler := fileserver - if w.stripPath { - prefix := w.requestPath - fsHandler = http.StripPrefix(prefix, fileserver) - } - - h := func(ctx *Context) { - writer := ctx.ResponseWriter - - if w.gzip && ctx.clientAllowsGzip() { - ctx.ResponseWriter.Header().Add(varyHeader, acceptEncodingHeader) - ctx.SetHeader(contentEncodingHeader, "gzip") - gzipResWriter := acquireGzipResponseWriter(ctx.ResponseWriter) //.ResponseWriter) - writer = gzipResWriter - defer releaseGzipResponseWriter(gzipResWriter) + hStatic := func(ctx *Context) { + upath := ctx.Request.URL.Path + if !strings.HasPrefix(upath, "/") { + upath = "/" + upath + ctx.Request.URL.Path = upath } - fsHandler.ServeHTTP(writer, ctx.Request) + // Note the request.url.path is changed but request.RequestURI is not + // so on custom errors we use the requesturi instead. + // this can be changed + _, prevStatusCode := serveFile(ctx, + w.filesystem, + path.Clean(upath), + false, + w.listDirectories, + (w.gzip && ctx.clientAllowsGzip()), + ) + + // check for any http errors after the file handler executed + if prevStatusCode >= 400 { // error found (404 or 400 or 500 usually) + if writer, ok := ctx.ResponseWriter.(*gzipResponseWriter); ok && writer != nil { + writer.ResetBody() + writer.Disable() + // ctx.ResponseWriter.Header().Del(contentType) // application/x-gzip sometimes lawl + // remove gzip headers + headers := ctx.ResponseWriter.Header() + headers[contentType] = nil + headers["X-Content-Type-Options"] = nil + headers[varyHeader] = nil + headers[contentEncodingHeader] = nil + headers[contentLength] = nil + } + // execute any custom error handler (per-party or global, if not found then it creates a new one and fires it) + ctx.Framework().Router.Errors.Fire(prevStatusCode, ctx) + return + } + + // go to the next middleware + if ctx.Pos < len(ctx.Middleware)-1 { + ctx.Next() + } + } + + var fileserver HandlerFunc + + if w.stripPath { + fileserver = StripPrefix(w.requestPath, hStatic) + } else { + fileserver = hStatic } if len(w.exceptions) > 0 { @@ -176,15 +216,17 @@ func (w *fsHandler) Build() HandlerFunc { for i := range w.exceptions { middleware[i] = Prioritize(w.exceptions[i]) } - middleware[len(w.exceptions)] = HandlerFunc(h) + middleware[len(w.exceptions)] = HandlerFunc(fileserver) w.handler = func(ctx *Context) { ctx.Middleware = append(middleware, ctx.Middleware...) ctx.Do() } - } else { - w.handler = h + return } + + w.handler = fileserver + }) return w.handler @@ -209,6 +251,683 @@ func StripPrefix(prefix string, h HandlerFunc) HandlerFunc { } } +// +------------------------------------------------------------+ +// | | +// | serve file handler | +// | edited from net/http/fs.go in order to support GZIP with | +// | custom iris http errors and fallback to non-compressed data| +// | when not supported. | +// | | +// +------------------------------------------------------------+ + +var htmlReplacer = strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", + // """ is shorter than """. + `"`, """, + // "'" is shorter than "'" and apos was not in HTML until HTML5. + "'", "'", +) + +func dirList(ctx *Context, f http.File) (string, int) { + dirs, err := f.Readdir(-1) + if err != nil { + // TODO: log err.Error() to the Server.ErrorLog, once it's possible + // for a handler to get at its Server via the http.ResponseWriter. See + // Issue 12438. + return "Error reading directory", StatusInternalServerError + + } + sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) + + ctx.ResponseWriter.Header().Set(contentType, contentHTML+"; charset="+ctx.Framework().Config.Charset) + fmt.Fprintf(ctx.ResponseWriter, "
\n") + for _, d := range dirs { + name := d.Name() + if d.IsDir() { + name += "/" + } + // name may contain '?' or '#', which must be escaped to remain + // part of the URL path, and not indicate the start of a query + // string or fragment. + url := url.URL{Path: name} + fmt.Fprintf(ctx.ResponseWriter, "%s\n", url.String(), htmlReplacer.Replace(name)) + } + fmt.Fprintf(ctx.ResponseWriter, "\n") + return "", StatusOK +} + +// errSeeker is returned by ServeContent's sizeFunc when the content +// doesn't seek properly. The underlying Seeker's error text isn't +// included in the sizeFunc reply so it's not sent over HTTP to end +// users. +var errSeeker = errors.New("seeker can't seek") + +// errNoOverlap is returned by serveContent's parseRange if first-byte-pos of +// all of the byte-range-spec values is greater than the content size. +var errNoOverlap = errors.New("invalid range: failed to overlap") + +// The algorithm uses at most sniffLen bytes to make its decision. +const sniffLen = 512 + +// if name is empty, filename is unknown. (used for mime type, before sniffing) +// if modtime.IsZero(), modtime is unknown. +// content must be seeked to the beginning of the file. +// The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response. +func serveContent(ctx *Context, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker, gzip bool) (string, int) /* we could use the TransactionErrResult but prefer not to create new objects for each of the errors on static file handlers*/ { + + setLastModified(ctx, modtime) + done, rangeReq := checkPreconditions(ctx, modtime) + if done { + return "", StatusNotModified + } + + code := StatusOK + + // If Content-Type isn't set, use the file's extension to find it, but + // if the Content-Type is unset explicitly, do not sniff the type. + ctypes, haveType := ctx.ResponseWriter.Header()[contentType] + var ctype string + if !haveType { + ctype = typeByExtension(filepath.Ext(name)) + if ctype == "" { + // read a chunk to decide between utf-8 text and binary + var buf [sniffLen]byte + n, _ := io.ReadFull(content, buf[:]) + ctype = http.DetectContentType(buf[:n]) + _, err := content.Seek(0, io.SeekStart) // rewind to output whole file + if err != nil { + return "seeker can't seek", StatusInternalServerError + + } + } + ctx.ResponseWriter.Header().Set(contentType, ctype) + } else if len(ctypes) > 0 { + ctype = ctypes[0] + } + + size, err := sizeFunc() + if err != nil { + return err.Error(), StatusInternalServerError + } + + // handle Content-Range header. + sendSize := size + var sendContent io.Reader = content + + if gzip { + // set the "Accept-Encoding" here in order to prevent the content-length header to be setted later on. + ctx.SetHeader(contentEncodingHeader, "gzip") + ctx.ResponseWriter.Header().Add(varyHeader, acceptEncodingHeader) + gzipResWriter := acquireGzipResponseWriter(ctx.ResponseWriter) + ctx.ResponseWriter = gzipResWriter + } + if size >= 0 { + ranges, err := parseRange(rangeReq, size) + if err != nil { + if err == errNoOverlap { + ctx.ResponseWriter.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", size)) + } + return err.Error(), StatusRequestedRangeNotSatisfiable + + } + if sumRangesSize(ranges) > size { + // The total number of bytes in all the ranges + // is larger than the size of the file by + // itself, so this is probably an attack, or a + // dumb client. Ignore the range request. + ranges = nil + } + switch { + case len(ranges) == 1: + // RFC 2616, Section 14.16: + // "When an HTTP message includes the content of a single + // range (for example, a response to a request for a + // single range, or to a request for a set of ranges + // that overlap without any holes), this content is + // transmitted with a Content-Range header, and a + // Content-Length header showing the number of bytes + // actually transferred. + // ... + // A response to a request for a single range MUST NOT + // be sent using the multipart/byteranges media type." + ra := ranges[0] + if _, err := content.Seek(ra.start, io.SeekStart); err != nil { + return err.Error(), StatusRequestedRangeNotSatisfiable + } + sendSize = ra.length + code = StatusPartialContent + ctx.ResponseWriter.Header().Set("Content-Range", ra.contentRange(size)) + case len(ranges) > 1: + sendSize = rangesMIMESize(ranges, ctype, size) + code = StatusPartialContent + + pr, pw := io.Pipe() + mw := multipart.NewWriter(pw) + ctx.ResponseWriter.Header().Set(contentType, "multipart/byteranges; boundary="+mw.Boundary()) + sendContent = pr + defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish. + go func() { + for _, ra := range ranges { + part, err := mw.CreatePart(ra.mimeHeader(ctype, size)) + if err != nil { + pw.CloseWithError(err) + return + } + if _, err := content.Seek(ra.start, io.SeekStart); err != nil { + pw.CloseWithError(err) + return + } + if _, err := io.CopyN(part, content, ra.length); err != nil { + pw.CloseWithError(err) + return + } + } + mw.Close() + pw.Close() + }() + } + ctx.ResponseWriter.Header().Set("Accept-Ranges", "bytes") + if ctx.ResponseWriter.Header().Get(contentEncodingHeader) == "" { + + ctx.ResponseWriter.Header().Set(contentLength, strconv.FormatInt(sendSize, 10)) + } + } + + ctx.SetStatusCode(code) + + if ctx.Method() != MethodHead { + io.CopyN(ctx.ResponseWriter, sendContent, sendSize) + } + + return "", code +} + +// scanETag determines if a syntactically valid ETag is present at s. If so, +// the ETag and remaining text after consuming ETag is returned. Otherwise, +// it returns "", "". +func scanETag(s string) (etag string, remain string) { + s = textproto.TrimString(s) + start := 0 + if strings.HasPrefix(s, "W/") { + start = 2 + } + if len(s[start:]) < 2 || s[start] != '"' { + return "", "" + } + // ETag is either W/"text" or "text". + // See RFC 7232 2.3. + for i := start + 1; i < len(s); i++ { + c := s[i] + switch { + // Character values allowed in ETags. + case c == 0x21 || c >= 0x23 && c <= 0x7E || c >= 0x80: + case c == '"': + return string(s[:i+1]), s[i+1:] + default: + break + } + } + return "", "" +} + +// etagStrongMatch reports whether a and b match using strong ETag comparison. +// Assumes a and b are valid ETags. +func etagStrongMatch(a, b string) bool { + return a == b && a != "" && a[0] == '"' +} + +// etagWeakMatch reports whether a and b match using weak ETag comparison. +// Assumes a and b are valid ETags. +func etagWeakMatch(a, b string) bool { + return strings.TrimPrefix(a, "W/") == strings.TrimPrefix(b, "W/") +} + +// condResult is the result of an HTTP request precondition check. +// See https://tools.ietf.org/html/rfc7232 section 3. +type condResult int + +const ( + condNone condResult = iota + condTrue + condFalse +) + +func checkIfMatch(ctx *Context) condResult { + im := ctx.Request.Header.Get("If-Match") + if im == "" { + return condNone + } + for { + im = textproto.TrimString(im) + if len(im) == 0 { + break + } + if im[0] == ',' { + im = im[1:] + continue + } + if im[0] == '*' { + return condTrue + } + etag, remain := scanETag(im) + if etag == "" { + break + } + if etagStrongMatch(etag, ctx.ResponseWriter.Header().Get("Etag")) { + return condTrue + } + im = remain + } + + return condFalse +} + +func checkIfUnmodifiedSince(ctx *Context, modtime time.Time) condResult { + ius := ctx.Request.Header.Get("If-Unmodified-Since") + if ius == "" || isZeroTime(modtime) { + return condNone + } + if t, err := http.ParseTime(ius); err == nil { + // The Date-Modified header truncates sub-second precision, so + // use mtime < t+1s instead of mtime <= t to check for unmodified. + if modtime.Before(t.Add(1 * time.Second)) { + return condTrue + } + return condFalse + } + return condNone +} + +func checkIfNoneMatch(ctx *Context) condResult { + inm := ctx.Request.Header.Get("If-None-Match") + if inm == "" { + return condNone + } + buf := inm + for { + buf = textproto.TrimString(buf) + if len(buf) == 0 { + break + } + if buf[0] == ',' { + buf = buf[1:] + } + if buf[0] == '*' { + return condFalse + } + etag, remain := scanETag(buf) + if etag == "" { + break + } + if etagWeakMatch(etag, ctx.ResponseWriter.Header().Get("Etag")) { + return condFalse + } + buf = remain + } + return condTrue +} + +func checkIfModifiedSince(ctx *Context, modtime time.Time) condResult { + if ctx.Method() != MethodGet && ctx.Method() != MethodHead { + return condNone + } + ims := ctx.Request.Header.Get("If-Modified-Since") + if ims == "" || isZeroTime(modtime) { + return condNone + } + t, err := http.ParseTime(ims) + if err != nil { + return condNone + } + // The Date-Modified header truncates sub-second precision, so + // use mtime < t+1s instead of mtime <= t to check for unmodified. + if modtime.Before(t.Add(1 * time.Second)) { + return condFalse + } + return condTrue +} + +func checkIfRange(ctx *Context, modtime time.Time) condResult { + if ctx.Method() != MethodGet { + return condNone + } + ir := ctx.Request.Header.Get("If-Range") + if ir == "" { + return condNone + } + etag, _ := scanETag(ir) + if etag != "" { + if etagStrongMatch(etag, ctx.ResponseWriter.Header().Get("Etag")) { + return condTrue + } + return condFalse + + } + // The If-Range value is typically the ETag value, but it may also be + // the modtime date. See golang.org/issue/8367. + if modtime.IsZero() { + return condFalse + } + t, err := http.ParseTime(ir) + if err != nil { + return condFalse + } + if t.Unix() == modtime.Unix() { + return condTrue + } + return condFalse +} + +var unixEpochTime = time.Unix(0, 0) + +// isZeroTime reports whether t is obviously unspecified (either zero or Unix()=0). +func isZeroTime(t time.Time) bool { + return t.IsZero() || t.Equal(unixEpochTime) +} + +func setLastModified(ctx *Context, modtime time.Time) { + if !isZeroTime(modtime) { + ctx.SetHeader("Last-Modified", modtime.UTC().Format(ctx.Framework().Config.TimeFormat)) + } +} + +func writeNotModified(ctx *Context) { + // RFC 7232 section 4.1: + // a sender SHOULD NOT generate representation metadata other than the + // above listed fields unless said metadata exists for the purpose of + // guiding cache updates (e.g., Last-Modified might be useful if the + // response does not have an ETag field). + h := ctx.ResponseWriter.Header() + delete(h, contentType) + + delete(h, contentLength) + if h.Get("Etag") != "" { + delete(h, "Last-Modified") + } + ctx.SetStatusCode(StatusNotModified) +} + +// checkPreconditions evaluates request preconditions and reports whether a precondition +// resulted in sending StatusNotModified or StatusPreconditionFailed. +func checkPreconditions(ctx *Context, modtime time.Time) (done bool, rangeHeader string) { + // This function carefully follows RFC 7232 section 6. + ch := checkIfMatch(ctx) + if ch == condNone { + ch = checkIfUnmodifiedSince(ctx, modtime) + } + if ch == condFalse { + + ctx.SetStatusCode(StatusPreconditionFailed) + return true, "" + } + switch checkIfNoneMatch(ctx) { + case condFalse: + if ctx.Method() == MethodGet || ctx.Method() == MethodHead { + writeNotModified(ctx) + return true, "" + } + ctx.SetStatusCode(StatusPreconditionFailed) + return true, "" + + case condNone: + if checkIfModifiedSince(ctx, modtime) == condFalse { + writeNotModified(ctx) + return true, "" + } + } + + rangeHeader = ctx.Request.Header.Get("Range") + if rangeHeader != "" { + if checkIfRange(ctx, modtime) == condFalse { + rangeHeader = "" + } + } + return false, rangeHeader +} + +// name is '/'-separated, not filepath.Separator. +func serveFile(ctx *Context, fs http.FileSystem, name string, redirect bool, showList bool, gzip bool) (string, int) { + const indexPage = "/index.html" + + // redirect .../index.html to .../ + // can't use Redirect() because that would make the path absolute, + // which would be a problem running under StripPrefix + if strings.HasSuffix(ctx.Request.URL.Path, indexPage) { + localRedirect(ctx, "./") + return "", StatusMovedPermanently + } + + f, err := fs.Open(name) + if err != nil { + msg, code := toHTTPError(err) + return msg, code + } + defer f.Close() + + d, err := f.Stat() + if err != nil { + msg, code := toHTTPError(err) + return msg, code + + } + + if redirect { + // redirect to canonical path: / at end of directory url + // ctx.Request.URL.Path always begins with / + url := ctx.Request.URL.Path + if d.IsDir() { + if url[len(url)-1] != '/' { + localRedirect(ctx, path.Base(url)+"/") + return "", StatusMovedPermanently + } + } else { + if url[len(url)-1] == '/' { + localRedirect(ctx, "../"+path.Base(url)) + return "", StatusMovedPermanently + } + } + } + + // redirect if the directory name doesn't end in a slash + if d.IsDir() { + url := ctx.Request.URL.Path + if url[len(url)-1] != '/' { + localRedirect(ctx, path.Base(url)+"/") + return "", StatusMovedPermanently + } + } + + // use contents of index.html for directory, if present + if d.IsDir() { + index := strings.TrimSuffix(name, "/") + indexPage + ff, err := fs.Open(index) + if err == nil { + defer ff.Close() + dd, err := ff.Stat() + if err == nil { + name = index + d = dd + f = ff + } + } + } + + // Still a directory? (we didn't find an index.html file) + if d.IsDir() { + if !showList { + return "", StatusForbidden + } + if checkIfModifiedSince(ctx, d.ModTime()) == condFalse { + writeNotModified(ctx) + return "", StatusNotModified + } + ctx.ResponseWriter.Header().Set("Last-Modified", d.ModTime().UTC().Format(ctx.Framework().Config.TimeFormat)) + return dirList(ctx, f) + + } + + // serveContent will check modification time + sizeFunc := func() (int64, error) { return d.Size(), nil } + return serveContent(ctx, d.Name(), d.ModTime(), sizeFunc, f, gzip) +} + +// toHTTPError returns a non-specific HTTP error message and status code +// for a given non-nil error value. It's important that toHTTPError does not +// actually return err.Error(), since msg and httpStatus are returned to users, +// and historically Go's ServeContent always returned just "404 Not Found" for +// all errors. We don't want to start leaking information in error messages. +func toHTTPError(err error) (msg string, httpStatus int) { + if os.IsNotExist(err) { + return "404 page not found", StatusNotFound + } + if os.IsPermission(err) { + return "403 Forbidden", StatusForbidden + } + // Default: + return "500 Internal Server Error", StatusInternalServerError +} + +// localRedirect gives a Moved Permanently response. +// It does not convert relative paths to absolute paths like Redirect does. +func localRedirect(ctx *Context, newPath string) { + if q := ctx.Request.URL.RawQuery; q != "" { + newPath += "?" + q + } + ctx.ResponseWriter.Header().Set("Location", newPath) + ctx.SetStatusCode(StatusMovedPermanently) +} + +func containsDotDot(v string) bool { + if !strings.Contains(v, "..") { + return false + } + for _, ent := range strings.FieldsFunc(v, isSlashRune) { + if ent == ".." { + return true + } + } + return false +} + +func isSlashRune(r rune) bool { return r == '/' || r == '\\' } + +// httpRange specifies the byte range to be sent to the client. +type httpRange struct { + start, length int64 +} + +func (r httpRange) contentRange(size int64) string { + return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size) +} + +func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader { + return textproto.MIMEHeader{ + "Content-Range": {r.contentRange(size)}, + contentType: {contentType}, + } +} + +// parseRange parses a Range header string as per RFC 2616. +// errNoOverlap is returned if none of the ranges overlap. +func parseRange(s string, size int64) ([]httpRange, error) { + if s == "" { + return nil, nil // header not present + } + const b = "bytes=" + if !strings.HasPrefix(s, b) { + return nil, errors.New("invalid range") + } + var ranges []httpRange + noOverlap := false + for _, ra := range strings.Split(s[len(b):], ",") { + ra = strings.TrimSpace(ra) + if ra == "" { + continue + } + i := strings.Index(ra, "-") + if i < 0 { + return nil, errors.New("invalid range") + } + start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:]) + var r httpRange + if start == "" { + // If no start is specified, end specifies the + // range start relative to the end of the file. + i, err := strconv.ParseInt(end, 10, 64) + if err != nil { + return nil, errors.New("invalid range") + } + if i > size { + i = size + } + r.start = size - i + r.length = size - r.start + } else { + i, err := strconv.ParseInt(start, 10, 64) + if err != nil || i < 0 { + return nil, errors.New("invalid range") + } + if i >= size { + // If the range begins after the size of the content, + // then it does not overlap. + noOverlap = true + continue + } + r.start = i + if end == "" { + // If no end is specified, range extends to end of the file. + r.length = size - r.start + } else { + i, err := strconv.ParseInt(end, 10, 64) + if err != nil || r.start > i { + return nil, errors.New("invalid range") + } + if i >= size { + i = size - 1 + } + r.length = i - r.start + 1 + } + } + ranges = append(ranges, r) + } + if noOverlap && len(ranges) == 0 { + // The specified ranges did not overlap with the content. + return nil, errNoOverlap + } + return ranges, nil +} + +// countingWriter counts how many bytes have been written to it. +type countingWriter int64 + +func (w *countingWriter) Write(p []byte) (n int, err error) { + *w += countingWriter(len(p)) + return len(p), nil +} + +// rangesMIMESize returns the number of bytes it takes to encode the +// provided ranges as a multipart response. +func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) { + var w countingWriter + mw := multipart.NewWriter(&w) + for _, ra := range ranges { + mw.CreatePart(ra.mimeHeader(contentType, contentSize)) + encSize += ra.length + } + mw.Close() + encSize += int64(w) + return +} + +func sumRangesSize(ranges []httpRange) (size int64) { + for _, ra := range ranges { + size += ra.length + } + return +} + // typeByExtension returns the MIME type associated with the file extension ext. // The extension ext should begin with a leading dot, as in ".html". // When ext has no associated type, typeByExtension returns "". diff --git a/policy.go b/policy.go index 08a0c367..bc30822f 100644 --- a/policy.go +++ b/policy.go @@ -294,6 +294,10 @@ type ( // RouterWrapperPolicy is the Policy which enables a wrapper on the top of // the builded Router. Usually it's useful for third-party middleware // when need to wrap the entire application with a middleware like CORS. + // + // Developers can Adapt more than one RouterWrapper + // those wrappers' execution comes from last to first. + // That means that the second wrapper will wrap the first, and so on. RouterWrapperPolicy func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) ) @@ -364,8 +368,26 @@ func (r RouterBuilderPolicy) Adapt(frame *Policies) { } // Adapt adaps a RouterWrapperPolicy object to the main *Policies. -func (r RouterWrapperPolicy) Adapt(frame *Policies) { - frame.RouterWrapperPolicy = r +func (rw RouterWrapperPolicy) Adapt(frame *Policies) { + if rw != nil { + wrapper := rw + prevWrapper := frame.RouterWrapperPolicy + + if prevWrapper != nil { + nextWrapper := rw + wrapper = func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + if next != nil { + nexthttpFunc := http.HandlerFunc(func(_w http.ResponseWriter, _r *http.Request) { + prevWrapper(_w, _r, next) + }) + nextWrapper(w, r, nexthttpFunc) + } + + } + } + frame.RouterWrapperPolicy = wrapper + } + } // RenderPolicy is the type which you can adapt custom renderers diff --git a/policy_routerwrapper_test.go b/policy_routerwrapper_test.go new file mode 100644 index 00000000..1d4b0bbc --- /dev/null +++ b/policy_routerwrapper_test.go @@ -0,0 +1,50 @@ +package iris_test + +import ( + "net/http" + "testing" + + . "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" + "gopkg.in/kataras/iris.v6/httptest" +) + +func TestRouterWrapperPolicySimple(t *testing.T) { + w1 := RouterWrapperPolicy(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + w.Write([]byte("DATA ")) + next(w, r) // continue to the main router + }) + + w2 := RouterWrapperPolicy(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + if r.RequestURI == "/" { + next(w, r) // continue to w1 + return + } + // else don't execute the router and the handler and fire not found + w.WriteHeader(StatusNotFound) + }) + + app := New() + app.Adapt( + httprouter.New(), + w1, // order matters, second wraps the first and so on, so the last(w2) is responsible to execute the next wrapper (if more than one) and the router + w2, + // w2 -> w1 -> httprouter -> handler + ) + + app.OnError(StatusNotFound, func(ctx *Context) { + ctx.Writef("not found") + }) + + app.Get("/", func(ctx *Context) { + ctx.Write([]byte("OK")) + }) + + app.Get("/routerDoesntContinue", func(ctx *Context) { + + }) + + e := httptest.New(app, t) + e.GET("/").Expect().Status(StatusOK).Body().Equal("DATA OK") + e.GET("/routerDoesntContinue").Expect().Status(StatusNotFound) +} diff --git a/response_recorder.go b/response_recorder.go index 956ba066..cd79bd73 100644 --- a/response_recorder.go +++ b/response_recorder.go @@ -18,15 +18,14 @@ func acquireResponseRecorder(underline *responseWriter) *ResponseRecorder { w := rrpool.Get().(*ResponseRecorder) w.responseWriter = underline w.headers = underline.Header() + w.ResetBody() return w } func releaseResponseRecorder(w *ResponseRecorder) { - w.ResetBody() if w.responseWriter != nil { releaseResponseWriter(w.responseWriter) } - rrpool.Put(w) } @@ -124,9 +123,18 @@ func (w *ResponseRecorder) ResetHeaders() { w.headers = w.responseWriter.Header() } +// ReseAllHeaders clears all headers, both temp and underline's response writer +func (w *ResponseRecorder) ReseAllHeaders() { + w.headers = http.Header{} + h := w.responseWriter.Header() + for k := range h { + h[k] = nil + } +} + // Reset resets the response body, headers and the status code header func (w *ResponseRecorder) Reset() { - w.ResetHeaders() + w.ReseAllHeaders() w.statusCode = StatusOK w.ResetBody() } @@ -135,9 +143,12 @@ func (w *ResponseRecorder) Reset() { // called automatically at the end of each request, see ReleaseCtx func (w *ResponseRecorder) flushResponse() { if w.headers != nil { + h := w.responseWriter.Header() + for k, values := range w.headers { + h[k] = nil for i := range values { - w.responseWriter.Header().Add(k, values[i]) + h.Add(k, values[i]) } } } diff --git a/response_writer.go b/response_writer.go index ad88e3dd..bb05e201 100644 --- a/response_writer.go +++ b/response_writer.go @@ -13,7 +13,9 @@ import ( type gzipResponseWriter struct { ResponseWriter - gzipWriter *gzip.Writer + gzipWriter *gzip.Writer // it contains the underline writer too + chunks []byte + disabled bool } var gzpool = sync.Pool{New: func() interface{} { return &gzipResponseWriter{} }} @@ -22,6 +24,8 @@ func acquireGzipResponseWriter(underline ResponseWriter) *gzipResponseWriter { w := gzpool.Get().(*gzipResponseWriter) w.ResponseWriter = underline w.gzipWriter = acquireGzipWriter(w.ResponseWriter) + w.chunks = w.chunks[0:0] + w.disabled = false return w } @@ -32,7 +36,32 @@ func releaseGzipResponseWriter(w *gzipResponseWriter) { // Write compresses and writes that data to the underline response writer func (w *gzipResponseWriter) Write(contents []byte) (int, error) { - return w.gzipWriter.Write(contents) + // save the contents to serve them (only gzip data here) + w.chunks = append(w.chunks, contents...) + return len(w.chunks), nil +} + +func (w *gzipResponseWriter) flushResponse() { + if w.disabled { + w.ResponseWriter.Write(w.chunks) + } else { + // if it's not disable write all chunks gzip compressed + w.gzipWriter.Write(w.chunks) // it writes to the underline responseWriter (look acquireGzipResponseWriter) + } + w.ResponseWriter.flushResponse() +} + +func (w *gzipResponseWriter) ResetBody() { + w.chunks = w.chunks[0:0] +} + +func (w *gzipResponseWriter) Disable() { + w.disabled = true +} + +func (w *gzipResponseWriter) releaseMe() { + releaseGzipResponseWriter(w) + w.ResponseWriter.releaseMe() } var rpool = sync.Pool{New: func() interface{} { return &responseWriter{statusCode: StatusOK} }} diff --git a/router.go b/router.go index df0b0ed1..3f207f9b 100644 --- a/router.go +++ b/router.go @@ -348,16 +348,6 @@ func (router *Router) Any(registeredPath string, handlersFn ...HandlerFunc) { } } -// if / then returns /*wildcard or /something then /something/*wildcard -// if empty then returns /*wildcard too -func validateWildcard(reqPath string, paramName string) string { - if reqPath[len(reqPath)-1] != slashByte { - reqPath += slash - } - reqPath += "*" + paramName - return reqPath -} - func (router *Router) registerResourceRoute(reqPath string, h HandlerFunc) RouteInfo { router.Head(reqPath, h) return router.Get(reqPath, h) @@ -582,26 +572,12 @@ func (router *Router) StaticHandler(reqPath string, systemPath string, showList path = fullpath[dotWSlashIdx+1:] } - h := NewStaticHandlerBuilder(systemPath). + return NewStaticHandlerBuilder(systemPath). Path(path). Listing(showList). Gzip(enableGzip). Except(exceptRoutes...). Build() - - managedStaticHandler := func(ctx *Context) { - h(ctx) - prevStatusCode := ctx.ResponseWriter.StatusCode() - if prevStatusCode >= 400 { // we have an error - // fire the custom error handler - router.Errors.Fire(prevStatusCode, ctx) - } - // go to the next middleware - if ctx.Pos < len(ctx.Middleware)-1 { - ctx.Next() - } - } - return managedStaticHandler } // StaticWeb returns a handler that serves HTTP requests @@ -623,7 +599,7 @@ func (router *Router) StaticHandler(reqPath string, systemPath string, showList func (router *Router) StaticWeb(reqPath string, systemPath string, exceptRoutes ...RouteInfo) RouteInfo { h := router.StaticHandler(reqPath, systemPath, false, false, exceptRoutes...) paramName := "file" - routePath := validateWildcard(reqPath, paramName) + routePath := router.Context.Framework().RouteWildcardPath(reqPath, paramName) handler := func(ctx *Context) { h(ctx) if fname := ctx.Param(paramName); fname != "" { @@ -659,10 +635,22 @@ func (router *Router) Layout(tmplLayoutFile string) *Router { return router } -// OnError registers a custom http error handler -func (router *Router) OnError(statusCode int, handlerFn HandlerFunc) { +// OnError registers a custom http error handler. +// Accepts the status code which the 'handlerFn' should be fired, +// third parameter is OPTIONAL, if setted then the 'handlerFn' will be executed +// only when this regex expression will be validated and matched to the ctx.Request.RequestURI, +// keep note that it will compare the whole request path but the error handler owns to this Party, +// this gives developers power tooling. +// +// Each party's static path prefix can have a set of its own error handlers, +// this works by wrapping the error handlers, so if a party doesn't needs a custom error handler, +// then it will execute the root's one (if setted, otherwise the framework creates one at runtime). +func (router *Router) OnError(statusCode int, handlerFn HandlerFunc, rgexp ...string) { staticPath := router.Context.Framework().policies.RouterReversionPolicy.StaticPath(router.relativePath) - + expr := "" + if len(rgexp) > 0 { + expr = rgexp[0] + } if staticPath == "/" { router.Errors.Register(statusCode, handlerFn) // register the user-specific error message, as the global error handler, for now. return @@ -676,9 +664,12 @@ func (router *Router) OnError(statusCode int, handlerFn HandlerFunc) { // get the previous prevErrHandler := router.Errors.GetOrRegister(statusCode) - func(statusCode int, staticPath string, prevErrHandler Handler, newHandler Handler) { // to separate the logic + func(statusCode int, staticPath string, prevErrHandler Handler, newHandler Handler, expr string) { // to separate the logic errHandler := HandlerFunc(func(ctx *Context) { - if strings.HasPrefix(ctx.Path(), staticPath) { // yes the user should use OnError from longest to lower static path's length in order this to work, so we can find another way, like a builder on the end. + + path := ctx.Request.RequestURI // This is relative to: http://support.iris-go.com/d/17-fallback-handler-for-non-matched-routes + // note: NOT Request.URL.Path (even if it's not overriden by static handlers.) + if strings.HasPrefix(path, staticPath) { // yes the user should use OnError from longest to lower static path's length in order this to work, so we can find another way, like a builder on the end. newHandler.Serve(ctx) return } @@ -686,9 +677,8 @@ func (router *Router) OnError(statusCode int, handlerFn HandlerFunc) { prevErrHandler.Serve(ctx) }) - router.Errors.Register(statusCode, errHandler) - }(statusCode, staticPath, prevErrHandler, handlerFn) - + router.Errors.RegisterRegex(statusCode, errHandler, expr) + }(statusCode, staticPath, prevErrHandler, handlerFn, expr) } // EmitError fires a custom http error handler to the client diff --git a/status.go b/status.go index e34c2e20..defa3520 100644 --- a/status.go +++ b/status.go @@ -1,6 +1,7 @@ package iris import ( + "regexp" "sync" ) @@ -153,7 +154,7 @@ type ErrorHandlers struct { mu sync.RWMutex } -// Register registers a handler to a http status +// Register registers a handler to a http status. func (e *ErrorHandlers) Register(statusCode int, handler Handler) { e.mu.Lock() if e.handlers == nil { @@ -171,6 +172,39 @@ func (e *ErrorHandlers) Register(statusCode int, handler Handler) { e.mu.Unlock() } +// RegisterRegex same as Register but it receives a third parameter which is the regex expression +// which is running versus the REQUESTED PATH, i.e "/api/users/42". +// +// If the match against the REQUEST PATH and the 'expr' failed then +// the previous registered error handler on this specific 'statusCode' will be executed. +// +// Returns an error if regexp.Compile failed, nothing special. +func (e *ErrorHandlers) RegisterRegex(statusCode int, handler Handler, expr string) error { + // if expr is empty, skip the validation and set the error handler as it's + if expr == "" { + e.Register(statusCode, handler) + return nil + } + r, err := regexp.Compile(expr) + if err != nil { + return err + } + + prevHandler := e.GetOrRegister(statusCode) + + e.Register(statusCode, HandlerFunc(func(ctx *Context) { + requestPath := ctx.Request.RequestURI + + if r.MatchString(requestPath) { + handler.Serve(ctx) + return + } + prevHandler.Serve(ctx) + })) + + return nil +} + // Get returns the handler which is responsible for // this 'statusCode' http error. func (e *ErrorHandlers) Get(statusCode int) Handler { diff --git a/status_test.go b/status_test.go index 90308fd0..f260ddae 100644 --- a/status_test.go +++ b/status_test.go @@ -103,3 +103,63 @@ func TestStatusMethodNotAllowed(t *testing.T) { testStatusMethodNotAllowed(httprouter.New(), t) testStatusMethodNotAllowed(gorillamux.New(), t) } + +func testRegisterRegex(routerPolicy iris.Policy, t *testing.T) { + + app := iris.New() + app.Adapt(routerPolicy) + + h := func(ctx *iris.Context) { + ctx.WriteString(ctx.Method()) + } + + app.OnError(iris.StatusNotFound, func(ctx *iris.Context) { + ctx.WriteString("root 404") + }) + + staticP := app.Party("/static") + { + // or app.Errors... same thing + staticP.Errors.RegisterRegex(iris.StatusNotFound, iris.HandlerFunc(func(ctx *iris.Context) { + ctx.WriteString("/static 404") + }), "/static/[0-9]+") + + // simulate a StaticHandler or StaticWeb simple behavior + // in order to get a not found error from a wildcard path registered on the root path of the "/static". + // Note: + // RouteWildcardPath when you want to work on all router adaptors:httprouter=> *file, gorillamux=> {file:.*} + staticP.Get(app.RouteWildcardPath("/", "file"), func(ctx *iris.Context) { + + i, err := ctx.ParamIntWildcard("file") + if i > 0 || err == nil { + // this handler supposed to accept only strings, for the sake of the test. + ctx.EmitError(iris.StatusNotFound) + return + } + + ctx.SetStatusCode(iris.StatusOK) + }) + + } + + app.Get("/mypath", h) + + e := httptest.New(app, t) + // print("-------------------TESTING ") + // println(app.Config.Other[iris.RouterNameConfigKey].(string) + "-------------------") + + e.GET("/mypath").Expect().Status(iris.StatusOK).Body().Equal("GET") + e.GET("/rootnotfound").Expect().Status(iris.StatusNotFound).Body().Equal("root 404") + + // test found on party + e.GET("/static/itshouldbeok").Expect().Status(iris.StatusOK) + // test no found on party ( by putting at least one integer after /static) + e.GET("/static/42").Expect().Status(iris.StatusNotFound).Body().Equal("/static 404") + + // println("-------------------------------------------------------") +} + +func TestRegisterRegex(t *testing.T) { + testRegisterRegex(httprouter.New(), t) + testRegisterRegex(gorillamux.New(), t) +}