Accessing HTTP headers

Discussion in 'Rebol' started by rebnoo, May 9, 2011.

  1. rebnoo

    rebnoo New Member

    Hello everyone,

    Stuck with a really simple problem -- googling/stackoverflow didn't help so far!

    1. How do I access the http header after a call to the _read_ function (i hope _read_ doesn't throw the header away!)
    2. How do I extract cookies from the above headers and send it out on a second call to _read_ (along with a bunch of other data)

    Thanks!
    Rebnoo
    ===========
    R2 (View)
  2. MaxV

    MaxV Member

    For accessing data, see this:
    http://www.rebol.com/docs/core23/rebolcore-13.html

    8.6 Posting CGI Requests (how-to use post)

    13.2 CGI Scripts (how-to use cgi)

    and for cookies:
    http://www.rebol.net/cookbook/recipes/0045.html

    setting cookie:
    Code:
        
    set-cookie: func [
       "Sets a cookie"
        key [string!] value
        /expires "set expiration date" exp-date [date!]
        ][
        print rejoin [
        "Set-Cookie: " key "=" value ";"
        either expires [join " expires=" to-idate exp-date][""]
        ]
        ]
    
    reading cookie:
    Code:
    get-cookie: func [
        "Returns value of a cookie"
         name [string!]
         ][
        select parse to-string select system/options/cgi/other-headers
        "HTTP_COOKIE" ";=" name
        ]
    
    Example:
    Code:
    ;setting
     set-cookie/expires "name" name now + 7
     set-cookie "birth" birth
    ;reading
    birth: get-cookie "birth"
    name: get-cookie "name"
    
    NOTE: cookies are set via HTTP headers, the set-cookie function will only work if it is called before the end of the HTTP headers - otherwise its output will be interpreted as normal text.
  3. rebnoo

    rebnoo New Member

    Thanks MaxV! But would I be accessing the cookies in the same manner even if I'm not running in a CGI environment?
  4. Graham

    Graham Developer Staff Member

    Look for http-tools on rebol.org as a client way, and also my-http.r
  5. rebnoo

    rebnoo New Member

    Thanks Graham! my-http.r certainly looks useful! And going by the timestamp on it, must be pretty stable too. But I can't seem to get it to work! I'm pretty sure I'm missing missing something very simple!

    >> do %my-http.r
    >> p: open http://localhost:8080/foo.html
    ** Script Error: querying has no value
    ** Near: http-command: either querying ["HEAD"] ["GET"]
    >>

    Any help here would be greatly appreciated!

    Thanks!
  6. Graham

    Graham Developer Staff Member

    There was probably an update to the protocol that needs to be changed in that script.

    try http-tools.r
  7. rebnoo

    rebnoo New Member

    Wow! http-tools.r works like a charm (but unfortunately doesn't seem to work on HTTPS -- I get a 302 Found when I hit one; any workarounds here?)! Thanks again Graham (both for writing and suggesting it)!

    Oh, I'd still be interested to know if _read_ does indeed discard the headers? And I've heard somewhere R3 fixes it, how exactly does it fix it?
  8. rebnoo

    rebnoo New Member

    Alright! Got the my-http.r to work. Just needed to set the "querying" variable and everything works! And works on HTTPS as well. Thanks everyone!

    >> do %my-http.r
    >> querying: false ;true if doing a HEAD op
    >> ;do a read as usual and cookies are saved and retrieved from cookies-db object
  9. Graham

    Graham Developer Staff Member

    'Read does throw away the header stuff .. it's intended as a high level function. So, if you need low level access, you need to just open the http port and inspect the port/locals
  10. notchent

    notchent Member

Share This Page