2020-06-20 07:09:01 +03:00
|
|
|
package renderer
|
|
|
|
|
|
|
|
import (
|
2020-07-10 22:33:39 +03:00
|
|
|
"bytes"
|
2020-06-20 07:09:01 +03:00
|
|
|
"errors"
|
2020-07-10 22:33:39 +03:00
|
|
|
"io"
|
2020-06-20 07:09:01 +03:00
|
|
|
"mime"
|
|
|
|
"strings"
|
2020-07-10 22:33:39 +03:00
|
|
|
"time"
|
2020-06-20 07:09:01 +03:00
|
|
|
|
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2020-07-10 22:33:39 +03:00
|
|
|
"github.com/spf13/viper"
|
2020-06-20 07:09:01 +03:00
|
|
|
"golang.org/x/text/encoding/ianaindex"
|
|
|
|
)
|
|
|
|
|
2020-07-10 22:33:39 +03:00
|
|
|
var ErrTooLarge = errors.New("page content would be too large")
|
|
|
|
var ErrTimedOut = errors.New("page download timed out")
|
|
|
|
|
2020-06-20 07:09:01 +03:00
|
|
|
// isUTF8 returns true for charsets that are compatible with UTF-8 and don't need to be decoded.
|
|
|
|
func isUTF8(charset string) bool {
|
|
|
|
utfCharsets := []string{"", "utf-8", "us-ascii"}
|
|
|
|
for i := range utfCharsets {
|
|
|
|
if strings.ToLower(charset) == utfCharsets[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanDisplay returns true if the response is supported by Amfora
|
|
|
|
// for displaying on the screen.
|
|
|
|
// It also doubles as a function to detect whether something can be stored in a Page struct.
|
|
|
|
func CanDisplay(res *gemini.Response) bool {
|
|
|
|
if gemini.SimplifyStatus(res.Status) != 20 {
|
|
|
|
// No content
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
mediatype, params, err := mime.ParseMediaType(res.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-22 06:49:43 +03:00
|
|
|
if !strings.HasPrefix(mediatype, "text/") {
|
2020-06-20 07:09:01 +03:00
|
|
|
// Amfora doesn't support other filetypes
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if isUTF8(params["charset"]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
enc, err := ianaindex.MIME.Encoding(params["charset"]) // Lowercasing is done inside
|
|
|
|
// Encoding sometimes returns nil, see #3 on this repo and golang/go#19421
|
|
|
|
return err == nil && enc != nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 06:55:24 +03:00
|
|
|
// MakePage creates a formatted, rendered Page from the given network response and params.
|
|
|
|
// You must set the Page.Width value yourself.
|
|
|
|
func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs.Page, error) {
|
2020-06-20 07:09:01 +03:00
|
|
|
if !CanDisplay(res) {
|
|
|
|
return nil, errors.New("not valid content for a Page")
|
|
|
|
}
|
|
|
|
|
2020-07-10 22:33:39 +03:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Duration(viper.GetInt("a-general.page_max_time")) * time.Second)
|
|
|
|
res.Body.Close()
|
|
|
|
}()
|
|
|
|
|
2020-07-26 19:12:54 +03:00
|
|
|
_, err := io.CopyN(buf, res.Body, viper.GetInt64("a-general.page_max_size")+1)
|
2020-07-10 22:33:39 +03:00
|
|
|
res.Body.Close()
|
|
|
|
rawText := buf.Bytes()
|
|
|
|
if err == nil {
|
2020-07-26 19:12:54 +03:00
|
|
|
// Content was larger than max size
|
2020-07-10 22:33:39 +03:00
|
|
|
return nil, ErrTooLarge
|
|
|
|
} else if err != io.EOF {
|
|
|
|
if strings.HasSuffix(err.Error(), "use of closed network connection") {
|
|
|
|
// Timed out
|
|
|
|
return nil, ErrTimedOut
|
|
|
|
}
|
|
|
|
// Some other error
|
2020-06-20 07:09:01 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-10 22:33:39 +03:00
|
|
|
// Otherwise, the error is EOF, which is what we want.
|
2020-06-20 07:09:01 +03:00
|
|
|
|
|
|
|
mediatype, params, _ := mime.ParseMediaType(res.Meta)
|
|
|
|
|
|
|
|
// Convert content first
|
|
|
|
var utfText string
|
|
|
|
if isUTF8(params["charset"]) {
|
|
|
|
utfText = string(rawText)
|
|
|
|
} else {
|
|
|
|
encoding, err := ianaindex.MIME.Encoding(params["charset"])
|
|
|
|
if encoding == nil || err != nil {
|
|
|
|
// Some encoding doesn't exist and wasn't caught in CanDisplay()
|
|
|
|
return nil, errors.New("unsupported encoding")
|
|
|
|
}
|
|
|
|
utfText, err = encoding.NewDecoder().String(string(rawText))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mediatype == "text/gemini" {
|
2020-07-03 06:55:24 +03:00
|
|
|
rendered, links := RenderGemini(utfText, width, leftMargin)
|
2020-06-20 07:09:01 +03:00
|
|
|
return &structs.Page{
|
2020-07-04 00:28:56 +03:00
|
|
|
Mediatype: structs.TextGemini,
|
|
|
|
Url: url,
|
|
|
|
Raw: utfText,
|
|
|
|
Content: rendered,
|
|
|
|
Links: links,
|
2020-06-20 07:09:01 +03:00
|
|
|
}, nil
|
2020-06-22 06:49:43 +03:00
|
|
|
} else if strings.HasPrefix(mediatype, "text/") {
|
2020-07-28 23:58:32 +03:00
|
|
|
if mediatype == "text/x-ansi" || strings.HasSuffix(url, ".ans") || strings.HasSuffix(url, ".ansi") {
|
2020-07-11 00:45:14 +03:00
|
|
|
// ANSI
|
|
|
|
return &structs.Page{
|
|
|
|
Mediatype: structs.TextAnsi,
|
|
|
|
Url: url,
|
|
|
|
Raw: utfText,
|
|
|
|
Content: RenderANSI(utfText, leftMargin),
|
|
|
|
Links: []string{},
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
// Treated as plaintext
|
|
|
|
return &structs.Page{
|
|
|
|
Mediatype: structs.TextPlain,
|
|
|
|
Url: url,
|
|
|
|
Raw: utfText,
|
|
|
|
Content: RenderPlainText(utfText, leftMargin),
|
|
|
|
Links: []string{},
|
|
|
|
}, nil
|
|
|
|
}
|
2020-06-20 07:09:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("displayable mediatype is not handled in the code, implementation error")
|
|
|
|
}
|