curl で pinterest.com にログインしようとしています。次の要求応答フローを取得しました。
- ログイン フォームを GET リクエストし、非表示フィールド (csrftoken) をスクレイピングします。
- POST-Request ログイン認証情報 (mail と pw) とスクレイピングされた csrftoken
- ログイン用のセッション Cookie を受け取る
Curl を使用すると、次のヘッダーが送受信されていることがわかります。
GET /login/?next=%2F HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Host: pinterest.com
Referer:
Accept: text/html,application/xhtml+xml,application/xml,*/*
Accept-Language: de-de,en-us
Connection: keep-alive
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Tue, 10 Apr 2012 15:03:24 GMT
ETag: "45d6a85f0ede46f13f4fc751842ce5b7"
Server: nginx/0.8.54
Set-Cookie: csrftoken=dec6cb66064f318790c6d51e3f3a9612; Max-Age=31449600; Path=/
Set-Cookie: _pinterest_sess="eJyryMwNcTXOdtI3zXcKNq0qznIxyXVxK/KqSsy3tY8vycxNtfUN8a3yc3E09nXxLPdztLVVK04tLs5MsfXNAopVpVf6VnlW+Qba2gIAuqgZIg=="; Domain=pinterest.com; HttpOnly; expires=Tue, 17-Apr-2012 15:03:24 GMT; Max-Age=1334675004; Path=/
Vary: Cookie, Accept-Encoding
Content-Length: 4496
Connection: keep-alive
したがって、ステップ 1 の後、csrftoken と _pinterest_sess の 2 つの Cookie が設定されます。しかし、cookiejar ファイル (私は CURLOPT_COOKIEFILE と CURLOPT_COOKIEJAR を使用して、curl が Cookie 処理を処理できるようにしています) を見ると、次のようになります。
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
pinterest.com FALSE / FALSE 1365519805 csrftoken dec6cb66064f318790c6d51e3f3a9612
#HttpOnly_.pinterest.com TRUE / FALSE -1626222087 _pinterest_sess "eJyryMwNcTXOdtI3zXcKNq0qznIxyXVxK/KqSsy3tY8vycxNtfUN8a3yc3E09nXxLPdztLVVK04tLs5MsfXNAopVpVf6VnlW+Qba2gIAuqgZIg=="
最初に注意すべきことは、_pinterest_sess Cookie 行の前にある #HttpOnly_ です。カールがそれをうまく処理すると思います。しかし、さらに見てみると、負の値が有効期限として設定されていることがわかります: -1626222087
Cookie は「expires=Tue, 17-Apr-2012 15:03:24 GMT」(今日から数えて約 7 日後) で設定されているため、それがどこから来ているのかわかりません。
次のリクエストでは、curl によって _pinterest_sess Cookie が設定されません。
POST /login/?next=%2F HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Host: pinterest.com
Referer: https://pinterest.com/login/?next=%2F
Cookie: csrftoken=dec6cb66064f318790c6d51e3f3a9612
Accept: text/html,application/xhtml+xml,application/xml,*/*
Accept-Language: de-de,en-us
Connection: keep-alive
Content-Length: 123
Content-Type: application/x-www-form-urlencoded
HTTP/1.1 302 FOUND
Content-Type: text/html; charset=utf-8
Date: Tue, 10 Apr 2012 15:05:26 GMT
ETag: "d41d8cd98f00b204e9800998ecf8427e"
Location: http://pinterest.com/
Server: nginx/0.8.54
Set-Cookie: _pinterest_sess="eJzLcssPCy4NTclIjvAOrjQzyywoCChISgvLDi+2tY9PrSjILEottvUN8a3yc4k09gtxrfRLt7VVK04tLs5MAYonV/qGeFb4ZkWW+4LES4tTi+KBEv4u6UZ+WYEmvlm+QOxZ6R/iWOEbEmgLAKNfJps="; Domain=pinterest.com; HttpOnly; expires=Tue, 17-Apr-2012 15:05:26 GMT; Max-Age=1334675126; Path=/
Vary: Cookie
Content-Length: 0
Connection: keep-alive
応答では、curl が最後の _pinterest_sess Cookie を送信しなかったため、別の _pinterest_sess Cookie が設定されています。
現在、何か間違ったことをしているのか、それとも curl が Cookie の有効期限の値を正しく解析できていないのかはわかりません。
どんな助けでも大歓迎です:)
// edit もう 1 つ: http://opensource.apple.com/source/curl/curl-57/curl/lib/cookie.cによると、関数 curl_getdate() を使用して日付を抽出します。その関数のドキュメントには、いくつかの例がリストされています ( http://curl.haxx.se/libcurl/c/curl_getdate.html ):
- 1994 年 11 月 6 日 (日) 08:49:37 GMT
- 日曜日、06-11-94 08:49:37 GMT
- 1994 年 11 月 6 日 08:49:37 日
- 1994 年 11 月 6 日 08:49:37 GMT
- 1994 年 11 月 6 日 08:49:37 GMT
- 11 月 6 日 08:49:37 1994
- 1994 年 11 月 6 日 08:49:37
- 1994 年 11 月 6 日 08:49:37
- 1994 年 11 月 6 日 08:49:37 GMT
- 08:49:37 1994 年 11 月 6 日
- 日曜日 94 11 月 6 日 08:49:37
- 1994年11月6日
- 1994 年 11 月 6 日
- 94年11月6日(日)
- 1994.11.6
- 日/11/6/94/GMT
- 日、1994 年 11 月 6 日 08:49:37 CET
- 1994 年 11 月 6 日 08:49:37 EST
- 2004 年 9 月 12 日 (日) 15:05:58 -0700
- 2004 年 9 月 11 日 (土) 21:32:11 +0200
- 20040912 15:05:58 -0700
- 20040911 +0200
ハイフンを含むすべての例で 2 桁の年しか使用されていないため、上記の有効期限日「Tue, 17-Apr-2012 15:03:24 GMT」と一致するものはありません。