Created
March 20, 2016 11:11
-
-
Save hartfordfive/91ec4f528227610516e4 to your computer and use it in GitHub Desktop.
Hijacking Golang http response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net/http" | |
| ) | |
| func main() { | |
| http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) { | |
| hj, ok := w.(http.Hijacker) | |
| if !ok { | |
| http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) | |
| return | |
| } | |
| conn, bufrw, err := hj.Hijack() | |
| if err != nil { | |
| http.Error(w, err.Error(), http.StatusInternalServerError) | |
| return | |
| } | |
| // Don't forget to close the connection: | |
| defer conn.Close() | |
| bufrw.WriteString("Now we're speaking raw TCP. Say hi: ") | |
| bufrw.Flush() | |
| s, err := bufrw.ReadString('\n') | |
| if err != nil { | |
| log.Printf("error reading string: %v", err) | |
| return | |
| } | |
| fmt.Fprintf(bufrw, "You said: %q\nBye.\n", s) | |
| bufrw.Flush() | |
| }) | |
| } |
@Beace you may use curl to do that.
curl -i http://localhost/hijackHave you get any response ? I copy this gist and test by postman, but no http response received. Why?
Same here, even use the curl -i http://localhost/hijack
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you get any response ? I copy this gist and test by postman, but no http response received. Why?