Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Maunium
synapse
Commits
a8a5dd3b
Commit
a8a5dd3b
authored
9 years ago
by
Matthew Hodgson
Browse files
Options
Downloads
Patches
Plain Diff
handle requests with missing content-length headers (e.g. YouTube)
parent
7178ab7d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/http/client.py
+26
-7
26 additions, 7 deletions
synapse/http/client.py
synapse/rest/media/v1/preview_url_resource.py
+2
-2
2 additions, 2 deletions
synapse/rest/media/v1/preview_url_resource.py
with
28 additions
and
9 deletions
synapse/http/client.py
+
26
−
7
View file @
a8a5dd3b
...
...
@@ -23,8 +23,9 @@ from canonicaljson import encode_canonical_json
from
twisted.internet
import
defer
,
reactor
,
ssl
,
protocol
from
twisted.web.client
import
(
RedirectAgent
,
Agent
,
readBody
,
FileBodyProducer
,
PartialDownloadError
,
BrowserLike
RedirectAgent
,
Agent
,
readBody
,
FileBodyProducer
,
PartialDownloadError
,
)
from
twisted.web.http
import
PotentialDataLoss
from
twisted.web.http_headers
import
Headers
from
twisted.web._newclient
import
ResponseDone
...
...
@@ -59,11 +60,11 @@ class SimpleHttpClient(object):
# The default context factory in Twisted 14.0.0 (which we require) is
# BrowserLikePolicyForHTTPS which will do regular cert validation
# 'like a browser'
self
.
agent
=
RedirectAgent
(
Agent
(
self
.
agent
=
Agent
(
reactor
,
connectTimeout
=
15
,
contextFactory
=
hs
.
get_http_client_context_factory
()
)
)
)
self
.
user_agent
=
hs
.
version_string
if
hs
.
config
.
user_agent_suffix
:
self
.
user_agent
=
"
%s %s
"
%
(
self
.
user_agent
,
hs
.
config
.
user_agent_suffix
,)
...
...
@@ -253,10 +254,6 @@ class SimpleHttpClient(object):
headers.
"""
def
body_callback
(
method
,
url_bytes
,
headers_dict
):
self
.
sign_request
(
destination
,
method
,
url_bytes
,
headers_dict
)
return
None
response
=
yield
self
.
request
(
"
GET
"
,
url
.
encode
(
"
ascii
"
),
...
...
@@ -309,6 +306,10 @@ class _ReadBodyToFileProtocol(protocol.Protocol):
def
connectionLost
(
self
,
reason
):
if
reason
.
check
(
ResponseDone
):
self
.
deferred
.
callback
(
self
.
length
)
elif
reason
.
check
(
PotentialDataLoss
):
# stolen from https://github.com/twisted/treq/pull/49/files
# http://twistedmatrix.com/trac/ticket/4840
self
.
deferred
.
callback
(
self
.
length
)
else
:
self
.
deferred
.
errback
(
reason
)
...
...
@@ -350,6 +351,24 @@ class CaptchaServerHttpClient(SimpleHttpClient):
# twisted dislikes google's response, no content length.
defer
.
returnValue
(
e
.
response
)
class
SpiderHttpClient
(
SimpleHttpClient
):
"""
Separate HTTP client for spidering arbitrary URLs.
Special in that it follows retries and has a UA that looks
like a browser.
used by the preview_url endpoint in the content repo.
"""
def
__init__
(
self
,
hs
):
SimpleHttpClient
.
__init__
(
self
,
hs
)
# clobber the base class's agent and UA:
self
.
agent
=
BrowserLikeRedirectAgent
(
Agent
(
reactor
,
connectTimeout
=
15
,
contextFactory
=
hs
.
get_http_client_context_factory
()
))
# Look like Chrome for now
#self.user_agent = ("Mozilla/5.0 (%s) (KHTML, like Gecko) Chrome Safari" % hs.version_string)
def
encode_urlencode_args
(
args
):
return
{
k
:
encode_urlencode_arg
(
v
)
for
k
,
v
in
args
.
items
()}
...
...
This diff is collapsed.
Click to expand it.
synapse/rest/media/v1/preview_url_resource.py
+
2
−
2
View file @
a8a5dd3b
...
...
@@ -19,7 +19,7 @@ from twisted.web.server import NOT_DONE_YET
from
twisted.internet
import
defer
from
lxml
import
html
from
synapse.util.stringutils
import
random_string
from
synapse.http.client
import
S
imple
HttpClient
from
synapse.http.client
import
S
pider
HttpClient
from
synapse.http.server
import
request_handler
,
respond_with_json
,
respond_with_json_bytes
import
os
...
...
@@ -33,7 +33,7 @@ class PreviewUrlResource(BaseMediaResource):
def
__init__
(
self
,
hs
,
filepaths
):
BaseMediaResource
.
__init__
(
self
,
hs
,
filepaths
)
self
.
client
=
S
imple
HttpClient
(
hs
)
self
.
client
=
S
pider
HttpClient
(
hs
)
def
render_GET
(
self
,
request
):
self
.
_async_render_GET
(
request
)
...
...
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