Example #1
0
// controlFrameCommonProcessing performs checks identical between
// all control frames. This includes the control bit, the version
// number, the type byte (which is checked against the byte
// provided), and the flags (which are checked against the bitwise
// OR of valid flags provided).
func controlFrameCommonProcessing(data []byte, frameType uint16, flags byte) error {
	// Check it's a control frame.
	if data[0] != 128 {
		return common.IncorrectFrame(_DATA_FRAME, int(frameType), 2)
	}

	// Check version.
	version := (uint16(data[0]&0x7f) << 8) + uint16(data[1])
	if version != 2 {
		return common.UnsupportedVersion(version)
	}

	// Check its type.
	realType := common.BytesToUint16(data[2:])
	if realType != frameType {
		return common.IncorrectFrame(int(realType), int(frameType), 2)
	}

	// Check the flags.
	if data[4] & ^flags != 0 {
		return common.InvalidField("flags", int(data[4]), int(flags))
	}

	return nil
}
Example #2
0
File: data.go Project: vonwenm/spdy
func (frame *DATA) ReadFrom(reader io.Reader) (int64, error) {
	c := common.ReadCounter{R: reader}
	data, err := common.ReadExactly(&c, 8)
	if err != nil {
		return c.N, err
	}

	// Check it's a data frame.
	if data[0]&0x80 == 1 {
		return c.N, common.IncorrectFrame(_CONTROL_FRAME, _DATA_FRAME, 3)
	}

	// Check flags.
	if data[4] & ^byte(common.FLAG_FIN) != 0 {
		return c.N, common.InvalidField("flags", int(data[4]), common.FLAG_FIN)
	}

	// Get and check length.
	length := int(common.BytesToUint24(data[5:8]))
	if length == 0 && data[4] == 0 {
		return c.N, common.IncorrectDataLength(length, 1)
	} else if length > common.MAX_FRAME_SIZE-8 {
		return c.N, common.FrameTooLarge
	}

	// Read in data.
	if length != 0 {
		frame.Data, err = common.ReadExactly(&c, length)
		if err != nil {
			return c.N, err
		}
	}

	frame.StreamID = common.StreamID(common.BytesToUint32(data[0:4]))
	frame.Flags = common.Flags(data[4])
	if frame.Data == nil {
		frame.Data = []byte{}
	}

	return c.N, nil
}