Skip to content

Commit eeaa098

Browse files
committed
fix bug: p.DOC nil panic.
1 parent 05ae01a commit eeaa098

5 files changed

Lines changed: 56 additions & 10 deletions

File tree

internal/fetcher/sites/boxun/boxun.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func SetPost(p *Post) error {
3939
}
4040

4141
func SetDate(p *Post) error {
42+
if p.URL == nil {
43+
return fmt.Errorf("[-] p.URL is nil")
44+
}
4245
rawdate := filepath.Base(p.URL.String())
4346
var Y, M, D, hh, mm int
4447
var err error
@@ -91,6 +94,9 @@ func SetDate(p *Post) error {
9194
}
9295

9396
func SetTitle(p *Post) error {
97+
if p.DOC == nil {
98+
return fmt.Errorf("[-] p.DOC is nil")
99+
}
94100
n := htmldoc.ElementsByTag(p.DOC, "title")
95101
if n == nil {
96102
return fmt.Errorf("[-] there is no element <title>")
@@ -107,7 +113,7 @@ func SetTitle(p *Post) error {
107113

108114
func SetBody(p *Post) error {
109115
if p.DOC == nil {
110-
return errors.New("[-] there is no DOC object to get and format.")
116+
return fmt.Errorf("[-] p.DOC is nil")
111117
}
112118
b, err := Boxun(p)
113119
if err != nil {
@@ -123,6 +129,9 @@ func SetBody(p *Post) error {
123129
}
124130

125131
func Boxun(p *Post) (string, error) {
132+
if p.DOC == nil {
133+
return "", fmt.Errorf("[-] p.DOC is nil")
134+
}
126135
doc := p.DOC
127136
body := ""
128137
// Fetch content nodes

internal/fetcher/sites/dwnews/dwnews.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func SetPost(p *Post) error {
3737
}
3838

3939
func SetDate(p *Post) error {
40+
if p.DOC == nil {
41+
return fmt.Errorf("[-] p.DOC is nil")
42+
}
4043
metas := htmldoc.MetasByName(p.DOC, "parsely-pub-date")
4144
cs := []string{}
4245
for _, meta := range metas {
@@ -54,6 +57,9 @@ func SetDate(p *Post) error {
5457
}
5558

5659
func SetTitle(p *Post) error {
60+
if p.DOC == nil {
61+
return fmt.Errorf("[-] p.DOC is nil")
62+
}
5763
n := htmldoc.ElementsByTag(p.DOC, "title")
5864
if n == nil {
5965
return fmt.Errorf("[-] there is no element <title>")
@@ -71,7 +77,7 @@ func SetTitle(p *Post) error {
7177

7278
func SetBody(p *Post) error {
7379
if p.DOC == nil {
74-
return errors.New("[-] there is no DOC object to get and format.")
80+
return fmt.Errorf("[-] p.DOC is nil")
7581
}
7682
b, err := Dwnews(p)
7783
if err != nil {
@@ -87,6 +93,9 @@ func SetBody(p *Post) error {
8793
}
8894

8995
func Dwnews(p *Post) (string, error) {
96+
if p.DOC == nil {
97+
return "", fmt.Errorf("[-] p.DOC is nil")
98+
}
9099
doc := p.DOC
91100
body := ""
92101
// Fetch content nodes

internal/fetcher/sites/rfa/rfa.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func SetPost(p *Post) error {
3939
}
4040

4141
func SetDate(p *Post) error {
42+
if p.DOC == nil {
43+
return fmt.Errorf("[-] p.DOC is nil")
44+
}
4245
doc := htmldoc.ElementsByTagAndType(p.DOC, "script", "application/ld+json")
4346
if doc == nil {
4447
return errors.New("[-] rfa SetDate err, cannot get target nodes.")
@@ -55,6 +58,9 @@ func SetDate(p *Post) error {
5558
}
5659

5760
func SetTitle(p *Post) error {
61+
if p.DOC == nil {
62+
return fmt.Errorf("[-] p.DOC is nil")
63+
}
5864
n := htmldoc.ElementsByTag(p.DOC, "title")
5965
if n == nil {
6066
return fmt.Errorf("[-] there is no element <title>")
@@ -68,7 +74,7 @@ func SetTitle(p *Post) error {
6874

6975
func SetBody(p *Post) error {
7076
if p.DOC == nil {
71-
return errors.New("[-] there is no DOC object to get and format.")
77+
return fmt.Errorf("[-] p.DOC is nil")
7278
}
7379
b, err := Rfa(p)
7480
if err != nil {
@@ -85,7 +91,7 @@ func SetBody(p *Post) error {
8591

8692
func Rfa(p *Post) (string, error) {
8793
if p.Raw == nil {
88-
return "", errors.New("\n[-] FmtBodyRfa() parameter is nil!\n")
94+
return "", errors.New("\n[-] p.Raw is nil!\n")
8995
}
9096
var ps [][]byte
9197
var b bytes.Buffer

internal/fetcher/sites/voachinese/voachinese.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func SetPost(p *Post) error {
3737
}
3838

3939
func SetDate(p *Post) error {
40+
if p.DOC == nil {
41+
return fmt.Errorf("[-] p.DOC is nil")
42+
}
4043
doc := htmldoc.ElementsByTag(p.DOC, "time")
4144
// p.Date = doc[0].Attr[0].Val // short but not robust enough
4245
d := []string{}
@@ -53,6 +56,9 @@ func SetDate(p *Post) error {
5356
}
5457

5558
func SetTitle(p *Post) error {
59+
if p.DOC == nil {
60+
return fmt.Errorf("[-] p.DOC is nil")
61+
}
5662
n := htmldoc.ElementsByTag(p.DOC, "title")
5763
if n == nil {
5864
return fmt.Errorf("[-] there is no element <title>")
@@ -66,7 +72,7 @@ func SetTitle(p *Post) error {
6672

6773
func SetBody(p *Post) error {
6874
if p.DOC == nil {
69-
return errors.New("[-] there is no DOC object to get and format.")
75+
return fmt.Errorf("[-] p.DOC is nil")
7076
}
7177
b, err := Voa(p)
7278
if err != nil {
@@ -82,6 +88,10 @@ func SetBody(p *Post) error {
8288
}
8389

8490
func Voa(p *Post) (string, error) {
91+
if p.DOC == nil {
92+
return "", fmt.Errorf("[-] p.DOC is nil")
93+
94+
}
8595
doc := p.DOC
8696
body := ""
8797
// Fetch content nodes

internal/htmldoc/htmldoc.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func ExtractLinks(weburl string) ([]string, error) {
8686

8787
func ElementsNext(doc *html.Node) []*html.Node {
8888
nodes := []*html.Node{}
89+
if doc == nil {
90+
return nil
91+
}
8992
visitNode := func(n *html.Node) {
9093
if n.NextSibling != nil {
9194
nodes = append(nodes, n)
@@ -96,7 +99,7 @@ func ElementsNext(doc *html.Node) []*html.Node {
9699
}
97100

98101
func ElementsRmByTag(doc *html.Node, name ...string) {
99-
if len(name) == 0 {
102+
if len(name) == 0 || doc == nil {
100103
return
101104
}
102105
visitNode := func(n *html.Node) {
@@ -123,7 +126,7 @@ func ElementsRmByTag(doc *html.Node, name ...string) {
123126

124127
func ElementsByTag(doc *html.Node, name ...string) []*html.Node {
125128
var nodes []*html.Node
126-
if len(name) == 0 {
129+
if len(name) == 0 || doc == nil {
127130
return nil
128131
}
129132
if doc.Type == html.ElementNode {
@@ -141,7 +144,7 @@ func ElementsByTag(doc *html.Node, name ...string) []*html.Node {
141144

142145
func ElementsByTagAndClass(doc *html.Node, tag, class string) []*html.Node {
143146
var nodes []*html.Node
144-
if tag == "" || class == "" {
147+
if tag == "" || class == "" || doc == nil {
145148
return nil
146149
}
147150
if doc.Type == html.ElementNode {
@@ -160,6 +163,9 @@ func ElementsByTagAndClass(doc *html.Node, tag, class string) []*html.Node {
160163
}
161164

162165
func ElementsByTagAndClass2(raw []byte, tag, class string) []byte {
166+
if raw == nil || tag == "" || class == "" {
167+
return nil
168+
}
163169
z := html.NewTokenizer(bytes.NewReader(raw))
164170
var b bytes.Buffer
165171
for {
@@ -184,7 +190,7 @@ func ElementsByTagAndClass2(raw []byte, tag, class string) []byte {
184190

185191
func ElementsByTagAndId(doc *html.Node, tag, id string) []*html.Node {
186192
var nodes []*html.Node
187-
if tag == "" || id == "" {
193+
if doc == nil || tag == "" || id == "" {
188194
return nil
189195
}
190196
if doc.Type == html.ElementNode {
@@ -203,6 +209,9 @@ func ElementsByTagAndId(doc *html.Node, tag, id string) []*html.Node {
203209
}
204210

205211
func ElementsByTagAndId2(raw []byte, tag, id string) []byte {
212+
if raw == nil || tag == "" || id == "" {
213+
return nil
214+
}
206215
z := html.NewTokenizer(bytes.NewReader(raw))
207216
for {
208217
tt := z.Next()
@@ -225,7 +234,7 @@ func ElementsByTagAndId2(raw []byte, tag, id string) []byte {
225234
}
226235
func ElementsByTagAndType(doc *html.Node, tag, attrType string) []*html.Node {
227236
var nodes []*html.Node
228-
if tag == "" || attrType == "" {
237+
if tag == "" || attrType == "" || doc == nil {
229238
return nil
230239
}
231240
if doc.Type == html.ElementNode {
@@ -245,6 +254,9 @@ func ElementsByTagAndType(doc *html.Node, tag, attrType string) []*html.Node {
245254

246255
func ElementsNextByTag(doc *html.Node, tag string) []*html.Node {
247256
var nodes []*html.Node
257+
if tag == "" || doc == nil {
258+
return nil
259+
}
248260
if doc == nil || tag == "" {
249261
return nil
250262
}

0 commit comments

Comments
 (0)