File: http_render.go

package info (click to toggle)
bombadillo 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: makefile: 59
file content (98 lines) | stat: -rw-r--r-- 2,200 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package http

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os/exec"
	"strings"
)

// Page represents the contents and links or an http/https document
type Page struct {
	Content string
	Links   []string
}

// Visit is the main entry to viewing a web document in bombadillo.
// It takes a url, a terminal width, and which web backend the user
// currently has set. Visit returns a Page and an error
func Visit(webmode, url string, width int) (Page, error) {
	if width > 80 {
		width = 80
	}
	var w string
	switch webmode {
	case "lynx":
		w = "-width"
	case "w3m":
		w = "-cols"
	case "elinks":
		w = "-dump-width"
	default:
		return Page{}, fmt.Errorf("Invalid webmode setting")
	}
	c, err := exec.Command(webmode, "-dump", w, fmt.Sprintf("%d", width), url).Output()
	if err != nil && c == nil {
		return Page{}, err
	}
	return parseLinks(string(c)), nil
}

// IsTextFile makes an http(s) head request to a given URL
// and determines if the content-type is text based. It then
// returns a bool
func IsTextFile(url string) bool {
	resp, err := http.Head(url)
	if err != nil {
		return false
	}
	ctype := resp.Header.Get("content-type")
	if strings.Contains(ctype, "text") || ctype == "" {
		return true
	}

	return false
}

func parseLinks(c string) Page {
	var out Page
	contentUntil := strings.LastIndex(c, "References")
	if contentUntil >= 1 {
		out.Content = c[:contentUntil]
	} else {
		out.Content = c
		out.Links = make([]string, 0)
		return out
	}
	links := c[contentUntil+11:]
	links = strings.TrimSpace(links)
	linkSlice := strings.Split(links, "\n")
	out.Links = make([]string, 0, len(linkSlice))
	for _, link := range linkSlice {
		ls := strings.SplitN(link, ".", 2)
		if len(ls) < 2 {
			continue
		}
		out.Links = append(out.Links, strings.TrimSpace(ls[1]))
	}
	return out
}

// Fetch makes an http(s) request and returns the []bytes
// for the response and an error. Fetch is used for saving
// the source file of an http(s) document
func Fetch(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return []byte{}, err
	}
	defer resp.Body.Close()

	bodyBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return []byte{}, err
	}

	return bodyBytes, nil
}