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