Skip to content

Commit d6513cf

Browse files
committed
Multiple changes to the validity package.
- Add FixedURL: use the same validity URL for all resources. - Rename ValidityURLRule to URLRule to reduce the redundancy; it makes golint happier. - Split url.go into url_rule.go and append_ext.go; having all rules in a single source file makes it too large. - Use http.ParseTime instead of httpheader.ParseDate (our own code).
1 parent 38014df commit d6513cf

File tree

10 files changed

+154
-136
lines changed

10 files changed

+154
-136
lines changed

cmd/webpackager/flag_packager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func getPhysicalURLRuleFromFlags() (urlrewrite.Rule, error) {
193193
return rule, nil
194194
}
195195

196-
func getValidityURLRuleFromFlags() (validity.ValidityURLRule, error) {
196+
func getValidityURLRuleFromFlags() (validity.URLRule, error) {
197197
return validity.AppendExtDotLastModified(*flagValidityExt), nil
198198
}
199199

config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ type Config struct {
5656
// ValidityURLRule specifies the rule to determine the validity URL,
5757
// where the validity data should be served.
5858
//
59-
// nil implies validity.AppendExtDotUnixTime(".validity", time.Now()).
60-
ValidityURLRule validity.ValidityURLRule
59+
// nil implies validity.DefaultURLRule, which appends ".validity" plus
60+
// the last modified time (in UNIX time) to the document URL.
61+
ValidityURLRule validity.URLRule
6162

6263
// Processor specifies the processor(s) applied to each HTTP response
6364
// before turning it into a signed exchange. The processors make sure
@@ -109,7 +110,7 @@ func (cfg *Config) populateDefaults() {
109110
cfg.PhysicalURLRule = urlrewrite.DefaultRules
110111
}
111112
if cfg.ValidityURLRule == nil {
112-
cfg.ValidityURLRule = validity.DefaultValidityURLRule
113+
cfg.ValidityURLRule = validity.DefaultURLRule
113114
}
114115
if cfg.Processor == nil {
115116
cfg.Processor = complexproc.DefaultProcessor

internal/httpheader/date.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

internal/httpheader/date_test.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

validity/url.go renamed to validity/append_ext.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,14 @@ package validity
1717
import (
1818
"fmt"
1919
"log"
20+
"net/http"
2021
"net/url"
2122
"time"
2223

2324
"github.com/google/webpackager/exchange"
24-
"github.com/google/webpackager/internal/httpheader"
2525
"github.com/google/webpackager/internal/urlutil"
2626
)
2727

28-
// ValidityURLRule decides the validity URL of a resource.
29-
type ValidityURLRule interface {
30-
// Apply returns the validity URL of a resource. physurl is the physical
31-
// URL of the resource; resp is the HTTP response; vp is the period the
32-
// signed exchange will be valid for. The physical URL is typically equal
33-
// to the request URL but different in some cases; see package urlrewrite
34-
// for more details.
35-
//
36-
// Note ValidityURLRule implementations can retrieve the request URL via
37-
// resp.Request.URL.
38-
Apply(physurl *url.URL, resp *exchange.Response, vp exchange.ValidPeriod) (*url.URL, error)
39-
}
40-
41-
// DefaultValidityURLRule is the default rule used by webpackager.Packager.
42-
var DefaultValidityURLRule ValidityURLRule = AppendExtDotLastModified(".validity")
43-
4428
// AppendExtDotLastModified generates the validity URL by appending ext
4529
// and the resource's last modified time. For example:
4630
//
@@ -66,7 +50,7 @@ var DefaultValidityURLRule ValidityURLRule = AppendExtDotLastModified(".validity
6650
//
6751
// The AppendExtDotLastModified rule ignores Query and Fragment in physurl.
6852
// The validity URLs will always have empty Query and Fragment.
69-
func AppendExtDotLastModified(ext string) ValidityURLRule {
53+
func AppendExtDotLastModified(ext string) URLRule {
7054
return &appendExtDotLastModified{ext}
7155
}
7256

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

9074
// AppendExtDotExchangeDate is like AppendExtDotLastModified but always
9175
// uses vp.Date instead of the last modified time.
92-
func AppendExtDotExchangeDate(ext string) ValidityURLRule {
76+
func AppendExtDotExchangeDate(ext string) URLRule {
9377
return &appendExtDotExchangeDate{ext}
9478
}
9579

validity/url_test.go renamed to validity/append_ext_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestAppendExtDotUnixTime(t *testing.T) {
3030
name string
3131
url string
3232
header http.Header
33-
rule validity.ValidityURLRule
33+
rule validity.URLRule
3434
vp exchange.ValidPeriod
3535
want string
3636
}{

validity/doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Package validity defines interfaces to represent validity URL rules.
15+
// Package validity handles the validity data of signed exchanges. It only
16+
// defines validity URLs at the moment though.
1617
package validity

validity/fixed_url.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package validity
16+
17+
import (
18+
"net/url"
19+
20+
"github.com/google/webpackager/exchange"
21+
)
22+
23+
// FixedURL uses url as the validity URL for all resources. It is useful,
24+
// for example, if you plan to provide empty validity data (consisting of
25+
// a single byte 0xa0) for all signed exchanges and serve it on a single URL.
26+
//
27+
// url can be relative, in which case the validity URL is resolved relative
28+
// from the physical URL.
29+
func FixedURL(url *url.URL) URLRule {
30+
return &fixedURL{url}
31+
}
32+
33+
type fixedURL struct {
34+
url *url.URL
35+
}
36+
37+
func (rule *fixedURL) Apply(physurl *url.URL, resp *exchange.Response, vp exchange.ValidPeriod) (*url.URL, error) {
38+
return physurl.ResolveReference(rule.url), nil
39+
}

validity/fixed_url_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package validity_test
16+
17+
import (
18+
"net/url"
19+
"testing"
20+
21+
"github.com/google/webpackager/exchange"
22+
"github.com/google/webpackager/exchange/exchangetest"
23+
"github.com/google/webpackager/internal/urlutil"
24+
"github.com/google/webpackager/validity"
25+
)
26+
27+
func TestFixedURL(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
url string
31+
rule validity.URLRule
32+
want string
33+
}{
34+
{
35+
name: "AbsoluteURL",
36+
url: "https://example.com/index.html",
37+
rule: validity.FixedURL(
38+
urlutil.MustParse("https://example.com/empty.validity"),
39+
),
40+
want: "https://example.com/empty.validity",
41+
},
42+
{
43+
name: "RelativeURL",
44+
url: "https://example.com/index.html",
45+
rule: validity.FixedURL(
46+
urlutil.MustParse("/empty.validity"),
47+
),
48+
want: "https://example.com/empty.validity",
49+
},
50+
}
51+
52+
for _, test := range tests {
53+
arg, err := url.Parse(test.url)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
resp := exchangetest.MakeEmptyResponse(test.url)
58+
// FixedURL does not use the ValidPeriod.
59+
got, err := test.rule.Apply(arg, resp, exchange.ValidPeriod{})
60+
if err != nil {
61+
t.Fatalf("got error(%q), want success", err)
62+
}
63+
if got.String() != test.want {
64+
t.Errorf("got %q, want %q", got, test.want)
65+
}
66+
}
67+
}

validity/url_rule.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package validity
16+
17+
import (
18+
"net/url"
19+
20+
"github.com/google/webpackager/exchange"
21+
)
22+
23+
// URLRule decides the validity URL of a resource.
24+
type URLRule interface {
25+
// Apply returns the validity URL of a resource. physurl is the physical
26+
// URL of the resource; resp is the HTTP response; vp is the period the
27+
// signed exchange will be valid for. The physical URL is typically equal
28+
// to the request URL but different in some cases; see package urlrewrite
29+
// for more details.
30+
//
31+
// Note implementations can retrieve the request URL via resp.Request.URL.
32+
Apply(physurl *url.URL, resp *exchange.Response, vp exchange.ValidPeriod) (*url.URL, error)
33+
}
34+
35+
// DefaultURLRule is the default rule used by webpackager.Packager.
36+
var DefaultURLRule URLRule = AppendExtDotLastModified(".validity")

0 commit comments

Comments
 (0)