PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/testmode.go

https://code.google.com/p/redwood-filter/
Go | 135 lines | 112 code | 20 blank | 3 comment | 28 complexity | 089daee5c2dac15f69464d2d0ad003cf MD5 | raw file
  1package main
  2
  3import (
  4	"code.google.com/p/go.net/html/charset"
  5	"fmt"
  6	"io/ioutil"
  7	"net/http"
  8	"net/url"
  9	"strings"
 10)
 11
 12// support for running "redwood -test http://example.com"
 13
 14// runURLTest prints debugging information about how the URL and its content would be rated.
 15func runURLTest(u string) {
 16	URL, err := url.Parse(u)
 17	if err != nil {
 18		fmt.Println("Could not parse the URL.")
 19		return
 20	}
 21
 22	if URL.Scheme == "" {
 23		url2, err := url.Parse("http://" + u)
 24		if err == nil {
 25			URL = url2
 26		}
 27	}
 28
 29	fmt.Println("URL:", URL)
 30	fmt.Println()
 31
 32	sc := scorecard{
 33		tally: URLRules.MatchingRules(URL),
 34	}
 35	sc.calculate("")
 36
 37	if len(sc.tally) == 0 {
 38		fmt.Println("No URL rules match.")
 39	} else {
 40		fmt.Println("The following URL rules match:")
 41		for s, _ := range sc.tally {
 42			fmt.Println(s)
 43		}
 44	}
 45
 46	if len(sc.scores) > 0 {
 47		fmt.Println()
 48		fmt.Println("The request has the following category scores:")
 49		printSortedTally(sc.scores)
 50	}
 51
 52	if len(sc.blocked) > 0 {
 53		fmt.Println()
 54		fmt.Println("The request is blocked by the following categories:")
 55		for _, c := range sc.blocked {
 56			fmt.Println(c)
 57		}
 58		fmt.Println()
 59		fmt.Println("But we'll check the content too anyway.")
 60	}
 61
 62	if changeQuery(URL) {
 63		fmt.Println()
 64		fmt.Println("URL modified to:", URL)
 65	}
 66
 67	fmt.Println()
 68	fmt.Println("Downloading content...")
 69	resp, err := http.Get(URL.String())
 70	if err != nil {
 71		fmt.Println(err)
 72		return
 73	}
 74	defer resp.Body.Close()
 75
 76	fmt.Println()
 77
 78	contentType, action := checkContentType(resp)
 79	switch action {
 80	case ALLOW:
 81		fmt.Println("The content doesn't seem to be text, so not running a phrase scan.")
 82		return
 83	case BLOCK:
 84		fmt.Println("The content has a banned MIME type:", contentType)
 85		return
 86	}
 87
 88	content, err := ioutil.ReadAll(resp.Body)
 89	if err != nil {
 90		fmt.Println("Error while reading response body:", err)
 91		return
 92	}
 93
 94	modified := false
 95	_, cs, _ := charset.DetermineEncoding(content, resp.Header.Get("Content-Type"))
 96	if strings.Contains(contentType, "html") {
 97		modified = pruneContent(URL, &content, cs)
 98	}
 99	if modified {
100		cs = "utf-8"
101		fmt.Println("Performed content pruning.")
102		fmt.Println()
103	}
104
105	scanContent(content, contentType, cs, sc.tally)
106	sc.calculate("")
107
108	if len(sc.tally) == 0 {
109		fmt.Println("No content phrases match.")
110	} else {
111		fmt.Println("The following rules match:")
112		printSortedTally(stringTally(sc.tally))
113	}
114
115	if len(sc.scores) > 0 {
116		fmt.Println()
117		fmt.Println("The response has the following category scores:")
118		printSortedTally(sc.scores)
119	}
120
121	if len(sc.blocked) > 0 {
122		fmt.Println()
123		fmt.Println("The page is blocked by the following categories:")
124		for _, c := range sc.blocked {
125			fmt.Println(c)
126		}
127	}
128}
129
130// printSortedTally prints tally's keys and values in descending order by value.
131func printSortedTally(tally map[string]int) {
132	for _, rule := range sortedKeys(tally) {
133		fmt.Println(rule, tally[rule])
134	}
135}