Skip to content

Commit

Permalink
refactor(x/net/ht
Browse files Browse the repository at this point in the history
tp/demo): Use echo read demo

Signed-off-by: hackerchai <[email protected]>
  • Loading branch information
hackerchai committed Sep 5, 2024
1 parent 16054ae commit f03b73f
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions x/net/http/_demo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,44 @@ package main

import (
"fmt"
//"io"
"io"

"github.com/goplus/llgo/x/net/http"
)

func echoHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("echoHandler called\n")
//TODO(hackerchai): read body and echo
// fmt.Printf("> %s %s HTTP/%d.%d\n", r.Method, r.RequestURI, r.ProtoMajor, r.ProtoMinor)
// for key, values := range r.Header {
// for _, value := range values {
// fmt.Printf("> %s: %s\n", key, value)
// }
// }
// fmt.Printf("URL: %s\n", r.URL.String())
// // fmt.Println("ContentLength: %d", r.ContentLength)
// // fmt.Println("TransferEncoding: %s", r.TransferEncoding)
// //TODO: read body and echo
// body, err := io.ReadAll(r.Body)
// println("body read")

// if err != nil {
// http.Error(w, "Error reading request body", http.StatusInternalServerError)
// return
// }
// defer r.Body.Close()
// fmt.Printf("body read")
// w.Header().Set("Content-Type", "text/plain")
// w.Write(body)

fmt.Printf("> %s %s HTTP/%d.%d\n", r.Method, r.RequestURI, r.ProtoMajor, r.ProtoMinor)
for key, values := range r.Header {
for _, value := range values {
fmt.Printf("> %s: %s\n", key, value)
}
}
fmt.Printf("URL: %s\n", r.URL.String())
fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr)
// fmt.Println("ContentLength: %d", r.ContentLength)
// fmt.Println("TransferEncoding: %s", r.TransferEncoding)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
return
}

defer r.Body.Close()
fmt.Printf("body read")
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("hello world\n"))
w.Write(body)

// fmt.Printf("> %s %s HTTP/%d.%d\n", r.Method, r.RequestURI, r.ProtoMajor, r.ProtoMinor)
// for key, values := range r.Header {
// for _, value := range values {
// fmt.Printf("> %s: %s\n", key, value)
// }
// }
// fmt.Printf("URL: %s\n", r.URL.String())
// fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr)
// w.Header().Set("Content-Type", "text/plain")
// w.Write([]byte("hello world\n"))
}

func main() {
Expand Down

0 comments on commit f03b73f

Please sign in to comment.