-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.sh
More file actions
84 lines (62 loc) · 1.26 KB
/
core.sh
File metadata and controls
84 lines (62 loc) · 1.26 KB
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
source ./shttp/vars.sh
# Status
shttp_status_ok() {
echo "Status: 200 OK"
}
shttp_status_created() {
echo "Status: 201 Created"
}
shttp_status_accepted() {
echo "Status: 202 Accepted"
}
shttp_status_badrequest() {
echo "Status: 400 Bad Request"
}
shttp_status_unauthorized() {
echo "Status: 401 Unauthorized"
}
shttp_status_forbidden() {
echo "Status: 403 Forbidden"
}
shttp_status_notfound() {
echo "Status: 404 Not Found"
}
shttp_status_methodnotallowed() {
echo "Status: 405 Method Not Allowed"
}
shttp_status_conflict() {
echo "Status: 409 Conflict"
}
# Response
shttp_response() {
content_type=$1
body=$2
echo $content_type
echo ""
echo $body
}
shttp_response_json() {
content_type="Content-Type: application/json"
body=$1
shttp_response "$content_type" "$body"
}
shttp_response_text() {
content_type="Content-Type: plain/text"
body=$1
shttp_response "$content_type" "$body"
}
shttp_response_empty() {
echo ""
}
# shttp request handling
shttp_dispatch() {
if [[ -n $REQUEST_METHOD ]]; then
handler="shttp_$(echo "$REQUEST_METHOD" | awk '{print tolower($0)}')"
if $(type -t "$handler" >/dev/null); then
eval "$handler"
else
shttp_status_methodnotallowed
shttp_response_empty
fi
fi
}