~ecs/hq

ref: 05e89aaebceb0242aaa43c75277e0b2cc05a11a0 hq/url.go -rw-r--r-- 1.4 KiB
05e89aaeEmber Sawady Improve multi error handling 6 months ago
                                                                                
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
package main

import (
	"errors"
	"net/url"
)

type u struct{}

func (u) Execute(args []string, ins []<-chan Datum, out chan Datum) error {
	defer func() {
		out <- Datum{End: true}
		close(out)
	}()
	if len(args) != 1 && args[0] != "param" {
		return errors.New("usage: " + args[0])
	}
	for _, d := Select(ins); d != nil; _, d = Select(ins) {
		if d.End {
			continue
		}
		if d.Text == nil {
			return errors.New("type error: expected text, got html")
		}
		if args[0] == "urlescape" {
			ret := url.PathEscape(*d.Text)
			out <- Datum{Text: &ret}
			continue
		}
		if args[0] == "urlunescape" {
			ret, err := url.PathUnescape(*d.Text)
			if err != nil {
				return err
			}
			out <- Datum{Text: &ret}
			continue
		}
		p, err := url.Parse(*d.Text)
		if err != nil {
			return err
		}
		var ret string
		switch args[0] {
		case "scheme":
			ret = p.Scheme
		case "opaque":
			ret = p.Opaque
		case "host":
			ret = p.Host
		case "path":
			ret = p.Path
		case "param":
			if len(args) != 1 {
				vs := p.Query()
				for _, arg := range args[1:] {
					if v := vs.Get(arg); v != "" {
						out <- Datum{Text: &v}
					}
				}
				continue
			}
			ret = p.RawQuery
		case "fragment":
			ret = p.Fragment
		}
		out <- Datum{Text: &ret}
	}
	return nil
}

func init() {
	Filters["scheme"] = u{}
	Filters["opaque"] = u{}
	Filters["host"] = u{}
	Filters["path"] = u{}
	Filters["param"] = u{}
	Filters["fragment"] = u{}
	Filters["urlescape"] = u{}
	Filters["urlunescape"] = u{}
}