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


Golang Request.Write方法代碼示例

本文整理匯總了Golang中net/http.Request.Write方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.Write方法的具體用法?Golang Request.Write怎麽用?Golang Request.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.Request的用法示例。


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

示例1: requestHandler

// requestHandler will handle a single web request.
func (cons *Http) requestHandler(resp http.ResponseWriter, req *http.Request) {
	if cons.withHeaders {
		// Read the whole package
		requestBuffer := bytes.NewBuffer(nil)
		if err := req.Write(requestBuffer); err != nil {
			resp.WriteHeader(http.StatusBadRequest)
			return // ### return, missing body or bad write ###
		}

		cons.Enqueue(requestBuffer.Bytes(), atomic.AddUint64(&cons.sequence, 1))
		resp.WriteHeader(http.StatusCreated)
	} else {
		// Read only the message body
		if req.Body == nil {
			resp.WriteHeader(http.StatusBadRequest)
			return // ### return, missing body ###
		}

		body := make([]byte, req.ContentLength)
		length, err := req.Body.Read(body)
		if err != nil {
			resp.WriteHeader(http.StatusBadRequest)
			return // ### return, missing body or bad write ###
		}

		cons.Enqueue(body[:length], atomic.AddUint64(&cons.sequence, 1))
		resp.WriteHeader(http.StatusCreated)
	}
}
開發者ID:oopcode,項目名稱:gollum,代碼行數:30,代碼來源:http.go

示例2: EncodeRequestToEvent

// EncodeRequestToEvent will write the request out in wire protocol and encode it to b64 and store it in an Event object
func EncodeRequestToEvent(r *http.Request) string {
	var asBytes bytes.Buffer
	r.Write(&asBytes)

	uEnc := b64.StdEncoding.EncodeToString(asBytes.Bytes())
	return uEnc
}
開發者ID:arguello,項目名稱:tyk,代碼行數:8,代碼來源:event_system.go

示例3: RoundTrip

// RoundTrip executes the Request and upgrades it. After a successful upgrade,
// clients may call SpdyRoundTripper.Connection() to retrieve the upgraded
// connection.
func (s *SpdyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	// TODO what's the best way to clone the request?
	r := *req
	req = &r
	req.Header.Add(httpstream.HeaderConnection, httpstream.HeaderUpgrade)
	req.Header.Add(httpstream.HeaderUpgrade, HeaderSpdy31)

	conn, err := s.dial(req)
	if err != nil {
		return nil, err
	}

	err = req.Write(conn)
	if err != nil {
		return nil, err
	}

	resp, err := http.ReadResponse(bufio.NewReader(conn), req)
	if err != nil {
		return nil, err
	}

	s.conn = conn

	return resp, nil
}
開發者ID:arvindkandhare,項目名稱:kubernetes,代碼行數:29,代碼來源:roundtripper.go

示例4: send

func send(req *http.Request) (resp *http.Response, err error) {
	addr := req.URL.Host
	if !hasPort(addr) {
		addr += ":http"
	}
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}
	err = req.Write(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}
	reader := bufio.NewReader(conn)
	resp, err = http.ReadResponse(reader, req)
	if err != nil {
		conn.Close()
		return nil, err
	}
	r := io.Reader(reader)
	if v := resp.Header["Content-Length"]; v != nil {
		n, err := strconv.Atoi(v[0])
		if err != nil {
			return nil, &badStringError{"invalid Content-Length", v[0]}
		}
		v := int64(n)
		r = io.LimitReader(r, v)
	}
	resp.Body = readClose{r, conn}
	return
}
開發者ID:micrypt,項目名稱:gofluidinfo,代碼行數:32,代碼來源:fluidinfo.go

示例5: send

func send(url *url.URL, addr string) result {
	var req http.Request
	req.URL = url

	now := time.Now()
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return result{0, err}
	}
	defer conn.Close()

	err = req.Write(conn)
	if err != nil {
		return result{0, err}
	}

	ch := make(chan respErr, 1)
	go func() {
		reader := bufio.NewReader(conn)
		response, err := http.ReadResponse(reader, &req)
		ch <- respErr{response, err}
	}()

	var res result

	select {
	case <-time.After(time.Duration(*timeout * 1e6)):
		res = result{time.Now().Sub(now), errors.New("Timeout!")}
	case rerr := <-ch:
		res = result{time.Now().Sub(now), rerr.err}
		rerr.resp.Body.Close()
	}

	return res
}
開發者ID:piranha,項目名稱:gofigure,代碼行數:35,代碼來源:gofigure.go

示例6: whoamI

func whoamI(w http.ResponseWriter, req *http.Request) {
	u, _ := url.Parse(req.URL.String())
	queryParams := u.Query()
	wait := queryParams.Get("wait")
	if len(wait) > 0 {
		duration, err := time.ParseDuration(wait)
		if err == nil {
			time.Sleep(duration)
		}
	}
	hostname, _ := os.Hostname()
	fmt.Fprintln(w, "Hostname:", hostname)
	ifaces, _ := net.Interfaces()
	for _, i := range ifaces {
		addrs, _ := i.Addrs()
		// handle err
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			fmt.Fprintln(w, "IP:", ip)
		}
	}
	req.Write(w)
}
開發者ID:emilevauge,項目名稱:whoamI,代碼行數:29,代碼來源:app.go

示例7: wsProxy

func (h *HTTPProxy) wsProxy(w http.ResponseWriter, r *http.Request) {
	hj, ok := w.(http.Hijacker)

	if !ok {
		h.clientErr(w, r, ErrInvalidAction)
		return
	}

	client, _, err := hj.Hijack()
	if err != nil {
		h.clientErr(w, r, err)
		return
	}
	defer client.Close()

	server, err := net.Dial("tcp", r.URL.Host)
	if err != nil {
		h.clientErr(w, r, err)
		return
	}
	defer server.Close()

	err = r.Write(server)
	if err != nil {
		h.clientErr(w, r, err)
		return
	}

	go passBytes(client, server)
}
開發者ID:uken,項目名稱:knuckles,代碼行數:30,代碼來源:http.go

示例8: ReverseProxy

func ReverseProxy(w http.ResponseWriter, req *http.Request) {
	var err error
	var inConn, outConn net.Conn

	log.Printf("%s %v", req.Method, req.URL)

	if inConn, err = net.Dial("tcp", webapp); err != nil {
		SiteIsDown(w)
		return
	}
	if outConn, _, err = w.(http.Hijacker).Hijack(); err != nil {
		// log.Printf("Cannot hijack connection: %s", err)
		inConn.Close()
		return
	}

	go func() {
		io.Copy(outConn, inConn)
		outConn.Close()
	}()
	go func() {
		req.Header["X-Forwarded-For"] = []string{req.RemoteAddr}
		req.Write(inConn)
		io.Copy(inConn, outConn)
		inConn.Close()
	}()
	return
}
開發者ID:pauek,項目名稱:Academio,代碼行數:28,代碼來源:main.go

示例9: ServeHTTP

func (this Tunnel) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	conn, err := this.session.Open()
	if err != nil {
		log.Println(err.Error())
		response.Write([]byte("<html><body>" + err.Error() + "</body></html>"))
		return
	}
	defer conn.Close()

	if err := request.Write(conn); err != nil {
		log.Println(err.Error())
		return
	}

	tunnelResponse, err := http.ReadResponse(bufio.NewReader(conn), request)
	if err != nil {
		response.Write([]byte("<html><body>" + err.Error() + "</body></html>"))
		return
	}

	for header, values := range tunnelResponse.Header {
		for _, value := range values {
			response.Header().Add(header, value)
		}
	}

	response.WriteHeader(tunnelResponse.StatusCode)
	if tunnelResponse.Body != nil {
		io.Copy(response, tunnelResponse.Body)
	}
}
開發者ID:pjvds,項目名稱:publichost,代碼行數:31,代碼來源:main.go

示例10: RoundTrip

func (t *logTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	var buf bytes.Buffer

	os.Stdout.Write([]byte("\n[request]\n"))
	if req.Body != nil {
		req.Body = ioutil.NopCloser(&readButCopy{req.Body, &buf})
	}
	req.Write(os.Stdout)
	if req.Body != nil {
		req.Body = ioutil.NopCloser(&buf)
	}
	os.Stdout.Write([]byte("\n[/request]\n"))

	res, err := t.rt.RoundTrip(req)

	fmt.Printf("[response]\n")
	if err != nil {
		fmt.Printf("ERROR: %v", err)
	} else {
		body := res.Body
		res.Body = nil
		res.Write(os.Stdout)
		if body != nil {
			res.Body = ioutil.NopCloser(&echoAsRead{body})
		}
	}

	return res, err
}
開發者ID:vokal,項目名稱:MC-Map-Uploader,代碼行數:29,代碼來源:google_oauth.go

示例11: send

func send(req *http.Request) (resp *http.Response, err error) {
	addr := req.URL.Host
	if !hasPort(addr) {
		addr += ":http"
	}
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}

	err = req.Write(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}

	reader := bufio.NewReader(conn)
	resp, err = http.ReadResponse(reader, req)
	if err != nil {
		conn.Close()
		return nil, err
	}

	r := io.Reader(reader)
	if n := resp.ContentLength; n != -1 {
		r = io.LimitReader(r, n)
	}
	resp.Body = readClose{r, conn}

	return
}
開發者ID:johanwestin,項目名稱:go-twitter,代碼行數:31,代碼來源:http_auth.go

示例12: RoundTrip

func (st *supervisorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	if req.URL == nil {
		return nil, errors.New("unix: nil Request.URL")
	}

	if req.Header == nil {
		return nil, errors.New("unix: nil Request.Header")
	}

	if req.URL.Scheme != "unix" {
		panic("unix: unsupported protocol scheme")
	}

	sock, err := net.Dial("unix", req.URL.Path)
	if err != nil {
		return nil, err
	}
	defer sock.Close()

	//create shallow copy of request object
	newReq := new(http.Request)
	*newReq = *req

	newReq.URL = supervisorURL
	newReq.Write(sock)

	return http.ReadResponse(bufio.NewReader(sock), req)
}
開發者ID:spx,項目名稱:go-supervisor,代碼行數:28,代碼來源:supervisord.go

示例13: connect

func (p *Proxy) connect(req *http.Request) (*http.Response, net.Conn, error) {
	if p.proxyURL != nil {
		log.Debugf("martian: CONNECT with downstream proxy: %s", p.proxyURL.Host)

		conn, err := net.Dial("tcp", p.proxyURL.Host)
		if err != nil {
			return nil, nil, err
		}
		pbw := bufio.NewWriter(conn)
		pbr := bufio.NewReader(conn)

		req.Write(pbw)
		pbw.Flush()

		res, err := http.ReadResponse(pbr, req)
		if err != nil {
			return nil, nil, err
		}

		return res, conn, nil
	}

	log.Debugf("martian: CONNECT to host directly: %s", req.URL.Host)

	conn, err := net.Dial("tcp", req.URL.Host)
	if err != nil {
		return nil, nil, err
	}

	return proxyutil.NewResponse(200, nil, req), conn, nil
}
開發者ID:rlugojr,項目名稱:martian,代碼行數:31,代碼來源:proxy.go

示例14: UpgradeHTTP

func (t *transport) UpgradeHTTP(req *http.Request, l log15.Logger) (*http.Response, net.Conn, error) {
	stickyBackend := t.getStickyBackend(req)
	backends := t.getOrderedBackends(stickyBackend, req)
	upconn, addr, err := dialTCP(context.Background(), l, backends)
	if err != nil {
		status := http.StatusServiceUnavailable
		if err == errTimeout {
			status = http.StatusGatewayTimeout
		}
		l.Error("dial failed", "status", status, "num_backends", len(backends))
		return nil, nil, err
	}
	conn := &streamConn{bufio.NewReader(upconn), upconn}
	req.URL.Host = addr

	if err := req.Write(conn); err != nil {
		conn.Close()
		l.Error("error writing request", "err", err, "backend", addr)
		return nil, nil, err
	}
	res, err := http.ReadResponse(conn.Reader, req)
	if err != nil {
		conn.Close()
		l.Error("error reading response", "err", err, "backend", addr)
		return nil, nil, err
	}
	t.setStickyBackend(res, stickyBackend)
	return res, conn, nil
}
開發者ID:ably-forks,項目名稱:flynn,代碼行數:29,代碼來源:transport.go

示例15: hijackWebsocket

func (p *WebsocketReverseProxy) hijackWebsocket(rw http.ResponseWriter, req *http.Request) {
	highjacker, ok := rw.(http.Hijacker)

	if !ok {
		http.Error(rw, "webserver doesn't support hijacking", http.StatusInternalServerError)
		return
	}

	conn, bufrw, err := highjacker.Hijack()
	defer conn.Close()

	conn2, err := net.Dial("tcp", p.Upstream)
	if err != nil {
		log.Printf("couldn't connect to backend websocket server: %v", err)
		http.Error(rw, "couldn't connect to backend server", http.StatusServiceUnavailable)
		return
	}
	defer conn2.Close()

	err = req.Write(conn2)
	if err != nil {
		log.Printf("writing WebSocket request to backend server failed: %v", err)
		return
	}

	bufferedBidirCopy(conn, bufrw, conn2, bufio.NewReadWriter(bufio.NewReader(conn2), bufio.NewWriter(conn2)))
}
開發者ID:rounds,項目名稱:oauth2_proxy,代碼行數:27,代碼來源:websocket_reverse_proxy.go


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