<commit_before>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"regexp"
	"strings"

	// For triggering manual rebuilds.
	"golang.org/x/oauth2/google"
	"google.golang.org/api/cloudbuild/v1"
)

var redirects = map[string]string{
	"/change":    "https://gvisor.googlesource.com/gvisor/",
	"/cl":        "https://gvisor-review.googlesource.com/",
	"/issue":     "https://github.com/google/gvisor/issues",
	"/issue/new": "https://github.com/google/gvisor/issues/new",

	// Redirects to compatibility docs.
	"/c":             "/docs/user_guide/compatibility",
	"/c/linux/amd64": "/docs/user_guide/compatibility/amd64",
}

var prefixHelpers = map[string]string{
	"cl":     "https://gvisor-review.googlesource.com/c/gvisor/+/%s",
	"change": "https://gvisor.googlesource.com/gvisor/+/%s",
	"issue":  "https://github.com/google/gvisor/issues/%s",

	// Redirects to compatibility docs.
	"c/linux/amd64": "/docs/user_guide/compatibility/amd64/#%s",
}

var (
	validId     = regexp.MustCompile(`^[A-Za-z0-9-]*/?$`)
	goGetHeader = `<meta name="go-import" content="gvisor.dev git https://github.com/google/gvisor">`
	goGetHTML5  = `<!doctype html><html><head><meta charset=utf-8>` + goGetHeader + `<title>Go-get</title></head><body></html>`
)

// wrappedHandler wraps an http.Handler.
//
// If the query parameters include go-get=1, then we redirect to a single
// static page that allows us to serve arbitrary Go packages.
func wrappedHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gg, ok := r.URL.Query()["go-get"]
		if ok && len(gg) == 1 && gg[0] == "1" {
			// Serve a trivial html page.
			w.Write([]byte(goGetHTML5))
			return
		}
		// Fallthrough.
		h.ServeHTTP(w, r)
	})
}

// redirectWithQuery redirects to the given target url preserving query parameters.
func redirectWithQuery(w http.ResponseWriter, r *http.Request, target string) {
	url := target
	if qs := r.URL.RawQuery; qs != "" {
		url += "?" + qs
	}<commit_msg>Add /pr support for links.<commit_after>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"regexp"
	"strings"

	// For triggering manual rebuilds.
	"golang.org/x/oauth2/google"
	"google.golang.org/api/cloudbuild/v1"
)

var redirects = map[string]string{
	"/change":    "https://github.com/google/gvisor",
	"/issue":     "https://github.com/google/gvisor/issues",
	"/issue/new": "https://github.com/google/gvisor/issues/new",
	"/pr":        "https://github.com/google/gvisor/pulls",

	// Redirects to compatibility docs.
	"/c":             "/docs/user_guide/compatibility",
	"/c/linux/amd64": "/docs/user_guide/compatibility/amd64",

	// Deprecated, but links continue to work.
	"/cl": "https://gvisor-review.googlesource.com",
}

var prefixHelpers = map[string]string{
	"change": "https://github.com/google/gvisor/commit/%s",
	"issue":  "https://github.com/google/gvisor/issues/%s",
	"pr":     "https://github.com/google/gvisor/pull/%s",

	// Redirects to compatibility docs.
	"c/linux/amd64": "/docs/user_guide/compatibility/amd64/#%s",

	// Deprecated, but links continue to work.
	"cl": "https://gvisor-review.googlesource.com/c/gvisor/+/%s",
}

var (
	validId     = regexp.MustCompile(`^[A-Za-z0-9-]*/?$`)
	goGetHeader = `<meta name="go-import" content="gvisor.dev/gvisor git https://github.com/google/gvisor">`
	goGetHTML5  = `<!doctype html><html><head><meta charset=utf-8>` + goGetHeader + `<title>Go-get</title></head><body></html>`
)

// wrappedHandler wraps an http.Handler.
//
// If the query parameters include go-get=1, then we redirect to a single
// static page that allows us to serve arbitrary Go packages.
func wrappedHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gg, ok := r.URL.Query()["go-get"]
		if ok && len(gg) == 1 && gg[0] == "1" {
			// Serve a trivial html page.
			w.Write([]byte(goGetHTML5))
			return
		}
		// Fallthrough.
		h.ServeHTTP(w, r)
	})
}

// redirectWithQuery redirects to the given target url preserving query parameters.
func redirectWithQuery(w http.ResponseWriter, r *http.Request, target string) {
	url := target
	if qs := r.URL.RawQuery; qs != "" {
		url += "?" + qs
	}
{'fraction_non_alphanumeric': 0.16444197256210605, 'fraction_numerical': 0.010011123470522803, 'mean_word_length': 3.5642978003384096, 'pattern_counts': {'":': 22, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 20, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7289050', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1805, 'n_words': 476}