]> Cypherpunks.ru repositories - gocheese.git/blobdiff - refresh.go
Simpler CSS inclusion
[gocheese.git] / refresh.go
index 4a5e962925d7b8ff2a2e7ee3ac0a58fb3ce06658..11325e275c3d6c9f2002f97cd2dce6079e25c211 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoCheese -- Python private package repository and caching proxy
-Copyright (C) 2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2019-2021 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -37,6 +37,8 @@ import (
        "golang.org/x/crypto/blake2b"
 )
 
+var pypiHTTPTransport http.Transport
+
 func blake2b256New() hash.Hash {
        h, err := blake2b.New256(nil)
        if err != nil {
@@ -54,12 +56,19 @@ func refreshDir(
        if _, err := os.Stat(filepath.Join(*root, pkgName, InternalFlag)); err == nil {
                return true
        }
-       resp, err := http.Get(*pypiURL + pkgName + "/")
+       c := http.Client{Transport: &pypiHTTPTransport}
+       resp, err := c.Get(*pypiURL + pkgName + "/")
        if err != nil {
                log.Println("error", r.RemoteAddr, "refresh", pkgName, err)
                http.Error(w, err.Error(), http.StatusBadGateway)
                return false
        }
+       if resp.StatusCode != http.StatusOK {
+               resp.Body.Close()
+               log.Println("error", r.RemoteAddr, "refresh", pkgName, "HTTP status:", resp.Status)
+               http.Error(w, "PyPI has non 200 status code", http.StatusBadGateway)
+               return false
+       }
        body, err := ioutil.ReadAll(resp.Body)
        resp.Body.Close()
        if err != nil {
@@ -154,6 +163,15 @@ func refreshDir(
                                return false
                        }
                        defer resp.Body.Close()
+                       if resp.StatusCode != http.StatusOK {
+                               log.Println(
+                                       "error", r.RemoteAddr,
+                                       "pypi", filename, "download",
+                                       "HTTP status:", resp.Status,
+                               )
+                               http.Error(w, "PyPI has non 200 status code", http.StatusBadGateway)
+                               return false
+                       }
                        hasher := hasherNew()
                        hasherSHA256 := sha256.New()
                        dst, err := TempFile(dirPath)