Skip to content

Commit

Permalink
Apply twitter-specific logic to x.com
Browse files Browse the repository at this point in the history
  • Loading branch information
deorus authored and artyom committed May 24, 2024
1 parent 98bef51 commit 270a7bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions internal/useragent/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//
// Basic usage:
//
// client := &http.Client{
// Transport: useragent.Set(http.DefaultTransport, "MyRobot/1.0"),
// }
// resp, err := client.Get("https://...")
// client := &http.Client{
// Transport: useragent.Set(http.DefaultTransport, "MyRobot/1.0"),
// }
// resp, err := client.Get("https://...")
package useragent

import (
Expand Down Expand Up @@ -49,7 +49,8 @@ func (t uaT) RoundTrip(r *http.Request) (*http.Response, error) {
r2.Header[k] = v
}
r2.Header.Set("User-Agent", t.userAgent)
if r.URL.Host == "twitter.com" || strings.HasSuffix(r.URL.Host, ".twitter.com") {
if r.URL.Host == "twitter.com" || strings.HasSuffix(r.URL.Host, ".twitter.com") ||
r.URL.Host == "x.com" || strings.HasSuffix(r.URL.Host, ".x.com") {
r2.Header.Set("User-Agent", "DiscourseBot/1.0")
}
return t.Transport.RoundTrip(r2)
Expand Down
5 changes: 3 additions & 2 deletions unfurlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ func (h *unfurlHandler) fetchData(ctx context.Context, URL string) (*pageChunk,
return &pageChunk{url: resp.Request.URL}, errors.New("bad status: " + resp.Status)
}
if resp.Header.Get("Content-Encoding") == "deflate" &&
strings.HasSuffix(resp.Request.Host, "twitter.com") {
// twitter sends unsolicited deflate-encoded responses
(strings.HasSuffix(resp.Request.Host, "twitter.com") ||
strings.HasSuffix(resp.Request.Host, "x.com")) {
// twitter/X sends unsolicited deflate-encoded responses
// violating RFC; workaround this.
// See https://golang.org/issues/18779 for background
var err error
Expand Down

0 comments on commit 270a7bf

Please sign in to comment.