Skip to content

Commit

Permalink
Add logic to continue on bad HEAD
Browse files Browse the repository at this point in the history
Some services won't support `HEAD` requests so if `HEAD` request fails, a notice message is logged but the program continues to try and download.

closes #5
  • Loading branch information
deankarn committed May 29, 2017
1 parent b27d0e3 commit 0fd1169
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 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 Down
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.", 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 0fd1169

Please sign in to comment.