Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
c2c50c8f
Commit
c2c50c8f
authored
Apr 22, 2013
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2630 from sergevanginderachter/uri-redirect
URI module -- make it support HTTP 301, 302, 303, 304, 307
parents
40c7afbb
5c0daf9e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
22 deletions
+52
-22
library/uri
+52
-22
No files found.
library/uri
View file @
c2c50c8f
...
@@ -23,6 +23,7 @@
...
@@ -23,6 +23,7 @@
import
shutil
import
shutil
import
tempfile
import
tempfile
import
base64
import
base64
import
datetime
try
:
try
:
import
json
import
json
except
ImportError
:
except
ImportError
:
...
@@ -206,7 +207,7 @@ def write_file(module, url, dest, content):
...
@@ -206,7 +207,7 @@ def write_file(module, url, dest, content):
else
:
else
:
if
not
os
.
access
(
os
.
path
.
dirname
(
dest
),
os
.
W_OK
):
if
not
os
.
access
(
os
.
path
.
dirname
(
dest
),
os
.
W_OK
):
os
.
remove
(
tmpsrc
)
os
.
remove
(
tmpsrc
)
module
.
fail_json
(
msg
=
"Destination
%
s not writable"
%
(
os
.
path
.
dirname
(
dest
)))
module
.
fail_json
(
msg
=
"Destination
dir
%
s not writable"
%
(
os
.
path
.
dirname
(
dest
)))
if
md5sum_src
!=
md5sum_dest
:
if
md5sum_src
!=
md5sum_dest
:
try
:
try
:
...
@@ -241,11 +242,44 @@ def uri(module, url, dest, user, password, body, method, headers, redirects, soc
...
@@ -241,11 +242,44 @@ def uri(module, url, dest, user, password, body, method, headers, redirects, soc
module
.
fail_json
(
msg
=
"Both a username and password need to be set."
)
module
.
fail_json
(
msg
=
"Both a username and password need to be set."
)
if
user
is
not
None
and
password
is
not
None
:
if
user
is
not
None
and
password
is
not
None
:
h
.
add_credentials
(
user
,
password
)
h
.
add_credentials
(
user
,
password
)
# is dest is set and is a directory, let's check if we get redirected and
# set the filename from that url
redirected
=
False
resp_redir
=
{}
r
=
{}
if
dest
is
not
None
:
dest
=
os
.
path
.
expanduser
(
dest
)
if
os
.
path
.
isdir
(
dest
):
# first check if we are redirected to a file download
h
.
follow_redirects
=
False
# Try the request
try
:
resp_redir
,
content_redir
=
h
.
request
(
url
,
method
=
method
,
body
=
body
,
headers
=
headers
)
# if we are redirected, update the url with the location header,
# and update dest with the new url filename
except
:
pass
if
resp_redir
[
'status'
]
in
[
"301"
,
"302"
,
"303"
,
"307"
]:
url
=
resp_redir
[
'location'
]
redirected
=
True
dest
=
os
.
path
.
join
(
dest
,
url_filename
(
url
))
# if destination file already exist, only download if file newer
if
os
.
path
.
exists
(
dest
):
t
=
datetime
.
datetime
.
utcfromtimestamp
(
os
.
path
.
getmtime
(
dest
))
tstamp
=
t
.
strftime
(
'
%
a,
%
d
%
b
%
Y
%
H:
%
M:
%
S +0000'
)
headers
[
'If-Modified-Since'
]
=
tstamp
# do safe redirects now, including 307
h
.
follow_redirects
=
True
# Make the request, or try to :)
# Make the request, or try to :)
try
:
try
:
resp
,
content
=
h
.
request
(
url
,
method
=
method
,
body
=
body
,
headers
=
headers
)
resp
,
content
=
h
.
request
(
url
,
method
=
method
,
body
=
body
,
headers
=
headers
)
return
resp
,
content
r
[
'redirected'
]
=
redirected
r
.
update
(
resp_redir
)
r
.
update
(
resp
)
return
r
,
content
,
dest
except
httplib2
.
RedirectMissingLocation
:
except
httplib2
.
RedirectMissingLocation
:
module
.
fail_json
(
msg
=
"A 3xx redirect response code was provided but no Location: header was provided to point to the new location."
)
module
.
fail_json
(
msg
=
"A 3xx redirect response code was provided but no Location: header was provided to point to the new location."
)
except
httplib2
.
RedirectLimit
:
except
httplib2
.
RedirectLimit
:
...
@@ -343,26 +377,23 @@ def main():
...
@@ -343,26 +377,23 @@ def main():
else
:
else
:
redirects
=
False
redirects
=
False
# If there is a dest, expand it and get the filename if one not explicitly set.
if
dest
is
not
None
:
dest
=
os
.
path
.
expanduser
(
dest
)
if
os
.
path
.
isdir
(
dest
):
dest
=
os
.
path
.
join
(
dest
,
url_filename
(
url
))
# Make the request
# Make the request
resp
,
content
=
uri
(
module
,
url
,
dest
,
user
,
password
,
body
,
method
,
dict_headers
,
redirects
,
socket_timeout
)
resp
,
content
,
dest
=
uri
(
module
,
url
,
dest
,
user
,
password
,
body
,
method
,
dict_headers
,
redirects
,
socket_timeout
)
# Write the file out if requested
# Write the file out if requested
if
dest
is
not
None
:
if
dest
is
not
None
:
write_file
(
module
,
url
,
dest
,
content
)
if
resp
[
'status'
]
==
"304"
:
status_code
=
"304"
# allow file attribute changes
changed
=
False
changed
=
True
else
:
module
.
params
[
'path'
]
=
dest
write_file
(
module
,
url
,
dest
,
content
)
file_args
=
module
.
load_file_common_arguments
(
module
.
params
)
# allow file attribute changes
file_args
[
'path'
]
=
dest
changed
=
True
changed
=
module
.
set_file_attributes_if_different
(
file_args
,
changed
)
module
.
params
[
'path'
]
=
dest
file_args
=
module
.
load_file_common_arguments
(
module
.
params
)
file_args
[
'path'
]
=
dest
changed
=
module
.
set_file_attributes_if_different
(
file_args
,
changed
)
resp
[
'path'
]
=
dest
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
uresp
=
{}
uresp
=
{}
...
@@ -377,13 +408,12 @@ def main():
...
@@ -377,13 +408,12 @@ def main():
uresp
[
'json'
]
=
js
uresp
[
'json'
]
=
js
except
:
except
:
pass
pass
if
resp
[
'status'
]
!=
status_code
:
if
resp
[
'status'
]
!=
status_code
:
module
.
fail_json
(
msg
=
"Status code was not "
+
status_code
,
content
=
content
,
**
uresp
)
module
.
fail_json
(
msg
=
"Status code was not "
+
status_code
,
content
=
content
,
**
uresp
)
elif
return_content
:
elif
return_content
:
module
.
exit_json
(
changed
=
True
,
content
=
content
,
**
uresp
)
module
.
exit_json
(
changed
=
changed
,
content
=
content
,
**
uresp
)
else
:
else
:
module
.
exit_json
(
changed
=
True
,
**
uresp
)
module
.
exit_json
(
changed
=
changed
,
**
uresp
)
# this is magic, see lib/ansible/module_common.py
# this is magic, see lib/ansible/module_common.py
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment