forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
594 lines (536 loc) · 15.9 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"encoding/json"
"fmt"
gourl "net/url"
"regexp"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/juju/mgo/v2/bson"
"github.com/juju/names/v4"
)
// Schema represents the different types of valid schemas.
type Schema string
const (
// CharmStore schema represents the original schema for a charm URL
CharmStore Schema = "cs"
// Local represents a local charm URL, describes as a file system path.
Local Schema = "local"
// CharmHub schema represents the new schema for another unique charm store.
CharmHub Schema = "ch"
// HTTP refers to the HTTP schema that is used for the V2 of the charm URL.
HTTP Schema = "http"
// HTTPS refers to the HTTP schema that is used for the V2 of the charm URL.
HTTPS Schema = "https"
)
// Prefix creates a url with the given prefix, useful for typed schemas.
func (s Schema) Prefix(url string) string {
return fmt.Sprintf("%s:%s", s, url)
}
// Matches attempts to compare if a schema string matches the schema.
func (s Schema) Matches(other string) bool {
return string(s) == other
}
func (s Schema) String() string {
return string(s)
}
// Location represents a charm location, which must declare a path component
// and a string representation.
type Location interface {
Path() string
String() string
}
// URL represents a charm or bundle location:
//
// cs:~joe/oneiric/wordpress
// cs:oneiric/wordpress-42
// local:oneiric/wordpress
// cs:~joe/wordpress
// cs:wordpress
// cs:precise/wordpress-20
// cs:development/precise/wordpress-20
// cs:~joe/development/wordpress
// ch:wordpress
//
type URL struct {
Schema string // "cs", "ch" or "local".
User string // "joe".
Name string // "wordpress".
Revision int // -1 if unset, N otherwise.
Series string // "precise" or "" if unset; "bundle" if it's a bundle.
Architecture string // "amd64" or "" if unset for charmstore (v1) URLs.
}
var (
validArch = regexp.MustCompile("^[a-z]+([a-z0-9]+)?$")
validSeries = regexp.MustCompile("^[a-z]+([a-z0-9]+)?$")
validName = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0-9]*)*$")
)
// ValidateSchema returns an error if the schema is invalid.
//
// Valid schemas for the URL are:
// - cs: charm store
// - ch: charm hub
// - local: local file
//
// http and https are not valid schemas, as they compiled to V1 charm URLs.
func ValidateSchema(schema string) error {
switch schema {
// ignore http/https schemas.
case CharmStore.String(), CharmHub.String(), Local.String():
return nil
}
return errors.NotValidf("schema %q", schema)
}
// IsValidSeries reports whether series is a valid series in charm or bundle
// URLs.
func IsValidSeries(series string) bool {
return validSeries.MatchString(series)
}
// ValidateSeries returns an error if the given series is invalid.
func ValidateSeries(series string) error {
if IsValidSeries(series) {
return nil
}
return errors.NotValidf("series name %q", series)
}
// IsValidArchitecture reports whether the architecture is a valid architecture
// in charm or bundle URLs.
func IsValidArchitecture(arch string) bool {
return validArch.MatchString(arch)
}
// ValidateArchitecture returns an error if the given architecture is invalid.
func ValidateArchitecture(arch string) error {
if IsValidArchitecture(arch) {
return nil
}
return errors.NotValidf("architecture name %q", arch)
}
// IsValidName reports whether name is a valid charm or bundle name.
func IsValidName(name string) bool {
return validName.MatchString(name)
}
// ValidateName returns an error if the given name is invalid.
func ValidateName(name string) error {
if IsValidName(name) {
return nil
}
return errors.NotValidf("name %q", name)
}
// WithRevision returns a URL equivalent to url but with Revision set
// to revision.
func (u *URL) WithRevision(revision int) *URL {
urlCopy := *u
urlCopy.Revision = revision
return &urlCopy
}
// WithArchitecture returns a URL equivalent to url but with Architecture set
// to architecture.
func (u *URL) WithArchitecture(arch string) *URL {
urlCopy := *u
urlCopy.Architecture = arch
return &urlCopy
}
// WithSeries returns a URL equivalent to url but with Series set
// to series.
func (u *URL) WithSeries(series string) *URL {
urlCopy := *u
urlCopy.Series = series
return &urlCopy
}
// MustParseURL works like ParseURL, but panics in case of errors.
func MustParseURL(url string) *URL {
u, err := ParseURL(url)
if err != nil {
panic(err)
}
return u
}
// ParseURL parses the provided charm URL string into its respective
// structure.
//
// Additionally, fully-qualified charmstore URLs are supported; note that this
// currently assumes that they will map to jujucharms.com (that is,
// fully-qualified URLs currently map to the 'cs' schema):
//
// https://jujucharms.com/name
// https://jujucharms.com/name/series
// https://jujucharms.com/name/revision
// https://jujucharms.com/name/series/revision
// https://jujucharms.com/u/user/name
// https://jujucharms.com/u/user/name/series
// https://jujucharms.com/u/user/name/revision
// https://jujucharms.com/u/user/name/series/revision
//
// A missing schema is assumed to be 'cs'.
func ParseURL(url string) (*URL, error) {
// Check if we're dealing with a v1 or v2 URL.
u, err := gourl.Parse(url)
if err != nil {
return nil, errors.Errorf("cannot parse charm or bundle URL: %q", url)
}
if u.RawQuery != "" || u.Fragment != "" || u.User != nil {
return nil, errors.Errorf("charm or bundle URL %q has unrecognized parts", url)
}
var curl *URL
switch {
case CharmHub.Matches(u.Scheme):
// Handle talking to the new style of the schema.
curl, err = parseIdentifierURL(u)
case u.Opaque != "":
u.Path = u.Opaque
curl, err = parseV1URL(u, url)
case CharmStore.Matches(u.Scheme):
curl, err = parseV1URL(u, url)
case HTTP.Matches(u.Scheme) || HTTPS.Matches(u.Scheme):
curl, err = parseHTTPURL(u)
default:
// Handle the fact that anything without a prefix is now a CharmHub
// charm URL.
curl, err = parseIdentifierURL(u)
}
if err != nil {
return nil, errors.Trace(err)
}
if curl.Schema == "" {
return nil, errors.Errorf("expected schema for charm or bundle URL: %q", url)
}
return curl, nil
}
func parseV1URL(url *gourl.URL, originalURL string) (*URL, error) {
r := URL{
Schema: CharmStore.String(),
}
if url.Scheme != "" {
r.Schema = url.Scheme
}
if err := ValidateSchema(r.Schema); err != nil {
return nil, errors.Annotatef(err, "cannot parse URL %q", url)
}
parts := strings.Split(url.Path[0:], "/")
if len(parts) < 1 || len(parts) > 4 {
return nil, errors.Errorf("charm or bundle URL has invalid form: %q", originalURL)
}
// ~<username>
if strings.HasPrefix(parts[0], "~") {
if Local.Matches(r.Schema) {
return nil, errors.Errorf("local charm or bundle URL with user name: %q", originalURL)
}
r.User, parts = parts[0][1:], parts[1:]
}
if len(parts) > 2 {
return nil, errors.Errorf("charm or bundle URL has invalid form: %q", originalURL)
}
// <series>
if len(parts) == 2 {
r.Series, parts = parts[0], parts[1:]
if err := ValidateSeries(r.Series); err != nil {
return nil, errors.Annotatef(err, "cannot parse URL %q", originalURL)
}
}
if len(parts) < 1 {
return nil, errors.Errorf("URL without charm or bundle name: %q", originalURL)
}
// <name>[-<revision>]
r.Name, r.Revision = extractRevision(parts[0])
if r.User != "" && !names.IsValidUser(r.User) {
return nil, errors.Errorf("charm or bundle URL has invalid user name: %q", originalURL)
}
if err := ValidateName(r.Name); err != nil {
return nil, errors.Annotatef(err, "cannot parse URL %q", url)
}
return &r, nil
}
func (u *URL) path() string {
var parts []string
if u.User != "" {
parts = append(parts, fmt.Sprintf("~%s", u.User))
}
if u.Architecture != "" {
parts = append(parts, u.Architecture)
}
if u.Series != "" {
parts = append(parts, u.Series)
}
if u.Revision >= 0 {
parts = append(parts, fmt.Sprintf("%s-%d", u.Name, u.Revision))
} else {
parts = append(parts, u.Name)
}
return strings.Join(parts, "/")
}
// FullPath returns the full path of a URL path including the schema.
func (u *URL) FullPath() string {
return fmt.Sprintf("%s:%s", u.Schema, u.Path())
}
// Path returns the path of the URL without the schema.
func (u *URL) Path() string {
return u.path()
}
// String returns the string representation of the URL.
func (u *URL) String() string {
return u.FullPath()
}
// GetBSON turns u into a bson.Getter so it can be saved directly
// on a MongoDB database with mgo.
//
// TODO (stickupkid): This should not be here, as this is purely for mongo
// data stores and that should be implemented at the site of data store, not
// dependant on the library.
func (u *URL) GetBSON() (interface{}, error) {
if u == nil {
return nil, nil
}
return u.String(), nil
}
// SetBSON turns u into a bson.Setter so it can be loaded directly
// from a MongoDB database with mgo.
//
// TODO (stickupkid): This should not be here, as this is purely for mongo
// data stores and that should be implemented at the site of data store, not
// dependant on the library.
func (u *URL) SetBSON(raw bson.Raw) error {
if raw.Kind == 10 {
return bson.SetZero
}
var s string
err := raw.Unmarshal(&s)
if err != nil {
return err
}
url, err := ParseURL(s)
if err != nil {
return err
}
*u = *url
return nil
}
// MarshalJSON will marshal the URL into a slice of bytes in a JSON
// representation.
func (u *URL) MarshalJSON() ([]byte, error) {
if u == nil {
panic("cannot marshal nil *charm.URL")
}
return json.Marshal(u.FullPath())
}
// UnmarshalJSON will unmarshal the URL from a JSON representation.
func (u *URL) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
url, err := ParseURL(s)
if err != nil {
return err
}
*u = *url
return nil
}
// MarshalText implements encoding.TextMarshaler by
// returning u.FullPath()
func (u *URL) MarshalText() ([]byte, error) {
if u == nil {
return nil, nil
}
return []byte(u.FullPath()), nil
}
// UnmarshalText implements encoding.TestUnmarshaler by
// parsing the data with ParseURL.
func (u *URL) UnmarshalText(data []byte) error {
url, err := ParseURL(string(data))
if err != nil {
return err
}
*u = *url
return nil
}
// Quote translates a charm url string into one which can be safely used
// in a file path. ASCII letters, ASCII digits, dot and dash stay the
// same; other characters are translated to their hex representation
// surrounded by underscores.
func Quote(unsafe string) string {
safe := make([]byte, 0, len(unsafe)*4)
for i := 0; i < len(unsafe); i++ {
b := unsafe[i]
switch {
case b >= 'a' && b <= 'z',
b >= 'A' && b <= 'Z',
b >= '0' && b <= '9',
b == '.',
b == '-':
safe = append(safe, b)
default:
safe = append(safe, fmt.Sprintf("_%02x_", b)...)
}
}
return string(safe)
}
// RewriteURL turns a HTTP(s) URL into a charm URL.
//
// Fully-qualified charmstore URLs are supported; note that this
// currently assumes that they will map to jujucharms.com (that is,
// fully-qualified URLs currently map to the 'cs' schema):
//
// https://jujucharms.com/name -> cs:name
// https://jujucharms.com/name/series -> cs:series/name
// https://jujucharms.com/name/revision -> cs:name-revision
// https://jujucharms.com/name/series/revision -> cs:series/name-revision
// https://jujucharms.com/u/user/name -> cs:~user/name
// https://jujucharms.com/u/user/name/series -> cs:~user/series/name
// https://jujucharms.com/u/user/name/revision -> cs:~user/name-revision
// https://jujucharms.com/u/user/name/series/revision -> cs:~user/series/name-revision
//
// A missing schema is assumed to be 'cs'.
func RewriteURL(url string) (string, error) {
u, err := gourl.Parse(url)
if err != nil {
return "", errors.Errorf("cannot parse charm or bundle URL: %q", url)
}
if u.Scheme != "http" && u.Scheme != "https" {
return "", errors.Errorf("unexpected url schema %q", u.Scheme)
}
if u.RawQuery != "" || u.Fragment != "" || u.User != nil {
return "", errors.Errorf("charm or bundle URL %q has unrecognized parts", url)
}
httpURL, err := parseHTTPURL(u)
if err != nil {
return "", errors.Trace(err)
}
return httpURL.String(), nil
}
func parseHTTPURL(url *gourl.URL) (*URL, error) {
r := URL{
Schema: CharmStore.String(),
}
parts := strings.Split(strings.Trim(url.Path, "/"), "/")
if parts[0] == "u" {
if len(parts) < 3 {
return nil, errors.Errorf(`charm or bundle URL %q malformed, expected "/u/<user>/<name>"`, url)
}
r.User, parts = parts[1], parts[2:]
}
r.Name, parts = parts[0], parts[1:]
r.Revision = -1
if len(parts) > 0 {
revision, err := strconv.Atoi(parts[0])
if err == nil {
r.Revision = revision
} else {
r.Series, parts = parts[0], parts[1:]
if err := ValidateSeries(r.Series); err != nil {
return nil, errors.Annotatef(err, "cannot parse URL %q", url)
}
if len(parts) == 1 {
r.Revision, err = strconv.Atoi(parts[0])
if err != nil {
return nil, errors.Errorf("charm or bundle URL has malformed revision: %q in %q", parts[0], url)
}
} else if len(parts) != 0 {
return nil, errors.Errorf("charm or bundle URL has invalid form: %q", url)
}
}
}
if r.User != "" && !names.IsValidUser(r.User) {
return nil, errors.Errorf("charm or bundle URL has invalid user name: %q", url)
}
if err := ValidateName(r.Name); err != nil {
return nil, errors.Annotatef(err, "cannot parse URL %q", url)
}
return &r, nil
}
// parseIdentifierURL will attempt to parse an identifier URL. The identifier
// URL is split up into 3 parts, some of which are optional and some are
// mandatory.
//
// - architecture (optional)
// - series (optional)
// - name
// - revision (optional)
//
// Examples are as follows:
//
// - ch:amd64/foo-1
// - ch:amd64/focal/foo-1
// - ch:foo-1
// - ch:foo
// - ch:amd64/focal/foo
//
func parseIdentifierURL(url *gourl.URL) (*URL, error) {
r := URL{
Schema: CharmHub.String(),
Revision: -1,
}
path := url.Path
if url.Opaque != "" {
path = url.Opaque
}
parts := strings.Split(strings.Trim(path, "/"), "/")
if len(parts) == 0 || len(parts) > 3 {
return nil, errors.Errorf(`charm or bundle URL %q malformed`, url)
}
var nameRev string
switch len(parts) {
case 3:
r.Architecture, r.Series, nameRev = parts[0], parts[1], parts[2]
case 2:
r.Architecture, nameRev = parts[0], parts[1]
default:
nameRev = parts[0]
}
// Mandatory
r.Name, r.Revision = extractRevision(nameRev)
if err := ValidateName(r.Name); err != nil {
return nil, errors.Annotatef(err, "cannot parse name and/or revision in URL %q", url)
}
// Optional
if r.Architecture != "" {
if err := ValidateArchitecture(r.Architecture); err != nil {
return nil, errors.Annotatef(err, "cannot parse architecture in URL %q", url)
}
}
if r.Series != "" {
if err := ValidateSeries(r.Series); err != nil {
return nil, errors.Annotatef(err, "cannot parse series in URL %q", url)
}
}
return &r, nil
}
// EnsureSchema will ensure that the scheme for a given URL is correct and
// valid. If the url does not specify a schema, the provided defaultSchema
// will be injected to it.
func EnsureSchema(url string, defaultSchema Schema) (string, error) {
u, err := gourl.Parse(url)
if err != nil {
return "", errors.Errorf("cannot parse charm or bundle URL: %q", url)
}
switch Schema(u.Scheme) {
case CharmStore, CharmHub, Local, HTTP, HTTPS:
return url, nil
case Schema(""):
// If the schema is empty, we fall back to the default schema.
return defaultSchema.Prefix(url), nil
default:
return "", errors.NotValidf("schema %q", u.Scheme)
}
}
func extractRevision(name string) (string, int) {
revision := -1
for i := len(name) - 1; i > 0; i-- {
c := name[i]
if c >= '0' && c <= '9' {
continue
}
if c == '-' && i != len(name)-1 {
var err error
revision, err = strconv.Atoi(name[i+1:])
if err != nil {
panic(err) // We just checked it was right.
}
name = name[:i]
}
break
}
return name, revision
}