-
-
Save danp/3098517c3d6a33d073fa095cd045ea77 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" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
) | |
var ( | |
respReader infiniteReader | |
) | |
type infiniteReader []byte | |
func (r infiniteReader) Read(p []byte) (int, error) { | |
return copy(p, r), nil | |
} | |
func dynamicHandler(w http.ResponseWriter, r *http.Request) { | |
io.Copy(ioutil.Discard, r.Body) | |
q := r.URL.Query() | |
var bodySize int64 | |
if v := q.Get("bodySize"); v != "" { | |
if i, err := strconv.ParseInt(v, 0, 64); err == nil { | |
bodySize = i | |
} | |
} | |
io.CopyN(w, respReader, bodySize) | |
} | |
func staticHandler(w http.ResponseWriter, r *http.Request) { | |
io.Copy(ioutil.Discard, r.Body) | |
fmt.Fprintf(w, "HI") | |
} | |
func main() { | |
respReader = infiniteReader(strings.Repeat("a", 1024)) | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "12345" | |
} | |
http.HandleFunc("/dynamic", dynamicHandler) | |
http.HandleFunc("/", staticHandler) | |
http.ListenAndServe(":"+port, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment