From: Rolf Rander Naess
Subject: problem with sockets in clisp
Date: 
Message-ID: <ukwbt2jp5of.fsf@pvv.org>
As an excercise I'm trying to make a simple HTTP-proxy using clisp and
clocc.  However, it seems my server terminates the connection before
all data is recieved by the web-browser.  If I write the recieved data
to standard output, I can see that the whole web-page is recieved by
the proxy, but the connection to the client is closed before
everything is sent out again.  It is random how much data gets
through, sometimes nothing, and sometimes almost everything.

Here is what I have so far:

(defun handle-http-request (socket)
  (let* ((req (read-line socket))
	 (split-req (cllib:split-string req " ")))
    (if (equal (first split-req) "QUIT")
	nil
      (let* ((url (cllib:url (second split-req)))
	     (fwsock (port:open-socket (cllib::url-host url)
				       (cllib::url-get-port url))))
	(ignore-errors
	  (unwind-protect
	      (progn
		(write-line req fwsock)
		(loop :for line = (read-line socket)
		      :while (not (equal line ""))
		      :do (write-line line fwsock))
		(write-line "" fwsock)
		(loop :for c = (read-line fwsock nil '+eof+)
		      :while (not (eq c '+eof+))
		      :do
		      (write-line c socket)))
	    (finish-output socket)
	    (close fwsock)
	    ))
	  t))))

(defun http-server (p)
  (let* ((serv (port:open-socket-server p))
	 (cont t)
	 (req  ""))
    (loop :for sock = (port:socket-accept serv)
	  :do
	  (setf cont (handle-http-request sock))
	  (close sock)
	  :while cont)
    (port:socket-server-close serv)))



regards
Rolf Rander

-- 
                                            (c) 2000 Rolf Rander N�ss
http://www.pvv.org/~rolfn/

My mailer limits .sigs to 4 lines. But ingeniously I bypassed this by

From: ···@usa.net
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <8ff4jv$qj1$1@nnrp1.deja.com>
In article <···············@pvv.org>,
  Rolf Rander Naess <··········@pvv.org> wrote:
>
> As an excercise I'm trying to make a simple HTTP-proxy using clisp and
> clocc.  However, it seems my server terminates the connection before
> all data is recieved by the web-browser.  If I write the recieved data
> to standard output, I can see that the whole web-page is recieved by
> the proxy, but the connection to the client is closed before
> everything is sent out again.  It is random how much data gets
> through, sometimes nothing, and sometimes almost everything.
>
> Here is what I have so far:

why aren't you using cllib:with-open-url?



Sent via Deja.com http://www.deja.com/
Before you buy.
From: Rolf Rander Naess
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <ukwpuqsn6n6.fsf@pvv.org>
[ ···@usa.net, 11 May 2000 20:19 ]
> In article <···············@pvv.org>,
>   Rolf Rander Naess <··········@pvv.org> wrote:
> >
> > As an excercise I'm trying to make a simple HTTP-proxy using clisp and
> > clocc.  However, it seems my server terminates the connection before
> > all data is recieved by the web-browser.  If I write the recieved data
> > to standard output, I can see that the whole web-page is recieved by
> > the proxy, but the connection to the client is closed before
> > everything is sent out again.  It is random how much data gets
> > through, sometimes nothing, and sometimes almost everything.
> >
> > Here is what I have so far:
> 
> why aren't you using cllib:with-open-url?

with-open-url calls open-url which calls url-open-http.

url-open-http sends:
  (format sock "GET ~a HTTP/1.0~2%" (url-path url))

that is, a GET request, and end-of-headers ("\n\n"). But I might want
a POST (or a HEAD, but I don't think any browsers use this), and there
might be (or - usually are) more headers after the GET (or POST).

(Actually, I think this is more a clisp-question, so I should make a
smaller example independent of cllib and test that)


Rolf Rander

-- 
                                            (c) 2000 Rolf Rander N�ss
http://www.pvv.org/~rolfn/

My mailer limits .sigs to 4 lines. But ingeniously I bypassed this by
From: ···@usa.net
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <8fhqjj$p26$1@nnrp1.deja.com>
In article <···············@pvv.org>,
  Rolf Rander Naess <··········@pvv.org> wrote:
> > why aren't you using cllib:with-open-url?
>
> with-open-url calls open-url which calls url-open-http.
>
> url-open-http sends:
>   (format sock "GET ~a HTTP/1.0~2%" (url-path url))

please try
(with-open-url (sock url :init nil)
   ...)


Sent via Deja.com http://www.deja.com/
Before you buy.
From: Ingvar
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <m24s83o5uj.fsf@head.cathouse.bofh.se>
Rolf Rander Naess <··········@pvv.org> writes:

> [ ···@usa.net, 11 May 2000 20:19 ]
[SNIP]
> > 
> > why aren't you using cllib:with-open-url?
> 
> with-open-url calls open-url which calls url-open-http.
> 
> url-open-http sends:
>   (format sock "GET ~a HTTP/1.0~2%" (url-path url))
> 
> that is, a GET request, and end-of-headers ("\n\n"). But I might want
> a POST (or a HEAD, but I don't think any browsers use this), and there
> might be (or - usually are) more headers after the GET (or POST).
> 
> (Actually, I think this is more a clisp-question, so I should make a
> smaller example independent of cllib and test that)

Hmm... Many browsers use it, but only after you've switched on "cache
locally; check timestamp at every access". It's one of the things I
had to implement in my stripped http server (I've decided to cram in
as much functionality as possible in no more than 4096 bytes of
indented C code).

And while we're discussing lisp, I've been playing around with CLX for
the last year and am now looking for something "on top" of it (my UIs
are very limited) and wonder if anyone have recommendations?

I've been leaning towards CLIM, but I've only seen the spec, no
programming reference and not many examples. My tries at reading
closure's source have been... interesting. I probably learned more
about how to open sockets than writing UI code from it.

//Ingvar (argh, fascist server! I don't want to have to manipulate articles
          in the spool to make you accept them!)
From: ···@usa.net
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <8fpirb$t1c$1@nnrp1.deja.com>
In article <···············@pvv.org>,
  Rolf Rander Naess <··········@pvv.org> wrote:
>
> As an excercise I'm trying to make a simple HTTP-proxy using clisp and
> clocc.  However, it seems my server terminates the connection before
> all data is recieved by the web-browser.  If I write the recieved data
> to standard output, I can see that the whole web-page is recieved by
> the proxy, but the connection to the client is closed before
> everything is sent out again.  It is random how much data gets
> through, sometimes nothing, and sometimes almost everything.

I cannot reproduce this.

I do this in one CLISP:

(defun write-lots-of-data (stream)
  (loop :for idx :from 0 :to 999
        :for str = (format nil "~3A ~70~~%" idx)
        :sum (length (write-string str stream))))
(defun test-server (p)
  (let* ((serv (lisp:socket-server p)))
    (format t " *** created: ~s~%" serv)
    (unwind-protect
         (loop :for sock = (when (lisp:socket-wait serv)
                             (lisp:socket-accept serv))
               :do (format t " +++ open  ~s~%" sock)
               (format t " === wrote ~:d chars~%" (write-lots-of-data
sock))
               (finish-output sock)
               (close sock)
               (format t " --- close ~s~%" sock))
      (lisp:socket-server-close serv)
      (format t " *** closed ~s~%" serv))))
 (test-server 12345)

and this in another CLISP (I get the same results independent on whether
both instances of CLISP run on the same machine or on two machines
separated by 300 miles and a firewall :-)

(defun read-lots-of-data (stream)
  (format t " +++ reading from ~s~%" stream)
  (loop :for line = (read-line stream nil nil)
        :while line :sum (1+ (length line)) :into sum
        :finally (progn (format t " --- read ~:d bytes~%" sum)
                        (close stream) (return sum))))
(defun test-socket (host port)
  (loop (read-lots-of-data (lisp:socket-connect port host))))
(test-socket "host" 12345)
;; (according to the first line printed by `test-server' above)

As expected, I get the following (IP addresses replaced with ???)
on the server side

 +++ open  #<UNBUFFERED SOCKET-STREAM CHARACTER ???:12345>
 === wrote 75,000 chars
 --- close #<CLOSED UNBUFFERED SOCKET-STREAM CHARACTER ???:12345>
 +++ open  #<UNBUFFERED SOCKET-STREAM CHARACTER ???:12345>
 === wrote 75,000 chars
 --- close #<CLOSED UNBUFFERED SOCKET-STREAM CHARACTER ???:12345>
 +++ open  #<UNBUFFERED SOCKET-STREAM CHARACTER ???:12345>
 === wrote 75,000 chars
 --- close #<CLOSED UNBUFFERED SOCKET-STREAM CHARACTER ???:12345>

(many dozens of times without a single glitch!)

and this on the client side:

 +++ reading from #<unbuffered socket-stream character ???:12345>
 --- read 75,000 bytes
 +++ reading from #<unbuffered socket-stream character ???:12345>
 --- read 75,000 bytes
 +++ reading from #<unbuffered socket-stream character ???:12345>
 --- read 75,000 bytes

In the future, please report CLISP bugs to clisp-list or file a bug
report on sourceforge (see http://clisp.cons.org)

thanks.


Sent via Deja.com http://www.deja.com/
Before you buy.
From: Rolf Rander Naess
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <ukwln1bfnth.fsf@pvv.org>
[ ···@usa.net, 15 May 2000 19:23 ]
> In article <···············@pvv.org>,
>   Rolf Rander Naess <··········@pvv.org> wrote:
> >
> > As an excercise I'm trying to make a simple HTTP-proxy using clisp and
> > clocc.  However, it seems my server terminates the connection before
> > all data is recieved by the web-browser.  If I write the recieved data
> > to standard output, I can see that the whole web-page is recieved by
> > the proxy, but the connection to the client is closed before
> > everything is sent out again.  It is random how much data gets
> > through, sometimes nothing, and sometimes almost everything.
> 
> I cannot reproduce this.

I'm not getting consistent result myself either.  When I telnet into
the port I get all the data.  But when trying to load it in netscape,
it only gives me some of it.  However, the slightly longer
proxy-example (shown in article <···············@pvv.org>) fails from
both clients.  I will try your lisp-client to see if it behaves
differently.

> In the future, please report CLISP bugs to clisp-list or file a bug
> report on sourceforge (see http://clisp.cons.org)

I startet out assuming that *I* did something wrong.  If I get a better
understanding of what is really going wrong (ie. if its lisp or
netscape), I'll report it as a bug.


rrn

-- 
                                            (c) 2000 Rolf Rander N�ss
http://www.pvv.org/~rolfn/

My mailer limits .sigs to 4 lines. But ingeniously I bypassed this by
From: William Deakin
Subject: Re: problem with sockets in clisp
Date: 
Message-ID: <39211440.846CAD68@pindar.com>
Rolf Rander Naess wrote:

> I startet out assuming that *I* did something wrong.  If I get a better
> understanding of what is really going wrong (ie. if its lisp or
> netscape), I'll report it as a bug.

Loathed as I am to suggest it but if you were to rewrite the socket stuff in
something else (maybe perl or C or something) and try this with the netscape.
I suspect this *could* be the problem since I know of some people I work with
have had similar strange errors with java applications talking to netscape and
different versions of ie.

Best Regards,

:) will