Skip to content

Instantly share code, notes, and snippets.

@TanAlex
Last active June 2, 2019 16:32
Show Gist options
  • Save TanAlex/d2d034fafee6aedf3c91ec8b577e90d0 to your computer and use it in GitHub Desktop.
Save TanAlex/d2d034fafee6aedf3c91ec8b577e90d0 to your computer and use it in GitHub Desktop.
[chunked_request go client]chunked_request #http
package main
import (
"fmt"
"io"
"io/ioutil"
"net/url"
"github.com/benburkert/http"
//"net/http"
//"encoding/binary"
"os"
)
const (
chunk1 = "First Chunk"
chunk2 = "Second Chunk"
)
func main() {
rd, wr := io.Pipe()
//u, _ := url.Parse("http://httpbin.org/post?show_env=1")
//u, _ := url.Parse("http://requestb.in/zox5gczo")
u, _ := url.Parse("https://httpbin.org/post")
req := &http.Request{
Method: "POST",
ProtoMajor: 1,
ProtoMinor: 1,
URL: u,
TransferEncoding: []string{"chunked"},
Body: rd,
Header: make(map[string][]string),
}
req.Header.Set("Content-Type", "audio/pcm;bit=16;rate=8000")
client := http.DefaultClient
go func() {
buf := make([]byte, 300)
f, _ := os.Open("./tq.pcm")
for {
n, _ := f.Read(buf)
if 0 == n {
break
}
wr.Write(buf)
}
wr.Close()
}()
resp, err := client.Do(req)
if nil != err {
fmt.Println("error =>", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if nil != err {
fmt.Println("error =>", err.Error())
} else {
fmt.Println(string(body))
}
}
package main
import (
"net/http"
"net/url"
"os"
"fmt"
)
func main() {
tr := http.DefaultTransport
client := &http.Client{
Transport: tr,
Timeout: 0,
}
r := os.Stdin
req := &http.Request{
Method: "POST",
URL: &url.URL{
Scheme: "http",
Host: "localhost:9094",
Path: "/",
},
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: -1,
Body: r,
}
fmt.Printf("Doing request\n")
_, err := client.Do(req)
fmt.Printf("Done request. Err: %v\n", err)
}
package main
import (
"net/http"
"net"
"fmt"
"io"
)
func handle(w http.ResponseWriter, req *http.Request) {
buf := make([]byte, 256)
var n int
for {
n, err := req.Body.Read(buf)
if err == io.EOF {
break
}
fmt.Printf(string(buf[:n]))
}
fmt.Printf(string(buf[:n]))
fmt.Printf("TRANSMISSION COMPLETE")
}
func main() {
/* Net listener */
n := "tcp"
addr := "127.0.0.1:9094"
l, err := net.Listen(n, addr)
if err != nil {
panic("AAAAH")
}
/* HTTP server */
server := http.Server{
Handler: http.HandlerFunc(handle),
}
server.Serve(l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment