package server import ( "context" "errors" "html/template" "net/http" "net/url" "path" "strings" "time" "unicode" "golang.org/x/net/html" "pageswiki/internal/store" ) type searchViewResult struct { store.SearchResult Name string Href string NameParts []searchTextPart PathParts []searchTextPart ExcerptParts []searchTextPart PreviewHTML template.HTML Updated string } type searchTextPart struct { Text string Match bool } // Highlight only creates text segments. html/template, not source text or // SQLite markers, owns every markup tag in the rendered result. func highlightSearchText(value, query string) []searchTextPart { runes := []rune(value) lower := []rune(strings.ToLower(value)) matched := make([]bool, len(runes)) for _, word := range strings.Fields(strings.ToLower(query)) { needle := []rune(word) for i := 0; len(needle) > 0 && i+len(needle) <= len(lower); i++ { if string(lower[i:i+len(needle)]) == word { for j := i; j < i+len(needle); j++ { matched[j] = true } } } } var parts []searchTextPart for start := 0; start < len(runes); { end := start + 1 for end < len(runes) && matched[end] == matched[start] { end++ } parts = append(parts, searchTextPart{Text: string(runes[start:end]), Match: matched[start]}) start = end } return parts } func searchExcerpt(raw, contentType, query string) string { if strings.HasPrefix(strings.ToLower(contentType), "text/html") { tokenizer := html.NewTokenizer(strings.NewReader(raw)) var text strings.Builder skip := "" for { kind := tokenizer.Next() if kind == html.ErrorToken { break } token := tokenizer.Token() if kind == html.StartTagToken && (token.Data == "script" || token.Data == "style") { skip = token.Data continue } if kind == html.EndTagToken && token.Data == skip { skip = "" continue } if skip != "" { continue } if kind == html.TextToken { text.WriteString(token.Data) } else { text.WriteByte(' ') } } raw = text.String() } raw = strings.Map(func(r rune) rune { if unicode.IsControl(r) && !unicode.IsSpace(r) { return -1 } return r }, raw) runes := []rune(strings.Join(strings.Fields(raw), " ")) if len(runes) <= 260 { return string(runes) } start := 0 lower := []rune(strings.ToLower(string(runes))) for _, term := range strings.Fields(strings.ToLower(query)) { if index := strings.Index(string(lower), term); index >= 0 { position := len([]rune(string(lower)[:index])) start = max(0, position-80) break } } start = min(start, len(runes)-260) result := string(runes[start : start+260]) if start > 0 { result = "… " + result } if start+260 < len(runes) { result += " …" } return result } func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-PagesWiki-Search-Profile", s.store.SearchProfile()) if r.Method != http.MethodGet && r.Method != http.MethodHead { w.Header().Set("Allow", "GET, HEAD") http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } query := strings.TrimSpace(r.URL.Query().Get("q")) if len(query) > 512 || len(strings.Fields(query)) > 16 { http.Error(w, translate(s.getLocale(s.domainUUIDForRequest(r)), "search.query_too_long"), http.StatusBadRequest) return } scope := strings.TrimSpace(r.URL.Query().Get("scope")) localityKind := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("locality"))) localityID := strings.TrimSpace(r.URL.Query().Get("locality_id")) if localityKind != "" { scope = "/" if localityKind != "domain" { node, err := s.store.CityNodeContext(r.Context(), s.domainUUIDForRequest(r), localityKind, localityID) if err != nil || !canReadCityNode(node, s.pathReadFilter(r)) { http.Error(w, "unknown City locality", http.StatusBadRequest) return } scope = node.PathPrefix } } if scope == "" { scope = "/" } scope = path.Clean("/" + strings.TrimLeft(scope, "/")) allowed := s.pathReadFilter(r) if !allowed(scope) { http.Error(w, "forbidden", http.StatusForbidden) return } domain := s.domainUUIDForRequest(r) location := s.getLocation(domain) options, filters, err := parseSearchOptions(r.URL.Query(), location) if err != nil { s.renderSearchFailure(w, r, query, scope, localityKind, localityID, err) return } performed := query != "" || options.HasFilters() var results []searchViewResult var cityResults []searchCityViewResult if performed { // City metadata and Page search share the original request budget. Do // not build the full City graph or traverse its Page placements here. ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) defer cancel() city, err := s.searchEngine().SearchCityInDomain(ctx, domain, scope, query, 30, allowed, options) if err != nil { s.renderSearchFailure(w, r, query, scope, localityKind, localityID, err) return } for _, result := range city { cityResults = append(cityResults, citySearchResultView(result, query, location)) } found, err := s.searchEngine().SearchPathsInDomainWithOptions( ctx, domain, scope, query, 100, allowed, options, ) if err != nil { s.renderSearchFailure(w, r, query, scope, localityKind, localityID, err) return } for _, result := range found { entry := store.BrowserEntry{ Path: result.Path, Filename: result.Filename, ContentType: result.ContentType, Size: int(result.Size), DocumentHash: result.DocumentHash, RevisionUUID: result.RevisionUUID, } name := path.Base(result.Filename) if result.Filename == "" { name = path.Base(result.Path) } results = append(results, searchViewResult{ SearchResult: result, Name: name, Href: (&url.URL{Path: result.Path}).EscapedPath(), NameParts: highlightSearchText(name, query), PathParts: highlightSearchText(result.Path, query), ExcerptParts: highlightSearchText(searchExcerpt(result.Excerpt, result.ContentType, query), query), PreviewHTML: template.HTML(s.entryPreviewHTML(entry, false)), Updated: time.Unix(result.UpdatedAt, 0).In(location).Format("2006-01-02 15:04"), }) } } if r.Context().Err() != nil { return } s.renderWithContext(w, r, "search.gohtml", map[string]any{ "Query": query, "Scope": scope, "Results": results, "CityResults": cityResults, "CityLimitReached": len(cityResults) >= 30, "SearchPerformed": performed, "Filters": filters, "ResetHref": "/v/search?" + searchBaseValues(query, scope, localityKind, localityID).Encode(), "ScopeHref": (&url.URL{Path: scope}).EscapedPath(), "ResultLimitReached": len(results) >= 100, "LocalityKind": localityKind, "LocalityID": localityID, }) } // The store has its own bounded search budget. A timeout there is retryable, // while a canceled HTTP request should not trigger more configuration reads or // template work. Never expose database details or the raw error in the page. func (s *Server) renderSearchFailure(w http.ResponseWriter, r *http.Request, query, scope, localityKind, localityID string, err error) { if r.Context().Err() != nil || errors.Is(err, context.Canceled) { return } status := http.StatusInternalServerError title, hint := "search.failed_title", "search.failed_hint" if errors.Is(err, store.ErrInvalidSearchOptions) { status = http.StatusBadRequest title, hint = "search.invalid_filters_title", "search.invalid_filters_hint" } else if errors.Is(err, context.DeadlineExceeded) { status = http.StatusServiceUnavailable title, hint = "search.timeout_title", "search.timeout_hint" w.Header().Set("Retry-After", "5") } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Cache-Control", "private, no-store") w.WriteHeader(status) if r.Method == http.MethodHead { return } retry := searchRetryValues(r, query, scope, localityKind, localityID) filters := searchFiltersFromValues(r.URL.Query()) filters.Timezone = s.getTimezoneName(s.domainUUIDForRequest(r)) filters.Open = true s.renderWithContext(w, r, "search.gohtml", map[string]any{ "Query": query, "Scope": scope, "ScopeHref": (&url.URL{Path: scope}).EscapedPath(), "LocalityKind": localityKind, "LocalityID": localityID, "SearchError": true, "SearchErrorTitle": title, "SearchErrorHint": hint, "SearchPerformed": true, "Filters": filters, "ResetHref": "/v/search?" + searchBaseValues(query, scope, localityKind, localityID).Encode(), "RetryHref": "/v/search?" + retry.Encode(), }) }