Add http_proxy support for http client (#5571)

- if 'http_proxy' environment variable is set, respect it when creating http client
- otherwise initialize a http client with timeout settings

Add ogjson to cache even when it fails

in this way we can prevent from requesting unparsable urls repeatedly

Extend expire time of cached link preview data to a week

There's no need to invalidate cache and send request again frequently

Revert timeout

Revert cache_expire_time
Этот коммит содержится в:
Josta Yee
2017-03-20 21:20:42 +08:00
коммит произвёл enahum
родитель 76f8420a52
Коммит e86add77ad
2 изменённых файлов: 32 добавлений и 6 удалений

Просмотреть файл

@@ -530,11 +530,11 @@ func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
og := app.GetOpenGraphMetadata(url) og := app.GetOpenGraphMetadata(url)
ogJSON, err := og.ToJSON() ogJSON, err := og.ToJSON()
openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 hour
if err != nil { if err != nil {
w.Write([]byte(`{"url": ""}`)) w.Write([]byte(`{"url": ""}`))
return return
} }
openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 houre
w.Write(ogJSON) w.Write(ogJSON)
} }

Просмотреть файл

@@ -4,7 +4,10 @@
package app package app
import ( import (
"net"
"net/http" "net/http"
"net/url"
"os"
"regexp" "regexp"
"time" "time"
@@ -17,13 +20,35 @@ import (
) )
var ( var (
c = &http.Client{ httpClient *http.Client
Timeout: 5 * time.Second,
}
httpTimeout = time.Duration(5 * time.Second)
linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
) )
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, httpTimeout)
}
func init() {
p, ok := os.LookupEnv("HTTP_PROXY")
if ok {
if u, err := url.Parse(p); err == nil {
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(u),
Dial: dialTimeout,
},
}
return
}
}
httpClient = &http.Client{
Timeout: httpTimeout,
}
}
func CreatePostAsUser(post *model.Post, siteURL string) (*model.Post, *model.AppError) { func CreatePostAsUser(post *model.Post, siteURL string) (*model.Post, *model.AppError) {
// Check that channel has not been deleted // Check that channel has not been deleted
var channel *model.Channel var channel *model.Channel
@@ -484,14 +509,15 @@ func GetFileInfosForPost(postId string, readFromMaster bool) ([]*model.FileInfo,
func GetOpenGraphMetadata(url string) *opengraph.OpenGraph { func GetOpenGraphMetadata(url string) *opengraph.OpenGraph {
og := opengraph.NewOpenGraph() og := opengraph.NewOpenGraph()
res, err := c.Get(url) res, err := httpClient.Get(url)
if err != nil { if err != nil {
l4g.Error(err.Error())
return og return og
} }
defer CloseBody(res) defer CloseBody(res)
if err := og.ProcessHTML(res.Body); err != nil { if err := og.ProcessHTML(res.Body); err != nil {
return og l4g.Error(err.Error())
} }
return og return og