Browse Source

Simplify HTTP not modified replies

pull/8/head
Daniel Martí 11 years ago
parent
commit
3120edd0a7
  1. 11
      cmd/fdroidcl/http.go
  2. 8
      cmd/fdroidcl/main.go

11
cmd/fdroidcl/http.go

@ -4,16 +4,13 @@
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
var errNotModified = errors.New("etag matches, file was not modified")
func respEtag(resp *http.Response) string {
etags, e := resp.Header["Etag"]
if !e || len(etags) == 0 {
@ -23,7 +20,7 @@ func respEtag(resp *http.Response) string {
}
func downloadEtag(url, path string) error {
log.Printf("Downloading %s", url)
fmt.Printf("Downloading %s...", url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
@ -39,7 +36,8 @@ func downloadEtag(url, path string) error {
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotModified {
return errNotModified
fmt.Println(" not modified")
return nil
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
@ -53,5 +51,6 @@ func downloadEtag(url, path string) error {
if err2 != nil {
return err2
}
fmt.Println(" done")
return nil
}

8
cmd/fdroidcl/main.go

@ -179,9 +179,8 @@ func indexPath(name string) string {
}
func updateIndex() error {
p := indexPath(repoName)
url := fmt.Sprintf("%s/%s", repoURL, "index.jar")
if err := downloadEtag(url, p); err != nil {
if err := downloadEtag(url, indexPath(repoName)); err != nil {
return err
}
return nil
@ -252,10 +251,7 @@ func main() {
switch cmd {
case "update":
err := updateIndex()
if err == errNotModified {
log.Print("Index up to date")
} else if err != nil {
if err := updateIndex(); err != nil {
log.Fatalf("Could not update index: %v", err)
}
case "list":

Loading…
Cancel
Save