當前位置: 首頁>>代碼示例>>Golang>>正文


Golang http.Conn類代碼示例

本文整理匯總了Golang中http.Conn的典型用法代碼示例。如果您正苦於以下問題:Golang Conn類的具體用法?Golang Conn怎麽用?Golang Conn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Conn類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Symbol

// Symbol looks up the program counters listed in the request,
// responding with a table mapping program counters to function names.
// The package initialization registers it as /debug/pprof/symbol.
func Symbol(c *http.Conn, r *http.Request) {
	c.SetHeader("content-type", "text/plain; charset=utf-8")

	// We don't know how many symbols we have, but we
	// do have symbol information.  Pprof only cares whether
	// this number is 0 (no symbols available) or > 0.
	fmt.Fprintf(c, "num_symbols: 1\n")

	var b *bufio.Reader
	if r.Method == "POST" {
		b = bufio.NewReader(r.Body)
	} else {
		b = bufio.NewReader(strings.NewReader(r.URL.RawQuery))
	}

	for {
		w, err := b.ReadSlice('+')
		if err == nil {
			w = w[0 : len(w)-1] // trim +
		}
		pc, _ := strconv.Btoui64(string(w), 0)
		if pc != 0 {
			f := runtime.FuncForPC(uintptr(pc))
			if f != nil {
				fmt.Fprintf(c, "%#x %s\n", pc, f.Name())
			}
		}

		// Wait until here to check for err; the last
		// symbol will have an err because it doesn't end in +.
		if err != nil {
			break
		}
	}
}
開發者ID:rapgamer,項目名稱:golang-china,代碼行數:38,代碼來源:pprof.go

示例2: TWITTER_REPLIES

func TWITTER_REPLIES(c *http.Conn, req *http.Request) {
	log.Stderrf(">REPLIES:");
	s := session_service.GetSession(c,req);
	for k,v := range s.Data {
		log.Stderrf("session kv:%s:%s", k, v);
	}
	auth_token, atx := s.Data["oauth_token"];
	if atx {
		log.Stderrf("TOKEN FOUND!");
		auth_token_secret := s.Data["oauth_token_secret"];
		r, finalUrl, err := twitter_client.MakeRequest("http://twitter.com/statuses/mentions.json", map[string]string{"oauth_token":auth_token}, auth_token_secret, false); //{"since_id":s.last_reply_id})
		if err != nil {
			log.Stderrf(":REPLIES:err:%s",err);
		}
		else {
			log.Stderrf(":REPLIES:r:%s:finalUrl:%s", r, finalUrl);
			b, _ := io.ReadAll(r.Body);
			print ("REPLIES!");
			str := bytes.NewBuffer(b).String();
       			println (str);
			j, ok, errtok := json.StringToJson(str);
			log.Stderr("REPLIES:j:%s:ok:%s:errtok:%s", j, ok, errtok);
			c.Write(strings.Bytes(j.String()));
		}
	
	}
	else {
		log.Stderrf("NO TOKEN FOUND!");
		http.Redirect(c, "/login/twitter?returnto=/twitter/replies", http.StatusFound); // should be 303 instead of 302?
	}
}
開發者ID:montsamu,項目名稱:go-twitter-oauth,代碼行數:31,代碼來源:example.go

示例3: DateServer

// exec a program, redirecting output
func DateServer(c *http.Conn, req *http.Request) {
	c.SetHeader("content-type", "text/plain; charset=utf-8")
	r, w, err := os.Pipe()
	if err != nil {
		fmt.Fprintf(c, "pipe: %s\n", err)
		return
	}
	pid, err := os.ForkExec("/bin/date", []string{"date"}, os.Environ(), "", []*os.File{nil, w, w})
	defer r.Close()
	w.Close()
	if err != nil {
		fmt.Fprintf(c, "fork/exec: %s\n", err)
		return
	}
	io.Copy(c, r)
	wait, err := os.Wait(pid, 0)
	if err != nil {
		fmt.Fprintf(c, "wait: %s\n", err)
		return
	}
	if !wait.Exited() || wait.ExitStatus() != 0 {
		fmt.Fprintf(c, "date: %v\n", wait)
		return
	}
}
開發者ID:fedgrant,項目名稱:tonika,代碼行數:26,代碼來源:triv.go

示例4: Send

func Send(c *http.Conn, req *http.Request) {
	query_params, _ := http.ParseQuery(req.URL.RawQuery)
	arg_game, ok_game := query_params["game"]
	arg_msg, ok_msg := query_params["msg"]
	if !ok_game {
		c.Write([]byte("Game parameter not set"))
		return
	}
	if !ok_msg {
		c.Write([]byte("Msg parameter not set"))
		return
	}
	game := arg_game[0]
	msg := arg_msg[0]
	params := strings.Split(game, ",", 2)
	if len(params) != 2 {
		c.Write([]byte("Malformed game parameter"))
		return
	}
	game_id, player_id := params[0], params[1]
	game_instance := room.Find(game_id)
	if room == nil {
		c.Write([]byte("Game not found"))
		return
	}
	result := game_instance.Send(player_id, msg)
	c.Write([]byte(result))
}
開發者ID:getmonitor,項目名稱:golang-game-server,代碼行數:28,代碼來源:server.go

示例5: 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

示例6: AddEntry

// url: /blog/entry/add
func AddEntry(c *http.Conn, req *http.Request) {
	req.ParseForm();
	heading, body := req.FormValue("heading"), req.FormValue("body");
	if len(heading) > 0 && len(body) > 0 {
		blog.AddEntry(heading, body)
	}
	c.SetHeader("Location", "/blog");
	c.WriteHeader(http.StatusFound);
}
開發者ID:tung,項目名稱:goblog,代碼行數:10,代碼來源:views.go

示例7: handleError

func (s *Swif) handleError(code int, c *http.Conn) {
	switch code {
	case http.StatusBadRequest:
		c.WriteHeader(code)
		io.WriteString(c, "Invalid Request")
	default:
		c.WriteHeader(http.StatusBadRequest)
		io.WriteString(c, "Invalid Request")
	}
}
開發者ID:surma-dump,項目名稱:swif,代碼行數:10,代碼來源:swif.go

示例8: HelloServer

// hello world, the web server
func HelloServer(c *http.Conn, req *http.Request)
{
  var creq CRequest;
  creq.conn = c;
  creq.req = req;
  //fmt.Printf("%s\n", creq.req);
  var e os.Error;
  creq.rwc, creq.buf, e = c.Hijack();
  if (e!=nil) {}
  req_channel <- creq;
}
開發者ID:jjo,項目名稱:src-juanjo,代碼行數:12,代碼來源:http_server.go

示例9: FlagServer

func FlagServer(c *http.Conn, req *http.Request) {
	c.SetHeader("content-type", "text/plain; charset=utf-8")
	fmt.Fprint(c, "Flags:\n")
	flag.VisitAll(func(f *flag.Flag) {
		if f.Value.String() != f.DefValue {
			fmt.Fprintf(c, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue)
		} else {
			fmt.Fprintf(c, "%s = %s\n", f.Name, f.Value.String())
		}
	})
}
開發者ID:fedgrant,項目名稱:tonika,代碼行數:11,代碼來源:triv.go

示例10: ServeHTTP

func (s FileServer) ServeHTTP(c *http.Conn, req *http.Request) {
	content, err := ioutil.ReadFile(s.loc)
	if err != nil {
		log.Stderr(err)
	} else {
		// get mime type
		mimeType := mime.TypeByExtension(path.Ext(s.loc)) // TODO cache this
		c.SetHeader("Content-Type", mimeType)
		fmt.Fprintf(c, "%s", content)
	}
}
開發者ID:bytbox,項目名稱:obsidian,代碼行數:11,代碼來源:serve.go

示例11: TestCode2

func TestCode2(c *http.Conn, req *http.Request) {
	dump, err := http.DumpRequest(req, true)
	if err != nil {
		io.WriteString(c, err.String())
		return
	}
	_, err = c.Write(dump)
	if err != nil {
		io.WriteString(c, err.String())
		return
	}
	log.Stdout(string(dump))
}
開發者ID:Jonersan,項目名稱:goproxy,代碼行數:13,代碼來源:server.go

示例12: Wait

func Wait(c *http.Conn, req *http.Request) {
	query_params, _ := http.ParseQuery(req.URL.RawQuery)
	arg_owner_id, ok_owner_id := query_params["owner_id"]
	if ok_owner_id {
		fmt.Println("Create owner_id=", arg_owner_id)
		owner_id := arg_owner_id[0]
		game_id := room.Wait(owner_id)
		fmt.Println("Create ", game_id)
		c.Write([]byte(game_id))
	} else {
		c.Write([]byte("Parameters not specified correctly"))
	}
}
開發者ID:getmonitor,項目名稱:golang-game-server,代碼行數:13,代碼來源:server.go

示例13: Connect

func Connect(c *http.Conn, req *http.Request) {
	query_params, _ := http.ParseQuery(req.URL.RawQuery)
	arg_owner, ok_owner := query_params["owner"]
	if ok_owner {
		fmt.Println("Connect owner=", arg_owner)
		owner := arg_owner[0]
		owner_id := room.Connect(owner)
		fmt.Println("Connected as ", owner_id)
		c.Write([]byte(owner_id))
	} else {
		c.Write([]byte("Parameters not specified correctly"))
	}
}
開發者ID:getmonitor,項目名稱:golang-game-server,代碼行數:13,代碼來源:server.go

示例14: expvarHandler

func expvarHandler(c *http.Conn, req *http.Request) {
	c.SetHeader("content-type", "application/json; charset=utf-8")
	fmt.Fprintf(c, "{\n")
	first := true
	for name, value := range vars {
		if !first {
			fmt.Fprintf(c, ",\n")
		}
		first = false
		fmt.Fprintf(c, "%q: %s", name, value)
	}
	fmt.Fprintf(c, "\n}\n")
}
開發者ID:rapgamer,項目名稱:golang-china,代碼行數:13,代碼來源:expvar.go

示例15: AddComment

// url: /blog/comment/add
func AddComment(c *http.Conn, req *http.Request) {
	req.ParseForm();
	entryIDString, text := req.FormValue("entry_id"), req.FormValue("text");
	if len(entryIDString) > 0 && len(text) > 0 {
		if entryID, parseErr := strconv.Atoi(entryIDString); parseErr == nil {
			if foundEntry := blog.FindEntry(entryID); foundEntry != nil {
				foundEntry.AddComment(text);
			}
		}
	}
	c.SetHeader("Location", "/blog/entry/" + entryIDString);
	c.WriteHeader(http.StatusFound);
}
開發者ID:tung,項目名稱:goblog,代碼行數:14,代碼來源:views.go


注:本文中的http.Conn類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。