This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhelpers.go
165 lines (155 loc) · 3.15 KB
/
helpers.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
// misc helpers
package main
import (
"fmt"
"math/rand"
"os"
"strings"
)
func FullPath(name string) string {
return os.ExpandEnv(strings.Replace(name, "~", os.Getenv("HOME"), 1))
}
// Return text representation for StreamType constants
func StreamType2String(t StreamType) string {
switch t {
case SAMPLE:
return "sample"
case HLS:
return "hls"
case HDS:
return "hds"
case WV:
return "wv"
case HTTP:
return "http"
default:
return "unknown"
}
}
//
func String2StreamType(s string) StreamType {
switch strings.ToLower(s) {
case "sample":
return SAMPLE
case "hls":
return HLS
case "hds":
return HDS
case "wv":
return WV
case "http":
return HTTP
default:
return UNKSTREAM
}
}
// Text representation of stream errors
func StreamErr2String(err ErrType) string {
switch err {
case SUCCESS:
return "success"
case HLSPARSER:
return "HLS parser" // debug
case BADREQUEST:
return "invalid request" // debug
case SLOW:
return "slow response"
case VERYSLOW:
return "very slow response"
case BADSTATUS:
return "bad status"
case BADURI:
return "bad URI"
case LISTEMPTY: // HLS specific
return "list empty"
case BADFORMAT: // HLS specific
return "bad format"
case TTLEXPIRED:
return "TTL expired"
case RTIMEOUT:
return "timeout on read"
case CTIMEOUT:
return "connection timeout"
case BADLENGTH:
return "bad content length value"
case BODYREAD:
return "response body error"
case REFUSED:
return "connection refused"
default:
return "unknown"
}
}
//
func String2StreamErr(s string) ErrType {
switch strings.ToLower(s) {
case "success":
return SUCCESS
case "debug":
return DEBUG_LEVEL
case "hlsparser":
return HLSPARSER
case "badrequest":
return BADREQUEST
case "warning":
return WARNING_LEVEL
case "slow":
return SLOW
case "veryslow":
return VERYSLOW
case "badstatus":
return BADSTATUS
case "baduri":
return BADURI
case "listempty": // HLS specific
return LISTEMPTY
case "badformat": // HLS specific
return BADFORMAT
case "ttlexpired":
return TTLEXPIRED
case "rtimeout":
return RTIMEOUT
case "error":
return ERROR_LEVEL
case "ctimeout":
return CTIMEOUT
case "badlength":
return BADLENGTH
case "bodyread":
return BODYREAD
case "critical":
return CRITICAL_LEVEL
case "refused":
return REFUSED
default:
return UNKERR
}
}
// Helper to make a href.
// First arg must be URL, second is text.
// Optional args is title (3d) and class (4d).
func href(url, text string, opts ...string) string {
switch len(opts) {
case 1:
return fmt.Sprintf("<a title=\"%s\" href=\"%s\">%s</a>", opts[0], url, text)
case 2:
return fmt.Sprintf("<a title=\"%s\" class=\"%s\" href=\"%s\">%s</a>", opts[0], opts[1], url, text)
default:
return fmt.Sprintf("<a href=\"%s\">%s</a>", url, text)
}
}
//
func span(text, class string) string {
return fmt.Sprintf("<span class=\"%s\">%s</span>", class, text)
}
func bytewe(res []byte, err error) []byte {
return res
}
// Returns proper user agent string for the HTTP-headers.
func UserAgent() string {
if len(cfg.UserAgents) > 0 {
return cfg.UserAgents[rand.Intn(len(cfg.UserAgents)-1)]
} else {
return fmt.Sprintf("%s/%s", SURFER, VERSION)
}
}