func (c Context) IsMITM() bool {
	if val, ok := c.Req.Context().Value(MitmCtxKey).(bool); ok {
		return val
	}
	return false
}
IsMITM returns true if it seems likely that the TLS connection
is being intercepted.
package httpserver
import (
	"bytes"
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"log"
	mathrand "math/rand"
	"net"
	"net/http"
	"net/url"
	"path"
	"strconv"
	"strings"
	"sync"
	"text/template"
	"time"
	"os"
	"github.com/tmpim/certmagic"
	"github.com/russross/blackfriday"
	"github.com/tmpim/casket/caskettls"
)
type Context struct {
	Root http.FileSystem
	Req  *http.Request
	URL  *url.URL
	Args []interface{}  
	responseHeader http.Header
}
func NewContextWithHeader(rh http.Header) Context {
	return Context{
		responseHeader: rh,
	}
}
func (c Context) Include(filename string, args ...interface{}) (string, error) {
	c.Args = args
	return ContextInclude(filename, c, c.Root)
}
func (c Context) Now(format string) string {
	return time.Now().Format(format)
}
func (c Context) NowDate() time.Time {
	return time.Now()
}
func (c Context) Cookie(name string) string {
	cookies := c.Req.Cookies()
	for _, cookie := range cookies {
		if cookie.Name == name {
			return cookie.Value
		}
	}
	return ""
}
func (c Context) Header(name string) string {
	return c.Req.Header.Get(name)
}
func (c Context) Hostname() string {
	ip := c.IP()
	hostnameList, err := net.LookupAddr(ip)
	if err != nil || len(hostnameList) == 0 {
		return c.Req.RemoteAddr
	}
	return hostnameList[0]
}
func (c Context) Env() map[string]string {
	osEnv := os.Environ()
	envVars := make(map[string]string, len(osEnv))
	for _, env := range osEnv {
		data := strings.SplitN(env, "=", 2)
		if len(data) == 2 && len(data[0]) > 0 {
			envVars[data[0]] = data[1]
		}
	}
	return envVars
}
func (c Context) IP() string {
	ip, _, err := net.SplitHostPort(c.Req.RemoteAddr)
	if err != nil {
		return c.Req.RemoteAddr
	}
	return ip
}
var networkInterfacesFn = net.InterfaceAddrs
func (c Context) ServerIP() string {
	addrs, err := networkInterfacesFn()
	if err != nil {
		return ""
	}
	for _, address := range addrs {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil || ipnet.IP.To16() != nil {
				return ipnet.IP.String()
			}
		}
	}
	return ""
}
func (c Context) URI() string {
	return c.Req.RequestURI
}
func (c Context) Host() (string, error) {
	host, _, err := net.SplitHostPort(c.Req.Host)
	if err != nil {
		if !strings.Contains(c.Req.Host, ":") {
			return c.Req.Host, nil
		}
		return "", err
	}
	return host, nil
}
func (c Context) Port() (string, error) {
	_, port, err := net.SplitHostPort(c.Req.Host)
	if err != nil {
		if !strings.Contains(c.Req.Host, ":") {
			return strconv.Itoa(certmagic.HTTPPort), nil
		}
		return "", err
	}
	return port, nil
}
func (c Context) Method() string {
	return c.Req.Method
}
func (c Context) PathMatches(pattern string) bool {
	return Path(c.Req.URL.Path).Matches(pattern)
}
func (c Context) Truncate(input string, length int) string {
	if length < 0 && len(input)+length > 0 {
		return input[len(input)+length:]
	}
	if length >= 0 && len(input) > length {
		return input[:length]
	}
	return input
}
func (c Context) StripHTML(s string) string {
	var buf bytes.Buffer
	var inTag, inQuotes bool
	var tagStart int
	for i, ch := range s {
		if inTag {
			if ch == '>' && !inQuotes {
				inTag = false
			} else if ch == '<' && !inQuotes {
				buf.WriteString(s[tagStart:i])
				tagStart = i
			} else if ch == '"' {
				inQuotes = !inQuotes
			}
			continue
		}
		if ch == '<' {
			inTag = true
			tagStart = i
			continue
		}
		buf.WriteRune(ch)
	}
	if inTag {
		buf.WriteString(s[tagStart:])
	}
	return buf.String()
}
func (c Context) Ext(pathStr string) string {
	return path.Ext(pathStr)
}
func (c Context) StripExt(path string) string {
	for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- {
		if path[i] == '.' {
			return path[:i]
		}
	}
	return path
}
func (c Context) Replace(input, find, replacement string) string {
	return strings.Replace(input, find, replacement, -1)
}
func (c Context) Markdown(filename string) (string, error) {
	body, err := c.Include(filename)
	if err != nil {
		return "", err
	}
	renderer := blackfriday.HtmlRenderer(0, "", "")
	extns := 0
	extns |= blackfriday.EXTENSION_TABLES
	extns |= blackfriday.EXTENSION_FENCED_CODE
	extns |= blackfriday.EXTENSION_STRIKETHROUGH
	extns |= blackfriday.EXTENSION_DEFINITION_LISTS
	markdown := blackfriday.Markdown([]byte(body), renderer, extns)
	return string(markdown), nil
}
func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (string, error) {
	file, err := fs.Open(filename)
	if err != nil {
		return "", err
	}
	defer file.Close()
	body, err := ioutil.ReadAll(file)
	if err != nil {
		return "", err
	}
	tpl, err := template.New(filename).Funcs(TemplateFuncs).Parse(string(body))
	if err != nil {
		return "", err
	}
	buf := includeBufs.Get().(*bytes.Buffer)
	buf.Reset()
	defer includeBufs.Put(buf)
	err = tpl.Execute(buf, ctx)
	if err != nil {
		return "", err
	}
	return buf.String(), nil
}
func (c Context) ToLower(s string) string {
	return strings.ToLower(s)
}
func (c Context) ToUpper(s string) string {
	return strings.ToUpper(s)
}
func (c Context) Split(s string, sep string) []string {
	return strings.Split(s, sep)
}
func (c Context) Join(a []string, sep string) string {
	return strings.Join(a, sep)
}
func (c Context) Slice(elems ...interface{}) []interface{} {
	return elems
}
func (c Context) Map(values ...interface{}) (map[string]interface{}, error) {
	if len(values)%2 != 0 {
		return nil, fmt.Errorf("Map expects an even number of arguments")
	}
	dict := make(map[string]interface{}, len(values)/2)
	for i := 0; i < len(values); i += 2 {
		key, ok := values[i].(string)
		if !ok {
			return nil, fmt.Errorf("Map keys must be strings")
		}
		dict[key] = values[i+1]
	}
	return dict, nil
}
func (c Context) Files(name string) ([]string, error) {
	dir, err := c.Root.Open(path.Clean(name))
	if err != nil {
		return nil, err
	}
	defer dir.Close()
	stat, err := dir.Stat()
	if err != nil {
		return nil, err
	}
	if !stat.IsDir() {
		return nil, fmt.Errorf("%v is not a directory", name)
	}
	dirInfo, err := dir.Readdir(0)
	if err != nil {
		return nil, err
	}
	names := make([]string, len(dirInfo))
	for i, fileInfo := range dirInfo {
		names[i] = fileInfo.Name()
	}
	return names, nil
}