| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package server |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"crypto/rand" |
| 12 |
"embed" |
| 13 |
"encoding/base64" |
| 14 |
"fmt" |
| 15 |
"io" |
| 16 |
"io/fs" |
| 17 |
"log" |
| 18 |
"net" |
| 19 |
"net/http" |
| 20 |
"net/url" |
| 21 |
"os" |
| 22 |
paths "path" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"sync/atomic" |
| 27 |
|
| 28 |
"golang.org/x/tools/gopls/internal/cache" |
| 29 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 30 |
"golang.org/x/tools/gopls/internal/filewatcher" |
| 31 |
"golang.org/x/tools/gopls/internal/golang" |
| 32 |
"golang.org/x/tools/gopls/internal/golang/splitpkg" |
| 33 |
"golang.org/x/tools/gopls/internal/progress" |
| 34 |
"golang.org/x/tools/gopls/internal/protocol" |
| 35 |
"golang.org/x/tools/gopls/internal/settings" |
| 36 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 37 |
"golang.org/x/tools/internal/event" |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func New(session *cache.Session, client protocol.ClientCloser, options *settings.Options) protocol.Server { |
| 43 |
const concurrentAnalyses = 1 |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
return &server{ |
| 48 |
diagnostics: make(map[protocol.DocumentURI]*fileDiagnostics), |
| 49 |
watchedGlobPatterns: nil, |
| 50 |
changedFiles: make(map[protocol.DocumentURI]unit), |
| 51 |
session: session, |
| 52 |
client: client, |
| 53 |
diagnosticsSema: make(chan unit, concurrentAnalyses), |
| 54 |
progress: progress.NewTracker(client), |
| 55 |
options: options, |
| 56 |
viewsToDiagnose: make(map[*cache.View]uint64), |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
type serverState int |
| 61 |
|
| 62 |
const ( |
| 63 |
serverCreated = serverState(iota) |
| 64 |
serverInitializing |
| 65 |
serverInitialized |
| 66 |
serverShutDown |
| 67 |
) |
| 68 |
|
| 69 |
func (s serverState) String() string { |
| 70 |
switch s { |
| 71 |
case serverCreated: |
| 72 |
return "created" |
| 73 |
case serverInitializing: |
| 74 |
return "initializing" |
| 75 |
case serverInitialized: |
| 76 |
return "initialized" |
| 77 |
case serverShutDown: |
| 78 |
return "shutDown" |
| 79 |
} |
| 80 |
return fmt.Sprintf("(unknown state: %d)", int(s)) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
type server struct { |
| 89 |
client protocol.ClientCloser |
| 90 |
|
| 91 |
stateMu sync.Mutex |
| 92 |
state serverState |
| 93 |
|
| 94 |
notifications []*protocol.ShowMessageParams |
| 95 |
|
| 96 |
session *cache.Session |
| 97 |
|
| 98 |
|
| 99 |
changedFilesMu sync.Mutex |
| 100 |
changedFiles map[protocol.DocumentURI]unit |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pendingFolders []protocol.WorkspaceFolder |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
watchedGlobPatternsMu sync.Mutex |
| 112 |
watchedGlobPatterns map[protocol.RelativePattern]unit |
| 113 |
watchRegistrationCount int |
| 114 |
|
| 115 |
fileWatcherMu sync.Mutex |
| 116 |
fileWatcher filewatcher.Watcher |
| 117 |
|
| 118 |
diagnosticsMu sync.Mutex |
| 119 |
diagnostics map[protocol.DocumentURI]*fileDiagnostics |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
diagnosticsSema chan unit |
| 124 |
|
| 125 |
progress *progress.Tracker |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
criticalErrorStatusMu sync.Mutex |
| 130 |
criticalErrorStatus *progress.WorkDone |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
ongoingProfileMu sync.Mutex |
| 135 |
ongoingProfile *os.File |
| 136 |
|
| 137 |
|
| 138 |
optionsMu sync.Mutex |
| 139 |
options *settings.Options |
| 140 |
|
| 141 |
|
| 142 |
efficacyMu sync.Mutex |
| 143 |
efficacyURI protocol.DocumentURI |
| 144 |
efficacyVersion int32 |
| 145 |
efficacyItems []protocol.CompletionItem |
| 146 |
efficacyPos protocol.Position |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
webOnce sync.Once |
| 151 |
web *web |
| 152 |
webErr error |
| 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 |
modificationMu sync.Mutex |
| 188 |
cancelPrevDiagnostics func() |
| 189 |
viewsToDiagnose map[*cache.View]uint64 |
| 190 |
lastModificationID uint64 |
| 191 |
|
| 192 |
runGovulncheckInProgress atomic.Bool |
| 193 |
} |
| 194 |
|
| 195 |
func (s *server) WorkDoneProgressCancel(ctx context.Context, params *protocol.WorkDoneProgressCancelParams) error { |
| 196 |
ctx, done := event.Start(ctx, "server.WorkDoneProgressCancel") |
| 197 |
defer done() |
| 198 |
|
| 199 |
return s.progress.Cancel(params.Token) |
| 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 |
type web struct { |
| 226 |
server *http.Server |
| 227 |
addr url.URL |
| 228 |
mux *http.ServeMux |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
func (s *server) getWeb() (*web, error) { |
| 234 |
s.webOnce.Do(func() { |
| 235 |
s.web, s.webErr = s.initWeb() |
| 236 |
}) |
| 237 |
return s.web, s.webErr |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func (s *server) initWeb() (*web, error) { |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
token := make([]byte, 8) |
| 255 |
if _, err := rand.Read(token); err != nil { |
| 256 |
return nil, fmt.Errorf("generating secret token: %v", err) |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 261 |
if err != nil { |
| 262 |
return nil, err |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
rootMux := http.NewServeMux() |
| 269 |
rootMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { |
| 270 |
http.Error(w, "request URI lacks authentication segment", http.StatusUnauthorized) |
| 271 |
}) |
| 272 |
rootMux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, req *http.Request) { |
| 273 |
http.Redirect(w, req, "/assets/favicon.ico", http.StatusMovedPermanently) |
| 274 |
}) |
| 275 |
rootMux.HandleFunc("/hang", func(w http.ResponseWriter, req *http.Request) { |
| 276 |
|
| 277 |
|
| 278 |
<-req.Context().Done() |
| 279 |
}) |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
fs := fs.FS(assets) |
| 284 |
|
| 285 |
rootMux.Handle("/assets/", http.FileServer(http.FS(fs))) |
| 286 |
|
| 287 |
secret := "/gopls/" + base64.RawURLEncoding.EncodeToString(token) |
| 288 |
webMux := http.NewServeMux() |
| 289 |
rootMux.Handle(secret+"/", withPanicHandler(http.StripPrefix(secret, webMux))) |
| 290 |
|
| 291 |
webServer := &http.Server{Addr: listener.Addr().String(), Handler: rootMux} |
| 292 |
go func() { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
if err := webServer.Serve(listener); err != nil { |
| 297 |
if err != http.ErrServerClosed { |
| 298 |
log.Print(err) |
| 299 |
} |
| 300 |
} |
| 301 |
}() |
| 302 |
|
| 303 |
web := &web{ |
| 304 |
server: webServer, |
| 305 |
addr: url.URL{Scheme: "http", Host: webServer.Addr, Path: secret}, |
| 306 |
mux: webMux, |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
webMux.HandleFunc("/src", func(w http.ResponseWriter, req *http.Request) { |
| 312 |
if err := req.ParseForm(); err != nil { |
| 313 |
http.Error(w, err.Error(), http.StatusBadRequest) |
| 314 |
return |
| 315 |
} |
| 316 |
uri := protocol.URIFromPath(req.Form.Get("file")) |
| 317 |
line, _ := strconv.Atoi(req.Form.Get("line")) |
| 318 |
col, _ := strconv.Atoi(req.Form.Get("col")) |
| 319 |
posn := protocol.Position{ |
| 320 |
Line: uint32(line - 1), |
| 321 |
Character: uint32(col - 1), |
| 322 |
} |
| 323 |
openClientEditor(req.Context(), s.client, protocol.Location{ |
| 324 |
URI: uri, |
| 325 |
Range: protocol.Range{Start: posn, End: posn}, |
| 326 |
}, s.Options()) |
| 327 |
}) |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
getSnapshot := func(w http.ResponseWriter, req *http.Request) (*cache.Snapshot, func(), bool) { |
| 334 |
if err := req.ParseForm(); err != nil { |
| 335 |
http.Error(w, err.Error(), http.StatusBadRequest) |
| 336 |
return nil, nil, false |
| 337 |
} |
| 338 |
viewID := req.Form.Get("view") |
| 339 |
if viewID == "" { |
| 340 |
http.Error(w, "no view=... parameter", http.StatusBadRequest) |
| 341 |
return nil, nil, false |
| 342 |
} |
| 343 |
view, err := s.session.View(viewID) |
| 344 |
if err != nil { |
| 345 |
http.Error(w, err.Error(), http.StatusNotFound) |
| 346 |
return nil, nil, false |
| 347 |
} |
| 348 |
snapshot, release, err := view.Snapshot() |
| 349 |
if err != nil { |
| 350 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 351 |
return nil, nil, false |
| 352 |
} |
| 353 |
return snapshot, release, true |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
webMux.Handle("/pkg/", http.StripPrefix("/pkg/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 358 |
snapshot, release, ok := getSnapshot(w, req) |
| 359 |
if !ok { |
| 360 |
return |
| 361 |
} |
| 362 |
defer release() |
| 363 |
|
| 364 |
|
| 365 |
pkgPath := metadata.PackagePath(req.URL.Path) |
| 366 |
mps := snapshot.MetadataGraph().ForPackagePath[pkgPath] |
| 367 |
if len(mps) == 0 { |
| 368 |
|
| 369 |
http.Error(w, "package not found", http.StatusNotFound) |
| 370 |
return |
| 371 |
} |
| 372 |
found := mps[0] |
| 373 |
|
| 374 |
|
| 375 |
pkgs, err := snapshot.TypeCheck(req.Context(), found.ID) |
| 376 |
if err != nil { |
| 377 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 378 |
return |
| 379 |
} |
| 380 |
content, err := golang.PackageDocHTML(snapshot.View().ID(), pkgs[0], web) |
| 381 |
if err != nil { |
| 382 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 383 |
return |
| 384 |
} |
| 385 |
w.Write(content) |
| 386 |
}))) |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
webMux.HandleFunc("/freesymbols", func(w http.ResponseWriter, req *http.Request) { |
| 391 |
snapshot, release, ok := getSnapshot(w, req) |
| 392 |
if !ok { |
| 393 |
return |
| 394 |
} |
| 395 |
defer release() |
| 396 |
|
| 397 |
|
| 398 |
loc := protocol.Location{ |
| 399 |
URI: protocol.DocumentURI(req.Form.Get("file")), |
| 400 |
} |
| 401 |
if _, err := fmt.Sscanf(req.Form.Get("range"), "%d:%d:%d:%d", |
| 402 |
&loc.Range.Start.Line, |
| 403 |
&loc.Range.Start.Character, |
| 404 |
&loc.Range.End.Line, |
| 405 |
&loc.Range.End.Character, |
| 406 |
); err != nil { |
| 407 |
http.Error(w, "invalid range", http.StatusInternalServerError) |
| 408 |
return |
| 409 |
} |
| 410 |
pkg, pgf, err := golang.NarrowestPackageForFile(req.Context(), snapshot, loc.URI) |
| 411 |
if err != nil { |
| 412 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 413 |
return |
| 414 |
} |
| 415 |
start, end, err := pgf.RangePos(loc.Range) |
| 416 |
if err != nil { |
| 417 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 418 |
return |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
html := golang.FreeSymbolsHTML(snapshot.View().ID(), pkg, pgf, start, end, web) |
| 423 |
w.Write(html) |
| 424 |
}) |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
webMux.HandleFunc("/assembly", func(w http.ResponseWriter, req *http.Request) { |
| 429 |
snapshot, release, ok := getSnapshot(w, req) |
| 430 |
if !ok { |
| 431 |
return |
| 432 |
} |
| 433 |
defer release() |
| 434 |
|
| 435 |
|
| 436 |
var ( |
| 437 |
pkgID = metadata.PackageID(req.Form.Get("pkg")) |
| 438 |
symbol = req.Form.Get("symbol") |
| 439 |
) |
| 440 |
if pkgID == "" || symbol == "" { |
| 441 |
http.Error(w, "/assembly requires pkg, symbol", http.StatusBadRequest) |
| 442 |
return |
| 443 |
} |
| 444 |
|
| 445 |
ctx := req.Context() |
| 446 |
pkgs, err := snapshot.TypeCheck(ctx, pkgID) |
| 447 |
if err != nil { |
| 448 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 449 |
return |
| 450 |
} |
| 451 |
pkg := pkgs[0] |
| 452 |
|
| 453 |
|
| 454 |
golang.AssemblyHTML(ctx, snapshot, w, pkg, symbol, web) |
| 455 |
}) |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
webMux.HandleFunc("/splitpkg", func(w http.ResponseWriter, req *http.Request) { |
| 460 |
snapshot, release, ok := getSnapshot(w, req) |
| 461 |
if !ok { |
| 462 |
return |
| 463 |
} |
| 464 |
defer release() |
| 465 |
|
| 466 |
|
| 467 |
pkgID := metadata.PackageID(req.Form.Get("pkg")) |
| 468 |
if pkgID == "" { |
| 469 |
http.Error(w, "/splitpkg requires pkg", http.StatusBadRequest) |
| 470 |
return |
| 471 |
} |
| 472 |
mp := snapshot.Metadata(pkgID) |
| 473 |
if mp == nil { |
| 474 |
http.Error(w, "no such package: "+string(pkgID), http.StatusInternalServerError) |
| 475 |
return |
| 476 |
} |
| 477 |
|
| 478 |
w.Write(splitpkg.HTML(mp.PkgPath)) |
| 479 |
}) |
| 480 |
|
| 481 |
|
| 482 |
webMux.HandleFunc("/splitpkg-json", func(w http.ResponseWriter, req *http.Request) { |
| 483 |
snapshot, release, ok := getSnapshot(w, req) |
| 484 |
if !ok { |
| 485 |
return |
| 486 |
} |
| 487 |
defer release() |
| 488 |
|
| 489 |
|
| 490 |
pkgID := metadata.PackageID(req.Form.Get("pkg")) |
| 491 |
if pkgID == "" { |
| 492 |
http.Error(w, "/splitpkg-json requires pkg", http.StatusBadRequest) |
| 493 |
return |
| 494 |
} |
| 495 |
pkgs, err := snapshot.TypeCheck(req.Context(), pkgID) |
| 496 |
if err != nil { |
| 497 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 498 |
return |
| 499 |
} |
| 500 |
pkg := pkgs[0] |
| 501 |
|
| 502 |
data, err := splitpkg.JSON(pkg, web) |
| 503 |
if err != nil { |
| 504 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 505 |
return |
| 506 |
} |
| 507 |
w.Write(data) |
| 508 |
}) |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
webMux.HandleFunc("/splitpkg-components", func(w http.ResponseWriter, req *http.Request) { |
| 514 |
snapshot, release, ok := getSnapshot(w, req) |
| 515 |
if !ok { |
| 516 |
return |
| 517 |
} |
| 518 |
defer release() |
| 519 |
|
| 520 |
|
| 521 |
pkgID := metadata.PackageID(req.Form.Get("pkg")) |
| 522 |
if pkgID == "" { |
| 523 |
http.Error(w, "/splitpkg-components requires pkg", http.StatusBadRequest) |
| 524 |
return |
| 525 |
} |
| 526 |
mp := snapshot.Metadata(pkgID) |
| 527 |
if mp == nil { |
| 528 |
http.Error(w, "no such package: "+string(pkgID), http.StatusInternalServerError) |
| 529 |
return |
| 530 |
} |
| 531 |
|
| 532 |
data, err := io.ReadAll(req.Body) |
| 533 |
if err != nil { |
| 534 |
msg := fmt.Sprintf("reading request body: %v", err) |
| 535 |
http.Error(w, msg, http.StatusBadRequest) |
| 536 |
return |
| 537 |
} |
| 538 |
|
| 539 |
if err := splitpkg.UpdateComponentsJSON(pkgID, data); err != nil { |
| 540 |
http.Error(w, err.Error(), http.StatusBadRequest) |
| 541 |
return |
| 542 |
} |
| 543 |
}) |
| 544 |
|
| 545 |
return web, nil |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
var assets embed.FS |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
func (w *web) SrcURL(filename string, line, col8 int) protocol.URI { |
| 561 |
query := fmt.Sprintf("file=%s&line=%d&col=%d", |
| 562 |
url.QueryEscape(filename), |
| 563 |
line, |
| 564 |
col8) |
| 565 |
return w.url("src", query, "") |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
func (w *web) PkgURL(viewID string, path golang.PackagePath, fragment string) protocol.URI { |
| 571 |
query := "view=" + url.QueryEscape(viewID) |
| 572 |
return w.url("pkg/"+string(path), query, fragment) |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
func (w *web) freesymbolsURL(viewID string, loc protocol.Location) protocol.URI { |
| 578 |
query := fmt.Sprintf("file=%s&range=%d:%d:%d:%d&view=%s", |
| 579 |
url.QueryEscape(string(loc.URI)), |
| 580 |
loc.Range.Start.Line, |
| 581 |
loc.Range.Start.Character, |
| 582 |
loc.Range.End.Line, |
| 583 |
loc.Range.End.Character, |
| 584 |
url.QueryEscape(viewID)) |
| 585 |
return w.url("freesymbols", query, "") |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
func (w *web) assemblyURL(viewID, packageID, symbol string) protocol.URI { |
| 590 |
query := fmt.Sprintf("view=%s&pkg=%s&symbol=%s", |
| 591 |
url.QueryEscape(viewID), |
| 592 |
url.QueryEscape(packageID), |
| 593 |
url.QueryEscape(symbol)) |
| 594 |
return w.url("assembly", query, "") |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
func (w *web) splitpkgURL(viewID, packageID string) protocol.URI { |
| 599 |
query := fmt.Sprintf("view=%s&pkg=%s", |
| 600 |
url.QueryEscape(viewID), |
| 601 |
url.QueryEscape(packageID)) |
| 602 |
return w.url("splitpkg", query, "") |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
func (w *web) url(path, query, fragment string) protocol.URI { |
| 609 |
url2 := w.addr |
| 610 |
url2.Path = paths.Join(url2.Path, strings.TrimPrefix(path, "/")) |
| 611 |
url2.RawQuery = query |
| 612 |
url2.Fragment = fragment |
| 613 |
return protocol.URI(url2.String()) |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
func withPanicHandler(h http.Handler) http.HandlerFunc { |
| 620 |
return func(w http.ResponseWriter, req *http.Request) { |
| 621 |
panicked := true |
| 622 |
defer func() { |
| 623 |
if panicked { |
| 624 |
bug.Report("panic in HTTP handler") |
| 625 |
} |
| 626 |
}() |
| 627 |
h.ServeHTTP(w, req) |
| 628 |
panicked = false |
| 629 |
} |
| 630 |
} |