Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amppkg_dl_sxg: add -cert_url_path to override cert-url #329

Merged
merged 1 commit into from
Jul 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion cmd/amppkg_dl_sxg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"

Expand All @@ -17,6 +19,7 @@ import (

var flagOutSXG = flag.String("out_sxg", "test.sxg", "Path to where the signed-exchange should be saved.")
var flagOutCert = flag.String("out_cert", "test.cert", "Path to where the cert-chain+cbor should be saved.")
var flagCertUrlBase = flag.String("cert_url_base", "", "Override scheme, hostname and parent path in cert-url.")

func getSXG(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
Expand Down Expand Up @@ -66,6 +69,12 @@ func getCert(url string) ([]byte, error) {
if err != nil {
return nil, errors.WithStack(err)
}
if resp.StatusCode != 200 {
return nil, errors.Errorf("cert-url response error: %s", resp.Status)
}
if contentType := resp.Header.Get("Content-Type"); contentType != "application/cert-chain+cbor" {
return nil, errors.Errorf("invalid content-type of cert-url: %s", contentType)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand All @@ -90,11 +99,24 @@ func main() {
if err != nil {
log.Fatalf("%+v", err)
}
cURL, err := url.Parse(certURL)
if err != nil {
log.Fatalf("%+v", err)
}
if *flagCertUrlBase != "" {
fURL, err := url.Parse(*flagCertUrlBase)
if err != nil {
log.Fatalf("%+v", err)
}
certHash := path.Base(cURL.Path)
cURL = fURL
cURL.Path = path.Join(cURL.Path, certHash)
}
err = ioutil.WriteFile(*flagOutSXG, sxg, 0644)
if err != nil {
log.Fatalf("%+v", err)
}
cert, err := getCert(certURL)
cert, err := getCert(cURL.String())
if err != nil {
log.Fatalf("%+v", err)
}
Expand Down