Hardware Reference
In-Depth Information
The Structure and Syntax of
Text-Based Protocols
You've seen a number of text-based protocols through-
out this topic, and you've even written a few of your own.
Protocols are basically ways of structuring data. Some
protocols simply relay information, and others give
commands—either explicitly or implicitly—through a
request. It's worth reviewing some of their grammars so
that when you run into similar ones, you'll know what to
look for.
Everything in the header is a parameter of the request:
who you're requesting from, the connection type, the type
of content to be exchanged, the content length. Everything
in the body is a parameter of the content: name and age.
Having a clear separation between the header formatting
and the content formatting makes it easier to separate
them when writing a program, as you've already seen.
Structured Data Formats
When you've got data to exchange that's more complex
than a list of name-value pairs, you need a structure for it.
For example, imagine an array of sensors spread around
your house:
Simple Data Formats
The simplest was the comma-separated value string you
wrote for Monski Pong. Comma-separated values (CSV)
and tab-separated values (TSV) are ubiquitous, and it's
common to end a CSV string with a newline, or a newline
and carriage return. The NMEA-0183 protocol used by
GPS receivers is a good example of CSV in practice.
Address
Location
Last read
Value
1
kitchen
12:30:00
PM
60
You also saw a few examples of organizing data in name-
value pairs . Any time you have a list of items, and each has
a name associated with it, you've got a name-value pair.
The environment variables in PHP—including $_GET , $_
POST , $_REQUEST , and $_SERVER —give you name-value
pairs. In programming languages, associative arrays are
basically lists of name-value pairs.
2
living room 05:40:00
AM
54
3
bathroom
01:15:00
AM
23
4
bedroom
09:25:00
AM
18
You've also used a few Internet transfer protocols , like
HyperText Transfer Protocol (HTTP) and Simple Mail
Transfer Protocol (SMTP) . They share a common format
as well: they open with a command like GET or POST, and
then follow with properties of the transfer, each on its own
line. Each line starts with the description of the property,
like the content-length, separated from the actual value by
a colon. The header of the transfer is separated from the
body of the message by two newlines, and the message is
usually closed with two newlines as well. Any data coming
from the client was sent as name-value pair arrays,
separated by a colon for information about the transfer,
or by an ampersand for data items in the content of the
transfer. Take a second look at this HTTP POST request
from Chapter 2 as an example:
5
hallway
06:20:00
AM
3
You've got more than just a list of single items, and more
than just a few name-value pairs. In fact, each line of the
table is a list of name-value pairs. This is where a struc-
tured data format comes in handy. JavaScript Object
Notation , or JSON , is a popular notation format for struc-
tured data like this.
POST /age_checker.php HTTP/1.1
Host: example.com
Connection:Close
Content-Type: application/x-www-form-urlencoded
Content-length: 16
name=tom&age=14
 
Search WWH ::




Custom Search