]> sjero.net Git - wget/blob - src/headers.c
5ceea7542f5dff14f042412fa0ec1a759aca88c3
[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 "rbuf.h"
43 #include "headers.h"
44
45 /* This file contains the generic routines for work with headers.
46    Currently they are used only by HTTP in http.c, but they can be
47    used by anything that cares about RFC822-style headers.
48
49    Header is defined in RFC2068, as quoted below.  Note that this
50    definition is not HTTP-specific -- it is virtually
51    indistinguishable from the one given in RFC822 or RFC1036.
52
53         message-header = field-name ":" [ field-value ] CRLF
54
55         field-name     = token
56         field-value    = *( field-content | LWS )
57
58         field-content  = <the OCTETs making up the field-value
59                          and consisting of either *TEXT or combinations
60                          of token, tspecials, and quoted-string>
61
62    The public functions are header_get() and header_process(), which
63    see.  */
64
65 \f
66 /* Get a header from read-buffer RBUF and return it in *HDR.
67
68    As defined in RFC2068 and elsewhere, a header can be folded into
69    multiple lines if the continuation line begins with a space or
70    horizontal TAB.  Also, this function will accept a header ending
71    with just LF instead of CRLF.
72
73    The header may be of arbitrary length; the function will allocate
74    as much memory as necessary for it to fit.  It need not contain a
75    `:', thus you can use it to retrieve, say, HTTP status line.
76
77    All trailing whitespace is stripped from the header, and it is
78    zero-terminated.  */
79 int
80 header_get (struct rbuf *rbuf, char **hdr, enum header_get_flags flags)
81 {
82   int i;
83   int bufsize = 80;
84
85   *hdr = (char *)xmalloc (bufsize);
86   for (i = 0; 1; i++)
87     {
88       int res;
89       /* #### Use DO_REALLOC?  */
90       if (i > bufsize - 1)
91         *hdr = (char *)xrealloc (*hdr, (bufsize <<= 1));
92       res = RBUF_READCHAR (rbuf, *hdr + i);
93       if (res == 1)
94         {
95           if ((*hdr)[i] == '\n')
96             {
97               if (!((flags & HG_NO_CONTINUATIONS)
98                     || i == 0
99                     || (i == 1 && (*hdr)[0] == '\r')))
100                 {
101                   char next;
102                   /* If the header is non-empty, we need to check if
103                      it continues on to the other line.  We do that by
104                      peeking at the next character.  */
105                   res = rbuf_peek (rbuf, &next);
106                   if (res == 0)
107                     return HG_EOF;
108                   else if (res == -1)
109                     return HG_ERROR;
110                   /*  If the next character is HT or SP, just continue.  */
111                   if (next == '\t' || next == ' ')
112                     continue;
113                 }
114
115               /* Strip trailing whitespace.  (*hdr)[i] is the newline;
116                  decrement I until it points to the last available
117                  whitespace.  */
118               while (i > 0 && ISSPACE ((*hdr)[i - 1]))
119                 --i;
120               (*hdr)[i] = '\0';
121               break;
122             }
123         }
124       else if (res == 0)
125         return HG_EOF;
126       else
127         return HG_ERROR;
128     }
129   DEBUGP (("%s\n", *hdr));
130   return HG_OK;
131 }
132 \f
133 /* Check whether HEADER begins with NAME and, if yes, skip the `:' and
134    the whitespace, and call PROCFUN with the arguments of HEADER's
135    contents (after the `:' and space) and ARG.  Otherwise, return 0.  */
136 int
137 header_process (const char *header, const char *name,
138                 int (*procfun) (const char *, void *),
139                 void *arg)
140 {
141   /* Check whether HEADER matches NAME.  */
142   while (*name && (TOLOWER (*name) == TOLOWER (*header)))
143     ++name, ++header;
144   if (*name || *header++ != ':')
145     return 0;
146
147   header += skip_lws (header);
148
149   return ((*procfun) (header, arg));
150 }
151 \f
152 /* Helper functions for use with header_process().  */
153
154 /* Extract a long integer from HEADER and store it to CLOSURE.  If an
155    error is encountered, return 0, else 1.  */
156 int
157 header_extract_number (const char *header, void *closure)
158 {
159   const char *p = header;
160   long result;
161
162   for (result = 0; ISDIGIT (*p); p++)
163     result = 10 * result + (*p - '0');
164
165   /* Failure if no number present. */
166   if (p == header)
167     return 0;
168
169   /* Skip trailing whitespace. */
170   p += skip_lws (p);
171
172   /* Indicate failure if trailing garbage is present. */
173   if (*p)
174     return 0;
175
176   *(long *)closure = result;
177   return 1;
178 }
179
180 /* Strdup HEADER, and place the pointer to CLOSURE.  */
181 int
182 header_strdup (const char *header, void *closure)
183 {
184   *(char **)closure = xstrdup (header);
185   return 1;
186 }
187
188 /* Write the value 1 into the integer pointed to by CLOSURE.  */
189 int
190 header_exists (const char *header, void *closure)
191 {
192   *(int *)closure = 1;
193   return 1;
194 }
195
196 /* Skip LWS (linear white space), if present.  Returns number of
197    characters to skip.  */
198 int
199 skip_lws (const char *string)
200 {
201   const char *p = string;
202
203   while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
204     ++p;
205   return p - string;
206 }