]>
Commit | Line | Data |
---|---|---|
528b2c61 | 1 | /* |
ef57eb7b | 2 | * Copyright (C) 1996-2016 The Squid Software Foundation and contributors |
528b2c61 | 3 | * |
4e0938ef AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
528b2c61 | 7 | */ |
4e0938ef AJ |
8 | |
9 | /* DEBUG: section 64 HTTP Range Header */ | |
10 | ||
f7f3304a | 11 | #include "squid.h" |
ed6e9fb9 | 12 | #include "fatal.h" |
e1f7507e | 13 | #include "HttpHeader.h" |
602d9612 | 14 | #include "HttpHeaderRange.h" |
e1f7507e | 15 | |
22aa015b | 16 | /** \todo CLEANUP: This file should be called something_stub.cc */ |
528b2c61 | 17 | |
789217a2 | 18 | void httpHeaderPutStr(HttpHeader * hdr, Http::HdrType type, const char *str) |
528b2c61 | 19 | { |
20 | fatal ("dummy function\n"); | |
21 | } | |
22 | ||
d9c252f2 | 23 | HttpHeaderEntry *httpHeaderGetEntry(const HttpHeader * hdr, HttpHeaderPos * pos) |
528b2c61 | 24 | { |
25 | fatal ("dummy function\n"); | |
26 | return NULL; | |
27 | } | |
28 | ||
789217a2 | 29 | String httpHeaderGetList(const HttpHeader * hdr, Http::HdrType id) |
528b2c61 | 30 | { |
31 | fatal ("dummy function\n"); | |
30abd221 | 32 | return String(); |
528b2c61 | 33 | } |
34 | ||
789217a2 | 35 | int httpHeaderHas(const HttpHeader * hdr, Http::HdrType type) |
528b2c61 | 36 | { |
37 | fatal ("dummy function\n"); | |
38 | return 0; | |
39 | } | |
40 | ||
d9c252f2 | 41 | void httpHeaderPutContRange(HttpHeader * hdr, const HttpHdrContRange * cr) |
528b2c61 | 42 | { |
43 | fatal ("dummy function\n"); | |
44 | } | |
45 | ||
528b2c61 | 46 | void |
47 | testRangeParser(char const *rangestring) | |
48 | { | |
30abd221 | 49 | String aString (rangestring); |
528b2c61 | 50 | HttpHdrRange *range = HttpHdrRange::ParseCreate (&aString); |
43ae1d95 | 51 | |
528b2c61 | 52 | if (!range) |
43ae1d95 | 53 | exit (1); |
54 | ||
528b2c61 | 55 | HttpHdrRange copy(*range); |
43ae1d95 | 56 | |
4c9eadc2 | 57 | assert (copy.specs.size() == range->specs.size()); |
528b2c61 | 58 | |
59 | HttpHdrRange::iterator pos = range->begin(); | |
43ae1d95 | 60 | |
528b2c61 | 61 | assert (*pos); |
43ae1d95 | 62 | |
00d77d6b | 63 | delete range; |
528b2c61 | 64 | } |
65 | ||
66 | HttpHdrRange * | |
67 | rangeFromString(char const *rangestring) | |
68 | { | |
30abd221 | 69 | String aString (rangestring); |
528b2c61 | 70 | HttpHdrRange *range = HttpHdrRange::ParseCreate (&aString); |
43ae1d95 | 71 | |
528b2c61 | 72 | if (!range) |
43ae1d95 | 73 | exit (1); |
74 | ||
528b2c61 | 75 | return range; |
76 | } | |
77 | ||
78 | void | |
79 | testRangeIter () | |
80 | { | |
81 | HttpHdrRange *range=rangeFromString("bytes=0-3, 1-, -2"); | |
4c9eadc2 | 82 | assert (range->specs.size() == 3); |
528b2c61 | 83 | size_t counter = 0; |
84 | HttpHdrRange::iterator i = range->begin(); | |
43ae1d95 | 85 | |
528b2c61 | 86 | while (i != range->end()) { |
43ae1d95 | 87 | ++counter; |
88 | ++i; | |
528b2c61 | 89 | } |
43ae1d95 | 90 | |
528b2c61 | 91 | assert (counter == 3); |
92 | i = range->begin(); | |
93 | assert (i - range->begin() == 0); | |
94 | ++i; | |
95 | assert (i - range->begin() == 1); | |
96 | assert (i - range->end() == -2); | |
97 | } | |
98 | ||
99 | void | |
100 | testRangeCanonization() | |
101 | { | |
102 | HttpHdrRange *range=rangeFromString("bytes=0-3, 1-, -2"); | |
4c9eadc2 | 103 | assert (range->specs.size() == 3); |
528b2c61 | 104 | |
105 | /* 0-3 needs a content length of 4 */ | |
106 | /* This passes in the extant code - but should it? */ | |
43ae1d95 | 107 | |
528b2c61 | 108 | if (!range->canonize(3)) |
43ae1d95 | 109 | exit(1); |
110 | ||
4c9eadc2 | 111 | assert (range->specs.size() == 3); |
43ae1d95 | 112 | |
00d77d6b | 113 | delete range; |
43ae1d95 | 114 | |
528b2c61 | 115 | range=rangeFromString("bytes=0-3, 1-, -2"); |
43ae1d95 | 116 | |
4c9eadc2 | 117 | assert (range->specs.size() == 3); |
43ae1d95 | 118 | |
528b2c61 | 119 | /* 0-3 needs a content length of 4 */ |
120 | if (!range->canonize(4)) | |
43ae1d95 | 121 | exit(1); |
122 | ||
00d77d6b | 123 | delete range; |
43ae1d95 | 124 | |
528b2c61 | 125 | range=rangeFromString("bytes=3-6"); |
43ae1d95 | 126 | |
4c9eadc2 | 127 | assert (range->specs.size() == 1); |
43ae1d95 | 128 | |
528b2c61 | 129 | /* 3-6 needs a content length of 4 or more */ |
130 | if (range->canonize(3)) | |
43ae1d95 | 131 | exit(1); |
132 | ||
00d77d6b | 133 | delete range; |
528b2c61 | 134 | |
135 | range=rangeFromString("bytes=3-6"); | |
43ae1d95 | 136 | |
4c9eadc2 | 137 | assert (range->specs.size() == 1); |
43ae1d95 | 138 | |
528b2c61 | 139 | /* 3-6 needs a content length of 4 or more */ |
140 | if (!range->canonize(4)) | |
43ae1d95 | 141 | exit(1); |
142 | ||
00d77d6b | 143 | delete range; |
43ae1d95 | 144 | |
528b2c61 | 145 | range=rangeFromString("bytes=1-1,2-3"); |
43ae1d95 | 146 | |
4c9eadc2 | 147 | assert (range->specs.size()== 2); |
43ae1d95 | 148 | |
528b2c61 | 149 | if (!range->canonize(4)) |
43ae1d95 | 150 | exit(1); |
151 | ||
4c9eadc2 | 152 | assert (range->specs.size() == 2); |
43ae1d95 | 153 | |
00d77d6b | 154 | delete range; |
528b2c61 | 155 | } |
156 | ||
157 | int | |
a0b3b22c | 158 | main(int argc, char **argv) |
528b2c61 | 159 | { |
608622b8 | 160 | try { |
a0b3b22c AJ |
161 | Mem::Init(); |
162 | /* enable for debugging to console */ | |
163 | // _db_init (NULL, NULL); | |
164 | // Debug::Levels[64] = 9; | |
165 | testRangeParser("bytes=0-3"); | |
166 | testRangeParser("bytes=-3"); | |
167 | testRangeParser("bytes=1-"); | |
168 | testRangeParser("bytes=0-3, 1-, -2"); | |
169 | testRangeIter(); | |
170 | testRangeCanonization(); | |
171 | } catch (const std::exception &e) { | |
172 | printf("Error: dying from an unhandled exception: %s\n", e.what()); | |
173 | return 1; | |
174 | } catch (...) { | |
175 | printf("Error: dying from an unhandled exception.\n"); | |
176 | return 1; | |
177 | } | |
528b2c61 | 178 | return 0; |
179 | } | |
f53969cc | 180 |