#!/usr/bin/newlisp # # Demonstration on how to go through an HTTP proxy server # with username and password authentication. # # Only complete URL's can be retrieved. # # Usage: # 1) Define with PROXY:setup the proxyserver, port, username and password first # 2) Then with PROXY:get-url go through proxy # # The setup will return an identifier for each defined proxyserver, so it # is possible to define more proxyservers in your program. # # July 20, 2005 - PvE. # Update july 21 2005: now redirections are followed correctly. #--------------------------------------------------------------------------- (context 'PROXY) # Define how many microseconds to wait for data on HTTP socket (constant 'timeout 1500000) # Define your buffersize (constant 'buffersize 2048) # Define handle to return when proxyserver is defined (set 'handle -1) # Create lists containing proxy definitions - return identifier (define (setup server port user passwd) (push server proxy-server (length proxy-server)) (push port proxy-port (length proxy-port)) (push user proxy-user (length proxy-user)) (push user proxy-passwd (length proxy-passwd)) (inc 'handle)) # Get url by using proxy ID

(define (PROXY:get-url url p) # Check if p refers to an existing proxyserver (if (<= p (- (length proxy-server) 1 )) (begin # First, create authentication string "username:password" base64 encoded (set 'auth (base64-enc (append (nth p proxy-user) ":" (nth p proxy-passwd)))) # Connect to proxyserver (set 'socket (net-connect (nth p proxy-server)(nth p proxy-port))) # Check if connected (if (!= socket nil) (begin # Then send the GET string with authentication (net-send socket (append "GET " url " HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\nProxy-authorization: Basic " auth "\r\n\r\n")) # Get HEADER info (net-receive socket 'buf buffersize "\r\n\r\n") # Check on redirection (set 'location (find "location*" (parse buf "\r\n") 1)) # Goto redirected URL (while (!= location nil) # Close the socket (net-close socket) # Create new URL (set 'url (nth 1 (parse (nth location (parse buf "\r\n")) " ")) ) # Connect to proxyserver again (set 'socket (net-connect (nth p proxy-server)(nth p proxy-port))) # Then send the new GET string with authentication (net-send socket (append "GET " url " HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\nProxy-authorization: Basic " auth "\r\n\r\n")) # Get HEADER info (net-receive socket 'buf buffersize "\r\n\r\n") # Check on redirection (set 'location (find "location*" (parse buf "\r\n") 1)) ) # Retrieve answer (set 'result "") (while (net-select socket "read" timeout) (set 'buf "") (net-receive socket 'buf buffersize) (set 'result (append result buf))) # Close the socket (net-close socket) # Return result result ) (begin "Cannot connect to proxyserver!") ) ) (append "No proxyserver defined for ID " (string p) "!") ) ) #--------------------------------------------------------------------------- # Demonstration on how to use the PROXY context (context 'MAIN) # Define a proxy, keep ID (set 'no (PROXY:setup "my-proxy-server" 3128 "peter" "password")) # Retrieve a URL with defined proxy (println (PROXY:get-url "http://www.newlisp.org/index.cgi" no)) # Exit newlisp (exit)