]> sjero.net Git - wget/blob - src/headers.c
[svn] Remove the "rbuf" buffering layer. Provide peeking primitives instead.
[wget] / src / headers.c
1 /* Generic support for headers.
2    Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
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.
10
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.
15
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.
19
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.  */
29
30 #include <config.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #else
37 # include <strings.h>
38 #endif
39
40 #include "wget.h"
41 #include "connect.h"
42 #include "headers.h"
43
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.
47
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.
51
52         message-header = field-name ":" [ field-value ] CRLF
53
54         field-name     = token
55         field-value    = *( field-content | LWS )
56
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>
60
61    The public functions are header_get() and header_process(), which
62    see.  */
63
64
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.  */
68 int
69 header_process (const char *header, const char *name,
70                 int (*procfun) (const char *, void *),
71                 void *arg)
72 {
73   /* Check whether HEADER matches NAME.  */
74   while (*name && (TOLOWER (*name) == TOLOWER (*header)))
75     ++name, ++header;
76   if (*name || *header++ != ':')
77     return 0;
78
79   header += skip_lws (header);
80
81   return ((*procfun) (header, arg));
82 }
83 \f
84 /* Helper functions for use with header_process().  */
85
86 /* Extract a long integer from HEADER and store it to CLOSURE.  If an
87    error is encountered, return 0, else 1.  */
88 int
89 header_extract_number (const char *header, void *closure)
90 {
91   const char *p = header;
92   long result;
93
94   for (result = 0; ISDIGIT (*p); p++)
95     result = 10 * result + (*p - '0');
96
97   /* Failure if no number present. */
98   if (p == header)
99     return 0;
100
101   /* Skip trailing whitespace. */
102   p += skip_lws (p);
103
104   /* Indicate failure if trailing garbage is present. */
105   if (*p)
106     return 0;
107
108   *(long *)closure = result;
109   return 1;
110 }
111
112 /* Strdup HEADER, and place the pointer to CLOSURE.  */
113 int
114 header_strdup (const char *header, void *closure)
115 {
116   *(char **)closure = xstrdup (header);
117   return 1;
118 }
119
120 /* Write the value 1 into the integer pointed to by CLOSURE.  */
121 int
122 header_exists (const char *header, void *closure)
123 {
124   *(int *)closure = 1;
125   return 1;
126 }
127
128 /* Skip LWS (linear white space), if present.  Returns number of
129    characters to skip.  */
130 int
131 skip_lws (const char *string)
132 {
133   const char *p = string;
134
135   while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
136     ++p;
137   return p - string;
138 }