File: ranges.c

package info (click to toggle)
libwebsockets 4.3.5-1%2Bdeb13u1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 30,724 kB
  • sloc: ansic: 194,409; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (214 lines) | stat: -rw-r--r-- 4,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "private-lib-core.h"

/*
 * RFC7233 examples
 *
 * o  The first 500 bytes (byte offsets 0-499, inclusive):
 *
 *      bytes=0-499
 *
 * o  The second 500 bytes (byte offsets 500-999, inclusive):
 *
 *      bytes=500-999
 *
 * o  The final 500 bytes (byte offsets 9500-9999, inclusive):
 *
 *      bytes=-500
 *
 * Or:
 *
 *      bytes=9500-
 *
 * o  The first and last bytes only (bytes 0 and 9999):
 *
 *      bytes=0-0,-1
 *
 * o  Other valid (but not canonical) specifications of the second 500
 *    bytes (byte offsets 500-999, inclusive):
 *
 *      bytes=500-600,601-999
 *      bytes=500-700,601-999
 */

/*
 * returns 1 if the range struct represents a usable range
 *   if no ranges header, you get one of these for the whole
 *   file.  Otherwise you get one for each valid range in the
 *   header.
 *
 * returns 0 if no further valid range forthcoming; rp->state
 *   may be LWSRS_SYNTAX or LWSRS_COMPLETED
 */

int
lws_ranges_next(struct lws_range_parsing *rp)
{
	static const char * const beq = "bytes=";

	while (1) {

		char c = rp->buf[rp->pos];

		switch (rp->state) {
		case LWSRS_SYNTAX:
		case LWSRS_COMPLETED:
			return 0;

		case LWSRS_NO_ACTIVE_RANGE:
			rp->state = LWSRS_COMPLETED;
			return 0;

		case LWSRS_BYTES_EQ: // looking for "bytes="
			if (c != beq[rp->pos]) {
				rp->state = LWSRS_SYNTAX;
				return -1;
			}
			if (rp->pos == 5)
				rp->state = LWSRS_FIRST;
			break;

		case LWSRS_FIRST:
			rp->start = 0;
			rp->end = 0;
			rp->start_valid = 0;
			rp->end_valid = 0;

			rp->state = LWSRS_STARTING;

			// fallthru

		case LWSRS_STARTING:
			if (c == '-') {
				rp->state = LWSRS_ENDING;
				break;
			}

			if (!(c >= '0' && c <= '9')) {
				rp->state = LWSRS_SYNTAX;
				return 0;
			}
			rp->start = (unsigned long long)(((unsigned long long)rp->start * 10) + (unsigned long long)(c - '0'));
			rp->start_valid = 1;
			break;

		case LWSRS_ENDING:
			if (c == ',' || c == '\0') {
				rp->state = LWSRS_FIRST;
				if (c == ',')
					rp->pos++;

				/*
				 * By the end of this, start and end are
				 * always valid if the range still is
				 */

				if (!rp->start_valid) { /* eg, -500 */
					if (rp->end > rp->extent)
						rp->end = rp->extent;

					rp->start = rp->extent - rp->end;
					rp->end = rp->extent - 1;
				} else
					if (!rp->end_valid)
						rp->end = rp->extent - 1;

				rp->did_try = 1;

				/* end must be >= start or ignore it */
				if (rp->end < rp->start) {
					if (c == ',')
						break;
					rp->state = LWSRS_COMPLETED;
					return 0;
				}

				return 1; /* issue range */
			}

			if (!(c >= '0' && c <= '9')) {
				rp->state = LWSRS_SYNTAX;
				return 0;
			}
			rp->end = (unsigned long long)(((unsigned long long)rp->end * 10) + (unsigned long long)(c - '0'));
			rp->end_valid = 1;
			break;
		}

		rp->pos++;
	}
}

void
lws_ranges_reset(struct lws_range_parsing *rp)
{
	rp->pos = 0;
	rp->ctr = 0;
	rp->start = 0;
	rp->end = 0;
	rp->start_valid = 0;
	rp->end_valid = 0;
	rp->state = LWSRS_BYTES_EQ;
}

/*
 * returns count of valid ranges
 */
int
lws_ranges_init(struct lws *wsi, struct lws_range_parsing *rp,
		unsigned long long extent)
{
	rp->agg = 0;
	rp->send_ctr = 0;
	rp->inside = 0;
	rp->count_ranges = 0;
	rp->did_try = 0;
	lws_ranges_reset(rp);
	rp->state = LWSRS_COMPLETED;

	rp->extent = extent;

	if (lws_hdr_copy(wsi, (char *)rp->buf, sizeof(rp->buf),
			 WSI_TOKEN_HTTP_RANGE) <= 0)
		return 0;

	rp->state = LWSRS_BYTES_EQ;

	while (lws_ranges_next(rp)) {
		rp->count_ranges++;
		rp->agg += rp->end - rp->start + 1;
	}

	lwsl_debug("%s: count %d\n", __func__, rp->count_ranges);
	lws_ranges_reset(rp);

	if (rp->did_try && !rp->count_ranges)
		return -1; /* "not satisfiable */

	lws_ranges_next(rp);

	return rp->count_ranges;
}