<commit_before>// Copyright 2015 <NAME>

package httpentity

import (
	"fmt"
	"strings"
)

func methods2string(methods map[string]func(request Request) Response) string {
	set := make(map[string]bool, len(methods)+2)
	for method := range methods {
		set[method] = true
	}
	set["OPTIONS"] = true
	if _, get := set["GET"]; get {
		set["HEAD"] = true
	}
	list := make([]string, len(set))
	i := uint(0)
	for method := range set {
		list[i] = method
		i++
	}
	return strings.Join(list, ", ")
}

// Takes the normalized path without the leading slash
func route(entity Entity, req Request, upath string) (ret Response) {
	if entity == nil {
		ret = statusNotFound()
	} else if upath == "" {
		callmethod := req.Method
		if callmethod == "HEAD" {
			callmethod = "GET"
		}
		methods := entity.Methods()
		handler, method_allowed := methods[req.Method]
		if method_allowed {
			ret = handler(req)
		} else {
			ret = statusMethodNotAllowed(methods2string(methods))
		}
		if callmethod == "OPTIONS" {
			ret.Status = 200
			ret.Headers.Set("Allow", methods2string(methods))
		}
	} else {
		parts := strings.SplitN(upath, "/", 2)
		if len(parts) != 2 {
			panic(fmt.Sprintf("path parser logic failure: %#v", upath))
		}
		child := parts[0]
		grandchildren := parts[1]
		ret = route(entity.Subentity(child, req), req, grandchildren)
	}
	return
}
<commit_msg>httpentity: fix OPTIONS handling<commit_after>// Copyright 2015 <NAME>

package httpentity

import (
	"fmt"
	"strings"
)

func methods2string(methods map[string]func(request Request) Response) string {
	set := make(map[string]bool, len(methods)+2)
	for method := range methods {
		set[method] = true
	}
	set["OPTIONS"] = true
	if _, get := set["GET"]; get {
		set["HEAD"] = true
	}
	list := make([]string, len(set))
	i := uint(0)
	for method := range set {
		list[i] = method
		i++
	}
	return strings.Join(list, ", ")
}

// Takes the normalized path without the leading slash
func route(entity Entity, req Request, upath string) (ret Response) {
	if entity == nil {
		ret = statusNotFound()
	} else if upath == "" {
		callmethod := req.Method
		if callmethod == "HEAD" {
			callmethod = "GET"
		}
		methods := entity.Methods()
		handler, method_allowed := methods[req.Method]
		if method_allowed {
			ret = handler(req)
		} else {
			if callmethod == "OPTIONS" {
				ret = StatusOK(nil)
			} else {
				ret = statusMethodNotAllowed(methods2string(methods))
			}
		}
		if callmethod == "OPTIONS" {
			ret.Headers.Set("Allow", methods2string(methods))
		}
	} else {
		parts := strings.SplitN(upath, "/", 2)
		if len(parts) != 2 {
			panic(fmt.Sprintf("path parser logic failure: %#v", upath))
		}
		child := parts[0]
		grandchildren := parts[1]
		ret = route(entity.Subentity(child, req), req, grandchildren)
	}
	return
}

{'fraction_non_alphanumeric': 0.1539573545355981, 'fraction_numerical': 0.010480664980122878, 'mean_word_length': 3.780656303972366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3914452', 'n_tokens_mistral': 1044, 'n_tokens_neox': 951, 'n_words': 247}