Skip to content

Commit ba72f4c

Browse files
committed
Merge branch 'poldi1405-master'
2 parents 37e59cf + a8aad05 commit ba72f4c

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

cmd/got/main.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bufio"
55
"context"
6+
"errors"
67
"fmt"
78
"log"
89
"net/url"
@@ -21,6 +22,8 @@ import (
2122

2223
var version string
2324

25+
var HeaderSlice []got.GotHeader
26+
2427
func main() {
2528

2629
// New context.
@@ -67,6 +70,11 @@ func main() {
6770
Usage: "Chunks that will be downloaded concurrently.",
6871
Aliases: []string{"c"},
6972
},
73+
&cli.StringSliceFlag{
74+
Name: "header",
75+
Usage: "Set these HTTP-Headers on the requests. The format has to be Key: Value",
76+
Aliases: []string{"H"},
77+
},
7078
},
7179
Version: version,
7280
Authors: []*cli.Author{
@@ -100,20 +108,24 @@ func run(ctx context.Context, c *cli.Context) error {
100108

101109
// 55 is just an estimation of the text showed with the progress.
102110
// it's working fine with $COLUMNS >= 47
103-
// TODO: hide progress bar on terminal size of $COLUMNS <= 46
104111
p.Width = getWidth() - 55
105112

106113
perc, err := progress.GetPercentage(float64(d.Size()), float64(d.TotalSize()))
107114
if err != nil {
108115
perc = 100
109116
}
110117

118+
var bar string
119+
if getWidth() <= 46 {
120+
bar = ""
121+
} else {
122+
bar = r + color(p.GetBar(perc, 100)) + l
123+
}
124+
111125
fmt.Printf(
112-
" %6.2f%% %s%s%s %s/%s @ %s/s%s\r",
126+
" %6.2f%% %s %s/%s @ %s/s%s\r",
113127
perc,
114-
r,
115-
color(p.GetBar(perc, 100)),
116-
l,
128+
bar,
117129
humanize.Bytes(d.Size()),
118130
humanize.Bytes(d.TotalSize()),
119131
humanize.Bytes(d.Speed()),
@@ -157,6 +169,18 @@ func run(ctx context.Context, c *cli.Context) error {
157169
}
158170
}
159171

172+
if c.StringSlice("header") != nil {
173+
header := c.StringSlice("header")
174+
175+
for _, h := range header {
176+
split := strings.SplitN(h, ":", 2)
177+
if len(split) == 1 {
178+
return errors.New("malformatted header " + h)
179+
}
180+
HeaderSlice = append(HeaderSlice, got.GotHeader{Key: split[0], Value: strings.TrimSpace(split[1])})
181+
}
182+
}
183+
160184
// Download from args.
161185
for _, url := range c.Args().Slice() {
162186

@@ -211,6 +235,7 @@ func download(ctx context.Context, c *cli.Context, g *got.Got, url string) (err
211235
URL: url,
212236
Dir: c.String("dir"),
213237
Dest: c.String("output"),
238+
Header: HeaderSlice,
214239
Interval: 150,
215240
ChunkSize: c.Uint64("size"),
216241
Concurrency: c.Uint("concurrency"),

download.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type (
3838

3939
Interval, ChunkSize, MinChunkSize, MaxChunkSize uint64
4040

41+
Header []GotHeader
42+
4143
StopProgress bool
4244

4345
ctx context.Context
@@ -52,12 +54,17 @@ type (
5254

5355
startedAt time.Time
5456
}
57+
58+
GotHeader struct {
59+
Key string
60+
Value string
61+
}
5562
)
5663

5764
// GetInfo returns URL info, and error if any.
5865
func (d Download) GetInfo() (*Info, error) {
5966

60-
req, err := NewRequest(d.ctx, "HEAD", d.URL)
67+
req, err := NewRequest(d.ctx, "HEAD", d.URL, d.Header)
6168

6269
if err != nil {
6370
return nil, err
@@ -86,7 +93,7 @@ func (d *Download) getInfoFromGetRequest() (*Info, error) {
8693
res *http.Response
8794
)
8895

89-
if req, err = NewRequest(d.ctx, "GET", d.URL); err != nil {
96+
if req, err = NewRequest(d.ctx, "GET", d.URL, d.Header); err != nil {
9097
return nil, err
9198
}
9299

@@ -473,7 +480,7 @@ func (d *Download) DownloadChunk(c Chunk, dest *os.File) error {
473480
res *http.Response
474481
)
475482

476-
if req, err = NewRequest(d.ctx, "GET", d.URL); err != nil {
483+
if req, err = NewRequest(d.ctx, "GET", d.URL, d.Header); err != nil {
477484
return err
478485
}
479486

download_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestGetInfoAndInit(t *testing.T) {
3131
t.Run("getInfoTest", getInfoTest)
3232
t.Run("okInitTest", okInitTest)
3333
t.Run("errInitTest", errInitTest)
34+
t.Run("sendHeadersTest", sendHeadersTest)
3435
}
3536

3637
func TestDownloading(t *testing.T) {
@@ -68,6 +69,35 @@ func getInfoTest(t *testing.T) {
6869
}
6970
}
7071

72+
func sendHeadersTest(t *testing.T) {
73+
74+
tmpFile := createTemp()
75+
defer clean(tmpFile)
76+
77+
dl := got.NewDownload(context.Background(), httpt.URL+"/header_values", tmpFile)
78+
dl.Header = []got.GotHeader{
79+
{
80+
Key: "x-test-header",
81+
Value: "foobar",
82+
},
83+
}
84+
85+
info, err := dl.GetInfo()
86+
87+
if err != nil {
88+
t.Error(err)
89+
return
90+
}
91+
92+
if info.Rangeable == false {
93+
t.Error("rangeable should be true")
94+
}
95+
96+
if info.Size != uint64(okFileStat.Size()) {
97+
t.Errorf("Invalid file size, wants %d but got %d", okFileStat.Size(), info.Size)
98+
}
99+
}
100+
71101
func getFilenameTest(t *testing.T) {
72102

73103
tmpFile := createTemp()

got.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ func NewWithContext(ctx context.Context) *Got {
7676
}
7777

7878
// NewRequest returns a new http.Request and error if any.
79-
func NewRequest(ctx context.Context, method, URL string) (req *http.Request, err error) {
79+
func NewRequest(ctx context.Context, method, URL string, header []GotHeader) (req *http.Request, err error) {
8080

8181
if req, err = http.NewRequestWithContext(ctx, method, URL, nil); err != nil {
8282
return
8383
}
8484

8585
req.Header.Set("User-Agent", DefaulUserAgent)
86+
87+
for _, h := range header {
88+
req.Header.Set(h.Key, h.Value)
89+
}
90+
8691
return
8792
}

got_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ func NewHttptestServer() *httptest.Server {
5353
http.ServeFile(w, r, "go.mod")
5454
return
5555

56+
case "/header_values":
57+
if r.Header.Get("x-test-header") == "foobar" {
58+
http.ServeFile(w, r, "go.mod")
59+
return
60+
}
61+
62+
w.WriteHeader(403)
63+
return
64+
5665
case "/not_found":
5766
}
5867

0 commit comments

Comments
 (0)