-
-
Save bonedaddy/42809305041fc705ff62361a75bf667a to your computer and use it in GitHub Desktop.
Golang HTTP multipart streaming
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 ( | |
"io" | |
"mime/multipart" | |
"net/http" | |
"net/url" | |
"os" | |
"fmt" | |
) | |
const boundary = "MachliJalKiRaniHaiJeevanUskaPaaniHai" | |
func main() { | |
tr := http.DefaultTransport | |
client := &http.Client{ | |
Transport: tr, | |
Timeout: 0, | |
} | |
fmt.Println("Set up pipe") | |
pR, pW := io.Pipe() | |
go func() { | |
// Set up multipart body for reading | |
multipartW := multipart.NewWriter(pW) | |
fmt.Println("Set up multipart writer") | |
multipartW.SetBoundary(boundary) | |
fmt.Println("Set up boundary") | |
partW, err0 := multipartW.CreateFormFile("fakefield", "fakefilename") | |
fmt.Println("Set up part writer") | |
if err0 != nil { | |
panic("Something is amiss creating a part") | |
} | |
connector := io.TeeReader(os.Stdin, partW) | |
buf := make([]byte, 256) | |
for { | |
/* stdin -> connector -> partW -> multipartW -> pW -> pR */ | |
_, err := connector.Read(buf) | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
fmt.Printf("The error reading from connector: %v", err) | |
} | |
} | |
}() | |
// Send http request chunk encoding the multipart message | |
req := &http.Request{ | |
Method: "POST", | |
URL: &url.URL{ | |
Scheme: "http", | |
Host: "localhost:9094", | |
Path: "/", | |
}, | |
ProtoMajor: 1, | |
ProtoMinor: 1, | |
ContentLength: -1, | |
Body: pR, | |
} | |
fmt.Printf("Doing request\n") | |
_, err := client.Do(req) | |
fmt.Printf("Done request. Err: %v\n", err) | |
} |
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 ( | |
"mime/multipart" | |
"net/http" | |
"net" | |
"fmt" | |
"io" | |
) | |
const boundary = "MachliJalKiRaniHaiJeevanUskaPaaniHai" | |
func handle(w http.ResponseWriter, req *http.Request) { | |
partReader := multipart.NewReader(req.Body, boundary) | |
buf := make([]byte, 256) | |
for { | |
part, err := partReader.NextPart() | |
if err == io.EOF { | |
break | |
} | |
var n int | |
for { | |
n, err = part.Read(buf) | |
if err == io.EOF { | |
break | |
} | |
fmt.Printf(string(buf[:n])) | |
} | |
fmt.Printf(string(buf[:n])) | |
} | |
} | |
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