简体   繁体   English

如何在Common Lisp中读取HTTP cookie?

[英]How to read a HTTP cookie in Common Lisp?

I would like to make a single CGI program in Common Lisp, that reads a cookie. 我想在Common Lisp中制作一个单一的CGI程序,该程序读取cookie。

It is clear to me that I can send a Cookie by sending HTTP headers. 对我来说很明显,我可以通过发送HTTP标头来发送Cookie。 Yet, I would like to know how to read a cookie on a server, through CGI program, from the client who is accessing the program. 但是,我想知道如何通过CGI程序从正在访问该程序的客户端读取服务器上的cookie。

Based on Kaz answer, we need to get an environment variable. 基于Kaz的答案,我们需要获取一个环境变量。 But I won't refer to the cl-cookbook[1], now we have this solution that comes with asdf: 但是我不会参考cl-cookbook [1],现在我们有了asdf附带的以下解决方案:

(uiop:getenv)

Also the Osicat library (os, files and directories) has (environment-variable name) for posix systems, incl. Osicat库(操作系统,文件和目录)也具有posix系统的(environment-variable name) ,包括。 windows. 视窗。

[1] the cl-cookbook on sourceforge is old and sometimes outdated. [1] sourceforge上的cl-cookbook很旧,有时过时了。 There is a copy on github, maintained (well, a bit) and editable: https://lispcookbook.github.io/cl-cookbook/os.html ( https://github.com/LispCookbook/cl-cookbook/ ). 在github上有一个副本,已维护(很好)并且可编辑: https : //lispcookbook.github.io/cl-cookbook/os.htmlhttps://github.com/LispCookbook/cl-cookbook/ ) 。

edit : the cookbook on github has been edited accordingly and should be deployed soon on https://lispcookbook.github.io/cl-cookbook/os.html 编辑 :github上的食谱已作相应编辑 ,应尽快在https://lispcookbook.github.io/cl-cookbook/os.html上部署

The CGI mechanism passes the cookie using the HTTP_COOKIE environment variable. CGI机制使用HTTP_COOKIE环境变量传递cookie。 ANSI Common Lisp doesn't have any API for accessing POSIX and Windows system environment variables. ANSI Common Lisp没有任何用于访问POSIX和Windows系统环境变量的API。 Implementation-specific functions, or else an implementation's FFI, is used to achieve the equivalent of C's getenv . 特定于实现的功能或实现的FFI用于实现C的getenv的等效功能。

The Common Lisp Cookbook suggests this: 通用Lisp食谱》建议:

* (defun my-getenv (name &optional default)
    #+CMU
    (let ((x (assoc name ext:*environment-list*
                    :test #'string=)))
      (if x (cdr x) default))
    #-CMU
    (or
     #+Allegro (sys:getenv name)
     #+CLISP (ext:getenv name)
     #+ECL (si:getenv name)
     #+SBCL (sb-unix::posix-getenv name)
     #+LISPWORKS (lispworks:environment-variable name)
     default))
MY-GETENV
* (my-getenv "HOME")
"/home/edi"
* (my-getenv "HOM")
NIL
* (my-getenv "HOM" "huh?")
"huh?"

Before using, I'd slightly modify this to: 在使用之前,我会对此稍作修改以:

(or #+CMU (let ((x ...)) (if ...))
    #+Allegro ...
    #+CLISP
    ...
    default)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM