Skip to content

Commit bab1fc8

Browse files
committed
tolerate weird server behaviour
An example (only the tip of the iceberg): > Range: bytes=-1000000 < HTTP/1.1 200 OK < Content-Length: 16681 === test two === > Range: bytes=0- < HTTP/1.1 206 Partial Content < Content-Length: 16681
1 parent 897f6cb commit bab1fc8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

logtail.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ function get_log() {
2525

2626
var range;
2727
var first_load;
28+
var must_get_206;
2829
if (log_file_size === 0) {
2930
/* Get the last 'load' bytes */
3031
range = "-" + load.toString();
3132
first_load = true;
33+
must_get_206 = false;
3234
} else {
3335
/* Get the (log_file_size - 1)th byte, onwards. */
3436
range = (log_file_size - 1).toString() + "-";
3537
first_load = false;
38+
must_get_206 = log_file_size > 1;
3639
}
3740

3841
/* The "log_file_size - 1" deliberately reloads the last byte, which we already
@@ -50,25 +53,27 @@ function get_log() {
5053
var content_size;
5154

5255
if (xhr.status === 206) {
53-
if (data.length > load)
54-
throw "Expected 206 Partial Content";
55-
5656
var c_r = xhr.getResponseHeader("Content-Range");
5757
if (!c_r)
5858
throw "Server did not respond with a Content-Range";
5959

6060
log_file_size = parseInt(c_r.split("/")[1]);
6161
content_size = xhr.getResponseHeader("Content-Length");
62-
63-
if (isNaN(log_file_size))
64-
throw "Invalid Content-Range size";
6562
} else if (xhr.status === 200) {
66-
if (!first_load)
63+
if (must_get_206)
6764
throw "Expected 206 Partial Content";
6865

6966
content_size = log_file_size = xhr.getResponseHeader("Content-Length");
67+
} else {
68+
throw "Unexpected status " + xhr.status;
7069
}
7170

71+
if (isNaN(content_size) || isNaN(log_file_size))
72+
throw "Invalid content_size or log_file_size";
73+
74+
if (first_load && data.length > load)
75+
throw "Server's response was too long";
76+
7277
var added = false;
7378

7479
if (first_load) {

0 commit comments

Comments
 (0)