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

Multiple changes to the validity package. #41

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/webpackager/flag_packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func getPhysicalURLRuleFromFlags() (urlrewrite.Rule, error) {
return rule, nil
}

func getValidityURLRuleFromFlags() (validity.ValidityURLRule, error) {
func getValidityURLRuleFromFlags() (validity.URLRule, error) {
return validity.AppendExtDotLastModified(*flagValidityExt), nil
}

Expand Down
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ type Config struct {
// ValidityURLRule specifies the rule to determine the validity URL,
// where the validity data should be served.
//
// nil implies validity.AppendExtDotUnixTime(".validity", time.Now()).
ValidityURLRule validity.ValidityURLRule
// nil implies validity.DefaultURLRule, which appends ".validity" plus
// the last modified time (in UNIX time) to the document URL.
ValidityURLRule validity.URLRule

// Processor specifies the processor(s) applied to each HTTP response
// before turning it into a signed exchange. The processors make sure
Expand Down Expand Up @@ -109,7 +110,7 @@ func (cfg *Config) populateDefaults() {
cfg.PhysicalURLRule = urlrewrite.DefaultRules
}
if cfg.ValidityURLRule == nil {
cfg.ValidityURLRule = validity.DefaultValidityURLRule
cfg.ValidityURLRule = validity.DefaultURLRule
}
if cfg.Processor == nil {
cfg.Processor = complexproc.DefaultProcessor
Expand Down
47 changes: 0 additions & 47 deletions internal/httpheader/date.go

This file was deleted.

63 changes: 0 additions & 63 deletions internal/httpheader/date_test.go

This file was deleted.

24 changes: 4 additions & 20 deletions validity/url.go → validity/append_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,14 @@ package validity
import (
"fmt"
"log"
"net/http"
"net/url"
"time"

"github.com/google/webpackager/exchange"
"github.com/google/webpackager/internal/httpheader"
"github.com/google/webpackager/internal/urlutil"
)

// ValidityURLRule decides the validity URL of a resource.
type ValidityURLRule interface {
// Apply returns the validity URL of a resource. physurl is the physical
// URL of the resource; resp is the HTTP response; vp is the period the
// signed exchange will be valid for. The physical URL is typically equal
// to the request URL but different in some cases; see package urlrewrite
// for more details.
//
// Note ValidityURLRule implementations can retrieve the request URL via
// resp.Request.URL.
Apply(physurl *url.URL, resp *exchange.Response, vp exchange.ValidPeriod) (*url.URL, error)
}

// DefaultValidityURLRule is the default rule used by webpackager.Packager.
var DefaultValidityURLRule ValidityURLRule = AppendExtDotLastModified(".validity")

// AppendExtDotLastModified generates the validity URL by appending ext
// and the resource's last modified time. For example:
//
Expand All @@ -66,7 +50,7 @@ var DefaultValidityURLRule ValidityURLRule = AppendExtDotLastModified(".validity
//
// The AppendExtDotLastModified rule ignores Query and Fragment in physurl.
// The validity URLs will always have empty Query and Fragment.
func AppendExtDotLastModified(ext string) ValidityURLRule {
func AppendExtDotLastModified(ext string) URLRule {
return &appendExtDotLastModified{ext}
}

Expand All @@ -79,7 +63,7 @@ func (rule *appendExtDotLastModified) Apply(physurl *url.URL, resp *exchange.Res
if date == "" {
return toValidityURL(physurl, rule.ext, vp.Date())
}
parsed, err := httpheader.ParseDate(date)
parsed, err := http.ParseTime(date)
if err != nil {
log.Printf("warning: failed to parse the header %q: %v", date, err)
return toValidityURL(physurl, rule.ext, vp.Date())
Expand All @@ -89,7 +73,7 @@ func (rule *appendExtDotLastModified) Apply(physurl *url.URL, resp *exchange.Res

// AppendExtDotExchangeDate is like AppendExtDotLastModified but always
// uses vp.Date instead of the last modified time.
func AppendExtDotExchangeDate(ext string) ValidityURLRule {
func AppendExtDotExchangeDate(ext string) URLRule {
return &appendExtDotExchangeDate{ext}
}

Expand Down
2 changes: 1 addition & 1 deletion validity/url_test.go → validity/append_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAppendExtDotUnixTime(t *testing.T) {
name string
url string
header http.Header
rule validity.ValidityURLRule
rule validity.URLRule
vp exchange.ValidPeriod
want string
}{
Expand Down
3 changes: 2 additions & 1 deletion validity/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package validity defines interfaces to represent validity URL rules.
// Package validity handles the validity data of signed exchanges. It only
// defines validity URLs at the moment though.
package validity
39 changes: 39 additions & 0 deletions validity/fixed_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validity

import (
"net/url"

"github.com/google/webpackager/exchange"
)

// FixedURL uses url as the validity URL for all resources. It is useful,
// for example, if you plan to provide empty validity data (consisting of
// a single byte 0xa0) for all signed exchanges and serve it on a single URL.
//
// url can be relative, in which case the validity URL is resolved relative
// from the physical URL.
func FixedURL(url *url.URL) URLRule {
return &fixedURL{url}
}

type fixedURL struct {
url *url.URL
}

func (rule *fixedURL) Apply(physurl *url.URL, resp *exchange.Response, vp exchange.ValidPeriod) (*url.URL, error) {
return physurl.ResolveReference(rule.url), nil
}
67 changes: 67 additions & 0 deletions validity/fixed_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validity_test

import (
"net/url"
"testing"

"github.com/google/webpackager/exchange"
"github.com/google/webpackager/exchange/exchangetest"
"github.com/google/webpackager/internal/urlutil"
"github.com/google/webpackager/validity"
)

func TestFixedURL(t *testing.T) {
tests := []struct {
name string
url string
rule validity.URLRule
want string
}{
{
name: "AbsoluteURL",
url: "https://example.com/index.html",
rule: validity.FixedURL(
urlutil.MustParse("https://example.com/empty.validity"),
),
want: "https://example.com/empty.validity",
},
{
name: "RelativeURL",
url: "https://example.com/index.html",
rule: validity.FixedURL(
urlutil.MustParse("/empty.validity"),
),
want: "https://example.com/empty.validity",
},
}

for _, test := range tests {
arg, err := url.Parse(test.url)
if err != nil {
t.Fatal(err)
}
resp := exchangetest.MakeEmptyResponse(test.url)
// FixedURL does not use the ValidPeriod.
got, err := test.rule.Apply(arg, resp, exchange.ValidPeriod{})
if err != nil {
t.Fatalf("got error(%q), want success", err)
}
if got.String() != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
}
}
36 changes: 36 additions & 0 deletions validity/url_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validity

import (
"net/url"

"github.com/google/webpackager/exchange"
)

// URLRule decides the validity URL of a resource.
type URLRule interface {
// Apply returns the validity URL of a resource. physurl is the physical
// URL of the resource; resp is the HTTP response; vp is the period the
// signed exchange will be valid for. The physical URL is typically equal
// to the request URL but different in some cases; see package urlrewrite
// for more details.
//
// Note implementations can retrieve the request URL via resp.Request.URL.
Apply(physurl *url.URL, resp *exchange.Response, vp exchange.ValidPeriod) (*url.URL, error)
}

// DefaultURLRule is the default rule used by webpackager.Packager.
var DefaultURLRule URLRule = AppendExtDotLastModified(".validity")