Arc Forumnew | comments | leaders | submitlogin
Arc, Twitter, and tcp-connect
2 points by zck 2045 days ago | discuss
As I might have mentioned before, I'm trying to write a Twitter library. Beyond OAuth being confusing -- but I think I've got it now -- I'm having trouble actually connecting to twitter. I'm using anarki for the moment, but this code works in stock arc3.1 also. Add this to ac.scm:

  (xdef tcp-connect (lambda (host port)
                      (let-values ([(from-server to-server)
                                    (tcp-connect host port)])
                                  (list from-server to-server))))
Then start up Arc, and let's define a utility function:

  (def write-message (host message)
    (let (from-server to-server) (tcp-connect host 80)
         (disp message to-server)
         (close to-server)
         (whilet line (readline from-server)
                 (prn line))
         (close from-server)))

So now we can do fun things like this:

  arc> (write-message ""
                      "GET / HTTP/1.1\r\\r\n\r\n")
  HTTP/1.1 200 OK
  Date: Wed, 07 Mar 2012 16:42:16 GMT
Any results from the server are cut for readability. That's fine, let's try twitter now:

  arc> (write-message ""
                      "GET / HTTP/1.1\r\\r\n\r\n")
Weird. Maybe they don't like www in their urls?

  arc> (write-message ""
                      "GET / HTTP/1.1\r\\r\n\r\n")
Sometimes I've gotten a result from one of these, but usually nothing:

  arc> (write-message ""
                      "GET / HTTP/1.1\r\nHost:\r\n\r\n")
  HTTP/1.1 301 Moved Permanently
  Date: Tue, 06 Mar 2012 16:37:31 GMT
Before we do anything drastic, let's try telnet:

  zck@zck-desktop:~$ telnet 80
  Connected to
  Escape character is '^]'.
  GET / HTTP/1.1
  HTTP/1.1 200 OK
  Date: Wed, 07 Mar 2012 16:46:06 GMT
Again, cut for readability.

So just sending those Maybe this is an arc thing. Let's try racket:

  > (define (write-message host message)
    (let-values (((from-server to-server) (tcp-connect host 80)))
      (display message to-server)
      (close-output-port to-server)
      (do ((response (read-line from-server) (read-line from-server)))
          ((eof-object? response) (lambda () nil))
          (display response)
      (close-input-port from-server)))

  > (write-message ""
                   "GET / HTTP/1.1\r\n\r\nHost:")
  HTTP/1.1 200 OK
  Date: Mon, 12 Mar 2012 15:12:04 GMT
Ok, so that works. Twitter?

  > (write-message ""
                   "GET / HTTP/1.1\r\n\r\nHost:")
Even though twitter prefers no www. on their urls:

  > (write-message ""
                   "GET / HTTP/1.1\r\n\r\nHost:")
So...nothing. Can anyone tell me what I'm forgetting? My money's on something that I'm not sending to them, or that I'm sending it badly escaped, or something. I can browse to twitter in Firefox just fine.