Feature - Implemented Follow redirects for 301 (affects visit_url, among other things)

heeheehee

Developer
Staff member
edit: Catch-22 has a more comprehensive patch below. Look at that instead.

As hinted at in Zarqon's post regarding why BatBrain et al were broken, Mafia currently only follows 302s. The attached patch amends this.

edit: contents, for those that don't feel like downloading the patch to read through it:
Code:
Index: src/net/sourceforge/kolmafia/request/GenericRequest.java
===================================================================
--- src/net/sourceforge/kolmafia/request/GenericRequest.java    (revision 11565)
+++ src/net/sourceforge/kolmafia/request/GenericRequest.java    (working copy)
@@ -1647,7 +1647,7 @@
                {
                        istream = this.formConnection.getInputStream();
                        this.responseCode = this.formConnection.getResponseCode();
-                       this.redirectLocation = this.responseCode != 302 ? null : this.formConnection.getHeaderField( "Location" );
+                       this.redirectLocation = this.responseCode != 302 && this.responseCode != 301 ? null : this.formConnection.getHeaderField( "Location" );
                }
                catch ( SocketTimeoutException e )
                {
 

Attachments

Last edited:
Ah, call me crazy, but it might be nicer if we had all the response code handling as part of a switch statement.

Edit: The above patch probably won't do anything useful, because KoLmafia needs to know that it shouldn't stop issuing requests:

Code:
shouldStop = this.responseCode == 302 ? this.handleServerRedirect() : true;

There's another danger here, infinite loop redirects. We'll need to add a counter that causes KoLmafia to abort after 5 redirects, I believe that's the RFC recommendation.

Edit 2: Here's what I came up with, unfortunately I won't have time to test it out until later.
 
Last edited:
Infinite loop redirects are really annoying, there are times when I can't log in to KoL without Mafia because it just keeps redirecting. I really should do something about my Firefox installation I guess :)
 
The old code I posted worked okay, but it wasn't exactly RFC compliant. I figure we may as well do it right while we have the chance, posting what I have so far because I won't be able to work on it for the next few hours.

Originally I wrote the code so that it would just throw an IOException if a POST request was getting redirected, but decided I may as well implement the whole lot according to RFC (with 302 as an exception, which will behave like all modern browsers and treat it as a 303).
 
Last edited:
Here's the (hopefully) finished patch. I tested with visit_url against the pages here and everything works as expected.

The only thing I couldn't test was infinite loop redirect handling, I couldn't find a site that was game enough to offer a test page for it :p
 

Attachments

Back
Top