-
Notifications
You must be signed in to change notification settings - Fork 0
/
gothmog.go
80 lines (71 loc) · 2.53 KB
/
gothmog.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
package main
import (
"fmt"
"net/http"
"strings"
)
type GothmogHandler struct {
rules *Rules
}
// gothmogFields hold all of the relevant information contained in an update request URI
type gothmogFields struct {
product string
version string
buildid string // TODO: this could potentially be an int, not sure if it should be
buildTarget string
locale string
channel string
osVersion string
instructionSet string // split out of the systemCapabilities field
memory string // split out of the systemCapabilities field
distribution string
distVersion string
}
// splitFields takes an entire update URI and parses the useful parts into a gothmogFields instance
// Example update URI: /update/6/Firefox/55.0/20170731163142/Linux_x86_64-gcc3/en-GB/beta/Linux 4.11.3-202.fc25.x86_64 (GTK 3.22.15,libpulse 10.0.0)/NA,8196/default/default/update.xml
// Notably, the first two parts and the final `update.xml` part are unused.
func splitFields(fields string) (gothmogFields, bool) {
// TODO: make sure bad data is handled correctly - add tests
sections := strings.Split(fields, "/")
if len(sections) != 14 {
return gothmogFields{}, false
}
// These two fields are extracted first to so the final return statement
// can be a simple gothmogFields literal.
var instructionSet, memory string
systemCapabilities := strings.Split(sections[10], ",")
switch len(systemCapabilities) {
case 0:
case 1:
instructionSet = systemCapabilities[0]
default:
instructionSet = systemCapabilities[0]
memory = systemCapabilities[1]
}
return gothmogFields{
product: sections[3],
version: sections[4],
buildid: sections[5],
buildTarget: sections[6],
locale: sections[7],
channel: sections[8],
osVersion: sections[9],
instructionSet: instructionSet,
memory: memory,
distribution: sections[11],
distVersion: sections[12],
}, true
}
func (g *GothmogHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Notably, we're throwing away query args here. In reality there are
// a few that we should be paying attention to, but for this simple
// implementation we're just ignoring them.
fields, ok := splitFields(strings.Split(req.URL.RequestURI(), "?")[0])
if ok != true {
rw.Header().Set("Content-Type", "text/plain")
rw.Write([]byte("Couldn't parse update URI"))
}
rule := findMatchingRule(g.rules, fields)
rw.Header().Set("Content-Type", "text/plain")
rw.Write([]byte(fmt.Sprintf("Matching rule is: %v", rule)))
}