本文整理汇总了Golang中http.NotFound函数的典型用法代码示例。如果您正苦于以下问题:Golang NotFound函数的具体用法?Golang NotFound怎么用?Golang NotFound使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NotFound函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: serveFile
func serveFile(c *http.Conn, r *http.Request) {
path := pathutil.Join(".", r.URL.Path)
// pick off special cases and hand the rest to the standard file server
switch ext := pathutil.Ext(path); {
case r.URL.Path == "/":
serveHTMLDoc(c, r, "doc/root.html")
return
case r.URL.Path == "/doc/root.html":
// hide landing page from its real name
http.NotFound(c, r)
return
case ext == ".html":
if strings.HasSuffix(path, "/index.html") {
// We'll show index.html for the directory.
// Use the dir/ version as canonical instead of dir/index.html.
http.Redirect(c, r.URL.Path[0:len(r.URL.Path)-len("index.html")], http.StatusMovedPermanently)
return
}
serveHTMLDoc(c, r, path)
return
case ext == ".go":
serveGoSource(c, r, path)
return
}
dir, err := os.Lstat(path)
if err != nil {
http.NotFound(c, r)
return
}
if dir != nil && dir.IsDirectory() {
if redirect(c, r) {
return
}
if index := path + "/index.html"; isTextFile(index) {
serveHTMLDoc(c, r, index)
return
}
serveDirectory(c, r, path)
return
}
if isTextFile(path) {
serveTextFile(c, r, path)
return
}
fileServer.ServeHTTP(c, r)
}
示例2: fileSchemaRefFromBlob
// Given a described blob, optionally follows a camliContent and
// returns the file's schema blobref and its fileinfo (if found).
func (pr *publishRequest) fileSchemaRefFromBlob(des *search.DescribedBlob) (fileref *blobref.BlobRef, fileinfo *search.FileInfo, ok bool) {
if des == nil {
http.NotFound(pr.rw, pr.req)
return
}
if des.Permanode != nil {
// TODO: get "forceMime" attr out of the permanode? or
// fileName content-disposition?
if cref := des.Permanode.Attr.Get("camliContent"); cref != "" {
cbr := blobref.Parse(cref)
if cbr == nil {
http.Error(pr.rw, "bogus camliContent", 500)
return
}
des = des.PeerBlob(cbr)
if des == nil {
http.Error(pr.rw, "camliContent not a peer in describe", 500)
return
}
}
}
if des.CamliType == "file" {
return des.BlobRef, des.File, true
}
http.Error(pr.rw, "failed to find fileSchemaRefFromBlob", 404)
return
}
示例3: handle_http
func handle_http(responseWrite http.ResponseWriter, request *http.Request) {
var proxyResponse *http.Response
var proxyResponseError os.Error
proxyHost := strings.Split(request.URL.Path[6:], "/")[0]
fmt.Printf("%s %s %s\n", request.Method, request.RawURL, request.RemoteAddr)
//TODO https
url := "http://" + request.URL.Path[6:]
proxyRequest, _ := http.NewRequest(request.Method, url, nil)
proxy := &http.Client{}
proxyResponse, proxyResponseError = proxy.Do(proxyRequest)
if proxyResponseError != nil {
http.NotFound(responseWrite, request)
return
}
for header := range proxyResponse.Header {
responseWrite.Header().Add(header, proxyResponse.Header.Get(header))
}
contentType := strings.Split(proxyResponse.Header.Get("Content-Type"), ";")[0]
if proxyResponseError != nil {
fmt.Fprintf(responseWrite, "pizda\n")
} else if ReplacemendContentType[contentType] {
body, _ := ioutil.ReadAll(proxyResponse.Body)
defer proxyResponse.Body.Close()
bodyString := replace_url(string(body), "/http/", proxyHost)
responseWrite.Write([]byte(bodyString))
} else {
io.Copy(responseWrite, proxyResponse.Body)
}
}
示例4: handle
func handle(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
// Fast path the root request.
if path == "/" {
render(chromeBytes, w)
return
}
switch path[1] {
case '.':
switch path {
case "/.rpc":
rpc.Handle(path, w, r)
case "/.test":
render(testChromeBytes, w)
default:
rpc.HandleStream(path[2:], w, r)
}
case '_':
switch path {
case "/_ah/start":
backend.Start(w, r)
case "/_ah/stop":
backend.Stop(w, r)
default:
http.NotFound(w, r)
}
default:
render(chromeBytes, w)
}
}
示例5: makeRedirectServer
func makeRedirectServer(redirects map[string]string,
context *Context) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
key := req.URL.Path[1:]
url, exists := redirects[key]
if !exists {
http.NotFound(w, req)
return
}
/*
* I don't use the http.Redirect because it generates HTML and
* I don't need that
*/
w.SetHeader("Location", url)
w.WriteHeader(http.StatusMovedPermanently)
var stat Statmsg
stat.Time = time.UTC()
stat.Key = key
stat.IP = w.RemoteAddr()
stat.Referer = req.Referer
stat.UA = req.UserAgent
context.Update(&stat)
}
}
示例6: serveHTMLDoc
func serveHTMLDoc(c *http.Conn, r *http.Request, path string) {
// get HTML body contents
src, err := ioutil.ReadFile(path)
if err != nil {
log.Stderrf("%v", err)
http.NotFound(c, r)
return
}
// if it begins with "<!DOCTYPE " assume it is standalone
// html that doesn't need the template wrapping.
if bytes.HasPrefix(src, strings.Bytes("<!DOCTYPE ")) {
c.Write(src)
return
}
// if it's the language spec, add tags to EBNF productions
if strings.HasSuffix(path, "go_spec.html") {
var buf bytes.Buffer
linkify(&buf, src)
src = buf.Bytes()
}
title := commentText(src)
servePage(c, title, "", src)
}
示例7: serveFile
func serveFile(c *http.Conn, r *http.Request) {
path := r.Url.Path
// pick off special cases and hand the rest to the standard file server
switch ext := pathutil.Ext(path); {
case path == "/":
serveHtmlDoc(c, r, "doc/root.html")
case r.Url.Path == "/doc/root.html":
// hide landing page from its real name
http.NotFound(c, r)
case ext == ".html":
serveHtmlDoc(c, r, path)
case ext == ".go":
serveGoSource(c, path, &Styler{highlight: r.FormValue("h")})
default:
// TODO:
// - need to decide what to serve and what not to serve
// - don't want to download files, want to see them
fileServer.ServeHTTP(c, r)
}
}
示例8: rootHandler
func rootHandler(w http.ResponseWriter, r *http.Request) {
p, err := loadPage(w, r)
if err != nil {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, "%s", p.Body)
}
示例9: getTitle
func getTitle(w http.ResponseWriter, r *http.Request) (title string, err os.Error) {
title = r.URL.Path[lenPath:]
if !titleValidator.MatchString(title) {
http.NotFound(w, r)
err = os.NewError("Invalid Page Title")
}
return
}
示例10: getKey
func getKey(w http.ResponseWriter, r *http.Request, lenPath int) (key string, err os.Error) {
var keyValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
key = r.URL.Path[lenPath:]
if !keyValidator.MatchString(key) {
http.NotFound(w, r)
err = os.NewError("Invalid Lookup Key")
}
return
}
示例11: Redirect
func Redirect(w http.ResponseWriter, r *http.Request) {
key := r.URL.Path[1:]
url := store.Get(key)
if url == "" {
http.NotFound(w, r)
return
}
http.Redirect(w, r, url, http.StatusFound)
}
示例12: makePageHandler
func makePageHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
if !titleValidator.MatchString(title) {
http.NotFound(w, r)
}
fn(w, r, title)
}
}
示例13: serveFile
func serveFile(c *http.Conn, r *http.Request) {
path := pathutil.Join(".", r.URL.Path)
// pick off special cases and hand the rest to the standard file server
switch ext := pathutil.Ext(path); {
case r.URL.Path == "/":
serveHTMLDoc(c, r, "doc/root.html")
return
case r.URL.Path == "/doc/root.html":
// hide landing page from its real name
http.NotFound(c, r)
return
case ext == ".html":
serveHTMLDoc(c, r, path)
return
case ext == ".go":
serveGoSource(c, r, path, &Styler{highlight: r.FormValue("h")})
return
}
dir, err := os.Lstat(path)
if err != nil {
http.NotFound(c, r)
return
}
if dir != nil && dir.IsDirectory() {
serveDirectory(c, r, path)
return
}
if isTextFile(path) {
serveTextFile(c, r, path)
return
}
fileServer.ServeHTTP(c, r)
}
示例14: GetNodeByName
func GetNodeByName(store *NodeStore) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
setContentText(w)
name := req.URL.Path[lenNamePath:]
node, present := store.Get(name)
if present {
fmt.Fprintln(w, node)
} else {
http.NotFound(w, req)
}
}
}
示例15: Redirect
func Redirect(w http.ResponseWriter, r *http.Request) {
key := r.URL.Path[1:]
if key == "favicon.ico" || key == "" {
http.NotFound(w, r)
return
}
var url string
if err := store.Get(&key, &url); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, url, http.StatusFound)
}