3 * $Id: HttpReply.cc,v 1.39 1999/12/30 17:36:17 wessels Exp $
5 * DEBUG: section 58 HTTP Reply (Response)
6 * AUTHOR: Alex Rousskov
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
41 /* these entity-headers must be ignored if a bogus server sends them in 304 */
42 static HttpHeaderMask Denied304HeadersMask
;
43 static http_hdr_type Denied304HeadersArr
[] =
45 HDR_ALLOW
, HDR_CONTENT_ENCODING
, HDR_CONTENT_LANGUAGE
, HDR_CONTENT_LENGTH
,
46 HDR_CONTENT_LOCATION
, HDR_CONTENT_RANGE
, HDR_LAST_MODIFIED
, HDR_LINK
,
51 static void httpReplyInit(HttpReply
* rep
);
52 static void httpReplyClean(HttpReply
* rep
);
53 static void httpReplyDoDestroy(HttpReply
* rep
);
54 static void httpReplyHdrCacheInit(HttpReply
* rep
);
55 static void httpReplyHdrCacheClean(HttpReply
* rep
);
56 static int httpReplyParseStep(HttpReply
* rep
, const char *parse_start
, int atEnd
);
57 static int httpReplyParseError(HttpReply
* rep
);
58 static int httpReplyIsolateStart(const char **parse_start
, const char **blk_start
, const char **blk_end
);
61 /* module initialization */
63 httpReplyInitModule(void)
65 httpHeaderMaskInit(&Denied304HeadersMask
, 0);
66 httpHeaderCalcMask(&Denied304HeadersMask
, (const int *) Denied304HeadersArr
, countof(Denied304HeadersArr
));
73 HttpReply
*rep
= memAllocate(MEM_HTTP_REPLY
);
74 debug(58, 7) ("creating rep: %p\n", rep
);
80 httpReplyInit(HttpReply
* rep
)
84 rep
->pstate
= psReadyToParseStartLine
;
85 httpBodyInit(&rep
->body
);
86 httpHeaderInit(&rep
->header
, hoReply
);
87 httpReplyHdrCacheInit(rep
);
88 httpStatusLineInit(&rep
->sline
);
92 httpReplyClean(HttpReply
* rep
)
95 httpBodyClean(&rep
->body
);
96 httpReplyHdrCacheClean(rep
);
97 httpHeaderClean(&rep
->header
);
98 httpStatusLineClean(&rep
->sline
);
102 httpReplyDestroy(HttpReply
* rep
)
105 debug(58, 7) ("destroying rep: %p\n", rep
);
107 httpReplyDoDestroy(rep
);
111 httpReplyReset(HttpReply
* rep
)
117 /* absorb: copy the contents of a new reply to the old one, destroy new one */
119 httpReplyAbsorb(HttpReply
* rep
, HttpReply
* new_rep
)
121 assert(rep
&& new_rep
);
124 /* cannot use Clean() on new reply now! */
125 httpReplyDoDestroy(new_rep
);
129 * httpReplyParse takes character buffer of HTTP headers (buf),
130 * which may not be NULL-terminated, and fills in an HttpReply
131 * structure (rep). The parameter 'end' specifies the offset to
132 * the end of the reply headers. The caller may know where the
133 * end is, but is unable to NULL-terminate the buffer. This function
134 * returns true on success.
137 httpReplyParse(HttpReply
* rep
, const char *buf
, ssize_t end
)
140 * this extra buffer/copy will be eliminated when headers become
141 * meta-data in store. Currently we have to xstrncpy the buffer
142 * becuase somebody may feed a non NULL-terminated buffer to
145 char *headers
= memAllocate(MEM_4K_BUF
);
147 /* reset current state, because we are not used in incremental fashion */
149 /* put a string terminator */
150 xstrncpy(headers
, buf
, 4096);
151 if (end
>= 0 && end
< 4096)
152 *(headers
+ end
) = '\0';
153 success
= httpReplyParseStep(rep
, headers
, 0);
154 memFree(headers
, MEM_4K_BUF
);
159 httpReplyPackInto(const HttpReply
* rep
, Packer
* p
)
162 httpStatusLinePackInto(&rep
->sline
, p
);
163 httpHeaderPackInto(&rep
->header
, p
);
164 packerAppend(p
, "\r\n", 2);
165 httpBodyPackInto(&rep
->body
, p
);
168 /* create memBuf, create mem-based packer, pack, destroy packer, return MemBuf */
170 httpReplyPack(const HttpReply
* rep
)
177 packerToMemInit(&p
, &mb
);
178 httpReplyPackInto(rep
, &p
);
183 /* swap: create swap-based packer, pack, destroy packer */
185 httpReplySwapOut(const HttpReply
* rep
, StoreEntry
* e
)
190 packerToStoreInit(&p
, e
);
191 httpReplyPackInto(rep
, &p
);
196 httpPackedReply(double ver
, http_status status
, const char *ctype
,
197 int clen
, time_t lmt
, time_t expires
)
199 HttpReply
*rep
= httpReplyCreate();
201 httpReplySetHeaders(rep
, ver
, status
, ctype
, NULL
, clen
, lmt
, expires
);
202 mb
= httpReplyPack(rep
);
203 httpReplyDestroy(rep
);
208 httpPacked304Reply(const HttpReply
* rep
)
210 static const http_hdr_type ImsEntries
[] =
211 {HDR_DATE
, HDR_CONTENT_TYPE
, HDR_EXPIRES
, HDR_LAST_MODIFIED
, /* eof */ HDR_OTHER
};
219 packerToMemInit(&p
, &mb
);
220 memBufPrintf(&mb
, "%s", "HTTP/1.0 304 Not Modified\r\n");
221 for (t
= 0; ImsEntries
[t
] != HDR_OTHER
; ++t
)
222 if ((e
= httpHeaderFindEntry(&rep
->header
, ImsEntries
[t
])))
223 httpHeaderEntryPackInto(e
, &p
);
224 memBufAppend(&mb
, "\r\n", 2);
230 httpReplySetHeaders(HttpReply
* reply
, double ver
, http_status status
, const char *reason
,
231 const char *ctype
, int clen
, time_t lmt
, time_t expires
)
235 httpStatusLineSet(&reply
->sline
, ver
, status
, reason
);
236 hdr
= &reply
->header
;
237 httpHeaderPutStr(hdr
, HDR_SERVER
, full_appname_string
);
238 httpHeaderPutStr(hdr
, HDR_MIME_VERSION
, "1.0");
239 httpHeaderPutTime(hdr
, HDR_DATE
, squid_curtime
);
241 httpHeaderPutStr(hdr
, HDR_CONTENT_TYPE
, ctype
);
242 stringInit(&reply
->content_type
, ctype
);
244 reply
->content_type
= StringNull
;
246 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, clen
);
248 httpHeaderPutTime(hdr
, HDR_EXPIRES
, expires
);
249 if (lmt
> 0) /* this used to be lmt != 0 @?@ */
250 httpHeaderPutTime(hdr
, HDR_LAST_MODIFIED
, lmt
);
251 reply
->date
= squid_curtime
;
252 reply
->content_length
= clen
;
253 reply
->expires
= expires
;
254 reply
->last_modified
= lmt
;
258 httpRedirectReply(HttpReply
* reply
, http_status status
, const char *loc
)
262 httpStatusLineSet(&reply
->sline
, 1.0, status
, httpStatusString(status
));
263 hdr
= &reply
->header
;
264 httpHeaderPutStr(hdr
, HDR_SERVER
, full_appname_string
);
265 httpHeaderPutTime(hdr
, HDR_DATE
, squid_curtime
);
266 httpHeaderPutInt(hdr
, HDR_CONTENT_LENGTH
, 0);
267 httpHeaderPutStr(hdr
, HDR_LOCATION
, loc
);
268 reply
->date
= squid_curtime
;
269 reply
->content_length
= 0;
273 httpReplyUpdateOnNotModified(HttpReply
* rep
, HttpReply
* freshRep
)
275 assert(rep
&& freshRep
);
277 httpReplyHdrCacheClean(rep
);
278 /* update raw headers */
279 httpHeaderUpdate(&rep
->header
, &freshRep
->header
,
280 (const HttpHeaderMask
*) &Denied304HeadersMask
);
282 httpReplyHdrCacheInit(rep
);
286 /* internal routines */
288 /* internal function used by Destroy and Absorb */
290 httpReplyDoDestroy(HttpReply
* rep
)
292 memFree(rep
, MEM_HTTP_REPLY
);
295 /* sync this routine when you update HttpReply struct */
297 httpReplyHdrCacheInit(HttpReply
* rep
)
299 const HttpHeader
*hdr
= &rep
->header
;
301 rep
->content_length
= httpHeaderGetInt(hdr
, HDR_CONTENT_LENGTH
);
302 rep
->date
= httpHeaderGetTime(hdr
, HDR_DATE
);
303 rep
->last_modified
= httpHeaderGetTime(hdr
, HDR_LAST_MODIFIED
);
304 rep
->expires
= httpHeaderGetTime(hdr
, HDR_EXPIRES
);
305 str
= httpHeaderGetStr(hdr
, HDR_CONTENT_TYPE
);
307 stringLimitInit(&rep
->content_type
, str
, strcspn(str
, ";\t "));
309 rep
->content_type
= StringNull
;
310 rep
->cache_control
= httpHeaderGetCc(hdr
);
311 rep
->content_range
= httpHeaderGetContRange(hdr
);
312 rep
->keep_alive
= httpMsgIsPersistent(rep
->sline
.version
, &rep
->header
);
313 /* final adjustments */
314 /* The s-maxage and max-age directive takes priority over Expires */
315 if (rep
->cache_control
&& rep
->cache_control
->s_maxage
>= 0)
316 rep
->expires
= squid_curtime
+ rep
->cache_control
->s_maxage
;
317 else if (rep
->cache_control
&& rep
->cache_control
->max_age
>= 0)
318 rep
->expires
= squid_curtime
+ rep
->cache_control
->max_age
;
321 * The HTTP/1.0 specs says that robust implementations should consider bad
322 * or malformed Expires header as equivalent to "expires immediately."
324 if (rep
->expires
< 0 && httpHeaderHas(hdr
, HDR_EXPIRES
))
325 rep
->expires
= squid_curtime
;
328 /* sync this routine when you update HttpReply struct */
330 httpReplyHdrCacheClean(HttpReply
* rep
)
332 stringClean(&rep
->content_type
);
333 if (rep
->cache_control
)
334 httpHdrCcDestroy(rep
->cache_control
);
335 if (rep
->content_range
)
336 httpHdrContRangeDestroy(rep
->content_range
);
340 * parses a 0-terminating buffer into HttpReply.
343 * 0 -- need more data (partial parse)
347 httpReplyParseStep(HttpReply
* rep
, const char *buf
, int atEnd
)
349 const char *parse_start
= buf
;
350 const char *blk_start
, *blk_end
;
351 const char **parse_end_ptr
= &blk_end
;
354 assert(rep
->pstate
< psParsed
);
356 *parse_end_ptr
= parse_start
;
357 if (rep
->pstate
== psReadyToParseStartLine
) {
358 if (!httpReplyIsolateStart(&parse_start
, &blk_start
, &blk_end
))
360 if (!httpStatusLineParse(&rep
->sline
, blk_start
, blk_end
))
361 return httpReplyParseError(rep
);
363 *parse_end_ptr
= parse_start
;
364 rep
->hdr_sz
= *parse_end_ptr
- buf
;
367 if (rep
->pstate
== psReadyToParseHeaders
) {
368 if (!httpMsgIsolateHeaders(&parse_start
, &blk_start
, &blk_end
)) {
370 blk_start
= parse_start
, blk_end
= blk_start
+ strlen(blk_start
);
374 if (!httpHeaderParse(&rep
->header
, blk_start
, blk_end
))
375 return httpReplyParseError(rep
);
377 httpReplyHdrCacheInit(rep
);
379 *parse_end_ptr
= parse_start
;
380 rep
->hdr_sz
= *parse_end_ptr
- buf
;
386 /* handy: resets and returns -1 */
388 httpReplyParseError(HttpReply
* rep
)
393 /* indicate an error */
394 rep
->sline
.status
= HTTP_INVALID_HEADER
;
398 /* find first CRLF */
400 httpReplyIsolateStart(const char **parse_start
, const char **blk_start
, const char **blk_end
)
402 int slen
= strcspn(*parse_start
, "\r\n");
403 if (!(*parse_start
)[slen
]) /* no CRLF found */
406 *blk_start
= *parse_start
;
407 *blk_end
= *blk_start
+ slen
;
408 while (**blk_end
== '\r') /* CR */
410 if (**blk_end
== '\n') /* LF */
413 *parse_start
= *blk_end
;
418 * Returns the body size of a HTTP response
421 httpReplyBodySize(method_t method
, HttpReply
* reply
)
423 if (METHOD_HEAD
== method
)
425 else if (reply
->sline
.status
== HTTP_OK
)
426 (void) 0; /* common case, continue */
427 else if (reply
->sline
.status
== HTTP_NO_CONTENT
)
429 else if (reply
->sline
.status
== HTTP_NOT_MODIFIED
)
431 else if (reply
->sline
.status
< HTTP_OK
)
433 return reply
->content_length
;