good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
chat
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
github
tinode
chat
Commits
3fbfc5ce
Commit
3fbfc5ce
authored
3 months ago
by
yinebebt
Browse files
Options
Downloads
Patches
Plain Diff
fix: use TagAttr and move trim space to sanitization
parent
49f7b809
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/linkpreview.go
+39
-27
39 additions, 27 deletions
server/linkpreview.go
with
39 additions
and
27 deletions
server/linkpreview.go
+
39
−
27
View file @
3fbfc5ce
...
...
@@ -86,11 +86,13 @@ func previewLink(w http.ResponseWriter, r *http.Request) {
if
cc
:=
resp
.
Header
.
Get
(
"Cache-Control"
);
cc
!=
""
{
w
.
Header
()
.
Set
(
"Cache-Control"
,
cc
)
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
r
.
Method
==
http
.
MethodHead
{
w
.
WriteHeader
(
http
.
StatusOK
)
return
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
err
:=
json
.
NewEncoder
(
w
)
.
Encode
(
extractMetadata
(
body
));
err
!=
nil
{
http
.
Error
(
w
,
"Failed to encode response"
,
http
.
StatusInternalServerError
)
}
...
...
@@ -101,50 +103,60 @@ func extractMetadata(body io.Reader) *linkPreview {
var
inTitleTag
bool
tokenizer
:=
html
.
NewTokenizer
(
body
)
lp
:
for
{
switch
tokenizer
.
Next
()
{
case
html
.
ErrorToken
:
re
turn
sanitizePreview
(
preview
)
b
re
ak
lp
case
html
.
StartTagToken
,
html
.
SelfClosingTagToken
:
token
:=
tokenizer
.
Token
()
if
token
.
DataAtom
==
atom
.
Meta
{
tag
,
hasAttr
:=
tokenizer
.
TagName
()
tagName
:=
atom
.
Lookup
(
tag
)
if
tagName
==
atom
.
Meta
&&
hasAttr
{
var
name
,
property
,
content
string
for
_
,
attr
:=
range
token
.
Attr
{
switch
attr
.
Key
{
for
{
key
,
val
,
moreAttr
:=
tokenizer
.
TagAttr
()
switch
atom
.
String
(
key
)
{
case
"name"
:
name
=
attr
.
V
al
name
=
string
(
v
al
)
case
"property"
:
property
=
attr
.
V
al
property
=
string
(
v
al
)
case
"content"
:
content
=
attr
.
Val
content
=
string
(
val
)
}
if
!
moreAttr
{
break
}
}
if
strings
.
HasPrefix
(
property
,
"og:"
)
&&
content
!=
""
{
switch
property
{
case
"og:title"
:
preview
.
Title
=
content
case
"og:description"
:
if
content
!=
""
{
if
strings
.
HasPrefix
(
property
,
"og:"
)
{
switch
property
{
case
"og:title"
:
preview
.
Title
=
content
case
"og:description"
:
preview
.
Description
=
content
case
"og:image"
:
preview
.
ImageURL
=
content
}
}
else
if
name
==
"description"
&&
preview
.
Description
==
""
{
preview
.
Description
=
content
case
"og:image"
:
preview
.
ImageURL
=
content
}
}
else
if
name
==
"description"
&&
preview
.
Description
==
""
{
preview
.
Description
=
content
}
}
else
if
t
oken
.
DataAtom
==
atom
.
Title
{
}
else
if
t
agName
==
atom
.
Title
{
inTitleTag
=
true
}
case
html
.
TextToken
:
if
preview
.
Title
==
""
&&
inTitleTag
{
preview
.
Title
=
strings
.
TrimSpace
(
tokenizer
.
Token
()
.
Data
)
}
case
html
.
EndTagToken
:
if
tokenizer
.
Token
()
.
DataAtom
==
atom
.
Title
{
if
inTitleTag
{
if
preview
.
Title
==
""
{
preview
.
Title
=
tokenizer
.
Token
()
.
Data
}
inTitleTag
=
false
}
case
html
.
EndTagToken
:
inTitleTag
=
false
}
if
preview
.
Title
!=
""
&&
preview
.
Description
!=
""
&&
preview
.
ImageURL
!=
""
{
break
...
...
@@ -174,13 +186,13 @@ func validateURL(u *url.URL) error {
func
sanitizePreview
(
preview
linkPreview
)
*
linkPreview
{
if
utf8
.
RuneCountInString
(
preview
.
Title
)
>
80
{
preview
.
Title
=
string
([]
rune
(
preview
.
Title
)[
:
80
])
preview
.
Title
=
string
([]
rune
(
strings
.
TrimSpace
(
preview
.
Title
)
)
[
:
80
])
}
if
utf8
.
RuneCountInString
(
preview
.
Description
)
>
256
{
preview
.
Description
=
string
([]
rune
(
preview
.
Description
)[
:
256
])
preview
.
Description
=
string
([]
rune
(
strings
.
TrimSpace
(
preview
.
Description
)
)
[
:
256
])
}
if
len
(
preview
.
ImageURL
)
>
2000
{
preview
.
ImageURL
=
preview
.
ImageURL
[
:
2000
]
preview
.
ImageURL
=
strings
.
TrimSpace
(
preview
.
ImageURL
)
[
:
2000
]
}
return
&
preview
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment