func (r rawUnaryHandler) Handle(ctx context.Context, treq *transport.Request, rw transport.ResponseWriter) error { if err := encoding.Expect(treq, Encoding); err != nil { return err } reqBody, err := ioutil.ReadAll(treq.Body) if err != nil { return err } reqMeta := meta.FromTransportRequest(treq) resBody, resMeta, err := r.UnaryHandler(ctx, reqMeta, reqBody) if err != nil { return err } if resMeta != nil { meta.ToTransportResponseWriter(resMeta, rw) } if _, err := rw.Write(resBody); err != nil { return err } return nil }
// ToTransportResponseWriter fills the given transport response with // information from the given ResMeta. func ToTransportResponseWriter(resMeta yarpc.ResMeta, w transport.ResponseWriter) { if hs := resMeta.GetHeaders(); hs.Len() > 0 { w.AddHeaders(transport.Headers(resMeta.GetHeaders())) } }
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 }