9 Header Field Encoding

▼ |  | Net: Networking Libraries

1 | HTTP Client
2 | URLs and HTTP
3 | URI Codec: Encoding and Decoding URIs
4 | FTP: Client
5 | Send URL: Opening a Web Browser
6 | SMTP: Sending E-Mail
7 | sendmail: Sending E-Mail
8 | Headers: Parsing and Constructing
9 | Header Field Encoding
10 | IMAP: Reading Mail
11 | POP3: Reading Mail
12 | MIME: Decoding Internet Data
13 | Base 64: Encoding and Decoding
14 | Quoted-Printable: Encoding and Decoding
15 | DNS: Domain Name Service Queries
16 | NNTP: Newsgroup Protocol
17 | TCP: Unit and Signature
18 | TCP Redirect: tcp^ via Channels
19 | SSL Unit: tcp^ via SSL
20 | CGI Scripts
21 | Cookie: Legacy HTTP Client Storage
22 | Git Repository Checkout
| Bibliography
| Index

On this page:

encode-for-header
decode-for-header
generalize-encoding

Racket
top contents ← prev up next →

## 9 Header Field Encoding🔗ℹ

(require net/unihead) | package: net-lib

The net/unihead module provides utilities for encoding and decoding header fields using the =?‹encoding›?‹transport›?...?= format.

> procedure (encode-for-header s) → string?
> s : string?

Encodes s for use in a header.

If s contains only ASCII characters, then the result string will have the same content as the given string. If s contains only Latin-1 characters, then on each CRLF-delimited line, the space-delimited sequence containing all non-ASCII characters in s is encoded with a =?ISO-8859-1?Q?...?= sequence. If s contains non-Latin-1 characters, then on each CRLF-delimited line, a space-delimited sequence containing all non-ASCII characters in s is encoded with a =?UTF-8?B?...?= sequence.

Examples:

> > (encode-for-header "English")
> "English"
> > (encode-for-header "français")
> "=?ISO-8859-1?Q?fran=E7ais?="
> > (encode-for-header "→")
> "=?UTF-8?B?4oaS?="
> > (encode-for-header "→\r\nboth → and français here")
> "=?UTF-8?B?4oaS?=\r\nboth =?UTF-8?B?4oaSIGFuZCBmcmFuw6dhaXM=?= here"

> procedure (decode-for-header s) → string?
> s : string?

Decodes header fields that use the =?‹encoding›?‹transport›?...?= encoding format. The specified ‹encoding› is generalized via generalize-encoding before decoding content.

Examples:

> > (decode-for-header "English")
> "English"
> > (decode-for-header "=?UTF-8?B?4oaS?= =?ISO-8859-1?Q?fran=E7ais?=")
> "→ français"

> procedure (generalize-encoding s) → (or string? bytes?)
> s : (or string? bytes?)

Generalizes the encoding name s to compensate for typical mailer bugs: Latin-1 and ASCII encodings are geenralized to WINDOWS-1252; GB and GB2312 are generalized to GBK; and KS_C_5601-1987 is generalized to CP949.
top contents ← prev up next →