Debian Codegraph @

http/context.go

raw
1 // Copyright (c) 2014-2019 Ludovic Fauvet
2 // Licensed under the MIT license
3
4 package http
5
6 import (
7 "net/http"
8 "net/url"
9 "strings"
10
11 . "github.com/etix/mirrorbits/config"
12 )
13
14 // RequestType defines the type of the request
15 type RequestType int
16
17 // SecureOption is the type that defines TLS requirements
18 type SecureOption int
19
20 const (
21 STANDARD RequestType = iota
22 MIRRORLIST
23 FILESTATS
24 MIRRORSTATS
25 CHECKSUM
26
27 UNDEFINED SecureOption = iota
28 WITHTLS
29 WITHOUTTLS
30 )
31
32 // Context represents the context of a request
33 type Context struct {
34 r *http.Request
35 w http.ResponseWriter
36 t Templates
37 v url.Values
38 typ RequestType
39 isMirrorList bool
40 isMirrorStats bool
41 isFileStats bool
42 isChecksum bool
43 isPretty bool
44 secureOption SecureOption
45 }
46
47 // NewContext returns a new instance of Context
48 func NewContext(w http.ResponseWriter, r *http.Request, t Templates) *Context {
49 c := &Context{r: r, w: w, t: t, v: r.URL.Query()}
50
51 if c.paramBool("mirrorlist") {
52 c.typ = MIRRORLIST
53 c.isMirrorList = true
54 } else if c.paramBool("stats") {
55 c.typ = FILESTATS
56 c.isFileStats = true
57 } else if c.paramBool("mirrorstats") {
58 c.typ = MIRRORSTATS
59 c.isMirrorStats = true
60 } else if c.paramBool("md5") || c.paramBool("sha1") || c.paramBool("sha256") {
61 c.typ = CHECKSUM
62 c.isChecksum = true
63 } else {
64 c.typ = STANDARD
65 }
66
67 if c.paramBool("pretty") {
68 c.isPretty = true
69 }
70
71 // Check for HTTPS requirements
72 proto := strings.ToLower(r.Header.Get("X-Forwarded-Proto"))
73 if proto == "https" {
74 c.secureOption = WITHTLS
75 } else if proto == "http" && GetConfig().AllowHTTPToHTTPSRedirects == false {
76 c.secureOption = WITHOUTTLS
77 }
78
79 // Check if the query sets (thus overrides) HTTPS requirements
80 v, ok := c.v["https"]
81 if ok {
82 if v[0] == "1" {
83 c.secureOption = WITHTLS
84 } else if v[0] == "0" {
85 c.secureOption = WITHOUTTLS
86 }
87 }
88
89 return c
90 }
91
92 // Request returns the underlying http.Request of the current request
93 func (c *Context) Request() *http.Request {
94 return c.r
95 }
96
97 // ResponseWriter returns the underlying http.ResponseWriter of the current request
98 func (c *Context) ResponseWriter() http.ResponseWriter {
99 return c.w
100 }
101
102 // Templates returns the instance of precompiled templates
103 func (c *Context) Templates() Templates {
104 return c.t
105 }
106
107 // Type returns the type of the current request
108 func (c *Context) Type() RequestType {
109 return c.typ
110 }
111
112 // IsMirrorlist returns true if the mirror list has been requested
113 func (c *Context) IsMirrorlist() bool {
114 return c.isMirrorList
115 }
116
117 // IsFileStats returns true if the file stats has been requested
118 func (c *Context) IsFileStats() bool {
119 return c.isFileStats
120 }
121
122 // IsMirrorStats returns true if the mirror stats has been requested
123 func (c *Context) IsMirrorStats() bool {
124 return c.isMirrorStats
125 }
126
127 // IsChecksum returns true if a checksum has been requested
128 func (c *Context) IsChecksum() bool {
129 return c.isChecksum
130 }
131
132 // IsPretty returns true if the pretty json has been requested
133 func (c *Context) IsPretty() bool {
134 return c.isPretty
135 }
136
137 // QueryParam returns the value associated with the given query parameter
138 func (c *Context) QueryParam(key string) string {
139 return c.v.Get(key)
140 }
141
142 // SecureOption returns the selected secure option
143 func (c *Context) SecureOption() SecureOption {
144 return c.secureOption
145 }
146
147 func (c *Context) paramBool(key string) bool {
148 _, ok := c.v[key]
149 return ok
150 }