]> gitweb.factorcode.org Git - factor.git/commitdiff
urls: derive-url resets the port if the new URL specifies a protocol. Fixes HTTP...
authorSlava Pestov <slava@factorcode.org>
Sun, 30 Jan 2011 03:50:13 +0000 (22:50 -0500)
committerSlava Pestov <slava@factorcode.org>
Sun, 30 Jan 2011 03:50:13 +0000 (22:50 -0500)
basis/urls/urls-tests.factor
basis/urls/urls.factor

index f2ecd6ec6921d0ecf6e6883e7b875c52865cd482..e188a1c64572466cba6e51f7a0fc62d618210d4d 100644 (file)
@@ -205,6 +205,26 @@ urls [
     derive-url
 ] unit-test
 
+[
+    T{ url
+        { protocol "https" }
+        { host "www.apple.com" }
+    }
+] [
+    T{ url
+        { protocol "http" }
+        { host "www.apple.com" }
+        { port 80 }
+    }
+
+    T{ url
+        { protocol "https" }
+        { host "www.apple.com" }
+    }
+
+    derive-url
+] unit-test
+
 [ "a" ] [
     <url> "a" "b" set-query-param "b" query-param
 ] unit-test
index 19aea0fdaca2ec5b059afc5b891531390bf231a5..f7881b13a7f86da8fda8a6b31644acb17402ea54 100644 (file)
@@ -1,4 +1,4 @@
-! Copyright (C) 2008, 2010 Slava Pestov.
+! Copyright (C) 2008, 2011 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel ascii combinators combinators.short-circuit
 sequences splitting fry namespaces make assocs arrays strings
@@ -92,6 +92,14 @@ M: string >url
         [ drop f ]
     } case ;
 
+: relative-url ( url -- url' )
+    clone
+        f >>protocol
+        f >>host
+        f >>port ;
+
+: relative-url? ( url -- ? ) protocol>> not ;
+
 <PRIVATE
 
 : unparse-username-password ( url -- )
@@ -113,8 +121,6 @@ M: string >url
         [ path>> "/" head? [ "/" % ] unless ]
     } cleave ;
 
-PRIVATE>
-
 M: url present
     [
         {
@@ -125,6 +131,8 @@ M: url present
         } cleave
     ] "" make ;
 
+PRIVATE>
+
 : url-append-path ( path1 path2 -- path )
     {
         { [ dup "/" head? ] [ nip ] }
@@ -134,6 +142,14 @@ M: url present
         [ [ "/" split1-last drop "/" ] dip 3append ]
     } cond ;
 
+<PRIVATE
+
+: derive-port ( url base -- url' )
+    over relative-url? [ [ port>> ] either? ] [ drop port>> ] if ;
+
+: derive-path ( url base -- url' )
+    [ path>> ] bi@ swap url-append-path ;
+
 PRIVATE>
 
 : derive-url ( base url -- url' )
@@ -142,20 +158,12 @@ PRIVATE>
         [ [ username>>  ] either? >>username ]
         [ [ password>>  ] either? >>password ]
         [ [ host>>      ] either? >>host ]
-        [ [ port>>      ] either? >>port ]
-        [ [ path>>      ] bi@ swap url-append-path >>path ]
+        [ derive-port             >>port ]
+        [ derive-path             >>path ]
         [ [ query>>     ] either? >>query ]
         [ [ anchor>>    ] either? >>anchor ]
     } 2cleave ;
 
-: relative-url ( url -- url' )
-    clone
-        f >>protocol
-        f >>host
-        f >>port ;
-
-: relative-url? ( url -- ? ) protocol>> not ;
-
 ! Half-baked stuff follows
 : secure-protocol? ( protocol -- ? )
     "https" = ;