示例#1
0
文件: inbound.go 项目: yarpc/yarpc-go
// TODO(apb): reduce commonality between Handle and HandleOneway
func (t thriftOnewayHandler) HandleOneway(ctx context.Context, treq *transport.Request) error {
	if err := encoding.Expect(treq, Encoding); err != nil {
		return err
	}

	body, err := ioutil.ReadAll(treq.Body)
	if err != nil {
		return err
	}

	// We disable enveloping if either the client or the transport requires it.
	proto := t.Protocol
	if !t.Enveloping {
		proto = disableEnvelopingProtocol{
			Protocol: proto,
			Type:     wire.OneWay, // we only decode oneway requests
		}
	}

	envelope, err := proto.DecodeEnveloped(bytes.NewReader(body))
	if err != nil {
		return encoding.RequestBodyDecodeError(treq, err)
	}

	if envelope.Type != wire.OneWay {
		return encoding.RequestBodyDecodeError(
			treq, errUnexpectedEnvelopeType(envelope.Type))
	}

	reqMeta := meta.FromTransportRequest(treq)
	return t.OnewayHandler.HandleOneway(ctx, reqMeta, envelope.Value)
}
示例#2
0
文件: inbound.go 项目: yarpc/yarpc-go
func (h jsonHandler) Handle(ctx context.Context, treq *transport.Request, rw transport.ResponseWriter) error {
	if err := encoding.Expect(treq, Encoding); err != nil {
		return err
	}

	reqBody, err := h.reader.Read(json.NewDecoder(treq.Body))
	if err != nil {
		return encoding.RequestBodyDecodeError(treq, err)
	}

	reqMeta := meta.FromTransportRequest(treq)
	results := h.handler.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(reqMeta), reqBody})

	if err := results[2].Interface(); err != nil {
		return err.(error)
	}

	if resMeta, ok := results[1].Interface().(yarpc.ResMeta); ok {
		meta.ToTransportResponseWriter(resMeta, rw)
	}

	result := results[0].Interface()
	if err := json.NewEncoder(rw).Encode(result); err != nil {
		return encoding.ResponseBodyEncodeError(treq, err)
	}

	return nil
}
示例#3
0
文件: inbound.go 项目: yarpc/yarpc-go
func (h jsonHandler) HandleOneway(ctx context.Context, treq *transport.Request) error {
	if err := encoding.Expect(treq, Encoding); err != nil {
		return err
	}

	reqBody, err := h.reader.Read(json.NewDecoder(treq.Body))
	if err != nil {
		return encoding.RequestBodyDecodeError(treq, err)
	}

	reqMeta := meta.FromTransportRequest(treq)
	results := h.handler.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(reqMeta), reqBody})

	if err := results[0].Interface(); err != nil {
		return err.(error)
	}

	return nil
}
示例#4
0
文件: inbound.go 项目: yarpc/yarpc-go
func (t thriftUnaryHandler) Handle(ctx context.Context, treq *transport.Request, rw transport.ResponseWriter) error {
	if err := encoding.Expect(treq, Encoding); err != nil {
		return err
	}

	body, err := ioutil.ReadAll(treq.Body)
	if err != nil {
		return err
	}

	// We disable enveloping if either the client or the transport requires it.
	proto := t.Protocol
	if !t.Enveloping {
		proto = disableEnvelopingProtocol{
			Protocol: proto,
			Type:     wire.Call, // we only decode requests
		}
	}

	envelope, err := proto.DecodeEnveloped(bytes.NewReader(body))
	if err != nil {
		return encoding.RequestBodyDecodeError(treq, err)
	}

	if envelope.Type != wire.Call {
		return encoding.RequestBodyDecodeError(
			treq, errUnexpectedEnvelopeType(envelope.Type))
	}

	reqMeta := meta.FromTransportRequest(treq)
	res, err := t.UnaryHandler.Handle(ctx, reqMeta, envelope.Value)
	if err != nil {
		return err
	}

	if resType := res.Body.EnvelopeType(); resType != wire.Reply {
		return encoding.ResponseBodyEncodeError(
			treq, errUnexpectedEnvelopeType(resType))
	}

	value, err := res.Body.ToWire()
	if err != nil {
		return err
	}

	if res.IsApplicationError {
		rw.SetApplicationError()
	}

	resMeta := res.Meta
	if resMeta != nil {
		meta.ToTransportResponseWriter(resMeta, rw)
	}

	err = proto.EncodeEnveloped(wire.Envelope{
		Name:  res.Body.MethodName(),
		Type:  res.Body.EnvelopeType(),
		SeqID: envelope.SeqID,
		Value: value,
	}, rw)
	if err != nil {
		return encoding.ResponseBodyEncodeError(treq, err)
	}

	return nil
}