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
|
package main
import (
"errors"
"net/http"
"net/url"
"strings"
"go.lindenii.runxiyu.org/lindenii-common/misc"
)
var (
err_duplicate_ref_spec = errors.New("Duplicate ref spec")
err_no_ref_spec = errors.New("No ref spec")
)
func get_param_ref_and_type(r *http.Request) (ref_type, ref string, err error) {
qr := r.URL.RawQuery
q, err := url.ParseQuery(qr)
if err != nil {
return
}
done := false
for _, _ref_type := range []string{"commit", "branch", "tag"} {
_ref, ok := q[_ref_type]
if ok {
if done {
err = err_duplicate_ref_spec
return
} else {
done = true
if len(_ref) != 1 {
err = err_duplicate_ref_spec
return
}
ref = _ref[0]
ref_type = _ref_type
}
}
}
if !done {
err = err_no_ref_spec
}
return
}
func parse_request_uri(request_uri string) (segments []string, params url.Values, err error) {
path, params_string, _ := strings.Cut(request_uri, "?")
segments = strings.Split(strings.TrimPrefix(path, "/"), "/")
for i, segment := range segments {
segments[i], err = url.PathUnescape(segment)
if err != nil {
return nil, nil, misc.Wrap_one_error(err_bad_request, err)
}
}
params, err = url.ParseQuery(params_string)
if err != nil {
return nil, nil, misc.Wrap_one_error(err_bad_request, err)
}
return
}
func redirect_with_slash(w http.ResponseWriter, r *http.Request) bool {
request_uri := r.RequestURI
path_end := strings.IndexAny(request_uri, "?#")
var path, rest string
if path_end == -1 {
path = request_uri
} else {
path = request_uri[:path_end]
rest = request_uri[path_end:]
}
if !strings.HasSuffix(path, "/") {
http.Redirect(w, r, path+"/"+rest, http.StatusSeeOther)
return true
}
return false
}
func redirect_without_slash(w http.ResponseWriter, r *http.Request) bool {
request_uri := r.RequestURI
path_end := strings.IndexAny(request_uri, "?#")
var path, rest string
if path_end == -1 {
path = request_uri
} else {
path = request_uri[:path_end]
rest = request_uri[path_end:]
}
if strings.HasSuffix(path, "/") {
http.Redirect(w, r, strings.TrimSuffix(path, "/")+rest, http.StatusSeeOther)
return true
}
return false
}
|