<commit_before>package handler

import (
	"errors"
	"net/http"
	"net/http/httputil"

	"github.com/gorilla/mux"

	"github.com/skygeario/skygear-server/pkg/core/cloudstorage"
	coreHttp "github.com/skygeario/skygear-server/pkg/core/http"
	"github.com/skygeario/skygear-server/pkg/core/imageprocessing"
	"github.com/skygeario/skygear-server/pkg/core/inject"
	"github.com/skygeario/skygear-server/pkg/core/server"
)

const (
	QueryNamePipeline = "pipeline"
)

var ErrBadAccess = errors.New("bad access")

func AttachGetHandler(
	server *server.Server,
	dependencyMap inject.DependencyMap,
) *server.Server {
	server.Handle("/get/{asset_name}", &GetHandlerFactory{
		dependencyMap,
	}).Methods("OPTIONS", "HEAD", "GET")
	return server
}

type GetHandlerFactory struct {
	DependencyMap inject.DependencyMap
}

func (f *GetHandlerFactory) NewHandler(request *http.Request) http.Handler {
	h := &GetHandler{}
	inject.DefaultRequestInject(h, f.DependencyMap, request)
	return h
}

/*
	@Operation GET /get/{asset_name} - Retrieve the asset
		Retrieve the asset.

		@Response 200
			The asset.
*/
type GetHandler struct {
	CloudStorageProvider cloudstorage.Provider `dependency:"CloudStorageProvider"`
}

func (h *GetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	originallySigned := false
	pipeline := ""
	hasPipeline := false
	vars := mux.Vars(r)
	assetName := vars["asset_name"]

	director := func(req *http.Request) {
		req.Header = coreHttp.RemoveSkygearHeader(req.Header)

		query := req.URL.Query()
		pipeline = query.Get(QueryNamePipeline)
		_, hasPipeline = query[QueryNamePipeline]
		// Do not support range request if image processing query is present.
		if hasPipeline {
			req.Header.Del("Range")
			req.Header.Del("If-Range")
			query.Del(QueryNamePipeline)
			req.URL.RawQuery = query.Encode()
		}

		// NOTE(louis): The err is ignored here because we have no way to return it.
		// However, this function does not return error normally.
		// The known condition that err could be returned is fail to sign
		// which is a configuration problem.
		u, signed, _ := h.CloudStorageProvider.RewriteGetURL(req.URL, assetName)
		originallySigned = signed
<commit_msg>Use panic instead of ignoring err<commit_after>package handler

import (
	"errors"
	"net/http"
	"net/http/httputil"

	"github.com/gorilla/mux"

	"github.com/skygeario/skygear-server/pkg/core/cloudstorage"
	coreHttp "github.com/skygeario/skygear-server/pkg/core/http"
	"github.com/skygeario/skygear-server/pkg/core/imageprocessing"
	"github.com/skygeario/skygear-server/pkg/core/inject"
	"github.com/skygeario/skygear-server/pkg/core/server"
)

const (
	QueryNamePipeline = "pipeline"
)

var ErrBadAccess = errors.New("bad access")

func AttachGetHandler(
	server *server.Server,
	dependencyMap inject.DependencyMap,
) *server.Server {
	server.Handle("/get/{asset_name}", &GetHandlerFactory{
		dependencyMap,
	}).Methods("OPTIONS", "HEAD", "GET")
	return server
}

type GetHandlerFactory struct {
	DependencyMap inject.DependencyMap
}

func (f *GetHandlerFactory) NewHandler(request *http.Request) http.Handler {
	h := &GetHandler{}
	inject.DefaultRequestInject(h, f.DependencyMap, request)
	return h
}

/*
	@Operation GET /get/{asset_name} - Retrieve the asset
		Retrieve the asset.

		@Response 200
			The asset.
*/
type GetHandler struct {
	CloudStorageProvider cloudstorage.Provider `dependency:"CloudStorageProvider"`
}

func (h *GetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	originallySigned := false
	pipeline := ""
	hasPipeline := false
	vars := mux.Vars(r)
	assetName := vars["asset_name"]

	director := func(req *http.Request) {
		req.Header = coreHttp.RemoveSkygearHeader(req.Header)

		query := req.URL.Query()
		pipeline = query.Get(QueryNamePipeline)
		_, hasPipeline = query[QueryNamePipeline]
		// Do not support range request if image processing query is present.
		if hasPipeline {
			req.Header.Del("Range")
			req.Header.Del("If-Range")
			query.Del(QueryNamePipeline)
			req.URL.RawQuery = query.Encode()
		}

		// NOTE(louis): We use panic here because we have no way to return it.
		// However, this function does not return error normally.
		// The known condition that err could be returned is fail to sign
		// which is a configuration problem.
		u, signed, err := h.CloudStorageProvider.RewriteGetURL(req.URL, assetName)
		if err != nil {
			panic(err)
		}
		originallySigned = signed

{'fraction_non_alphanumeric': 0.12159709618874773, 'fraction_numerical': 0.0013611615245009074, 'mean_word_length': 4.4906600249066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4087065', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1436, 'n_words': 333}