2019-09-18 04:57:21 +00:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-09-19 03:27:56 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-09-18 04:57:21 +00:00
|
|
|
|
|
|
|
// "tildegit.org/sloum/mailcap"
|
|
|
|
)
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
type Capsule struct {
|
|
|
|
MimeMaj string
|
|
|
|
MimeMin string
|
|
|
|
Status int
|
|
|
|
Content string
|
|
|
|
Links []string
|
|
|
|
}
|
2019-09-18 04:57:21 +00:00
|
|
|
|
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + F U N C T I O N S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
func Retrieve(host, port, resource string) (string, error) {
|
2019-09-18 04:57:21 +00:00
|
|
|
if host == "" || port == "" {
|
2019-09-19 03:27:56 +00:00
|
|
|
return "", fmt.Errorf("Incomplete request url")
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := host + ":" + port
|
|
|
|
|
|
|
|
conf := &tls.Config{
|
2019-09-19 03:27:56 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2019-09-18 04:57:21 +00:00
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
conn, err := tls.Dial("tcp", addr, conf)
|
2019-09-18 04:57:21 +00:00
|
|
|
if err != nil {
|
2019-09-19 03:27:56 +00:00
|
|
|
return "", err
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
defer conn.Close()
|
2019-09-18 04:57:21 +00:00
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
send := "gemini://" + addr + "/" + resource + "\r\n"
|
2019-09-18 04:57:21 +00:00
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
_, err = conn.Write([]byte(send))
|
2019-09-18 04:57:21 +00:00
|
|
|
if err != nil {
|
2019-09-19 03:27:56 +00:00
|
|
|
return "", err
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
2019-09-19 03:27:56 +00:00
|
|
|
return "", err
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
return string(result), nil
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 23:15:53 +00:00
|
|
|
func Fetch(host, port, resource string) ([]byte, error) {
|
|
|
|
rawResp, err := Retrieve(host, port, resource)
|
|
|
|
if err != nil {
|
|
|
|
return make([]byte, 0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := strings.SplitN(rawResp, "\r\n", 2)
|
|
|
|
if len(resp) != 2 {
|
|
|
|
if err != nil {
|
|
|
|
return make([]byte, 0), fmt.Errorf("Invalid response from server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header := strings.SplitN(resp[0], " ", 2)
|
|
|
|
if len([]rune(header[0])) != 2 {
|
|
|
|
header = strings.SplitN(resp[0], "\t", 2)
|
|
|
|
if len([]rune(header[0])) != 2 {
|
|
|
|
return make([]byte,0), fmt.Errorf("Invalid response format from server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get status code single digit form
|
|
|
|
status, err := strconv.Atoi(string(header[0][0]))
|
|
|
|
if err != nil {
|
|
|
|
return make([]byte, 0), fmt.Errorf("Invalid status response from server")
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != 2 {
|
|
|
|
switch status {
|
|
|
|
case 1:
|
|
|
|
return make([]byte, 0), fmt.Errorf("[1] Queries cannot be saved.")
|
|
|
|
case 3:
|
|
|
|
return make([]byte, 0), fmt.Errorf("[3] Redirects cannot be saved.")
|
|
|
|
case 4:
|
|
|
|
return make([]byte, 0), fmt.Errorf("[4] Temporary Failure.")
|
|
|
|
case 5:
|
|
|
|
return make([]byte, 0), fmt.Errorf("[5] Permanent Failure.")
|
|
|
|
case 6:
|
|
|
|
return make([]byte, 0), fmt.Errorf("[6] Client Certificate Required (Not supported by Bombadillo)")
|
|
|
|
default:
|
|
|
|
return make([]byte, 0), fmt.Errorf("Invalid response status from server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(resp[1]), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
func Visit(host, port, resource string) (Capsule, error) {
|
|
|
|
capsule := MakeCapsule()
|
|
|
|
rawResp, err := Retrieve(host, port, resource)
|
2019-09-18 04:57:21 +00:00
|
|
|
if err != nil {
|
2019-09-19 03:27:56 +00:00
|
|
|
return capsule, err
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
2019-09-19 03:27:56 +00:00
|
|
|
|
|
|
|
resp := strings.SplitN(rawResp, "\r\n", 2)
|
|
|
|
if len(resp) != 2 {
|
|
|
|
if err != nil {
|
|
|
|
return capsule, fmt.Errorf("Invalid response from server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header := strings.SplitN(resp[0], " ", 2)
|
|
|
|
if len([]rune(header[0])) != 2 {
|
|
|
|
header = strings.SplitN(resp[0], "\t", 2)
|
|
|
|
if len([]rune(header[0])) != 2 {
|
|
|
|
return capsule, fmt.Errorf("Invalid response format from server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body := resp[1]
|
2019-09-18 04:57:21 +00:00
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
// Get status code single digit form
|
|
|
|
capsule.Status, err = strconv.Atoi(string(header[0][0]))
|
|
|
|
if err != nil {
|
|
|
|
return capsule, fmt.Errorf("Invalid status response from server")
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
// Parse the meta as needed
|
|
|
|
var meta string
|
|
|
|
|
|
|
|
switch capsule.Status {
|
|
|
|
case 1:
|
|
|
|
// handle search
|
|
|
|
return capsule, fmt.Errorf("Gemini input not yet supported")
|
|
|
|
case 2:
|
|
|
|
mimeAndCharset := strings.Split(header[1], ";")
|
|
|
|
meta = mimeAndCharset[0]
|
|
|
|
minMajMime := strings.Split(meta, "/")
|
|
|
|
if len(minMajMime) < 2 {
|
|
|
|
return capsule, fmt.Errorf("Improperly formatted mimetype received from server")
|
|
|
|
}
|
|
|
|
capsule.MimeMaj = minMajMime[0]
|
|
|
|
capsule.MimeMin = minMajMime[1]
|
|
|
|
if capsule.MimeMaj == "text" && capsule.MimeMin == "gemini" {
|
|
|
|
rootUrl := fmt.Sprintf("gemini://%s:%s", host, port)
|
|
|
|
capsule.Content, capsule.Links = parseGemini(body, rootUrl)
|
|
|
|
} else {
|
|
|
|
capsule.Content = body
|
|
|
|
}
|
|
|
|
return capsule, nil
|
|
|
|
case 3:
|
|
|
|
// The client will handle informing the user of a redirect
|
|
|
|
// and then request the new url
|
|
|
|
capsule.Content = header[1]
|
|
|
|
return capsule, nil
|
|
|
|
case 4:
|
|
|
|
return capsule, fmt.Errorf("[4] Temporary Failure. %s", header[1])
|
|
|
|
case 5:
|
|
|
|
return capsule, fmt.Errorf("[5] Permanent Failure. %s", header[1])
|
|
|
|
case 6:
|
|
|
|
return capsule, fmt.Errorf("[6] Client Certificate Required (Not supported by Bombadillo)")
|
|
|
|
default:
|
|
|
|
return capsule, fmt.Errorf("Invalid response status from server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseGemini(b, rootUrl string) (string, []string) {
|
|
|
|
splitContent := strings.Split(b, "\n")
|
|
|
|
links := make([]string, 0, 10)
|
|
|
|
|
|
|
|
for i, ln := range splitContent {
|
|
|
|
splitContent[i] = strings.Trim(ln, "\r\n")
|
|
|
|
if len([]rune(ln)) > 3 && ln[:2] == "=>" {
|
2019-09-19 22:32:26 +00:00
|
|
|
var link, decorator string
|
|
|
|
subLn := strings.Trim(ln[2:], "\r\n\t \a")
|
|
|
|
splitPoint := strings.IndexAny(subLn, " \t")
|
|
|
|
|
|
|
|
if splitPoint < 0 || len([]rune(subLn)) - 1 <= splitPoint {
|
|
|
|
link = subLn
|
|
|
|
decorator = subLn
|
|
|
|
} else {
|
|
|
|
link = strings.Trim(subLn[:splitPoint], "\t\n\r \a")
|
|
|
|
decorator = strings.Trim(subLn[splitPoint:], "\t\n\r \a")
|
2019-09-19 03:27:56 +00:00
|
|
|
}
|
2019-09-19 22:32:26 +00:00
|
|
|
|
|
|
|
if len(link) > 0 && link[0] == '/' {
|
|
|
|
link = fmt.Sprintf("%s%s", rootUrl, link)
|
|
|
|
} else if len(link) > 0 && strings.Index(link, "://") < 0 {
|
|
|
|
link = fmt.Sprintf("%s/%s", rootUrl, link)
|
2019-09-19 03:27:56 +00:00
|
|
|
}
|
2019-09-19 22:32:26 +00:00
|
|
|
links = append(links, link)
|
2019-09-19 03:27:56 +00:00
|
|
|
linknum := fmt.Sprintf("[%d]", len(links))
|
2019-09-19 22:32:26 +00:00
|
|
|
splitContent[i] = fmt.Sprintf("%-5s %s", linknum, decorator)
|
2019-09-19 03:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(splitContent, "\n"), links
|
|
|
|
}
|
|
|
|
|
2019-09-18 04:57:21 +00:00
|
|
|
|
2019-09-19 03:27:56 +00:00
|
|
|
func MakeCapsule() Capsule {
|
|
|
|
return Capsule{"", "", 0, "", make([]string, 0, 5)}
|
2019-09-18 04:57:21 +00:00
|
|
|
}
|
|
|
|
|