💾 Archived View for tozip.chickenkiller.com › 2022-06-09-fetch-tls.gmi captured on 2023-03-20 at 17:52:43. Gemini links have been rewritten to link to archived content
View Raw
More Information
⬅️ Previous capture (2022-07-16)
-=-=-=-=-=-=-
Simplest TLS (gemini) fetch in Go
Created 2022-06-09
Below is the "simplest thing that could possibly work" in terms of fetching a file from a Gemini server. There is no verification of certificates, or any of that ilk. This example just fetches the root document "/", which should translate to "/index.gmi". It is trivial to fetch any document you like, though.
I don't think any further elaboration is required. I have yet to figure out the whole certificate thing.
/*
Retrieve a resource from gemini using Go.
Demonstrates the use of TLS. No verfication
Created 2022-06-09
package main
import (
"fmt"
"log"
"crypto/tls"
"io/ioutil"
)
func main() {
log.SetFlags(log.Lshortfile)
conf := &tls.Config{
InsecureSkipVerify: true, // don't fuss over the server's certificate
}
uri := "gemini.conman.org:1965"
//uri = "blinkyshark.chickenkiller.com:1965"
conn, err := tls.Dial("tcp", uri, conf)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
n, err := conn.Write([]byte("gemini://" + uri + "/" + "\r\n"))
if err != nil {
log.Println(n, err)
return
}
result, err := ioutil.ReadAll(conn)
full_response := string(result) // includes the status line
if err != nil {
log.Println(result, err)
return
}
fmt.Println(full_response)
}
References
Gemini protocol
Simple Golang HTTPS/TLS examples
getgem.go - source code