2019-10-23 05:02:32 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-26 03:06:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-10-23 05:02:32 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-11-15 03:30:58 +00:00
|
|
|
// Page represents the contents and links or an http/https document
|
|
|
|
type Page struct {
|
2019-10-23 05:13:08 +00:00
|
|
|
Content string
|
|
|
|
Links []string
|
2019-10-23 05:02:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:30:58 +00:00
|
|
|
// 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) {
|
2019-10-23 05:02:32 +00:00
|
|
|
if width > 80 {
|
|
|
|
width = 80
|
|
|
|
}
|
2019-11-14 06:23:57 +00:00
|
|
|
var w string
|
|
|
|
switch webmode {
|
|
|
|
case "lynx":
|
|
|
|
w = "-width"
|
|
|
|
case "w3m":
|
|
|
|
w = "-cols"
|
|
|
|
case "elinks":
|
|
|
|
w = "-dump-width"
|
|
|
|
default:
|
2019-11-15 03:30:58 +00:00
|
|
|
return Page{}, fmt.Errorf("Invalid webmode setting")
|
2019-11-14 06:23:57 +00:00
|
|
|
}
|
|
|
|
c, err := exec.Command(webmode, "-dump", w, fmt.Sprintf("%d", width), url).Output()
|
2019-10-23 05:02:32 +00:00
|
|
|
if err != nil {
|
2019-11-15 03:30:58 +00:00
|
|
|
return Page{}, err
|
2019-10-23 05:02:32 +00:00
|
|
|
}
|
|
|
|
return parseLinks(string(c)), nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:30:58 +00:00
|
|
|
// 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
|
2019-10-26 03:06:13 +00:00
|
|
|
func IsTextFile(url string) bool {
|
2019-11-14 06:23:57 +00:00
|
|
|
resp, err := http.Head(url)
|
2019-10-26 03:06:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-14 06:23:57 +00:00
|
|
|
ctype := resp.Header.Get("content-type")
|
|
|
|
if strings.Contains(ctype, "text") || ctype == "" {
|
|
|
|
return true
|
2019-10-26 03:06:13 +00:00
|
|
|
}
|
2019-10-29 05:14:11 +00:00
|
|
|
|
2019-11-14 06:23:57 +00:00
|
|
|
return false
|
2019-10-26 03:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:30:58 +00:00
|
|
|
func parseLinks(c string) Page {
|
|
|
|
var out Page
|
2019-10-23 05:02:32 +00:00
|
|
|
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
|
|
|
|
}
|
2019-10-26 03:06:13 +00:00
|
|
|
|
2019-11-15 03:30:58 +00:00
|
|
|
// 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
|
2019-10-26 03:06:13 +00:00
|
|
|
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
|
|
|
|
}
|