-
Notifications
You must be signed in to change notification settings - Fork 14
Set S3 redirect metadata when processing redirect files #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,6 +164,16 @@ def save_page_to_aws(response, uri) | |
| # Upload this file directly to AWS::S3 | ||
| opts = {:acl => "public-read"} | ||
| opts[:content_type] = response['content-type'] rescue "text/html" | ||
|
|
||
| # Detect a meta-redirect and set an S3 hosting redirect metadata item | ||
| if response =~ /META http-equiv='refresh' content='0;URL="(.*)"/ | ||
| location = $1 | ||
| if location =~ /^(?:[^\/]|http:\/\/|https\:\/\/).*/ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is prepending a slash if the location starts with http or https, is that needed for s3 redirects? Also wouldn't this redirect to the wrong place if the location is not absolute. So if we are at http://www.google.com/section/page1 and that page has a meta refresh to url='page2' then this would redirect to /page2 instead of /section/page2. |
||
| location.prepend('/') | ||
| end | ||
| opts[:website_redirect_location] = location | ||
| end | ||
|
|
||
| @log.info "Uploading #{key} to s3 with content type #{opts[:content_type]}" | ||
| if response.respond_to?(:read_body) | ||
| body = process_body(response.read_body, uri, opts) | ||
|
|
@@ -197,7 +207,6 @@ def process_success(response, parsed_uri) | |
| end | ||
|
|
||
| # If we hit a redirect we save the redirect as a meta refresh page | ||
| # TODO: for AWS S3 hosting we could instead create a redirect? | ||
| def process_redirect(url, destination_url) | ||
| body = "<html><head><META http-equiv='refresh' content='0;URL=\"#{destination_url}\"'></head><body>You are being redirected to <a href='#{destination_url}'>#{destination_url}</a>.</body></html>" | ||
| save_page(body, url) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are likely some common cases where this regex would fail. For example:
<meta http-equiv="refresh" content="0;url=http://example.com" />or
<meta http-equiv="refresh" content="2;url='http://example.com'" />I'll merge this request and then likely modify this to catch a wider range of meta redirects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, my bad, this is just a little hack to use the existing process_redirect. Got it! :)