Last active
November 25, 2023 18:23
-
-
Save fwoodruff/d704d1650cd4a596c3bb9392f1087281 to your computer and use it in GitHub Desktop.
QUIC Client
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 ( | |
"bytes" | |
"context" | |
"crypto/tls" | |
"log" | |
"net/http" | |
"time" | |
"github.com/quic-go/quic-go" | |
) | |
func main() { | |
tlsConfig := &tls.Config{ | |
InsecureSkipVerify: true, // testing only | |
NextProtos: []string{"h3", "http/1.1"}, | |
} | |
url := "localhost:8080" | |
req, _ := http.NewRequest("GET", url, nil) | |
var buf bytes.Buffer | |
req.Write(&buf) | |
requestBytes := buf.Bytes() | |
ctx := context.Background() | |
connection, err := quic.DialAddr(ctx, url, tlsConfig, nil) | |
if err != nil { | |
println(err.Error()) | |
return | |
} | |
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | |
defer cancel() | |
stream, err := connection.OpenUniStreamSync(ctx) | |
if err != nil { | |
log.Fatal(err) | |
return | |
} | |
n, err := stream.Write(requestBytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err = stream.Close(); err != nil { | |
log.Fatal(err) | |
} | |
println("ok") | |
println(n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment