Debian / debiman

debiman generates a static manpage HTML repository out of a Debian archive
Apache License 2.0
188 stars 46 forks source link

Search box does not work #87

Closed bdrung closed 7 years ago

bdrung commented 7 years ago

Go to http://example.com/man/ and enter a name in the search box right to the text "Directly jump to manpage". This will lead to /man/jump, but this file does not exist on the server:

The requested URL /man/jump was not found on this server.

I haven't found documentation how to make the search work.

stapelberg commented 7 years ago

Which webserver are you using? I’m assuming you’ve seen https://github.com/Debian/debiman/tree/master/example.

Have your webserver strip the /man prefix, then reverse proxy the request to the auxserver.

Let me know whether this works out.

bdrung commented 7 years ago

I used

    <Directory /srv/man>
        Require all granted

        # To set the correct Content-Type (e.g. text/html).
        RemoveType .gz
        AddEncoding gzip gz
        FilterDeclare gzip CONTENT_SET
        FilterProvider gzip inflate "%{req:Accept-Encoding} !~ /gzip,.*gzip/"
        FilterChain gzip
        Options +Multiviews
</Directory>

from https://github.com/Debian/debiman/tree/master/example. So I also need

    <Location /auxserver/>
        ProxyPass "http://localhost:2431/"
        ProxyPassReverse "http://localhost:2431/"
    </Location>

        ErrorDocument 404 /auxserver/%{REQUEST_URI}?%{QUERY_STRING}

and debiman-auxserver running on the localhost:2431?

stapelberg commented 7 years ago

auxserver, not auxproxy. But yes, plus the appropriate config to strip the /man prefix first (as auxserver expects an absolute URL).

I only configured nginx, so I can’t help out with an apache syntax fragment right now. Let me know if you can’t get it to work and I can take a look in the evening.

bdrung commented 7 years ago

I added the apache settings and tweaked auxserver:

diff --git a/cmd/debiman-auxserver/aux.go b/cmd/debiman-auxserver/aux.go
index d8e6c33..1bc4c33 100644
--- a/cmd/debiman-auxserver/aux.go
+++ b/cmd/debiman-auxserver/aux.go
@@ -88,6 +88,10 @@ func main() {
        http.HandleFunc("/suggest", server.HandleSuggest)
        http.HandleFunc("/", server.HandleRedirect)

+       http.HandleFunc("/man/jump", server.HandleJump)
+       http.HandleFunc("/man/suggest", server.HandleSuggest)
+       http.HandleFunc("/man/", server.HandleRedirect)
+
        log.Printf("Loaded %d manpage entries, %d suites, %d languages from index %q",
                len(idx.Entries), len(idx.Suites), len(idx.Langs), *indexPath)

diff --git a/internal/aux/aux.go b/internal/aux/aux.go
index cf38923..e3a243d 100644
--- a/internal/aux/aux.go
+++ b/internal/aux/aux.go
@@ -11,6 +11,7 @@ import (
        "sort"
        "strings"
        "sync"
+       "log"

        "github.com/Debian/debiman/internal/manpage"
        "github.com/Debian/debiman/internal/redirect"
@@ -80,6 +81,7 @@ func (s *Server) redirect(r *http.Request) (string, error) {
 }

 func (s *Server) HandleRedirect(w http.ResponseWriter, r *http.Request) {
+       log.Printf("HandleRedirect: %q", *r)
        redir, err := s.redirect(r)
        if err != nil {
                if nf, ok := err.(*redirect.NotFoundError); ok {
@@ -115,10 +117,11 @@ func (s *Server) HandleRedirect(w http.ResponseWriter, r *http.Request) {
        // StatusTemporaryRedirect (HTTP 307) means subsequent requests
        // should use the old URI, which is what we want — the redirect
        // target will likely change in the future.
-       http.Redirect(w, r, redir, http.StatusTemporaryRedirect)
+       http.Redirect(w, r, "/man" + redir, http.StatusTemporaryRedirect)
 }

 func (s *Server) HandleJump(w http.ResponseWriter, r *http.Request) {
+       log.Printf("HandleJump: %q", *r)
        q := r.FormValue("q")
        if strings.TrimSpace(q) == "" {
                http.Error(w, "No q= query parameter specified", http.StatusBadRequest)
@@ -153,6 +156,7 @@ func (s *Server) suggest(q string) []string {
 }

 func (s *Server) HandleSuggest(w http.ResponseWriter, r *http.Request) {
+       log.Printf("HandleSuggest: %q", *r)
        q := r.FormValue("q")
        if strings.TrimSpace(q) == "" {
                http.Error(w, "No q= query parameter specified", http.StatusBadRequest)

Would be nice if auxserver would get a parameter for this prefix.

stapelberg commented 7 years ago

…why not remove the path prefix in Apache? That should work without any code changes.

bdrung commented 7 years ago

How do I add the path again for the http.Redirect response?

stapelberg commented 7 years ago

Perhaps https://serverfault.com/a/690555 helps, if https://serverfault.com/q/561892 doesn’t work.

bdrung commented 7 years ago

When using

        ProxyPass /auxserver/man/ "http://localhost:2431/"

I can use an unmodified cmd/debiman-auxserver/aux.go, but the ProxyPassReverse is not used by the redirect:

$ wget http://localhost:2431/jump?q=sl    
--2017-08-14 13:43:20--  http://localhost:2431/jump?q=sl
Resolving localhost (localhost)... ::1, 127.0.0.1                     
Connecting to localhost (localhost)|::1|:2431... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:2431... connected.
HTTP request sent, awaiting response... 307 Temporary Redirect
Location: /stretch/sl/sl.6.en.html [following]                        
stapelberg commented 7 years ago

The following definition works for me with Apache 2.4.27 to serve manpages under the sub directory /sub:

<VirtualHost mansub.localhost:3080>
    ServerName mansub.localhost

    ServerAdmin webmaster@localhost
    DocumentRoot /srv/

    LogLevel alert

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Add gzip to the Accept-Encoding to prevent apache from serving an
    # HTTP 406 Not Acceptable response. We keep the original
    # Accept-Encoding value and later on use mod_deflate to uncompress if
    # necessary.
    RequestHeader set Accept-Encoding "expr=gzip,%{req:Accept-Encoding}" early

    ExpiresActive On
    ExpiresDefault "access plus 1 hours"

    <Files ~ "^rwmap">
        Order allow,deny
        Deny from all
    </Files>

    <Location /sub/auxserver/>
        ProxyPass "http://localhost:2431/"
        ProxyPassReverse "http://localhost:2431/"
        Header edit Location / /sub/
    </Location>

    <Location /sub>
        RewriteEngine on
        RewriteCond %{REQUEST_URI} "^/sub/(.*)"
        RewriteRule .* - [E=REWRITTEN_URI:%1]
        ErrorDocument 404 /sub/auxserver/%{env:REWRITTEN_URI}?%{QUERY_STRING}
    </Location>

    Alias "/sub" "/srv/man"
    <Directory /srv/man>
        Require all granted

        # To set the correct Content-Type (e.g. text/html).
        RemoveType .gz
        AddEncoding gzip gz
        FilterDeclare gzip CONTENT_SET
        FilterProvider gzip inflate "%{req:Accept-Encoding} !~ /gzip,.*gzip/"
        FilterChain gzip
        Options +Multiviews
    </Directory>

</VirtualHost>

For completeness, the following configuration works for me with nginx 1.13.3:

server {
        listen 127.0.0.5:80;

        root /srv;

        keepalive_requests 10000;

        expires 1h;

        location /sub { 
                # We cannot use try_files because then gzip_static always will
                # not be effective anymore.
                rewrite (.*)/$ $1/index.html;

                rewrite /sub/(.*) /$1  break;

                # We only have gzip-compressed files:
                gzip_static always;

                # Uncompress files for clients which do not support gzip:
                gunzip on;

                root /srv/man; 
                error_page 404 = @auxserver;
        }

        location @auxserver {
                proxy_pass http://localhost:2431;
                proxy_redirect / /sub/;
        }
}
stapelberg commented 7 years ago

Actually, since we needed to add the -base_url flag to have correct URLs in the CSS, I decided to add path stripping/adding within debiman-auxserver in commit https://github.com/Debian/debiman/commit/35bbbaed1d14f491b4db5101979b7378d581695a

Hence, no rewriting is required in Apache/nginx anymore.

bdrung commented 7 years ago

Thanks for the example apache/nginx config and for adding -base_url to debiman-auxserver.