braid-http 1.3.77 → 1.3.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/braid-http-client.js +44 -3
- package/braid-http-server.js +4 -4
- package/package.json +1 -1
package/braid-http-client.js
CHANGED
|
@@ -612,7 +612,7 @@ var subscription_parser = (cb) => ({
|
|
|
612
612
|
function parse_update (state) {
|
|
613
613
|
// If we don't have headers yet, let's try to parse some
|
|
614
614
|
if (!state.headers) {
|
|
615
|
-
var parsed = parse_headers(state.input)
|
|
615
|
+
var parsed = parse_headers(state.input, true)
|
|
616
616
|
|
|
617
617
|
// If header-parsing fails, send the error upstream
|
|
618
618
|
if (parsed.result === 'error')
|
|
@@ -636,13 +636,53 @@ function parse_update (state) {
|
|
|
636
636
|
}
|
|
637
637
|
|
|
638
638
|
// Parsing helpers
|
|
639
|
-
function parse_headers (input) {
|
|
639
|
+
function parse_headers (input, check_for_encoding_blocks) {
|
|
640
640
|
|
|
641
641
|
// Find the start of the headers
|
|
642
642
|
var start = 0
|
|
643
643
|
while (input[start] === 13 || input[start] === 10) start++
|
|
644
644
|
if (start === input.length) return {result: 'waiting'}
|
|
645
645
|
|
|
646
|
+
// Check for an "Encoding" block like this:
|
|
647
|
+
//
|
|
648
|
+
// Encoding: dt
|
|
649
|
+
// Length: 411813
|
|
650
|
+
// <binary dt file>
|
|
651
|
+
//
|
|
652
|
+
// Such a block will start with an "e", not an "h"
|
|
653
|
+
if (check_for_encoding_blocks &&
|
|
654
|
+
(input[start] === 101 || input[start] === 69)) {
|
|
655
|
+
|
|
656
|
+
// Look for two newlines
|
|
657
|
+
var end = start
|
|
658
|
+
var count = 0
|
|
659
|
+
while (++end) {
|
|
660
|
+
if (end > input.length) return {result: 'waiting'}
|
|
661
|
+
if (input[end - 1] === 10) count++
|
|
662
|
+
if (count === 2) break
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// Extract the header string
|
|
666
|
+
var headers_source = input.slice(start, end).map(x => String.fromCharCode(x)).join('')
|
|
667
|
+
|
|
668
|
+
// Parse
|
|
669
|
+
var m = headers_source.match(/Encoding:\s*(\w+)\r?\nLength:\s*(\d+)\r?\n/i)
|
|
670
|
+
if (!m) return {
|
|
671
|
+
result: 'error',
|
|
672
|
+
message: `Parse error in encoding block: ${JSON.stringify(headers_source)}`
|
|
673
|
+
}
|
|
674
|
+
var headers = {
|
|
675
|
+
encoding: m[1],
|
|
676
|
+
length: m[2]
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// Update the input
|
|
680
|
+
input = input.slice(end)
|
|
681
|
+
|
|
682
|
+
// And return the parsed result
|
|
683
|
+
return { result: 'success', headers, input }
|
|
684
|
+
}
|
|
685
|
+
|
|
646
686
|
// Look for the double-newline at the end of the headers.
|
|
647
687
|
var end = start
|
|
648
688
|
while (++end) {
|
|
@@ -714,7 +754,8 @@ function parse_body (state) {
|
|
|
714
754
|
|
|
715
755
|
// Parse Body Snapshot
|
|
716
756
|
|
|
717
|
-
var content_length = parseInt(state.headers['content-length']
|
|
757
|
+
var content_length = parseInt(state.headers['content-length'] ??
|
|
758
|
+
(state.headers.patches === undefined && state.headers['length']))
|
|
718
759
|
if (!isNaN(content_length)) {
|
|
719
760
|
|
|
720
761
|
// We've read a Content-Length, so we have a block to parse
|
package/braid-http-server.js
CHANGED
|
@@ -597,7 +597,7 @@ function braidify (req, res, next) {
|
|
|
597
597
|
}
|
|
598
598
|
|
|
599
599
|
async function send_update(res, data, url, peer) {
|
|
600
|
-
var {version, parents, patches, patch, body, status} = data
|
|
600
|
+
var {version, parents, patches, patch, body, status, encoding} = data
|
|
601
601
|
|
|
602
602
|
if (status) {
|
|
603
603
|
assert(typeof status === 'number', 'sendUpdate: status must be a number')
|
|
@@ -613,7 +613,7 @@ async function send_update(res, data, url, peer) {
|
|
|
613
613
|
res.setHeader(key, val)
|
|
614
614
|
}
|
|
615
615
|
function write_body (body) {
|
|
616
|
-
if (res.isSubscription) res.write('\r\n')
|
|
616
|
+
if (res.isSubscription && !encoding) res.write('\r\n')
|
|
617
617
|
write_binary(res, body)
|
|
618
618
|
}
|
|
619
619
|
|
|
@@ -662,7 +662,7 @@ async function send_update(res, data, url, peer) {
|
|
|
662
662
|
status === 200 ? 'OK'
|
|
663
663
|
: 404 ? 'Not Found'
|
|
664
664
|
: 'Unknown'
|
|
665
|
-
if (res.isSubscription) res.write(`HTTP ${status} ${reason}\r\n`)
|
|
665
|
+
if (res.isSubscription && !encoding) res.write(`HTTP ${status} ${reason}\r\n`)
|
|
666
666
|
|
|
667
667
|
// Write the headers or virtual headers
|
|
668
668
|
for (var [header, value] of Object.entries(data)) {
|
|
@@ -698,7 +698,7 @@ async function send_update(res, data, url, peer) {
|
|
|
698
698
|
let binary = typeof body === 'string' ? new TextEncoder().encode(body) : body,
|
|
699
699
|
length = get_binary_length(binary)
|
|
700
700
|
assert(length !== undefined && length !== 'undefined')
|
|
701
|
-
set_header('Content-Length', length)
|
|
701
|
+
set_header(encoding ? 'Length' : 'Content-Length', length)
|
|
702
702
|
write_body(binary)
|
|
703
703
|
} else
|
|
704
704
|
write_patches(res, patches)
|