Пример #1
0
func (p *Proxy) connLoop(s_ net.Conn) {
	s_, err := p.prepConn(s_)
	if err != nil {
		return
	}
	st := server.NewStampedServerConn(s_, nil)

	// Read and parse first request
	req0, err := st.Read()
	if err != nil {
		log.Printf("Read first Request: %s\n", err)
		st.Close()
		return
	}
	req0.Host = strings.ToLower(strings.TrimSpace(req0.Host))
	if req0.Host == "" {
		st.Write(req0, http.NewResponse400String(req0, "GoReverseProxy: missing host"))
		st.Close()
		return
	}

	// Connect to host
	host := p.config.ActualHost(req0.Host)
	if host == "" {
		st.Write(req0, http.NewResponse400String(req0, "GoReverseProxy: unknwon host"))
		st.Close()
		return
	}
	p.fdl.Lock()
	c_, err := net.Dial("tcp", host)
	if err != nil {
		log.Printf("Dial server: %s\n", err)
		if c_ != nil {
			c_.Close()
		}
		p.fdl.Unlock()
		st.Write(req0, http.NewResponse400String(req0, "GoReverseProxy: error dialing host"))
		st.Close()
		return
	}
	c_, err = p.prepConn(c_)
	if err != nil {
		st.Write(req0, http.NewResponse400String(req0, "GoReverseProxy: error on host conn"))
		st.Close()
		return
	}
	ct := server.NewStampedClientConn(c_, nil)
	q := p.register(st, ct)

	ch := make(chan *http.Request, 5)
	go p.backLoop(ch, q)
	p.frontLoop(ch, q, req0)
}
Пример #2
0
func (qx *queryCodec) WriteResponse(resp *rpc.Response, ret interface{}) (err os.Error) {

	if resp.Error != "" {
		return qx.Query.Write(http.NewResponse400String(qx.Query.Req, resp.Error))
	}

	if ret == nil {
		return qx.Query.Write(http.NewResponse200(qx.Query.Req))
	}

	r := ret.(*Ret)

	var body []byte
	if r.Value != nil {
		body, err = json.Marshal(r.Value)
		if err != nil {
			qx.Query.Write(http.NewResponse500(qx.Query.Req))
			return err
		}
	}

	httpResp := http.NewResponse200Bytes(qx.Query.Req, body)
	httpResp.Header = make(http.Header)
	for _, setCookie := range r.SetCookies {
		httpResp.Header.Add("Set-Cookie", setCookie.String())
	}

	//dump, _ := http.DumpResponse(httpResp, true)
	//log.Printf("RPC-Resp:\n%s\n", string(dump))

	return qx.Query.Write(httpResp)
}