Browse Source

Move http code into http.go

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

46
cmd/fdroidcl/http.go

@ -0,0 +1,46 @@
/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */
package main
import (
"errors"
"io/ioutil"
"net/http"
"os"
)
var errNotModified = errors.New("etag matches, file was not modified")
func downloadEtag(url, path string) error {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
etagPath := path + "-etag"
if _, err := os.Stat(path); err == nil {
etag, _ := ioutil.ReadFile(etagPath)
req.Header.Add("If-None-Match", string(etag))
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotModified {
return errNotModified
}
jar, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(path, jar, 0644)
err2 := ioutil.WriteFile(etagPath, []byte(resp.Header["Etag"][0]), 0644)
if err != nil {
return err
}
if err2 != nil {
return err2
}
return nil
}

38
cmd/fdroidcl/main.go

@ -4,12 +4,9 @@
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
@ -160,41 +157,6 @@ func init() {
}
}
var errNotModified = errors.New("etag matches, file was not modified")
func downloadEtag(url, path string) error {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
etagPath := path + "-etag"
if _, err := os.Stat(path); err == nil {
etag, _ := ioutil.ReadFile(etagPath)
req.Header.Add("If-None-Match", string(etag))
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotModified {
return errNotModified
}
jar, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(path, jar, 0644)
err2 := ioutil.WriteFile(etagPath, []byte(resp.Header["Etag"][0]), 0644)
if err != nil {
return err
}
if err2 != nil {
return err2
}
return nil
}
func indexPath(repoName string) string {
return repoName + ".jar"
}

Loading…
Cancel
Save