-
Notifications
You must be signed in to change notification settings - Fork 43
/
dcs-web.go
414 lines (371 loc) · 12.1 KB
/
dcs-web.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// vim:ts=4:sw=4:noexpandtab
package main
import (
"encoding/json"
"flag"
"fmt"
"hash/fnv"
"io"
"log"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/Debian/dcs/cmd/dcs-web/common"
"github.com/Debian/dcs/cmd/dcs-web/health"
"github.com/Debian/dcs/cmd/dcs-web/search"
"github.com/Debian/dcs/cmd/dcs-web/show"
"github.com/Debian/dcs/goroutinez"
"github.com/Debian/dcs/index"
dcsregexp "github.com/Debian/dcs/regexp"
_ "github.com/Debian/dcs/varz"
"github.com/prometheus/client_golang/prometheus"
_ "golang.org/x/net/trace"
"golang.org/x/net/websocket"
)
var (
listenAddress = flag.String("listen_address",
":28080",
"listen address ([host]:port)")
listenAddressTLS = flag.String("listen_address_tls",
"",
"listen address ([host]:port) for TLS")
memprofile = flag.String("memprofile", "", "Write memory profile to this file")
staticPath = flag.String("static_path",
"./static/",
"Path to static assets such as *.css")
accessLogPath = flag.String("access_log_path",
"",
"Where to write access.log entries (in Apache Common Log Format). Disabled if empty.")
tlsCertPath = flag.String("tls_cert_path", "", "Path to a .pem file containing the TLS certificate.")
tlsKeyPath = flag.String("tls_key_path", "", "Path to a .pem file containing the TLS private key.")
accessLog *os.File
resultsPathRe = regexp.MustCompile(`^/results/([^/]+)/(perpackage_` + strconv.Itoa(resultsPerPackage) + `_)?page_([0-9]+).json$`)
packagesPathRe = regexp.MustCompile(`^/results/([^/]+)/packages.json$`)
redirectPathRe = regexp.MustCompile(`^/(?:perpackage-)?results/([^/]+)(?:/[0-9]+)?/page_([0-9]+)`)
activeQueries = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "queries_active",
Help: "Number of active queries (i.e. not all results are in yet).",
})
failedQueries = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "queries_failed",
Help: "Number of failed queries.",
})
)
func init() {
prometheus.MustRegister(activeQueries)
prometheus.MustRegister(failedQueries)
}
func validateQuery(query string) error {
// Parse the query and see whether the resulting trigram query is
// non-empty. This is to catch queries like “package:debian”.
fakeUrl, err := url.Parse(query)
if err != nil {
return err
}
rewritten := search.RewriteQuery(*fakeUrl)
log.Printf("rewritten query = %q\n", rewritten.String())
re, err := dcsregexp.Compile(rewritten.Query().Get("q"))
if err != nil {
return err
}
indexQuery := index.RegexpQuery(re.Syntax)
log.Printf("trigram = %v, sub = %v", indexQuery.Trigram, indexQuery.Sub)
if len(indexQuery.Trigram) == 0 && len(indexQuery.Sub) == 0 {
return fmt.Errorf("Empty index query")
}
return nil
}
func EventsHandler(w http.ResponseWriter, r *http.Request) {
// The additional ":" at the end is necessary so that we don’t need to
// distinguish between the two cases (X-Forwarded-For, without a port, and
// RemoteAddr, with a part) in the code below.
src := r.Header.Get("X-Forwarded-For") + ":"
if src == ":" || (!strings.HasPrefix(r.RemoteAddr, "[::1]:") &&
!strings.HasPrefix(r.RemoteAddr, "127.0.0.1:")) {
src = r.RemoteAddr
}
q := "q=" + url.QueryEscape(r.URL.Path[len("/events/"):])
log.Printf("[%s] (events) Received query %q\n", src, q)
if err := validateQuery("?" + q); err != nil {
log.Printf("[%s] Query %q failed validation: %v\n", src, q, err)
http.Error(w, "Invalid query", http.StatusBadRequest)
return
}
// Uniquely (well, good enough) identify this query for a couple of minutes
// (as long as we want to cache results). We could try to normalize the
// query before hashing it, but that seems hardly worth the complexity.
h := fnv.New64()
io.WriteString(h, q)
identifier := fmt.Sprintf("%x", h.Sum64())
cached, err := maybeStartQuery(identifier, src, q)
if err != nil {
log.Printf("[%s] could not start query: %v\n", src, err)
http.Error(w, "Could not start query", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
// Create an apache common log format entry.
if accessLog != nil {
responseCode := 200
if cached {
responseCode = 304
}
remoteIP := src
if idx := strings.LastIndex(remoteIP, ":"); idx > -1 {
remoteIP = remoteIP[:idx]
}
fmt.Fprintf(accessLog, "%s - - [%s] \"GET /events/%s HTTP/1.1\" %d -\n",
remoteIP, time.Now().Format("02/Jan/2006:15:04:05 -0700"), q, responseCode)
}
// TODO: use Last-Event-ID header
lastseen := -1
sent := 0
for {
message, sequence := getEvent(identifier, lastseen)
lastseen = sequence
// This message was obsoleted by a more recent one, e.g. a more
// recent progress update obsoletes all earlier progress updates.
if *message.obsolete {
continue
}
if len(message.data) == 0 {
break
}
if _, err := fmt.Fprintf(w, "id: %d\ndata: %s\n\n", sequence, message.data); err != nil {
log.Printf("[%s] aborting, could not write: %v\n", src, err)
return
}
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
sent++
}
if sent == 0 {
w.WriteHeader(http.StatusNoContent)
fmt.Fprintln(w, "No content")
}
}
func InstantServer(ws *websocket.Conn) {
// The additional ":" at the end is necessary so that we don’t need to
// distinguish between the two cases (X-Forwarded-For, without a port, and
// RemoteAddr, with a part) in the code below.
src := ws.Request().Header.Get("X-Forwarded-For") + ":"
remoteaddr := ws.Request().RemoteAddr
if src == ":" || (!strings.HasPrefix(remoteaddr, "[::1]:") &&
!strings.HasPrefix(remoteaddr, "127.0.0.1:")) {
src = remoteaddr
}
log.Printf("Accepted websocket connection from %q\n", src)
type Query struct {
Query string
}
var q Query
for {
err := json.NewDecoder(ws).Decode(&q)
if err != nil {
log.Printf("[%s] error reading query: %v\n", src, err)
return
}
log.Printf("[%s] Received query %v\n", src, q)
if err := validateQuery("?" + q.Query); err != nil {
log.Printf("[%s] Query %q failed validation: %v\n", src, q.Query, err)
ws.Write([]byte(`{"Type":"error", "ErrorType":"invalidquery"}`))
continue
}
// Uniquely (well, good enough) identify this query for a couple of minutes
// (as long as we want to cache results). We could try to normalize the
// query before hashing it, but that seems hardly worth the complexity.
h := fnv.New64()
io.WriteString(h, q.Query)
identifier := fmt.Sprintf("%x", h.Sum64())
cached, err := maybeStartQuery(identifier, src, q.Query)
if err != nil {
log.Printf("[%s] could not start query: %v\n", src, err)
ws.Write([]byte(`{"Type":"error", "ErrorType":"failed"}`))
continue
}
// Create an apache common log format entry.
if accessLog != nil {
responseCode := 200
if cached {
responseCode = 304
}
remoteIP := src
if idx := strings.LastIndex(remoteIP, ":"); idx > -1 {
remoteIP = remoteIP[:idx]
}
fmt.Fprintf(accessLog, "%s - - [%s] \"GET /instantws?%s HTTP/1.1\" %d -\n",
remoteIP, time.Now().Format("02/Jan/2006:15:04:05 -0700"), q.Query, responseCode)
}
lastseen := -1
for {
message, sequence := getEvent(identifier, lastseen)
lastseen = sequence
// This message was obsoleted by a more recent one, e.g. a more
// recent progress update obsoletes all earlier progress updates.
if *message.obsolete {
continue
}
if len(message.data) == 0 {
// TODO: tell the client that a new query can be sent
break
}
written, err := ws.Write(message.data)
if err != nil {
log.Printf("[%s] Error writing to websocket, closing: %v\n", src, err)
return
}
if written != len(message.data) {
log.Printf("[%s] Could only write %d of %d bytes to websocket, closing.\n", src, written, len(message.data))
return
}
}
log.Printf("[%s] query done. waiting for a new one\n", src)
}
}
func ResultsHandler(w http.ResponseWriter, r *http.Request) {
// TODO: ideally, this would also start the search in the background to avoid waiting for the round-trip to the client.
// Try to match /page_n.json or /perpackage_2_page_n.json
matches := resultsPathRe.FindStringSubmatch(r.URL.Path)
log.Printf("matches for %q = %v\n", r.URL.Path, matches)
if matches == nil || len(matches) != 4 {
// See whether it’s /packages.json, then.
matches = packagesPathRe.FindStringSubmatch(r.URL.Path)
if matches == nil || len(matches) != 2 {
matches = redirectPathRe.FindStringSubmatch(r.URL.Path)
if len(matches) < 3 {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
pageSuffix := "&page=" + matches[2]
if matches[2] == "0" {
pageSuffix = ""
}
http.Redirect(w, r, "/search?q="+matches[1]+pageSuffix, http.StatusFound)
return
}
queryid := matches[1]
_, ok := state[queryid]
if !ok {
http.Error(w, "No such query.", http.StatusNotFound)
return
}
startJsonResponse(w)
packages := state[queryid].allPackagesSorted
if err := json.NewEncoder(w).Encode(struct{ Packages []string }{packages}); err != nil {
http.Error(w, fmt.Sprintf("Could not encode packages: %v", err), http.StatusInternalServerError)
}
return
}
queryid := matches[1]
page, err := strconv.Atoi(matches[3])
if err != nil {
log.Fatalf("Could not convert %q into a number: %v\n", matches[3], err)
}
perpackage := (matches[2] == "perpackage_2_")
_, ok := state[queryid]
if !ok {
http.Error(w, "No such query.", http.StatusNotFound)
return
}
if !perpackage {
err = writeResults(queryid, page, w, w, r)
} else {
err = writePerPkgResults(queryid, page, w, w, r)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
flag.Parse()
common.Init(*tlsCertPath, *tlsKeyPath, *staticPath)
if *accessLogPath != "" {
var err error
accessLog, err = os.OpenFile(*accessLogPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
}
if *clickLogPath != "" {
var err error
clickLog, err = os.OpenFile(*clickLogPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("Debian Code Search webapp")
health.StartChecking()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Check if a static file was requested with full name
name := filepath.Join(*staticPath, r.URL.Path)
if r.URL.Path == "/" {
name = filepath.Join(*staticPath, "index.html")
}
if _, err := os.Stat(name); err == nil {
http.ServeFile(w, r, name)
return
}
// Or maybe /faq, which resolves to /faq.html
name = name + ".html"
if _, err := os.Stat(name); err == nil {
http.ServeFile(w, r, name)
return
}
if err := common.Templates.ExecuteTemplate(w, "index.html", map[string]interface{}{
"criticalcss": common.CriticalCss,
"version": common.Version,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/favicon.ico", http.NotFound)
http.HandleFunc("/goroutinez", goroutinez.Goroutinez)
http.HandleFunc("/search", Search)
http.HandleFunc("/show", show.Show)
http.HandleFunc("/memprof", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("writing memprof")
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}
})
http.HandleFunc("/results/", ResultsHandler)
http.HandleFunc("/perpackage-results/", PerPackageResultsHandler)
http.HandleFunc("/queryz", QueryzHandler)
http.HandleFunc("/track", Track)
http.HandleFunc("/events/", EventsHandler)
// Used by the service worker.
http.HandleFunc("/placeholder.html", func(w http.ResponseWriter, r *http.Request) {
if err := common.Templates.ExecuteTemplate(w, "placeholder.html", map[string]interface{}{
"criticalcss": common.CriticalCss,
"version": common.Version,
"q": "%q%",
"q_escaped": "%q_escaped%",
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.Handle("/instantws", websocket.Handler(InstantServer))
http.Handle("/metrics", prometheus.Handler())
if *listenAddressTLS != "" {
go log.Fatal(http.ListenAndServeTLS(*listenAddressTLS, *tlsCertPath, *tlsKeyPath, nil))
}
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}