-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (93 loc) · 2.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
var (
counter int
)
type answer struct {
Method string
URL *url.URL
Proto string // "HTTP/1.0"
Header http.Header
Body io.ReadCloser
ContentLength int64
TransferEncoding []string
Host string
}
func handle(w http.ResponseWriter, r *http.Request) {
a := answer{
Method: r.Method,
URL: r.URL,
Proto: r.Proto,
Header: r.Header,
Body: r.Body,
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Host: r.Host,
}
js, err := json.Marshal(a)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
/*Return a code
example: ... -H "RETURN_CODE: 503"...
will respond with status_code of 503
*/
if r.Header.Get("RETURN_CODE") != "" {
header, err := strconv.Atoi(r.Header.Get("RETURN_CODE"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Error(w, "", header)
}
/*Return CODE [0] and after [1] tries return [2]
example: ... -H "RETURN_CODE_AFTER: 500,2,200
will respond with status_code 500 twice, followed by 200
*/
if r.Header.Get("RETURN_CODE_AFTER") != "" {
vals := strings.Split(r.Header.Get("RETURN_CODE_AFTER"), ",")
initHeader, err := strconv.Atoi(vals[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tries, err := strconv.Atoi(vals[1])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
finHeader, err := strconv.Atoi(vals[2])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if counter < tries {
http.Error(w, "", initHeader)
counter++
} else {
http.Error(w, "", finHeader)
counter = 0
}
}
if r.Header.Get("CACHE_CONTROL") != "" {
val := r.Header.Get("CACHE_CONTROL")
w.Header().Set("Cache-Control", val)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
http.HandleFunc("/", handle)
log.Printf("started on :8080\n")
log.Fatal(http.ListenAndServe(":8080", nil))
}