2014-01-08 41 views
1

SWI序言HTTP服務器庫提供了一種優雅的訪問傳入HTTP GET查詢參數/學期:如何從SWI序言HTTP GET特定的cookie值的請求

http_parameters(Request, [term(QueryTerm, [])]), 

是否有等效的方法,允許檢索一個特定的cookie名稱/值。

例如。虛構謂語我想存在:

http_parameters(Request, [cookie('my_cookieName', MyCookieValue), [])]), 

http_parameters似乎只能訪問「搜索(X)的價值(即僅表單數據)。

由於請求對象僅僅是一個名單,我可以這樣做:

nth1(_, Request, cookie(CookieL)), 

然後分析出從CookieL名稱/值對......它只是醜陋的,如果有一個謂語已經直奔那裏。

回答

0

查看了http_parameters源代碼後,我可以看到prolog將爲cookie列表的名稱/值對解析做所有的繁重工作。

所以已經建立了一個HTTP處理程序:

:- http_handler(root(.), myroothandler, []). 

,並定義謂詞將自動了解SWI HTTP請求對象(因爲它已被定義爲一個處理程序):

myroothandler(Request) :- 
    http_cookie_value(Request, 'mycookiename', Value). 

然後新的餅乾提取器本身:

% extracts a cookie value from the swi http request object 
% fails if either no cookie list in request or cookie Name not found 
http_cookie_value(Request, Name, Value) :- 

    % cookie list must exist in the request, and will live in CookieList 
    memberchk(cookie(CookieList), Request), 

    % find one cookie in the cookie list whose Name is Value. 
    nth1(_, CookieList, Name=Value).