/
serve.go
293 lines (265 loc) · 6.76 KB
/
serve.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
package main
import (
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"regexp/syntax"
"strconv"
"strings"
"time"
"github.com/bmatcuk/doublestar"
"github.com/btidor/src.codes/internal"
"github.com/google/codesearch/index"
)
const (
maxFileSize = (1 << 20)
maxFileVisits = 500
maxContext = 10
nl = '\n'
)
type Index struct {
*index.Index
Package string
}
func serve() {
var indexes = make(map[string][]*Index)
for distro := range distros {
matches, err := filepath.Glob(
filepath.Join(fastDir, "grep", distro, "*", "*", "*.csi"),
)
if err != nil {
panic(err)
}
if len(matches) < 1 {
panic("no indexes found for distro " + distro)
}
for _, filename := range matches {
pkg := filepath.Base(filepath.Dir(filename))
indexes[distro] = append(indexes[distro], &Index{index.Open(filename), pkg})
}
}
var server = &http.Server{
Handler: grepHandler{indexes},
ReadTimeout: 10 * time.Second,
WriteTimeout: 60 * time.Second,
}
var err error
if socket != "" {
fmt.Printf("Listening on %s\n", socket)
var listener net.Listener
listener, err = net.Listen("unix", socket)
if err != nil {
panic(err)
}
err = server.Serve(listener)
} else {
fmt.Println("Listening on :5050")
server.Addr = ":5050"
err = server.ListenAndServe()
}
panic(err)
}
type Globs struct {
G []string
}
func (gs Globs) MatchPath(path string) bool {
for _, glob := range gs.G {
if ok, _ := doublestar.Match(glob, path); ok {
return true
} else if ok, _ = doublestar.Match(glob+"/**", path); ok {
return true
}
}
return false
}
func (gs Globs) CanMatchPrefix(part string) bool {
prefix := part + "/"
for _, glob := range gs.G {
var gfx string
if i := strings.IndexRune(glob, '*'); i < 0 {
gfx = glob
} else {
gfx = glob[:i]
}
if len(gfx) <= len(prefix) && strings.HasPrefix(prefix, gfx) {
return true
} else if strings.HasPrefix(gfx, prefix) {
return true
}
}
return false
}
func (gs Globs) MustMatchPrefix(part string) bool {
for _, glob := range gs.G {
components := strings.Split(glob, "/")
// fmt.Printf("%s %s %s\n", part, glob, components)
for i, comp := range components[:len(components)-1] {
if comp == "**" {
continue
} else if ok, _ := doublestar.Match(comp, part); ok {
if components[i+1] == "**" {
return true
}
}
break
}
}
return false
}
type grepHandler struct {
indexes map[string][]*Index
}
func (g grepHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Cache-Control", "no-cache")
w.Header().Add("Content-Security-Policy", "default-src 'none';")
var parts = strings.Split(r.URL.Path, "/")
if len(parts) != 2 {
// Invalid path
internal.HTTPError(w, r, 404)
return
}
distro := parts[1]
if distro == "" {
// Requests to `/` show a welcome message
fmt.Fprintf(w, "Hello from grep@%s!\n", commit)
return
} else if distro == "robots.txt" {
// Requests to `/robots.txt` return our robots config
fmt.Fprintf(w, "User-agent: *\nDisallow: /\n")
return
}
ixlist, ok := g.indexes[distro]
if !ok {
// Unknown distro
internal.HTTPError(w, r, 404)
return
}
if !strings.Contains(r.Header.Get("User-Agent"), "Firefox/") ||
r.Header.Get("Sec-Fetch-Mode") != "navigate" {
// Workaround: this should be text/plain, but it's the only way I've found
// to make the Fetch API actually stream responses back. (I think they're
// being blocked by CORB?) See:
// https://www.reddit.com/r/webdev/comments/bwrpjl/how_to_stop_browser_from_buffering_a_streaming/
w.Header().Add("Content-Type", "text/event-stream; charset=utf-8")
}
var start = time.Now()
var query = r.URL.Query().Get("q")
if query == "" {
// Missing query
internal.HTTPError(w, r, 400)
return
}
var after = r.URL.Query().Get("after")
var includes = Globs{r.URL.Query()["include"]}
var excludes = Globs{r.URL.Query()["exclude"]}
var context int
if c := r.URL.Query().Get("context"); c != "" {
if i, err := strconv.ParseInt(c, 10, 64); err == nil {
if i < 0 {
context = 0
} else if i > maxContext {
context = maxContext
} else {
context = int(i)
}
}
}
// Set up regexp flags. Compile(), and also Javascript, default to Perl
// matching options, so we should start with that flag. Then add `m` to
// always allow ^ and $ to match the start/end of the line.
var intFlags = syntax.Perl & ^syntax.OneLine & syntax.NonGreedy
var charFlags = "mU"
// Turn on case-insensitivity if requested
var rawFlags = r.URL.Query().Get("flags")
if strings.Contains(rawFlags, "i") {
intFlags |= syntax.FoldCase
charFlags += "i"
}
if strings.Contains(rawFlags, "s") {
intFlags |= syntax.DotNL
charFlags += "s"
}
rsyntax, err := syntax.Parse(query, intFlags)
if err != nil {
internal.HTTPError(w, r, 400)
return
}
rsyntax = rsyntax.Simplify()
re, err := regexp.Compile("(?" + charFlags + ")" + rsyntax.String())
if err != nil {
internal.HTTPError(w, r, 400)
return
}
var grep = Grep{Context: context, Regexp: re, Stdout: w}
var iquery = index.RegexpQuery(rsyntax)
var count, files int
var resume string
var errors []error
overall:
for _, ix := range ixlist {
if len(includes.G) > 0 && !includes.CanMatchPrefix(ix.Package) {
continue
} else if excludes.MustMatchPrefix(ix.Package) {
continue
} else if after != "" && ix.Package <= after {
continue
}
perfile:
for _, fileid := range ix.PostingQuery(iquery) {
relative := ix.Name(fileid)
// Handle include and exclude options
if len(includes.G) > 0 && !includes.MatchPath(relative) {
continue perfile
} else if excludes.MatchPath(relative) {
continue perfile
} else if after != "" && relative <= after {
continue perfile
}
// Find source file
prefix := string(relative[0])
if strings.HasPrefix(relative, "lib") {
prefix = relative[0:4]
}
absolute := filepath.Join(bulkDir, "grep", distro, prefix, relative)
f, err := os.Open(absolute)
if err != nil {
errors = append(errors, err)
continue
}
// Search source file to confirm matches
n, err := grep.Reader(f, relative)
count += n
files++
if err != nil {
errors = append(errors, err)
}
f.Close()
if files >= maxFileVisits {
resume = relative
break overall
}
}
}
fmt.Fprintf(w, "\nQuery:\t%q\n", query)
if after != "" {
fmt.Fprintf(w, "After:\t%s\n", after)
}
fmt.Fprintf(w, "Flags:\t%q\tContext:\t%d\tIncludes:\t%v\tExcludes:\t%v\n",
charFlags, context, includes, excludes)
fmt.Fprintf(w, "Count:\t%d\n", count)
fmt.Fprintf(w, "Time:\t%s\n", time.Since(start))
if resume != "" {
fmt.Fprintf(w, "Resume:\t%s\n", resume)
}
if len(errors) > 0 {
fmt.Fprintf(w, "Errors:\t")
for _, err := range errors {
fmt.Fprintf(w, "%#v ", err)
}
fmt.Fprintf(w, "\n")
}
}