Example #1
0
func (ctx *Context) ServeStaticFile(filePath string, isAttachment bool) {
	ctx.isSelfResponse = true
	if ext.FileExists(filePath) {
		fileName := filepath.Base(filePath)
		if isAttachment {
			ctx.w.Header().Add("Content-Disposition", "attachment; filename="+fileName)
		} else {
			ctx.w.Header().Add("Content-Disposition", "inline; filename="+fileName)
		}
		http.ServeFile(ctx.w, ctx.r, filePath)
	} else {
		ctx.w.WriteHeader(http.StatusNotFound)
		ctx.w.Write([]byte("404 - Not found"))
	}
}
Example #2
0
File: gf.go Project: goframework/gf
func (*gfHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.Close = true

	path := r.URL.EscapedPath()

	if mForceHttps != 0 {
		if mServerHttpAddr == DEFAULT_HTTP_PORT && mServerHttpsAddr == DEFAULT_HTTPS_PORT {
			w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
		}

		if r.TLS == nil {
			host := getHost(r)
			httpsUrl := "https://" + host
			if mServerHttpsAddr != DEFAULT_HTTPS_PORT {
				httpsUrl = httpsUrl + mServerHttpsAddr
			}

			http.Redirect(w, r, httpsUrl, http.StatusFound)
			return
		}
	}

	if path == DEFAULT_FAVICON_PATH {
		path = mStaticWebPath + DEFAULT_FAVICON_PATH[1:]
	}

	if strings.HasPrefix(path, mStaticWebPath) {
		if r.Method == METHOD_GET {
			path = path[len(mStaticWebPath):]
			staticFile, err := filepath.Abs(mStaticDir + path)
			if err == nil && strings.HasPrefix(staticFile, mStaticDir) && ext.FileExists(staticFile) {
				w.Header().Add("Cache-Control", "max-age=0, must-revalidate")
				if mEnableGzip == 1 {
					fsgzip.ServeFile(w, r, staticFile)
				} else {
					http.ServeFile(w, r, staticFile)
				}
				return
			}
		}

		//Not GET method or file not exist
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("404 - Not found"))
		return
	}

	var context *Context = nil

	for _, pf := range mListFilter {
		if ext.WildMatch(pf.Pattern, path) {
			if context == nil {
				context = createContext(w, r)
				if context.DB != nil {
					defer context.DB.Close()
				}
				if !csrfProtectHTTP(context) {
					return
				}
			}

			pf.HandleFunc(context)

			if context.RedirectStatus != 0 {
				context.Session.Save(r, w)
				http.Redirect(w, r, context.RedirectPath, context.RedirectStatus)
				return
			}

			if context.FinishFilter {
				break
			}
		}
	}

	for _, pf := range mListHandle {
		methodMatched := ext.ArrayContains(pf.Methods, r.Method)
		if !methodMatched {
			continue
		}
		if vars, matched := ext.VarMatch(pf.Pattern, path); matched {
			/*-----------------------------*/
			if context == nil {
				context = createContext(w, r)
				if context.DB != nil {
					defer context.DB.Close()
				}
				if !csrfProtectHTTP(context) {
					return
				}
			}
			context.RouteVars = vars

			pf.HandleFunc(context)
			context.Session.Save(r, w)
			/*-----------------------------*/

			if context.RedirectStatus != 0 {
				http.Redirect(w, r, context.RedirectPath, context.RedirectStatus)
				return
			}

			if context.isSelfResponse {
				return
			}

			renderView(context)

			return
		}
	}

	w.WriteHeader(http.StatusNotFound)
	if mHandle404 != nil {
		if context == nil {
			context = createContext(w, r)
			if context.DB != nil {
				defer context.DB.Close()
			}
			if !csrfProtectHTTP(context) {
				return
			}
		}

		mHandle404(context)
		renderView(context)
	} else {
		w.Write([]byte("404 - Not found"))
	}
}