libwebsockets/lib/ranges.c

212 lines
4.4 KiB
C
Raw Permalink Normal View History

RFC7233 HTTP Ranges support for server This adds a serverside implementation of RFC7233 HTTP ranges. - LWS_WITH_RANGES is on by default at cmake - Accept-Ranges: bytes is added if LWS_WITH_RANGES is enabled - Both single ranges and multipart (2+) ranges are supported Test with curl like this Single $ $ curl -s -r 64-95 http://localhost:7681/libwebsockets.org-logo.png | hexdump -C 00000000 2e 01 fd 9d 12 27 00 00 00 19 74 45 58 74 53 6f |.....'....tEXtSo| 00000010 66 74 77 61 72 65 00 77 77 77 2e 69 6e 6b 73 63 |ftware.www.inksc| Multipart $ curl -s -r 64-95,128-143 http://localhost:7681/libwebsockets.org-logo.png | hexdump -C 00000000 5f 6c 77 73 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 |_lws..Content-Ty| 00000010 70 65 3a 20 69 6d 61 67 65 2f 70 6e 67 0d 0a 43 |pe: image/png..C| 00000020 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79 |ontent-Range: by| 00000030 74 65 73 20 36 34 2d 39 35 2f 37 30 32 39 0d 0a |tes 64-95/7029..| 00000040 0d 0a 2e 01 fd 9d 12 27 00 00 00 19 74 45 58 74 |.......'....tEXt| 00000050 53 6f 66 74 77 61 72 65 00 77 77 77 2e 69 6e 6b |Software.www.ink| 00000060 73 63 5f 6c 77 73 0d 0a 43 6f 6e 74 65 6e 74 2d |sc_lws..Content-| 00000070 54 79 70 65 3a 20 69 6d 61 67 65 2f 70 6e 67 0d |Type: image/png.| 00000080 0a 43 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 |.Content-Range: | 00000090 62 79 74 65 73 20 31 32 38 2d 31 34 33 2f 37 30 |bytes 128-143/70| 000000a0 32 39 0d 0a 0d 0a 05 14 50 40 05 15 a5 c4 60 43 |29......P@....`C| 000000b0 91 c4 4a d4 c4 fc 5f 6c 77 73 0d 00 |..J..._lws..| The corresponding header part is like this 0x0030: 4854 5450 2f31 2e31 2032 3036 HTTP/1.1.206 0x0040: 200d 0a73 6572 7665 723a 206c 7773 7773 ...server:.lwsws 0x0050: 0d0a 636f 6e74 656e 742d 7479 7065 3a20 ..content-type:. 0x0060: 6d75 6c74 6970 6172 742f 6279 7465 7261 multipart/bytera 0x0070: 6e67 6573 0d0a 6163 6365 7074 2d72 616e nges..accept-ran 0x0080: 6765 733a 2062 7974 6573 0d0a 636f 6e74 ges:.bytes..cont 0x0090: 656e 742d 6c65 6e67 7468 3a20 3138 380d ent-length:.188. 0x00a0: 0a63 6163 6865 2d63 6f6e 7472 6f6c 3a20 .cache-control:. 0x00b0: 7072 6976 6174 6520 6d61 782d 6167 653a private.max-age: 0x00c0: 2036 300d 0a63 6f6e 6e65 6374 696f 6e3a .60..connection: 0x00d0: 206b 6565 702d 616c 6976 650d 0a65 7461 .keep-alive..eta 0x00e0: 673a 2030 3030 3031 4237 3535 3444 3433 g:.00001B7554D43 0x00f0: 3033 330d 0a0d 0a 033....
2016-12-12 13:36:25 +08:00
/*
* libwebsockets - small server side websockets and web server implementation
*
* RFC7233 ranges parser
*
* Copyright (C) 2016 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "private-libwebsockets.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=";
char c;
while (1) {
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 = (rp->start * 10) + (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 = (rp->end * 10) + (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;
}