예제 #1
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
}
예제 #2
0
파일: inbound.go 프로젝트: yarpc/yarpc-go
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
}
예제 #3
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)
}
예제 #4
0
파일: inbound.go 프로젝트: yarpc/yarpc-go
func (r rawOnewayHandler) HandleOneway(ctx context.Context, treq *transport.Request) 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)
	return r.OnewayHandler(ctx, reqMeta, reqBody)
}
예제 #5
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
}
예제 #6
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
}