Skip to content

Commit

Permalink
Merge pull request #6 from joeybloggs/v2
Browse files Browse the repository at this point in the history
Add logic to continue on bad HEAD
  • Loading branch information
deankarn committed May 29, 2017
2 parents 7a84e61 + 697a4ee commit 26df310
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
go:
- 1.7.5
- 1.8.1
- 1.8.3
- tip

notifications:
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package go-download
===================
![Project status](https://img.shields.io/badge/version-2.0.0-green.svg)
![Project status](https://img.shields.io/badge/version-2.1.0-green.svg)
[![Build Status](https://travis-ci.org/joeybloggs/go-download.svg?branch=master)](https://travis-ci.org/joeybloggs/go-download)
[![Coverage Status](https://coveralls.io/repos/github/joeybloggs/go-download/badge.svg?branch=master)](https://coveralls.io/github/joeybloggs/go-download?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/joeybloggs/go-download)](https://goreportcard.com/report/github.com/joeybloggs/go-download)
Expand All @@ -17,6 +17,10 @@ It Features:
```shell
go get -u github.com/joeybloggs/go-download
```
or if your looking for the standalone client
```shell
go get -u github.com/joeybloggs/go-download/cmd/goget
```

## Examples

Expand Down
25 changes: 23 additions & 2 deletions cmd/goget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"io"
"log"

"os"

download "github.com/joeybloggs/go-download"
"github.com/vbauerster/mpb"
)

func main() {

url := os.Args[len(os.Args)-1]

progress := mpb.New().SetWidth(80)
defer progress.Stop()

Expand All @@ -25,11 +29,28 @@ func main() {
},
}

f, err := download.Open("https://storage.googleapis.com/golang/go1.8.1.src.tar.gz", options)
f, err := download.Open(url, options)
if err != nil {
log.Fatal(err)
}
defer f.Close()

// f implements io.Reader, write file somewhere or do some other sort of work with it
info, err := f.Stat()
if err != nil {
log.Fatal(err)
}

var output *os.File
name := info.Name()
output, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Fatal(err)
}
defer output.Close()

if _, err = io.Copy(output, f); err != nil {
log.Fatal(err)
}

log.Printf("Success. %s saved.", name)
}
21 changes: 13 additions & 8 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -112,15 +113,19 @@ func OpenContext(ctx context.Context, url string, options *Options) (*File, erro
}

if resp.StatusCode != http.StatusOK {
return nil, &InvalidResponseCode{got: resp.StatusCode, expected: http.StatusOK}
}

f.size = resp.ContentLength

if t := resp.Header.Get("Accept-Ranges"); t == "bytes" {
err = f.downloadRangeBytes(ctx)
} else {
// not all services support HEAD requests
// so if this fails just move along to the
// GET portion, with a warning
log.Printf("notice: unexpected HEAD response code '%d', proceeding with download.\n", resp.StatusCode)
err = f.download(ctx)
} else {
f.size = resp.ContentLength

if t := resp.Header.Get("Accept-Ranges"); t == "bytes" {
err = f.downloadRangeBytes(ctx)
} else {
err = f.download(ctx)
}
}

if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func TestBadOptions(t *testing.T) {

w.WriteHeader(http.StatusNotFound)
})
mux.HandleFunc("/testdata/bad-head-good-get", func(w http.ResponseWriter, r *http.Request) {

if r.Method == http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

f, _ := os.Open(data)
defer f.Close()

io.Copy(w, f)
})
mux.HandleFunc("/testdata/bad-content-length", func(w http.ResponseWriter, r *http.Request) {

fi, _ := os.Stat(data)
Expand Down Expand Up @@ -182,6 +194,13 @@ func TestBadOptions(t *testing.T) {
if err != nil {
t.Fatal(err)
}
f.Close()

url = server.URL + "/testdata/bad-head-good-get"
f, err = Open(url, nil)
if err != nil {
t.Fatal(err)
}
defer f.Close()
}

Expand Down

0 comments on commit 26df310

Please sign in to comment.