Ejemplo n.º 1
0
func NewDynamicClientProts(url func() string, compact bool) (recv, send thrift.TProtocol) {
	recvbuf := thrift.NewTMemoryBuffer()
	send = getSendProt(url, recvbuf, compact)
	if compact {
		recv = thrift.NewTCompactProtocol(recvbuf)
	} else {
		recv = thrift.NewTBinaryProtocol(recvbuf, true, true)
	}
	return recv, send
}
Ejemplo n.º 2
0
func (h ThriftOverHTTPHandler) ServeHTTP(out http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		var in *thrift.TMemoryBuffer
		size := int(req.ContentLength)
		if size > 0 {
			in = thrift.NewTMemoryBufferLen(size)
		} else {
			in = thrift.NewTMemoryBuffer()
		}

		in.ReadFrom(req.Body)
		defer req.Body.Close()

		compact := false

		if in.Len() > 0 && in.Bytes()[0] == thrift.COMPACT_PROTOCOL_ID {
			compact = true
		}

		outbuf := thrift.NewTMemoryBuffer()

		var iprot thrift.TProtocol
		var oprot thrift.TProtocol

		if compact {
			iprot = thrift.NewTCompactProtocol(in)
			oprot = thrift.NewTCompactProtocol(outbuf)
		} else {
			iprot = thrift.NewTBinaryProtocol(in, true, true)
			oprot = thrift.NewTBinaryProtocol(outbuf, true, true)
		}

		ok, err := h.handle(iprot, oprot)

		if ok {
			outbuf.WriteTo(out)
		} else {
			http.Error(out, err.Error(), 500)
		}
	} else {
		http.Error(out, "Must POST TBinary encoded thrift RPC", 401)
	}
}
Ejemplo n.º 3
0
func getSendProt(url func() string, recvbuf *thrift.TMemoryBuffer, compact bool) thrift.TProtocol {
	sendbuf := thrift.NewTMemoryBuffer()
	var underlying thrift.TProtocol
	if compact {
		underlying = thrift.NewTCompactProtocol(sendbuf)
	} else {
		underlying = thrift.NewTBinaryProtocol(sendbuf, true, true)
	}
	return &sendProt{&http.Client{Transport: &http.Transport{}}, url, sendbuf, recvbuf, underlying}
}
Ejemplo n.º 4
0
func (h *ThriftOverHTTPHandler) ServeHTTP(out http.ResponseWriter, req *http.Request) {
	start := time.Now()
	if req.Method == "POST" {
		inbuf := h.getBuf()
		defer h.buffers.Put(inbuf)
		outbuf := h.getBuf()
		defer h.buffers.Put(outbuf)

		inbuf.ReadFrom(req.Body)
		defer req.Body.Close()

		compact := false

		if inbuf.Len() > 0 && inbuf.Bytes()[0] == thrift.COMPACT_PROTOCOL_ID {
			compact = true
		}

		var iprot thrift.TProtocol
		var oprot thrift.TProtocol

		if compact {
			iprot = thrift.NewTCompactProtocol(inbuf)
			oprot = thrift.NewTCompactProtocol(outbuf)
		} else {
			iprot = thrift.NewTBinaryProtocol(inbuf, true, true)
			oprot = thrift.NewTBinaryProtocol(outbuf, true, true)
		}

		ok, err := h.Process(iprot, oprot)

		if ok {
			outbuf.WriteTo(out)
		} else {
			http.Error(out, err.Error(), 500)
		}
	} else {
		http.Error(out, "Must POST TBinary encoded thrift RPC", 401)
	}
	if h.stats != nil {
		h.stats.TimeSince("servehttp", start)
	}
}