Skip to content
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

Redirect to relative path not working without appending '/' #26

Closed
elakamarcus opened this issue Nov 24, 2017 · 19 comments
Closed

Redirect to relative path not working without appending '/' #26

elakamarcus opened this issue Nov 24, 2017 · 19 comments

Comments

@elakamarcus
Copy link

Snippet from lines 652-655. When i use the script to some sites that will send a "Location: /folder/page.html" the script would try http:/folder/page.html. After adding '/' to the URL, if missing, this is resolved.

# Must have protocol
url = "http://#{url}" if url !~ /^http(s)?:\/\//
url = url+"/" if url !~ /\/$/

The culprit, line 262:

base_url = uri.to_s[0, uri.to_s.rindex('/')] 

p.s. my apologies if this is fixed in some version i have missed or break some other dependency. I've only tested with a few sites and parameters.

@digininja
Copy link
Owner

digininja commented Nov 24, 2017 via email

@elakamarcus
Copy link
Author

Thanks, I’ll try that. It’s likely the case that I’m not on the right version.

@digininja
Copy link
Owner

digininja commented Nov 24, 2017 via email

@elakamarcus
Copy link
Author

I just did a git clone, and got the same issue. After i added the code as shown above (snipped below also), it worked.

url = url+"/" if url !~ /\/$/

Sorry I maybe wasn't clear. In my initial post, the snippet is code I've already added to my local version of your script.

@digininja
Copy link
Owner

Your snippet is adding a / to the end of the URL if there isn't one already there, wouldn't that break the majority of the redirects?

/test.php

would become

/test.php/

which is wrong.

@elakamarcus
Copy link
Author

At the moment inputting a target url like this: http://www.site.com
and get a redir location tag Location: /welcome/index.php
will result in cewl trying to go http:/welcome/index.php
which is wrong.

what i did solved my immediate hurdle. I did not work on a general case, my expectation is that is what these issues are for.

@digininja
Copy link
Owner

digininja commented Nov 24, 2017 via email

@digininja
Copy link
Owner

digininja commented Nov 24, 2017

Just seen this in the code:

 702 # The spider doesn't work properly if there isn't a / on the end
 703 if url !~ /\/$/
 704     # Commented out for Yori
 705    #  url = "#{url}/"
 706 end 

I can't remember who Yori is or why he wanted it commented out but looks like I knew it was a bug a while ago.

@digininja
Copy link
Owner

I've put the line back in in the master branch and updated the version number.

Now to wait for the comment reminding me why the line was commented out.

Thanks for the report.

@elakamarcus
Copy link
Author

Yeah, that's the part i noticed and used. But as you say, it only solved this situation outlined in my comment above.

@elakamarcus
Copy link
Author

Appreciate you looking into it. Thank you.

@elakamarcus
Copy link
Author

I left that 'adding / to the end' commented out, and instead I did the below modification on my local copy. See if it causes less issues:

      if res.redirect?
        puts "Redirect URL" if @debug
        if uri.to_s[-1] == '/'
                base_url = uri.to_s[0, uri.to_s.rindex('/')]
        else
                base_url = uri.to_s
        end
        new_url = URI.parse(construct_complete_url(base_url, res['Location']))

        # If auth is used then a name:pass@ gets added, this messes the tree
        # up so easiest to just remove it
        current_uri = uri.to_s.gsub(/:\/\/[^:]*:[^@]*@/, "://")
        @next_urls.push current_uri => new_url.to_s
      elsif res.code == "401"
        puts "Authentication required, can't continue on this branch - #{uri}" if @verbose
      else
        block.call(res)
      end

@digininja
Copy link
Owner

digininja commented Dec 4, 2017 via email

@spadou
Copy link

spadou commented Sep 22, 2018

Always adding a trailing / is a problem if the server doesn't handle the URL with the / the same way that without the /. Most of the time this isn't visible because the server will serve the same content for both URLs or do a redirect but when this isn't the case it will break.

For example it isn't possible to crawl a Wikipedia page because adding the trailing / will show a missing article page instead of the correct page:

https://en.wikipedia.org/wiki/Example    -> show the correct article page
https://en.wikipedia.org/wiki/Example/   -> show the missing article page

Forcing the trailing / will probably also breaks things if the URL contains query string parameters.

The trailing / should probably only be added to construct a full URL from a relative location to avoid breaking the others cases.

@deargle
Copy link

deargle commented Oct 17, 2018

Just ran into the same wikipedia-crawling problem as @SamuelPadou . Trailing slash returns 404.

@Xm0d3us
Copy link

Xm0d3us commented Jan 4, 2019

Same issue here. Trailing slash makes it impossible to crawl Wikipedia

@digininja
Copy link
Owner

I've just tried the following two commands and both work fine:

CeWL $ ./cewl.rb   https://en.wikipedia.org --verbose
CeWL $ ./cewl.rb   https://en.wikipedia.org/ --verbose

What problem are you having? Are you definitely crawling the right URL, not one that redirects to different domains before showing content?

@deargle
Copy link

deargle commented Jan 4, 2019

Try these examples: #26 (comment)

@digininja
Copy link
Owner

OK, got rid of it in fc249dc

I wish I could change spiders, this one has so many little niggles that I've had to work around I've lost track of what fixes do what and why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants