]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrRange.cc
3 * $Id: HttpHdrRange.cc,v 1.45 2007/08/13 17:20:51 hno Exp $
5 * DEBUG: section 64 HTTP Range Header
6 * AUTHOR: Alex Rousskov
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see 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.
38 #include "HttpHeaderRange.h"
39 #include "client_side_request.h"
40 #include "HttpReply.h"
43 * Currently only byte ranges are supported
45 * Essentially, there are three types of byte ranges:
47 * 1) first-byte-pos "-" last-byte-pos // range
48 * 2) first-byte-pos "-" // trailer
49 * 3) "-" suffix-length // suffix (last length bytes)
52 * When Range field is parsed, we have no clue about the content
53 * length of the document. Thus, we simply code an "absent" part
54 * using HttpHdrRangeSpec::UnknownPosition constant.
56 * Note: when response length becomes known, we convert any range
57 * spec into type one above. (Canonization process).
62 #define known_spec(s) ((s) > HttpHdrRangeSpec::UnknownPosition)
65 size_t HttpHdrRange::ParsedCount
= 0;
66 int64_t const HttpHdrRangeSpec::UnknownPosition
= -1;
72 HttpHdrRangeSpec::HttpHdrRangeSpec() : offset(UnknownPosition
), length(UnknownPosition
){}
74 /* parses range-spec and returns new object on success */
76 HttpHdrRangeSpec::Create(const char *field
, int flen
)
78 HttpHdrRangeSpec spec
;
80 if (!spec
.parseInit(field
, flen
))
83 return new HttpHdrRangeSpec(spec
);
87 HttpHdrRangeSpec::parseInit(const char *field
, int flen
)
94 /* is it a suffix-byte-range-spec ? */
96 if (!httpHeaderParseOffset(field
+ 1, &length
))
99 /* must have a '-' somewhere in _this_ field */
100 if (!((p
= strchr(field
, '-')) || (p
- field
>= flen
))) {
101 debugs(64, 2, "ignoring invalid (missing '-') range-spec near: '" << field
<< "'");
104 if (!httpHeaderParseOffset(field
, &offset
))
109 /* do we have last-pos ? */
110 if (p
- field
< flen
) {
113 if (!httpHeaderParseOffset(p
, &last_pos
))
116 HttpHdrRangeSpec::HttpRange
aSpec (offset
, last_pos
+ 1);
118 length
= aSpec
.size();
122 /* we managed to parse, check if the result makes sence */
124 debugs(64, 2, "ignoring invalid (zero length) range-spec near: '" << field
<< "'");
132 HttpHdrRangeSpec::packInto(Packer
* packer
) const
134 if (!known_spec(offset
)) /* suffix */
135 packerPrintf(packer
, "-%"PRId64
, length
);
136 else if (!known_spec(length
)) /* trailer */
137 packerPrintf(packer
, "%"PRId64
"-", offset
);
139 packerPrintf(packer
, "%"PRId64
"-%"PRId64
,
140 offset
, offset
+ length
- 1);
144 HttpHdrRangeSpec::outputInfo( char const *note
) const
146 debugs(64, 5, "HttpHdrRangeSpec::canonize: " << note
<< ": [" <<
147 offset
<< ", " << offset
+ length
<<
148 ") len: " << length
);
151 /* fills "absent" positions in range specification based on response body size
152 * returns true if the range is still valid
153 * range is valid if its intersection with [0,length-1] is not empty
156 HttpHdrRangeSpec::canonize(int64_t clen
)
159 HttpRange
object(0, clen
);
161 if (!known_spec(offset
)) /* suffix */
163 assert(known_spec(length
));
164 offset
= object
.intersection(HttpRange (clen
- length
, clen
)).start
;
165 } else if (!known_spec(length
)) /* trailer */
167 assert(known_spec(offset
));
168 HttpRange newRange
= object
.intersection(HttpRange (offset
, clen
));
169 length
= newRange
.size();
171 /* we have a "range" now, adjust length if needed */
172 assert(known_spec(length
));
174 assert(known_spec(offset
));
176 HttpRange newRange
= object
.intersection (HttpRange (offset
, offset
+ length
));
178 length
= newRange
.size();
185 /* merges recepient with donor if possible; returns true on success
186 * both specs must be canonized prior to merger, of course */
188 HttpHdrRangeSpec::mergeWith(const HttpHdrRangeSpec
* donor
)
191 #if MERGING_BREAKS_NOTHING
192 /* Note: this code works, but some clients may not like its effects */
193 int64_t rhs
= offset
+ length
; /* no -1 ! */
194 const int64_t donor_rhs
= donor
->offset
+ donor
->length
; /* no -1 ! */
195 assert(known_spec(offset
));
196 assert(known_spec(donor
->offset
));
198 assert(donor
->length
> 0);
199 /* do we have a left hand side overlap? */
201 if (donor
->offset
< offset
&& offset
<= donor_rhs
) {
202 offset
= donor
->offset
; /* decrease left offset */
206 /* do we have a right hand side overlap? */
207 if (donor
->offset
<= rhs
&& rhs
< donor_rhs
) {
208 rhs
= donor_rhs
; /* increase right offset */
212 /* adjust length if offsets have been changed */
214 assert(rhs
> offset
);
215 length
= rhs
- offset
;
217 /* does recepient contain donor? */
219 offset
<= donor
->offset
&& donor
->offset
< rhs
;
230 HttpHdrRange::HttpHdrRange () : clen (HttpHdrRangeSpec::UnknownPosition
)
234 HttpHdrRange::ParseCreate(const String
* range_spec
)
236 HttpHdrRange
*r
= new HttpHdrRange
;
238 if (!r
->parseInit(range_spec
)) {
246 /* returns true if ranges are valid; inits HttpHdrRange */
248 HttpHdrRange::parseInit(const String
* range_spec
)
251 const char *pos
= NULL
;
254 assert(this && range_spec
);
256 debugs(64, 8, "parsing range field: '" << range_spec
->buf() << "'");
257 /* check range type */
259 if (range_spec
->caseCmp("bytes=", 6))
262 /* skip "bytes="; hack! */
263 pos
= range_spec
->buf() + 5;
265 /* iterate through comma separated list */
266 while (strListGetItem(range_spec
, ',', &item
, &ilen
, &pos
)) {
267 HttpHdrRangeSpec
*spec
= HttpHdrRangeSpec::Create(item
, ilen
);
269 * HTTP/1.1 draft says we must ignore the whole header field if one spec
270 * is invalid. However, RFC 2068 just says that we must ignore that spec.
274 specs
.push_back(spec
);
279 debugs(64, 8, "parsed range range count: " << count
<< ", kept " <<
281 return specs
.count
!= 0;
284 HttpHdrRange::~HttpHdrRange()
287 delete specs
.pop_back();
290 HttpHdrRange::HttpHdrRange(HttpHdrRange
const &old
) : specs()
292 specs
.reserve(old
.specs
.size());
294 for (const_iterator i
= old
.begin(); i
!= old
.end(); ++i
)
295 specs
.push_back(new HttpHdrRangeSpec ( **i
));
297 assert(old
.specs
.size() == specs
.size());
300 HttpHdrRange::iterator
301 HttpHdrRange::begin()
303 return specs
.begin();
306 HttpHdrRange::iterator
312 HttpHdrRange::const_iterator
313 HttpHdrRange::begin() const
315 return specs
.begin();
318 HttpHdrRange::const_iterator
319 HttpHdrRange::end() const
325 HttpHdrRange::packInto(Packer
* packer
) const
327 const_iterator pos
= begin();
330 while (pos
!= end()) {
332 packerAppend(packer
, ",", 1);
334 (*pos
)->packInto(packer
);
341 HttpHdrRange::merge (Vector
<HttpHdrRangeSpec
*> &basis
)
343 /* reset old array */
346 * take one spec from "goods" and merge it with specs from
347 * "specs" (if any) until there is no overlap */
348 iterator i
= basis
.begin();
350 while (i
!= basis
.end()) {
351 if (specs
.size() && (*i
)->mergeWith(specs
.back())) {
352 /* merged with current so get rid of the prev one */
353 delete specs
.pop_back();
354 continue; /* re-iterate */
357 specs
.push_back (*i
);
361 debugs(64, 3, "HttpHdrRange::merge: had " << basis
.size() <<
362 " specs, merged " << basis
.size() - specs
.size() << " specs");
367 HttpHdrRange::getCanonizedSpecs (Vector
<HttpHdrRangeSpec
*> ©
)
369 /* canonize each entry and destroy bad ones if any */
371 for (iterator
pos (begin()); pos
!= end(); ++pos
) {
372 if ((*pos
)->canonize(clen
))
373 copy
.push_back (*pos
);
378 debugs(64, 3, "HttpHdrRange::getCanonizedSpecs: found " <<
379 specs
.size() - copy
.size() << " bad specs");
382 #include "HttpHdrContRange.h"
385 * canonizes all range specs within a set preserving the order
386 * returns true if the set is valid after canonization;
387 * the set is valid if
388 * - all range specs are valid and
389 * - there is at least one range spec
392 HttpHdrRange::canonize(HttpReply
*rep
)
396 if (rep
->content_range
)
397 clen
= rep
->content_range
->elength
;
399 clen
= rep
->content_length
;
401 return canonize (clen
);
405 HttpHdrRange::canonize (int64_t newClen
)
408 debugs(64, 3, "HttpHdrRange::canonize: started with " << specs
.count
<<
409 " specs, clen: " << clen
);
410 Vector
<HttpHdrRangeSpec
*> goods
;
411 getCanonizedSpecs(goods
);
413 debugs(64, 3, "HttpHdrRange::canonize: finished with " << specs
.count
<<
415 return specs
.count
> 0;
418 /* hack: returns true if range specs are too "complex" for Squid to handle */
419 /* requires that specs are "canonized" first! */
421 HttpHdrRange::isComplex() const
425 /* check that all rangers are in "strong" order */
426 const_iterator
pos (begin());
428 while (pos
!= end()) {
429 /* Ensure typecasts is safe */
430 assert ((*pos
)->offset
>= 0);
432 if ((*pos
)->offset
< offset
)
435 offset
= (*pos
)->offset
+ (*pos
)->length
;
444 * hack: returns true if range specs may be too "complex" when "canonized".
445 * see also: HttpHdrRange::isComplex.
448 HttpHdrRange::willBeComplex() const
451 /* check that all rangers are in "strong" order, */
452 /* as far as we can tell without the content length */
455 for (const_iterator
pos (begin()); pos
!= end(); ++pos
) {
456 if (!known_spec((*pos
)->offset
)) /* ignore unknowns */
459 /* Ensure typecasts is safe */
460 assert ((*pos
)->offset
>= 0);
462 if ((*pos
)->offset
< offset
)
465 offset
= (*pos
)->offset
;
467 if (known_spec((*pos
)->length
)) /* avoid unknowns */
468 offset
+= (*pos
)->length
;
475 * Returns lowest known offset in range spec(s),
476 * or HttpHdrRangeSpec::UnknownPosition
477 * this is used for size limiting
480 HttpHdrRange::firstOffset() const
482 int64_t offset
= HttpHdrRangeSpec::UnknownPosition
;
484 const_iterator pos
= begin();
486 while (pos
!= end()) {
487 if ((*pos
)->offset
< offset
|| !known_spec(offset
))
488 offset
= (*pos
)->offset
;
497 * Returns lowest offset in range spec(s), 0 if unknown.
498 * This is used for finding out where we need to start if all
499 * ranges are combined into one, for example FTP REST.
500 * Use 0 for size if unknown
503 HttpHdrRange::lowestOffset(int64_t size
) const
505 int64_t offset
= HttpHdrRangeSpec::UnknownPosition
;
506 const_iterator pos
= begin();
509 while (pos
!= end()) {
510 int64_t current
= (*pos
)->offset
;
512 if (!known_spec(current
)) {
513 if ((*pos
)->length
> size
|| !known_spec((*pos
)->length
))
514 return 0; /* Unknown. Assume start of file */
516 current
= size
- (*pos
)->length
;
519 if (current
< offset
|| !known_spec(offset
))
525 return known_spec(offset
) ? offset
: 0;
529 * Return true if the first range offset is larger than the configured
531 * Note that exceeding the limit (returning true) results in only
532 * grabbing the needed range elements from the origin.
535 HttpHdrRange::offsetLimitExceeded() const
538 /* not a range request */
541 if (-1 == Config
.rangeOffsetLimit
)
545 if (firstOffset() == -1)
549 if (Config
.rangeOffsetLimit
>= firstOffset())
550 /* below the limit */
557 HttpHdrRange::contains(HttpHdrRangeSpec
& r
) const
559 assert(r
.length
>= 0);
560 HttpHdrRangeSpec::HttpRange
rrange(r
.offset
, r
.offset
+ r
.length
);
562 for (const_iterator i
= begin(); i
!= end(); ++i
) {
563 HttpHdrRangeSpec::HttpRange
irange((*i
)->offset
, (*i
)->offset
+ (*i
)->length
);
564 HttpHdrRangeSpec::HttpRange intersection
= rrange
.intersection(irange
);
566 if (intersection
.start
== irange
.start
&& intersection
.size() == irange
.size())
573 const HttpHdrRangeSpec
*
574 HttpHdrRangeIter::currentSpec() const
576 if (pos
.incrementable())
583 HttpHdrRangeIter::updateSpec()
585 assert (debt_size
== 0);
588 if (pos
.incrementable()) {
589 debt(currentSpec()->length
);
594 HttpHdrRangeIter::debt() const
596 debugs(64, 3, "HttpHdrRangeIter::debt: debt is " << debt_size
);
600 void HttpHdrRangeIter::debt(int64_t newDebt
)
602 debugs(64, 3, "HttpHdrRangeIter::debt: was " << debt_size
<< " now " << newDebt
);