package main import ( "fmt" "io/ioutil" "net" "net/url" "path" "strings" "time" ) var GopherTimeout time.Duration = time.Duration(settings.TimeOutSeconds) * time.Second func RetrieveGopher(u *url.URL) (string, error) { conn, err := net.DialTimeout("tcp", u.Host, time.Duration(settings.TimeOutSeconds) * time.Second) if err != nil { return ConvertErrorToHTML(50, "Unable to connect to host"), nil } gophertype := '1' send := "/" if len(u.Path) > 2 { send = u.Path[2:] gophertype = rune(u.Path[1]) } else if len(u.Path) == 2 { gophertype = rune(u.Path[1]) } if u.RawQuery != "" { send = send + "?" + u.RawQuery } _, err = conn.Write([]byte(send+"\n")) if err != nil { return ConvertErrorToHTML(50, "Unable to make request to host; connection closed"), nil } result, err := ioutil.ReadAll(conn) if err != nil { return ConvertErrorToHTML(50, "Unable to read response from host"), nil } return ParseGopherBody(string(result), u, gophertype) } func EditHTMLFile(html string) string { bodyStart := strings.Index(html, "
-1 { html = html[bodyStart:] bodyStartEnd := strings.Index(html, ">") if bodyStartEnd > -1 { html = html[bodyStartEnd+1:] } } bodyEnd := strings.Index(html, "") if bodyEnd > -1 { html = html[:bodyEnd] } html = strings.ReplaceAll(html, "