当前位置: 首页>>代码示例>>Golang>>正文


Golang http.NotFound函数代码示例

本文整理汇总了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)
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:54,代码来源:godoc.go

示例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
}
开发者ID:ipeet,项目名称:camlistore,代码行数:29,代码来源:publish.go

示例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)
	}
}
开发者ID:kalloc,项目名称:goanonymizer,代码行数:33,代码来源:anonymizer.go

示例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)
	}
}
开发者ID:elimisteve,项目名称:togethr,代码行数:30,代码来源:main.go

示例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)
	}
}
开发者ID:wladh,项目名称:go-redir-ws,代码行数:27,代码来源:ws.go

示例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)
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:26,代码来源:godoc.go

示例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)
	}
}
开发者ID:8l,项目名称:go-learn,代码行数:25,代码来源:godoc.go

示例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)
}
开发者ID:wuttikorn,项目名称:GOWS,代码行数:8,代码来源:GOWS_II.go

示例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
}
开发者ID:mutsuda,项目名称:Nash,代码行数:8,代码来源:nash.go

示例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
}
开发者ID:hernan43,项目名称:dpcmp,代码行数:9,代码来源:dpcompare.go

示例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)
}
开发者ID:rsec,项目名称:goto,代码行数:9,代码来源:main.go

示例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)
	}
}
开发者ID:Fumon,项目名称:fugoServer,代码行数:9,代码来源:wiki.go

示例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)
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:41,代码来源:godoc.go

示例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)
		}
	}
}
开发者ID:Crest,项目名称:gresec,代码行数:12,代码来源:http.go

示例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)
}
开发者ID:rsec,项目名称:goto,代码行数:13,代码来源:main.go


注:本文中的http.NotFound函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。