Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/httpx/encodings.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func DecodeData(data []byte, headers http.Header) ([]byte, error) {
// Non UTF-8
if contentTypes, ok := headers["Content-Type"]; ok {
contentType := strings.ToLower(strings.Join(contentTypes, ";"))
// the charset parameter value can be a quoted string (charset="gbk")
contentType = strings.ReplaceAll(contentType, `"`, "")

switch {
case stringsutil.ContainsAny(contentType, "charset=gb2312", "charset=gbk"):
Expand Down
2 changes: 1 addition & 1 deletion common/httpx/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func ExtractTitle(r *Response) (title string) {
}

func CanHaveTitleTag(mimeType string) bool {
return slices.Contains(supportedTitleMimeTypes, mimeType)
return slices.Contains(supportedTitleMimeTypes, strings.ToLower(strings.TrimSpace(mimeType)))
}

func getTitleWithDom(r *Response) (*html.Node, error) {
Expand Down
72 changes: 72 additions & 0 deletions common/httpx/title_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package httpx

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/projectdiscovery/retryablehttp-go"
"github.com/stretchr/testify/require"
)

func TestCanHaveTitleTag(t *testing.T) {
tests := []struct {
mimeType string
expected bool
}{
{"text/html", true},
{"TEXT/HTML", true},
{"Text/Html", true},
{"text/html ", true},
{" application/xhtml+xml", true},
{"text/plain", false},
{"application/json", false},
{"", false},
}

for _, tt := range tests {
t.Run(tt.mimeType, func(t *testing.T) {
require.Equal(t, tt.expected, CanHaveTitleTag(tt.mimeType))
})
}
}

func TestExtractTitleDecodesCharset(t *testing.T) {
options := DefaultOptions
options.CdnCheck = "false"
options.Timeout = 2 * time.Second
options.RetryMax = 0

ht, err := New(&options)
require.Nil(t, err)

// <title>中文</title> with the title text encoded in GBK
gbk := []byte{0xd6, 0xd0, 0xce, 0xc4}
body := append([]byte("<html><head><title>"), gbk...)
body = append(body, []byte("</title></head></html>")...)

tests := []struct {
name string
contentType string
}{
{"quoted charset", `text/html; charset="gbk"`},
{"unquoted charset", "text/html; charset=gbk"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", tt.contentType)
_, _ = w.Write(body)
}))
defer srv.Close()

req, err := retryablehttp.NewRequest(http.MethodGet, srv.URL, nil)
require.Nil(t, err)
resp, err := ht.Do(req, UnsafeOptions{})
require.Nil(t, err)
require.Equal(t, "中文", ExtractTitle(resp))
})
}
}