HTTP Servers
Method Checks
Handlers often accept one HTTP method and reject others.
method check
Checking a method keeps read and write actions separate.
Method Checks
method_checks.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
type serverRequest struct {
Method string
Path string
}
func main() {
var method = "GET"
req := serverRequest{Method: method, Path: "/items"}
status := 200
body := "list items"
if req.Method == "POST" {
status = 201
body = "created item"
}
if req.Method != "GET" && req.Method != "POST" {
status = 405
body = "method not allowed"
}
fmt.Println("method=", req.Method)
fmt.Println("status=", status)
fmt.Println("body=", body)
}
package main
import "fmt"
type serverRequest struct {
Method string
Path string
}
func main() {
var method = "POST"
req := serverRequest{Method: method, Path: "/items"}
status := 200
body := "list items"
if req.Method == "POST" {
status = 201
body = "created item"
}
if req.Method != "GET" && req.Method != "POST" {
status = 405
body = "method not allowed"
}
fmt.Println("method=", req.Method)
fmt.Println("status=", status)
fmt.Println("body=", body)
}
package main
import "fmt"
type serverRequest struct {
Method string
Path string
}
func main() {
var method = "DELETE"
req := serverRequest{Method: method, Path: "/items"}
status := 200
body := "list items"
if req.Method == "POST" {
status = 201
body = "created item"
}
if req.Method != "GET" && req.Method != "POST" {
status = 405
body = "method not allowed"
}
fmt.Println("method=", req.Method)
fmt.Println("status=", status)
fmt.Println("body=", body)
}
method ← "GET", req ← main.serverRequest{Method:"GET", Path:"/items"}
10func main() {11 var method→ "GET" = "GET" //@method="POST", "DELETE"12 req→ main.serverRequest{Method:"GET", Path:"/items"} := serverRequest{Method: method"GET", Path: "/items"}13 status→ 200 := 20014 body→ "list items" := "list items"1516 if req.Method == "POST" {17 status = 20118 body = "created item"19 }20 if req.Method != "GET" && req.Method != "POST" {21 status = 40522 body = "method not allowed"23 }2425 fmt.Println("method=", req.Method"GET")26 fmt.Println("status=", status200)27 fmt.Println("body=", body"list items")28}outputmethod= GET status= 200 body= list items
method ← "POST", req ← main.serverRequest{Method:"POST", Path:"/items"}
10func main() {11 var method→ "POST" = "POST"12 req→ main.serverRequest{Method:"POST", Path:"/items"} := serverRequest{Method: method"POST", Path: "/items"}13 status→ 200 := 20014 body→ "list items" := "list items"status ← 201, body ← "created item"
16if req.Method"POST" == "POST" {17 status→ 201 = 20118 body→ "created item" = "created item"19}fmt.Println("method=", req.Method)
25 fmt.Println("method=", req.Method"POST")26 fmt.Println("status=", status201)27 fmt.Println("body=", body"created item")28}outputmethod= POST status= 201 body= created item
method ← "DELETE", req ← main.serverRequest{Method:"DELETE", Path:"/items"}
10func main() {11 var method→ "DELETE" = "DELETE"12 req→ main.serverRequest{Method:"DELETE", Path:"/items"} := serverRequest{Method: method"DELETE", Path: "/items"}13 status→ 200 := 20014 body→ "list items" := "list items"status ← 405, body ← "method not allowed"
19}20if req.Method"DELETE" != "GET" && req.Method != "POST" {21 status→ 405 = 40522 body→ "method not allowed" = "method not allowed"23}fmt.Println("method=", req.Method)
25 fmt.Println("method=", req.Method"DELETE")26 fmt.Println("status=", status405)27 fmt.Println("body=", body"method not allowed")28}outputmethod= DELETE status= 405 body= method not allowed