1 /* Generic support for headers.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
44 /* This file contains the generic routines for work with headers.
45 Currently they are used only by HTTP in http.c, but they can be
46 used by anything that cares about RFC822-style headers.
48 Header is defined in RFC2068, as quoted below. Note that this
49 definition is not HTTP-specific -- it is virtually
50 indistinguishable from the one given in RFC822 or RFC1036.
52 message-header = field-name ":" [ field-value ] CRLF
55 field-value = *( field-content | LWS )
57 field-content = <the OCTETs making up the field-value
58 and consisting of either *TEXT or combinations
59 of token, tspecials, and quoted-string>
61 The public functions are header_get() and header_process(), which
65 /* Check whether HEADER begins with NAME and, if yes, skip the `:' and
66 the whitespace, and call PROCFUN with the arguments of HEADER's
67 contents (after the `:' and space) and ARG. Otherwise, return 0. */
69 header_process (const char *header, const char *name,
70 int (*procfun) (const char *, void *),
73 /* Check whether HEADER matches NAME. */
74 while (*name && (TOLOWER (*name) == TOLOWER (*header)))
76 if (*name || *header++ != ':')
79 header += skip_lws (header);
81 return ((*procfun) (header, arg));
84 /* Helper functions for use with header_process(). */
86 /* Extract a long integer from HEADER and store it to CLOSURE. If an
87 error is encountered, return 0, else 1. */
89 header_extract_number (const char *header, void *closure)
91 const char *p = header;
94 for (result = 0; ISDIGIT (*p); p++)
95 result = 10 * result + (*p - '0');
97 /* Failure if no number present. */
101 /* Skip trailing whitespace. */
104 /* Indicate failure if trailing garbage is present. */
108 *(long *)closure = result;
112 /* Strdup HEADER, and place the pointer to CLOSURE. */
114 header_strdup (const char *header, void *closure)
116 *(char **)closure = xstrdup (header);
120 /* Write the value 1 into the integer pointed to by CLOSURE. */
122 header_exists (const char *header, void *closure)
128 /* Skip LWS (linear white space), if present. Returns number of
129 characters to skip. */
131 skip_lws (const char *string)
133 const char *p = string;
135 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')