Skip to content

Commit 270a7bf

Browse files
deorusartyom
authored andcommitted
Apply twitter-specific logic to x.com
1 parent 98bef51 commit 270a7bf

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

internal/useragent/useragent.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
//
66
// Basic usage:
77
//
8-
// client := &http.Client{
9-
// Transport: useragent.Set(http.DefaultTransport, "MyRobot/1.0"),
10-
// }
11-
// resp, err := client.Get("https://...")
8+
// client := &http.Client{
9+
// Transport: useragent.Set(http.DefaultTransport, "MyRobot/1.0"),
10+
// }
11+
// resp, err := client.Get("https://...")
1212
package useragent
1313

1414
import (
@@ -49,7 +49,8 @@ func (t uaT) RoundTrip(r *http.Request) (*http.Response, error) {
4949
r2.Header[k] = v
5050
}
5151
r2.Header.Set("User-Agent", t.userAgent)
52-
if r.URL.Host == "twitter.com" || strings.HasSuffix(r.URL.Host, ".twitter.com") {
52+
if r.URL.Host == "twitter.com" || strings.HasSuffix(r.URL.Host, ".twitter.com") ||
53+
r.URL.Host == "x.com" || strings.HasSuffix(r.URL.Host, ".x.com") {
5354
r2.Header.Set("User-Agent", "DiscourseBot/1.0")
5455
}
5556
return t.Transport.RoundTrip(r2)

unfurlist.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,9 @@ func (h *unfurlHandler) fetchData(ctx context.Context, URL string) (*pageChunk,
472472
return &pageChunk{url: resp.Request.URL}, errors.New("bad status: " + resp.Status)
473473
}
474474
if resp.Header.Get("Content-Encoding") == "deflate" &&
475-
strings.HasSuffix(resp.Request.Host, "twitter.com") {
476-
// twitter sends unsolicited deflate-encoded responses
475+
(strings.HasSuffix(resp.Request.Host, "twitter.com") ||
476+
strings.HasSuffix(resp.Request.Host, "x.com")) {
477+
// twitter/X sends unsolicited deflate-encoded responses
477478
// violating RFC; workaround this.
478479
// See https://golang.org/issues/18779 for background
479480
var err error

0 commit comments

Comments
 (0)