Thank you for the answer. I defined a new symbol to have access to
stdout and then used with-output-stream* (doesn't work well without
the * of course). Dynamic scope proves to be really useful.
USING: kernel sequences combinators io io.sockets io.encodings.ascii
prettyprint namespaces ;
IN: scratchpad
SYMBOL: old-output-stream
output-stream get old-output-stream set
"www.google.com" 80 <inet>
ascii
[
"GET / HTTP/1.0\r\n\r\n" write flush
old-output-stream get [ [ . ] each-line ] with-output-stream*
]
with-client
Could you explain why "output-stream get" and "output-stream get-
global" return different values ?
( scratchpad ) output-stream get
--- Data stack:
T{ pane-stream f ~pane~ }
( scratchpad ) output-stream get-global
--- Data stack:
T{ pane-stream f ~pane~ }
T{ encoder f ~output-port~ utf8 }
( scratchpad )
Ludovic Kuty
On 11 Jan 2009, at 21:54, Slava Pestov wrote:
> Hi,
>
> You can work with multiple streams at a time by using the stream-*
> words, instead of words such as readln. For example, readln is defined
> as
>
> : readln ( -- line ) input-stream get stream-readln ;
>
> If you use the stream-* version, you can pass the stream around on the
> stack or put it in a variable, and work with multiple streams. You
> still have to use with-disposal or with-destructors to ensure the
> streams get closed in the event of an error. Also,
>
> [ readln dup ] [ ] [ drop ] produce
>
> is the same same as
>
> input-stream get lines
>
> There is also an each-line combinator which applies a quotation to
> each line; this runs in constant space, instead of 'lines' which
> slurps everything into memory.
>
> Hope this helps,
>
> Slava
>
> On Sun, Jan 11, 2009 at 6:27 AM, Ludovic Kuty <[email protected]> wrote:
>> Hello,
>>
>> I want to print the response given by a Web server using socket
>> primitives (not an HTTP library). I managed to produce the following
>> working code running in the factor Workspace.
>>
>> USING: io.sockets io.encodings.ascii ;
>> "www.google.com" 80 <inet>
>> ascii
>> [
>> "GET / HTTP/1.0\r\n\r\n" write flush
>> [ readln dup ] [ ] [ drop ] produce
>> ]
>> with-client
>> [ . ] each
>>
>> But if I prefer to print the HTTP reponse directly in the quotation
>> used by with-client instead of returning a result and then printing
>> it
>> with "[ . ] each", what should I do ?
>>
>> Indeed the default input and output streams are rebound by with-
>> client
>> and thus inside the quotation I do not have access to stdout (the
>> output stream to the console). It took me some time to realize this
>> and have a working code. I thought of getting access to the output
>> stream stored in the global namespace but it doesn't work (\ output-
>> stream get-global). In fact that doesn't make sense maybe. I also
>> thought of using with-output-stream but I can't see how to place it
>> in
>> the code since I need two different output-streams at the same time.
>>
>> Also, if you have any comment, insight or remark about the code,
>> please let me know.
>>
>> Thank in advance,
>>
>> Ludovic Kuty
>>
>> OUTPUT:
>>
>> "HTTP/1.0 302 Found"
>> "Location: http://www.google.be/"
>> "Cache-Control: private"
>> "Content-Type: text/html; charset=UTF-8"
>> "Set-Cookie:
>> PREF
>> =ID=db53d028f07bf514:TM=1231675166:LM=1231675166:S=x-4Mym8gM1pvKy6Y;
>> expires=Tue, 11-Jan-2011 11:59:26 GMT; path=/; domain=.google.com"
>> "Date: Sun, 11 Jan 2009 11:59:26 GMT"
>> "Server: gws"
>> "Content-Length: 218"
>> ""
>> "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/
>> html;charset=utf-8\">"
>> "<TITLE>302 Moved</TITLE></HEAD><BODY>"
>> "<H1>302 Moved</H1>"
>> "The document has moved"
>> "<A HREF=\"http://www.google.be/\">here</A>."
>> "</BODY></HTML>"
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Check out the new SourceForge.net Marketplace.
>> It is the best place to buy or sell services for
>> just about anything Open Source.
>> http://p.sf.net/sfu/Xq1LFB
>> _______________________________________________
>> Factor-talk mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>
> ------------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It is the best place to buy or sell services for
> just about anything Open Source.
> http://p.sf.net/sfu/Xq1LFB
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk
------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk