Created
February 21, 2019 14:52
-
-
Save wpjunior/0ab461d68996f04525f4177811b4fb33 to your computer and use it in GitHub Desktop.
tracing-bad-http-requests.go
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 ( | |
"bufio" | |
"bytes" | |
"fmt" | |
"io" | |
"net" | |
"net/http" | |
"github.com/alecthomas/repr" | |
) | |
func handleConnection(conn net.Conn) { | |
memBuf := &bytes.Buffer{} | |
multiReader := io.TeeReader(conn, memBuf) | |
buf := bufio.NewReader(multiReader) | |
req, err := http.ReadRequest(buf) | |
defer conn.Close() | |
if err == nil { | |
fmt.Fprint(conn, "HTTP/1.1 200 OK\r\nContent-Length: 76\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 19 Jul 1972 19:00:00 GMT\r\n\r\nGo is a general-purpose language designed with systems programming in mind.\n") | |
} else { | |
fmt.Println("***********************************************") | |
fmt.Printf("Deu erro %s\n", err) | |
fmt.Printf("Req:") | |
repr.Print(req) | |
fmt.Printf("\nBuffer capturado:") | |
fmt.Println(memBuf.String()) | |
fmt.Println("***********************************************") | |
} | |
} | |
func main() { | |
ln, err := net.Listen("tcp", ":8888") | |
if err != nil { | |
fmt.Println("Deu merda!", err) | |
// handle error | |
} | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
// handle error | |
} | |
go handleConnection(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment