Created
December 9, 2013 14:20
-
-
Save mdobson/7872888 to your computer and use it in GitHub Desktop.
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) | |
print("Made connection\n") | |
if !ok { | |
http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) | |
return | |
} | |
conn, bufrw, err := hj.Hijack() | |
print("Hijacked\n") | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
defer conn.Close() | |
bufrw.WriteString("Now we're speaking raw TCP. Say hi: ") | |
bufrw.Flush() | |
print("Initial flush\n") | |
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() | |
}) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment