Type-length-value数据格式

Type-length-value

From Wikipedia, the free encyclopedia

Within data communication protocols, optional information may be encoded as a type-length-value or TLV element inside of the protocol. TLV is also known as tag-length value.[1]

The type and length are fixed in size (typically 1-4 bytes), and the value field is of variable size. These fields are used as follows:

Type
A binary code, often simply alphanumeric, which indicates the kind of field that this part of the message represents;
Length
The size of the value field (typically in bytes);
Value
Variable-sized series of bytes which contains data for this part of the message.

Some advantages of using a TLV representation:

  • TLV sequences are easily searched using generalized parsing functions;
  • New message elements which are received at an older node can be safely skipped and the rest of the message can be parsed. This is similar to the way that unknown XML tags can be safely skipped;
  • TLV elements can be placed in any order inside the message body;
  • TLV elements are typically used in a binary format which makes parsing faster and the data smaller;
  • It is easier to generate XML from TLV to make human inspection of the data possible.

Contents

   [hide

[edit]Examples

Imagine a message to make a telephone call. In a first version of a system this might use two message elements, a "command" and a "phoneNumberToCall":

command_c/4/makeCall_c/phoneNumberToCall_c/8/"722-4246"

Here command_cmakeCall_c and phoneNumberToCall_c are integer constants and 4 and 8 are the lengths of the "value" fields, respectively.

Later (in version 2) a new field containing the calling number could be added:

command_c/4/makeCall_c/callingNumber_c/14/"1-613-715-9719"/phoneNumberToCall_c/8/"722-4246"

A version 1 system which received a message from a version 2 system would first read the command_c element and then read an element of typecallingNumber_c. The version 1 system does not understand ;callingNumber_c

so the length field is read (i.e. 14) and the system skips forward 14 bytes to read

phoneNumberToCall_c

which it understands, and message parsing carries on.

An example of usage is the Link Layer Discovery Protocol which allows for the sending of organizational-specific information as a TLV element within LLDP packets. Another example is the RR protocol used in GSM cell phones, defined in 3GPP 04.18.

In the RR protocol, each message is defined as a sequence of information elements.

Many other protocols use TLVs, such as COPSIS-IS, and RADIUS.

[edit]Other ways of representing data

Core TCP/IP protocols (particularly IPTCP, and UDP) use predefined, static fields.

Common TCP/IP-based protocols such as HTTPFTPSMTPPOP3, and SIP use text-based "Field: Value" pairs formatted according to RFC 2822.

ASN.1 specifies several TLV-based encoding rules (BERDER), as well as non-TLV based ones (PERXER).

CSN.1 describes encoding rules using non-TLV semantics.

More recently, XML has been used to implement messaging between different nodes in a network. These messages are typically prefixed with line-based text commands, such as with BEEP.

[edit]See also

[edit]References

### HTTP `Content-Length` 头部的使用及其问题 #### 定义与作用 `Content-Length` 是一个用于指示消息体长度的实体头部字段。它表示以字节为单位的消息主体大小[^1]。 #### 使用场景 当服务器发送响应给客户端或者客户端向服务器提交数据时,如果已知确切的数据量,则可以设置此头来告知接收方即将接收到多少字节的内容。这有助于接收端提前分配足够的缓冲区空间并判断传输是否完成。 对于短连接,在HTTP/1.0中如果没有指定`Transfer-Encoding: chunked`编码方式的话就必须提供`Content-Length`;而在HTTP/1.1版本里,默认支持分块传送(chunked transfer),因此即使不给出具体的长度也能正常工作。 #### 常见问题及解决方案 ##### 1. 不匹配错误 最常见的情况之一就是实际传输的数据量与声明的数量不符。这种情况下可能会导致解析失败或者是部分读取不到预期之外的信息。为了避免此类情况发生,应该确保每次请求前都准确计算好要发送出去的数据总量再填入到该头部之中。 ##### 2. 动态内容处理不当 动态生成的内容可能难以预先知道其最终尺寸,这时强行设定固定数值容易引发上述提到过的不一致现象。针对这种情况可以选择采用分片传输机制(`Transfer-Encoding: chunked`) 或者是在全部准备好之后才一次性发出整个报文连同正确的`Content-Length`一起传递过去。 ```python import requests headers = {'Content-Type': 'application/json'} data = {"key": "value"} response = requests.post('https://example.com/api', json=data, headers=headers) print(f'Status Code: {response.status_code}') print(f'Headers:\n{response.headers}') ``` #### 性能考量 频繁更改`Content-Length`值会增加额外开销,特别是在高并发环境下可能导致性能下降。所以在设计API接口时应尽可能保持稳定不变的负载模式以便优化网络交互效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值