Paste: http proxy

Author: Jon
Mode: factor
Date: Thu, 4 Feb 2016 16:43:03
Plain Text |
diff --git a/basis/http/client/client.factor b/basis/http/client/client.factor
index b1a9dae..7282295 100644
--- a/basis/http/client/client.factor
+++ b/basis/http/client/client.factor
@@ -16,7 +16,7 @@ ERROR: too-many-redirects ;
 : write-request-line ( request -- request )
     dup
     [ method>> write bl ]
-    [ url>> relative-url present write bl ]
+    [ [ url>> ] [ proxy>> ] bi [ relative-url ] unless present write bl ]
     [ "HTTP/" write version>> write crlf ]
     tri ;
 
@@ -112,7 +112,7 @@ SYMBOL: redirects
     [ read-chunked ] [ each-block ] if ; inline
 
 : <request-socket> ( -- stream )
-    request get url>> url-addr ascii <client> drop
+    request get [ proxy>> ] [ url>> ] bi or url-addr ascii <client> drop
     1 minutes over set-timeout ;
 
 : (with-http-request) ( request quot: ( chunk -- ) -- response )

Annotation: https proxy POC

Author: jon
Mode: factor
Date: Fri, 5 Feb 2016 15:28:31
Plain Text |
! https tunneling setup for curl, wget, factor
!
! jon@zog:~/factor$ nc -l 3129
! CONNECT www.google.fr:443 HTTP/1.1
! Host: www.google.fr:443
! User-Agent: curl/7.35.0
! Proxy-Connection: Keep-Alive
! 
! HTTP/1.1 200 Connection established
! 
! "
Cr�r��q�������G4ޓ�{��+ɹ�v�0�,�(�$�
! �kj98���2�.�*�&��=5��
! www.google.fr   ��g@32��ED�1�-�)�%��</�A�
!              
! 2
! 
!  	
!  
^C
! jon@zog:~/factor$ nc -l 3129
! CONNECT www.google.fr:443 HTTP/1.1
! User-Agent: Wget/1.15 (linux-gnu)
! Host: www.google.fr:443
! 
! HTTP/1.1 200 Connection established
! 
! 84��hj�]���X�P}��8�t���	`Ћ�7���0�,�(�$�
! �kj98���2�.�*�&��=5��
! �/�+�'�#��	��g@32��ED�1�-�)�%��</�A���
! www.google.fr                                �	��
!              
! 2
! 
!  	
!  
^C
! jon@zog:~/factor$ nc -l 3129
! connect www.google.fr:443/ HTTP/1.1
! connection: close
! host: www.google.fr
! user-agent: Factor http.client
! 
! HTTP/1.1 200 Connection established
! 
! ��q�>d����O��
!                       ��]�^N%�a7	��S��� T
���o(d<7�^9
!                                                               U
! [m6�D :
ڲ��0�,�(�$�
! �kj98���2�.�*�&��=5��
! �/�+�'�#��	��g@32��ED�1�-�)�%��</�A���
!                                              �	�
!                                                             
! 2
! 
!  	
! #�PM��[�$���_)|���8C1��Q_��p��TD���%�d,<0&�6�AXA
! [���G��ى��%X6j�
!                  @<���`y̅!��w�[|�X@E�y����s��ӻ�z2
d���2�:Gw�Ş

!  
^C                               �كAU�sټ&�J]5L	x���



 ERROR: too-many-redirects ;
@@ -16,7 +16,8 @@ ERROR: too-many-redirects ;
 : write-request-line ( request -- request )
     dup
     [ method>> write bl ]
-    [ url>> relative-url present write bl ]
+    ! present adds an extra "//" when the protocol is f, remove it
+    [ url>> clone f >>protocol present 2 tail write bl ]
     [ "HTTP/" write version>> write crlf ]
     tri ;
 
@@ -112,14 +113,23 @@ SYMBOL: redirects
     [ read-chunked ] [ each-block ] if ; inline
 
 : <request-socket> ( -- stream )
-    request get url>> url-addr ascii <client> drop
+    request get [ proxy>> ] [ url>> ] bi [ url-addr ] bi@
+remote-address set ascii <client> local-address set
     1 minutes over set-timeout ;
 
+: ?https-tunnel ( -- )
+    request get clone "connect" >>method write-request
+    read-response drop send-secure-handshake ;
+
 : (with-http-request) ( request quot: ( chunk -- ) -- response )
     swap
     request [
         <request-socket> [
             [
+                [ in>> ] [ out>> ] bi
+                [ ?https-tunnel ] with-streams*
+            ]
+            [
                 out>>
                 [ request get write-request ]
                 with-output-stream*
@@ -133,7 +143,7 @@ SYMBOL: redirects
                         2tri f
                     ] if
                 ] with-input-stream*
-            ] bi
+            ] tri
         ] with-disposal
         [ do-redirect ] [ nip ] if
     ] with-variable ; inline recursive
diff --git a/basis/http/http.factor b/basis/http/http.factor
index 9e1f637..e053215 100644
--- a/basis/http/http.factor
+++ b/basis/http/http.factor
@@ -134,6 +134,7 @@ TUPLE: cookie name value version comment path domain expires max-age http-only s
 TUPLE: request
 method
 url
+proxy
 version
 header
 post-data
@@ -149,6 +150,7 @@ redirects ;
 : <request> ( -- request )
     request new
         "1.1" >>version
+        "http://localhost:3129" >url >>proxy
         <url>
             H{ } clone >>query
         >>url

New Annotation

Summary:
Author:
Mode:
Body: