aboutsummaryrefslogtreecommitdiff
path: root/lib/http/range_example_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-03-11 20:16:59 +0700
committerShulhan <ms@kilabit.info>2023-03-12 11:37:17 +0700
commit7d4c26cef490689eb5817c17e102d1e7128c682e (patch)
treeb69ea23471932b0a8054bb1a371f8b21a156361c /lib/http/range_example_test.go
parent8e2fa2b18662da4e42bc2be5f20103dd5e2cb8f8 (diff)
downloadpakakeh.go-7d4c26cef490689eb5817c17e102d1e7128c682e.tar.xz
lib/http: add function to parse multipart Range response for Client
The ParseMultipartRange parse the multipart/byteranges body or response from HTTP Range request. Each Content-Range position and body part in the multipart will be stored under RangePosition.
Diffstat (limited to 'lib/http/range_example_test.go')
-rw-r--r--lib/http/range_example_test.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/http/range_example_test.go b/lib/http/range_example_test.go
index 9ed44bd2..0f5fc54e 100644
--- a/lib/http/range_example_test.go
+++ b/lib/http/range_example_test.go
@@ -1,11 +1,59 @@
package http_test
import (
+ "bytes"
"fmt"
+ "log"
+ "strings"
libhttp "github.com/shuLhan/share/lib/http"
)
+func ExampleParseMultipartRange() {
+ var (
+ boundary = `zxcv`
+ )
+
+ var body = `--zxcv
+Content-Range: bytes 0-6/50
+
+Part 1
+--zxcv
+
+Missing Content-Range header, skipped.
+--zxcv
+Content-Range: bytes 7-13
+
+Invalid Content-Range, missing size, skipped.
+--zxcv
+Content-Range: bytes 14-19/50
+
+Part 2
+--zxcv--
+`
+
+ body = strings.ReplaceAll(body, "\n", "\r\n")
+
+ var (
+ reader = bytes.NewReader([]byte(body))
+
+ r *libhttp.Range
+ err error
+ )
+ r, err = libhttp.ParseMultipartRange(reader, boundary)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var pos libhttp.RangePosition
+ for _, pos = range r.Positions() {
+ fmt.Printf("%s: %s\n", pos.String(), pos.Content())
+ }
+ // Output:
+ // 0-6: Part 1
+ // 14-19: Part 2
+}
+
func ExampleParseRange() {
var r libhttp.Range