Skip to content

Instantly share code, notes, and snippets.

@hartfordfive
Created March 20, 2016 11:11
Show Gist options
  • Select an option

  • Save hartfordfive/91ec4f528227610516e4 to your computer and use it in GitHub Desktop.

Select an option

Save hartfordfive/91ec4f528227610516e4 to your computer and use it in GitHub Desktop.
Hijacking Golang http response
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
Copy link

Beace commented Mar 14, 2018

Have you get any response ? I copy this gist and test by postman, but no http response received. Why?

@AnikHasibul
Copy link

AnikHasibul commented Jul 29, 2018

@Beace you may use curl to do that.

curl -i http://localhost/hijack

@liweijian
Copy link

Have 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