# Golang ## URL ![](https://i.imgur.com/T28ZLcW.png) ## DNS ### Check <b>hosts</b> file - `location: C:\windows\system32\drivers\etc\hosts` ### Check local DNS cache - `ipconfig /displaydns` ### Ask DNS server - Example: - HiNet DNS (`168.95.1.1`、`168.95.192.1`) - google DNS ( `8.8.8.8` ) ## HTTP ### GET ``` GET /path/to/myfile.html?key1=value1 HTTP/1.1 Host:192.168.100.61:8080 User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding:gzip,deflate,sdch Accept-Charset:UTF-8,*;q=0.5 ``` ### POST ``` POST / HTTP/1.1 Host: 127.0.0.1:566 User-Agent: curl/7.74.0 Accept: */* Content-Length: 34 Content-Type: application/x-www-form-urlencoded k=v&username=bob&password=p@ssw0rd ``` ## Process ### index.html ```htmlembedded= <form> username: <input type="text" name="username"><br> password: <input type="password" name="password"><br> <input type="submit" value="submit"> </form> ``` ### main.go ```go= package main import ( "fmt" "io" "net/http" "os" "regexp" "strings" "github.com/anaskhan96/soup" ) func main() { // url := "https://www.ptt.cc/bbs/MobileComm/index6505.html" // soupExample(url) // regExample(url) port := "3333" if len(os.Args) > 1 { port = os.Args[1] } httpSrvExample(port) } func checkErr(err error) { if err != nil { os.Exit(1) } } func getRoot(w http.ResponseWriter, request *http.Request) { addr := request.RemoteAddr remoteIP := strings.Split(addr, ":")[0] fmt.Printf("%s: got / request\n", remoteIP) io.WriteString(w, "This is my website!\n") } func getHello(w http.ResponseWriter, request *http.Request) { addr := request.RemoteAddr remoteIP := strings.Split(addr, ":")[0] fmt.Printf("%s: got /hello request\n", remoteIP) io.WriteString(w, "Hello, HTTP!\n") } func reqCheck(writer http.ResponseWriter, request *http.Request) { addr := request.RemoteAddr remoteIP := strings.Split(addr, ":")[0] fmt.Printf("%s: got /reqCheck request\n", remoteIP) fmt.Fprintln(writer, "Paramaters: ") for key, value := range request.URL.Query() { fmt.Fprintln(writer, key, "=>", value[0]) } fmt.Fprintln(writer, "\nHeaders: ") for key, value := range request.Header { fmt.Fprintln(writer, key, "=>", value[0]) } fmt.Fprintln(writer, "\nBody: ") bodyBytes, err := io.ReadAll(request.Body) checkErr(err) bodyString := string(bodyBytes) fmt.Fprintln(writer, bodyString) } func httpSrvExample(port string) { http.HandleFunc("/", getRoot) http.HandleFunc("/hello", getHello) http.HandleFunc("/reqCheck", reqCheck) err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) checkErr(err) } func soupExample(url string) { resp, err := soup.Get(url) checkErr(err) doc := soup.HTMLParse(resp) divs := doc.FindAll("div", "class", "title") for _, div := range divs { link := div.Find("a") if link.Error != nil { continue } fmt.Println(link.Attrs()["href"], link.Text()) } } func regExample(url string) { resp, err := http.Get(url) if resp.StatusCode != http.StatusOK { println("http request error") os.Exit(1) } defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) checkErr(err) bodyString := string(bodyBytes) r, err := regexp.Compile("href=\"(.*?.html)\">(.*?)</a") matches := r.FindAllStringSubmatch(bodyString, -1) for _, match := range matches[9:] { fmt.Println(match[1], match[2]) } } ```