Example #1
0
func (c *serverCodec) ReadRequestHeader(request *rpc.Request) error {
	name, messageType, seq, err := c.protocol.ReadMessageBegin(c.transport)
	if err != nil {
		return err
	}
	if messageType != MessageTypeCall { // Currently don't support one way
		return errors.New("thrift: expected Call message type")
	}

	// TODO: should use a limited size cache for the nameCache to avoid a possible
	//       memory overflow from nefarious or broken clients
	newName := c.nameCache[name]
	if newName == "" {
		newName = CamelCase(name)
		if !strings.ContainsRune(newName, '.') {
			newName = "Thrift." + newName
		}
		c.nameCache[name] = newName
	}

	c.mu.Lock()
	c.methodName[uint64(seq)] = name
	c.mu.Unlock()

	request.ServiceMethod = newName
	request.Seq = uint64(seq)

	return nil
}
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	var sz uint16
	err := binary.Read(c.c, binary.BigEndian, &sz)
	if err != nil {
		return err
	}
	log.Print("sz: ", sz)
	raw_message := make([]byte, sz)
	n, err := io.ReadFull(c.c, raw_message)
	if err != nil || n != int(sz) {
		return err
	}
	c.req.reset()
	log.Print("raw_message: ", string(raw_message))
	err = json.Unmarshal(raw_message, &(c.req))
	log.Print("after unmarshal: ", c.req)
	if err != nil {
		log.Print("err: ", err)
		return err
	}
	r.ServiceMethod = c.req.Method
	c.mutex.Lock()
	c.seq++
	c.pending[c.seq] = c.req.Id
	c.req.Id = nil
	r.Seq = c.seq
	c.mutex.Unlock()
	return nil
}
Example #3
0
func (c *RedisServerCodec) innerReadRequestHeader(r *rpc.Request) (err error) {
	defer func() {
		if ret := recover(); ret != nil {
			err = errors.Errorf("Read Header:%s", ret)
		}
	}()

	m, err := bufioReadMessage(c.r)
	if err != nil {
		return
	}

	arr, err := m.Array()
	if err != nil {
		err = errors.Errorf("must by array type:%v", m.Type)
		return
	}

	op, _ := arr[0].Str()
	op = strings.ToLower(op)
	method, ok := methodMap[op]
	if !ok {
		err = errors.Errorf("method not supported:%s", op)
		return
	}
	r.ServiceMethod = method
	c.seq++
	r.Seq = c.seq
	c.msg = m
	return nil
}
Example #4
0
File: Client.go Project: juxuny/go
func (this *Client) Call(method string, arg interface{}, reply interface{}) (e error) {

	clientConn := new(clientConn)
	clientConn.rest()
	this.codec = NewClientCodec(clientConn)
	req := rpc.Request{}
	req.Seq = defaultIdCreator.NewId()
	req.ServiceMethod = method
	e = this.codec.WriteRequest(&req, arg)
	buffer := bytes.NewBuffer(clientConn.Data)
	resp, e := http.Post(this.addr, "application/json", buffer)
	if e != nil {
		return
	}
	clientConn.SetResponse(resp)
	tmp := &rpc.Response{}
	e = this.codec.ReadResponseHeader(tmp)
	if e != nil {
		return
	}
	e = this.codec.ReadResponseBody(reply)
	if e != nil {
		return
	}
	fmt.Println(string(clientConn.Data))
	return
}
Example #5
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	var req protorpcpb.Request
	if err := readFrame(c.rwc, &req); err != nil {
		return err
	}
	r.ServiceMethod = req.GetMethod()
	r.Seq = req.GetSeq()
	return nil
}
Example #6
0
func writeRequest(c *clientCodec) error {
	head := rpc.Request{}
	head.ServiceMethod = "TestFunc"
	head.Seq = 1

	body := testpb.Request{}
	body.Query = proto.String("ping www.tencent.com")
	return c.WriteRequest(&head, &body)
}
Example #7
0
func (c *codec) ReadRequestHeader(r *rpc.Request) error {
	c.header = internal.Header{} // reset
	if err := c.decode(&c.header); err != nil {
		return err
	}

	r.ServiceMethod = c.header.Method
	r.Seq = c.header.SeqID
	return nil
}
Example #8
0
// ReadRequestHeader receives and decodes a net/rpc request header r.
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	// This is almost identical to ReadResponseHeader(), above.
	var err error
	var hdr ProtoRPCRequestHeader
	if err = c.m.ReadMessage(&hdr); err != nil {
		// Don't log an error here, since this is where normal EOF
		// happens over net/rpc channels, e.g., if a client finishes and
		// disconnects.
		return err
	}
	r.Seq = *hdr.Seq
	r.ServiceMethod = *hdr.Op
	return nil
}
Example #9
0
// ReadRequestHeader reads the header protobuf (which is prefixed by a uvarint
// indicating its size) from the connection, decodes it, and stores the fields
// in the given request.
func (s *ServerCodec) ReadRequestHeader(req *rpc.Request) error {
	var header wire.Header
	if err := ReadProto(s.r, &header); err != nil {
		return err
	}
	if header.Method == nil {
		return fmt.Errorf("header missing method: %s", header)
	}
	if header.Seq == nil {
		return fmt.Errorf("header missing seq: %s", header)
	}
	req.ServiceMethod = *header.Method
	req.Seq = *header.Seq
	return nil
}
Example #10
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	msg := <-c.msgs
	c.req.reset()
	if err := json.Unmarshal(msg.Body, &c.req); err != nil {
		return err
	}
	r.ServiceMethod = c.req.Method
	c.Lock()
	c.seq++
	c.pending[c.seq] = prop{msg.CorrelationId, msg.ReplyTo}
	r.Seq = c.seq
	c.Unlock()

	return nil
}
Example #11
0
func (c *serverCodec) ReadRequestHeader(req *rpc.Request) error {
	line, err := c.r.ReadSlice('\n')
	if err != nil {
		return err
	}
	if len(line) <= 2 {
		return fmt.Errorf("incomplete request header")
	}
	llen, err := strconv.Atoi(string(line[:len(line)-2])) // ignore trailing CRLF
	if err != nil {
		return err
	}
	if llen < 0 {
		return fmt.Errorf("negative request header length")
	}
	b := make([]byte, llen+2) // 2 bytes for trailing CRLF
	_, err = io.ReadFull(c.r, b)
	if err != nil {
		return err
	}

	var header struct {
		Method string `json:"interface_name"`
		//Seq    uint64 `json:"seq"`
	}
	err = json.Unmarshal(b, &header)
	if err != nil {
		return err
	}
	req.ServiceMethod = header.Method
	// req.Seq = header.Seq
	return nil
}
Example #12
0
func (this *ServerCodec) ReadRequestHeader(r *rpc.Request) (e error) {
	data, e := ioutil.ReadAll(this.conn)
	if e != nil {
		log.Println(e.Error())
		return
	}
	e = json.Unmarshal(data, &this.req)
	if e != nil {
		this.resp.Error = e.Error()
		return
	}
	r.Seq = this.req.Id
	r.ServiceMethod = this.req.Method
	log.Println("req:", string(*this.req.Params))
	return
}
Example #13
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	header := wire.RequestHeader{}
	err := readRequestHeader(c.r, &header)
	if err != nil {
		return err
	}

	c.mutex.Lock()
	c.seq++
	c.pending[c.seq] = header.GetId()
	r.ServiceMethod = header.GetMethod()
	r.Seq = c.seq
	c.mutex.Unlock()

	c.reqHeader = header
	return nil
}
Example #14
0
func (server *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	server.curDelivery = <-server.message

	if server.curDelivery.CorrelationId == "" {
		return errors.New("no routing key in delivery")
	}

	r.Seq = atomic.AddUint64(&server.seq, 1)

	server.lock.Lock()
	server.calls[r.Seq] = route{server.curDelivery.MessageId, server.curDelivery.CorrelationId}
	server.lock.Unlock()

	r.ServiceMethod = server.curDelivery.ReplyTo

	return nil
}
Example #15
0
func TestRW(t *testing.T) {
	var buf bufc

	var c codec
	c.w = &buf
	var req rpc.Request
	req.Seq = 1
	req.ServiceMethod = "foo"

	c.WriteRequest(&req, nil)
	c.r = bufio.NewReader(&buf)
	c.ReadRequestHeader(&req)

	if req.Seq != 1 || req.ServiceMethod != "foo" {
		t.Errorf("Request decode/encode mismatch")
	}

}
Example #16
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	data, err := ReadRpc(c.rwc)
	if err != nil {
		return err
	}
	req := &PbRpcRequest{}
	err = proto.Unmarshal(data, req)
	if err != nil {
		return err
	}

	c.req = req

	r.Seq = req.GetId()
	r.ServiceMethod = req.GetMethod()

	return nil
}
Example #17
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	c.req.Reset()
	packer, err := readPack(c.r, &c.req)
	if err != nil {
		return err
	}

	c.packer = packer
	r.ServiceMethod = c.req.Method
	c.mutex.Lock()
	c.seq++
	c.pending[c.seq] = c.req.Id
	c.req.Id = 0
	r.Seq = c.seq
	c.mutex.Unlock()

	return nil
}
Example #18
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	c.req.reset()
	if err := c.dec.Decode(&c.req); err != nil {
		return err
	}
	r.ServiceMethod = c.req.Method

	// JSON request id can be any JSON value;
	// RPC package expects uint64.  Translate to
	// internal uint64 and save JSON on the side.
	c.mutex.Lock()
	c.seq++
	c.pending[c.seq] = c.req.Id
	c.req.Id = nil
	r.Seq = c.seq
	c.mutex.Unlock()

	return nil
}
Example #19
0
func (c *serverCodec) ReadRequestHeader(request *rpc.Request) error {
	name, messageType, seq, err := c.protocol.ReadMessageBegin(c.transport)
	if err != nil {
		return err
	}
	name = CamelCase(name)
	if strings.ContainsRune(name, '.') {
		request.ServiceMethod = name
	} else {
		request.ServiceMethod = "Thrift." + name
	}
	request.Seq = uint64(seq)

	if messageType != messageTypeCall { // Currently don't support one way
		return errors.New("Exception Call message type")
	}

	return nil
}
Example #20
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) (err error) {

	c.req.reset()

	msg, err := c.zsock.RecvMessageBytes(0)
	if err != nil || len(msg) < 2 {
		return err
	}
	identity := string(msg[0])
	//XXX only last one?
	o, err := c.req.UnmarshalMsg(msg[len(msg)-1])

	if err != nil || c.req.Name == "" {
		glog.Errorf("o=%#v err=%s", o, err)
		return fmt.Errorf("zerorpc: Error %s c.req=%#v leftbytes=%#v", err.Error(), c.req, o)
	}

	if c.req.Header.Version != PROTOCAL_VERSION {
		return fmt.Errorf("zerorpc: Version not matching with request, expecting %d but sending %d", PROTOCAL_VERSION, c.req.Header.Version)
	}

	switch c.req.Name {
	case "_zpc_hb":
		c.req.Name = "InternalService.HeartBeat"
	case "_zerorpc_inspect":
		c.req.Name = "InternalService.Inspect"
	}

	glog.V(1).Infof("Receiving Event %s", c.req)

	c.mutex.Lock()
	defer c.mutex.Unlock()

	c.checkIdentity(identity)
	c.req.Identity = identity
	r.ServiceMethod = c.req.Name
	c.seq++
	c.pending[c.seq] = c.req
	r.Seq = c.seq

	return
}
Example #21
0
func (s *codec) ReadRequestHeader(req *rpc.Request) error {
	if s.Shutdown {
		return rpc.ErrShutdown
	}

	var header rpc4.Header
	if err := readProto(s.r, &header); err != nil {
		return err
	}
	if header.GetMethod() == "" {
		return fmt.Errorf("header missing method: %s", header)
	}

	req.ServiceMethod = header.GetMethod()
	req.Seq = header.GetSequence()

	s.requestPayload = header.GetPayloadPresent()

	return nil
}
Example #22
0
func (c *ServerCodec) ReadRequestHeader(r *rpc.Request) (err error) {
	if c.payload != nil {
		panic("payload is not nil")
	}

	var msg *RpcMessage
	msg, err = Decode(c.r)
	if err != nil {
		return
	}

	if *msg.Type != *MessageType_REQUEST.Enum() {
		err = fmt.Errorf("Wrong message type.")
		return
	}

	r.ServiceMethod = *msg.Service + "." + *msg.Method
	r.Seq = *msg.Id
	c.payload = msg.Request
	return nil
}
Example #23
0
// PreWriteRequest adds Authorization info in requests
func (plugin *AuthorizationClientPlugin) PreWriteRequest(r *rpc.Request, body interface{}) error {
	var b bytes.Buffer
	enc := gob.NewEncoder(&b)
	plugin.AuthorizationAndServiceMethod.ServiceMethod = r.ServiceMethod
	err := enc.Encode(plugin.AuthorizationAndServiceMethod)
	if err != nil {
		return err
	}

	r.ServiceMethod = b.String()
	return nil
}
Example #24
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) (err error) {
	// If return error:
	// - codec will be closed
	// So, try to send error reply to client before returning error.
	var raw json.RawMessage
	if err := c.dec.Decode(&raw); err != nil {
		c.encmutex.Lock()
		c.enc.Encode(serverResponse{Version: "2.0", ID: &null, Error: errParse})
		c.encmutex.Unlock()
		return err
	}

	if len(raw) > 0 && raw[0] == '[' {
		c.req.Version = "2.0"
		c.req.Method = "JSONRPC2.Batch"
		c.req.Params = &raw
		c.req.ID = &null
	} else if err := json.Unmarshal(raw, &c.req); err != nil {
		if err.Error() == "bad request" {
			c.encmutex.Lock()
			c.enc.Encode(serverResponse{Version: "2.0", ID: &null, Error: errRequest})
			c.encmutex.Unlock()
		}
		return err
	}

	r.ServiceMethod = c.req.Method

	// JSON request id can be any JSON value;
	// RPC package expects uint64.  Translate to
	// internal uint64 and save JSON on the side.
	c.mutex.Lock()
	c.seq++
	c.pending[c.seq] = c.req.ID
	c.req.ID = nil
	r.Seq = c.seq
	c.mutex.Unlock()

	return nil
}
Example #25
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	err := c.readRequestHeader(c.r, &c.reqHeader)
	if err != nil {
		return err
	}

	r.Seq = c.reqHeader.Id
	if c.reqHeader.Method == nil {
		if int(c.reqHeader.MethodId) >= len(c.methods) {
			return fmt.Errorf("unexpected method-id: %d >= %d",
				c.reqHeader.MethodId, len(c.methods))
		}
		r.ServiceMethod = c.methods[c.reqHeader.MethodId]
	} else if int(c.reqHeader.MethodId) > len(c.methods) {
		return fmt.Errorf("unexpected method-id: %d > %d",
			c.reqHeader.MethodId, len(c.methods))
	} else if int(c.reqHeader.MethodId) == len(c.methods) {
		c.methods = append(c.methods, *c.reqHeader.Method)
		r.ServiceMethod = *c.reqHeader.Method
	}
	return nil
}
Example #26
0
func (c *serverCodec) ReadRequestHeader(rpcreq *rpc.Request) error {
	f, err := read(c.r)
	if err != nil {
		return err
	}

	req := &Request{}
	if err := proto.Unmarshal(f, req); err != nil {
		return err
	}

	rpcreq.ServiceMethod = req.GetMethod()

	c.mutex.Lock()
	rpcreq.Seq = c.seq
	c.seq++
	c.req = req
	c.pending[rpcreq.Seq] = req.GetId()
	c.mutex.Unlock()

	return nil
}
Example #27
0
// PostReadRequestHeader extracts Authorization header from ServiceMethod field.
func (plugin *AuthorizationServerPlugin) PostReadRequestHeader(r *rpc.Request) (err error) {
	b := bytes.NewBufferString(r.ServiceMethod)
	var aAndS AuthorizationAndServiceMethod
	dec := gob.NewDecoder(b)
	err = dec.Decode(&aAndS)
	if err == nil {
		r.ServiceMethod = aAndS.ServiceMethod

		if plugin.AuthorizationFunc != nil {
			err = plugin.AuthorizationFunc(&aAndS)
		}
	}
	return
}
Example #28
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) error {
	var header wire.Header

	if err := readProto(c.r, &header); err != nil {
		return err
	}
	if header.Method == nil {
		return fmt.Errorf("ServerCodec.ReadRequestHeader: header missing method: %s", header)
	}
	if header.Id == nil {
		return fmt.Errorf("ServerCodec.ReadRequestHeader: header missing seq: %s", header)
	}

	r.ServiceMethod = header.GetMethod()

	c.mutex.Lock()
	c.seq++
	c.pending[c.seq] = header.GetId()
	r.Seq = c.seq
	c.mutex.Unlock()

	return nil
}
Example #29
0
func (cdc *HrpcServerCodec) ReadRequestHeader(req *rpc.Request) error {
	hdr := common.HrpcRequestHeader{}
	if cdc.lg.TraceEnabled() {
		cdc.lg.Tracef("%s: Reading HRPC request header.\n", cdc.conn.RemoteAddr())
	}
	cdc.conn.SetDeadline(time.Now().Add(cdc.hsv.ioTimeo))
	err := binary.Read(cdc.conn, binary.LittleEndian, &hdr)
	if err != nil {
		if err == io.EOF && cdc.numHandled > 0 {
			return newIoError(cdc, fmt.Sprintf("Remote closed connection "+
				"after writing %d message(s)", cdc.numHandled), common.DEBUG)
		}
		return newIoError(cdc,
			fmt.Sprintf("Error reading request header: %s", err.Error()), common.WARN)
	}
	if cdc.lg.TraceEnabled() {
		cdc.lg.Tracef("%s: Read HRPC request header %s\n",
			cdc.conn.RemoteAddr(), asJson(&hdr))
	}
	if hdr.Magic != common.HRPC_MAGIC {
		return newIoErrorWarn(cdc, fmt.Sprintf("Invalid request header: expected "+
			"magic number of 0x%04x, but got 0x%04x", common.HRPC_MAGIC, hdr.Magic))
	}
	if hdr.Length > common.MAX_HRPC_BODY_LENGTH {
		return newIoErrorWarn(cdc, fmt.Sprintf("Length prefix was too long.  "+
			"Maximum length is %d, but we got %d.", common.MAX_HRPC_BODY_LENGTH,
			hdr.Length))
	}
	req.ServiceMethod = common.HrpcMethodIdToMethodName(hdr.MethodId)
	if req.ServiceMethod == "" {
		return newIoErrorWarn(cdc, fmt.Sprintf("Unknown MethodID code 0x%04x",
			hdr.MethodId))
	}
	req.Seq = hdr.Seq
	cdc.length = hdr.Length
	return nil
}
Example #30
0
func (c *serverCodec) ReadRequestHeader(r *rpc.Request) (err error) {
	// Read message
	// log.Println("Server ready to receive")
	parts, err := c.sock.RecvMultipart(0)
	if err != nil {
		return
	}
	// log.Println("Server recieved multipart mesage:", Stringify(parts))

	nId := len(parts) - 2
	ids := parts[:nId]
	hdr := parts[nId]
	data := parts[nId+1]

	// Read header and body into proto buf
	c.req.SetBuf(data)

	// Unmarshal header
	h := new(Header)
	err = proto.Unmarshal(hdr, h)
	if err != nil {
		return
	}

	// Increment sequence id and save ids for sending request
	seq := <-SeqNum
	c.packetIds[seq] = ids
	c.clientIds[seq] = h.GetId()

	r.Seq = seq
	r.ServiceMethod = h.GetServiceMethod()

	// log.Println("Server recieved service request:", r)

	return
}