Esempio n. 1
0
func (frame *SYN_REPLY) ReadFrom(reader io.Reader) (int64, error) {
	data, err := common.ReadExactly(reader, 12)
	if err != nil {
		return 0, err
	}

	err = controlFrameCommonProcessing(data[:5], _SYN_REPLY, common.FLAG_FIN)
	if err != nil {
		return 12, err
	}

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

	// Read in data.
	header, err := common.ReadExactly(reader, length-4)
	if err != nil {
		return 12, err
	}

	frame.Flags = common.Flags(data[4])
	frame.StreamID = common.StreamID(common.BytesToUint32(data[8:12]))
	frame.rawHeader = header

	return int64(length + 8), nil
}
Esempio n. 2
0
func decodeSetting(data []byte) *common.Setting {
	if len(data) < 8 {
		return nil
	}

	setting := new(common.Setting)
	setting.Flags = common.Flags(data[0])
	setting.ID = common.BytesToUint24(data[1:])
	setting.Value = common.BytesToUint32(data[4:])

	return setting
}
Esempio n. 3
0
func decodeSetting(data []byte) *common.Setting {
	if len(data) < 8 {
		return nil
	}

	setting := new(common.Setting)
	setting.ID = common.BytesToUint24Reverse(data[0:]) // Might need to reverse this.
	setting.Flags = common.Flags(data[3])
	setting.Value = common.BytesToUint32(data[4:])

	return setting
}
Esempio n. 4
0
func (frame *SETTINGS) ReadFrom(reader io.Reader) (int64, error) {
	c := common.ReadCounter{R: reader}

	data, err := common.ReadExactly(&c, 12)
	if err != nil {
		return c.N, err
	}

	err = controlFrameCommonProcessing(data[:5], _SETTINGS, common.FLAG_SETTINGS_CLEAR_SETTINGS)
	if err != nil {
		return c.N, err
	}

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

	// Check size.
	numSettings := int(common.BytesToUint32(data[8:12]))
	if length != 4+(8*numSettings) {
		return c.N, common.IncorrectDataLength(length, 4+(8*numSettings))
	}

	// Read in data.
	settings, err := common.ReadExactly(&c, 8*numSettings)
	if err != nil {
		return c.N, err
	}

	frame.Flags = common.Flags(data[4])
	frame.Settings = make(common.Settings)
	for i := 0; i < numSettings; i++ {
		j := i * 8
		setting := decodeSetting(settings[j:])
		if setting == nil {
			return c.N, errors.New("Error: Failed to parse settings.")
		}
		frame.Settings[setting.ID] = setting
	}

	return c.N, nil
}
Esempio n. 5
0
func (frame *SYN_STREAM) ReadFrom(reader io.Reader) (int64, error) {
	c := common.ReadCounter{R: reader}
	data, err := common.ReadExactly(&c, 18)
	if err != nil {
		return c.N, err
	}

	err = controlFrameCommonProcessing(data[:5], _SYN_STREAM, common.FLAG_FIN|common.FLAG_UNIDIRECTIONAL)
	if err != nil {
		return c.N, err
	}

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

	// Read in data.
	header, err := common.ReadExactly(&c, length-10)
	if err != nil {
		return c.N, err
	}

	frame.Flags = common.Flags(data[4])
	frame.StreamID = common.StreamID(common.BytesToUint32(data[8:12]))
	frame.AssocStreamID = common.StreamID(common.BytesToUint32(data[12:16]))
	frame.Priority = common.Priority(data[16] >> 5)
	frame.Slot = data[17]
	frame.rawHeader = header

	if !frame.StreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}
	if frame.StreamID.Zero() {
		return c.N, common.StreamIdIsZero
	}
	if !frame.AssocStreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}

	return c.N, nil
}
Esempio n. 6
0
File: data.go Progetto: 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
}
Esempio n. 7
0
func (frame *HEADERS) ReadFrom(reader io.Reader) (int64, error) {
	c := common.ReadCounter{R: reader}
	data, err := common.ReadExactly(&c, 16)
	if err != nil {
		return c.N, err
	}

	err = controlFrameCommonProcessing(data[:5], _HEADERS, common.FLAG_FIN)
	if err != nil {
		return c.N, err
	}

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

	// Read in data.
	header, err := common.ReadExactly(&c, length-8)
	if err != nil {
		return c.N, err
	}

	frame.Flags = common.Flags(data[4])
	frame.StreamID = common.StreamID(common.BytesToUint32(data[8:12]))
	frame.rawHeader = header

	if !frame.StreamID.Valid() {
		return c.N, common.StreamIdTooLarge
	}
	if frame.StreamID.Zero() {
		return c.N, common.StreamIdIsZero
	}

	return c.N, nil
}