aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2019-04-26 17:52:19 +0300
committerMarin Ivanov <[email protected]>2019-04-26 17:52:19 +0300
commite74246db707ed168fa0444f5dfcbfa20b2aeacbe (patch)
treec400e6af698c7f02101f5f520ef44696a761ed7d
parent174e6b2bbdd299b478ae00875d265c06459c5ad0 (diff)
http: Wait for all the headers before serving the response
-rw-r--r--http.go23
1 files changed, 18 insertions, 5 deletions
diff --git a/http.go b/http.go
index c8199a3..cf665b4 100644
--- a/http.go
+++ b/http.go
@@ -10,11 +10,24 @@ import (
"time"
)
-func httpHandler(conn net.Conn, delay time.Duration) {
+func httpReadHeaders(conn net.Conn) error {
rd := bufio.NewReader(conn)
- rd.ReadLine()
- _, err := fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n")
- if err != nil {
+ for {
+ line, _, err := rd.ReadLine()
+ if err != nil {
+ return err
+ }
+ if len(line) == 0 {
+ return nil
+ }
+ }
+}
+
+func httpHandler(conn net.Conn, delay time.Duration) {
+ if err := httpReadHeaders(conn); err != nil {
+ return
+ }
+ if _, err := fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\n"); err != nil {
return
}
@@ -30,7 +43,7 @@ func httpHandler(conn net.Conn, delay time.Duration) {
case <-eof:
return
case <-tick:
- _, err := fmt.Fprintf(conn, "X-%x: %x\r\n", rand.Uint32(), rand.Uint32())
+ _, err := fmt.Fprintf(conn, "X-%0x: %0x\r\n", rand.Uint32(), rand.Uint32())
if err != nil {
return
}