Go Pentester - HTTP Servers(1)
2021-04-12 13:26
标签:nil art cut comm https switch reg asics type HTTP Server Basics Use net/http package and useful third-party packages by building simple servers. Building a Simple Server Run the above program and test it. You can also use http.ListenAndServeTLS(), which will start a server using HTTPS and TLS. Build a Simple Router Test the above program by the following commands. Building simple Middleware A simple middleware, which is a sort of wrapper that will execute on all incoming requests regardless of the destination function. Run the program and issue a request. Go Pentester - HTTP Servers(1) 标签:nil art cut comm https switch reg asics type 原文地址:https://www.cnblogs.com/keepmoving1113/p/12398127.htmlpackage main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s\n", r.URL.Query().Get("name"))
}
func main() {
http.HandleFunc("/hello", hello)
http.ListenAndServe(":8000",nil)
}
curl -i http://localhost:8000/hello?name=eric
package main
import (
"fmt"
"net/http"
)
type router struct {
}
func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/a":
fmt.Fprintf(w, "Executing /a\n")
case "/b":
fmt.Fprintf(w, "Executing /b\n")
case "/c":
fmt.Fprintf(w, "Executing /c\n")
default:
http.Error(w, "404 Not Found", 404)
}
}
func main() {
var r router
http.ListenAndServe(":8000", &r)
}
curl http://localhost:8000/a
curl http://localhost:8000/d
package main
import (
"fmt"
"log"
"net/http"
"time"
)
type logger struct {
Inner http.Handler
}
func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("start %s\n", time.Now().String())
l.Inner.ServeHTTP(w,r)
log.Printf("finish %s",time.Now().String())
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello\n")
}
func main() {
f := http.HandlerFunc(hello)
l := logger{Inner: f}
http.ListenAndServe(":8000", &l)
}
curl http://localhost:8000
上一篇:完成 keystone 证书加密的 HTTPS 服务提升
下一篇:HTML常用标签
文章标题:Go Pentester - HTTP Servers(1)
文章链接:http://soscw.com/essay/74731.html