HTTPSERVER: Fix 2
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Ensures gofmt doesn't remove the "net" and "os" imports above (feel free to remove this!)
|
||||
var (
|
||||
_ = net.Listen
|
||||
_ = os.Exit
|
||||
)
|
||||
|
||||
func main() {
|
||||
// You can use print statements as follows for debugging, they'll be visible when running tests.
|
||||
fmt.Println("Logs from your program will appear here!")
|
||||
|
||||
// Uncomment this block to pass the first stage
|
||||
|
||||
l, err := net.Listen("tcp", "0.0.0.0:4221")
|
||||
if err != nil {
|
||||
fmt.Println("Failed to bind to port 4221")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer l.Close()
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
fmt.Println("Error accepting connection: ", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
req := make([]byte, 1024)
|
||||
|
||||
conn.Read(req)
|
||||
|
||||
if !strings.HasPrefix(string(req), "GET / HTTP/1.1") {
|
||||
conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n"))
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
|
||||
}
|
||||
Reference in New Issue
Block a user