Max Williams
2009-Feb-10 16:56 UTC
test if an external url is valid before redirecting to it?
In our site, users can have a url associated with their account to which they are redirected when their login expires. This can differ from user to user, and is prone to being mis-copied, or the sites in question not being there any more etc. I''d like to test if the url is valid before sending the user on to it. So, i want, in the controller, to do something like this - if external_url_is_valid?(user.expiry_url) redirect_to user.expiry_url else redirect_to "our_default_expiry_page" end Can anyone tell me a nice, simple and efficient way of doing the "external_url_is_valid?" bit? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Timberlake
2009-Feb-10 17:39 UTC
Re: test if an external url is valid before redirecting to it?
On Tue, Feb 10, 2009 at 6:56 PM, Max Williams < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > In our site, users can have a url associated with their account to which > they are redirected when their login expires. This can differ from user > to user, and is prone to being mis-copied, or the sites in question not > being there any more etc. I''d like to test if the url is valid before > sending the user on to it. > > So, i want, in the controller, to do something like this - > > if external_url_is_valid?(user.expiry_url) > redirect_to user.expiry_url > else > redirect_to "our_default_expiry_page" > end > > Can anyone tell me a nice, simple and efficient way of doing the > "external_url_is_valid?" bit? > -- > Posted via http://www.ruby-forum.com/. > > > >require ''net/http'' require ''uri'' def external_url_is_valid?(url) uri = URI.parse(url) response = Net::HTTP.start(uri.host, uri.port) {|http| http.head(uri.path)} response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection) end Watch the validity of the url - www.google.com is not valid http://www.google.com also won''t work, http://www.google.com/ will I wouldn''t do this at the point of redirection, I would do it at the point of saving the redirection url Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Max Williams
2009-Feb-10 17:52 UTC
Re: test if an external url is valid before redirecting to it?
That''s perfect, thanks Andrew. You''re probably right about testing the validity at the point of adding it rather than (or as well as) the redirect. I think i''ll put a remote test button next to the input field, and maybe call it automatically on change of the field. Anyway, thanks again. max -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---