スポンサーリンク
ウェブブラウジングの根幹を支える技術に cookie(クッキー) があります。
これはエンジニアのみならずほとんどの人のウェブブラウジングで使われています。
私はこのcookieがどんな物かについて、なんとなくの役割だけは知っていましたが、実際にどのようにやり取りされるかをよく知りませんでした。
しかしとある案件で詳しく知る必要があったので、調べたことをメモしておきたいと思います。
とあるWebサービス上で別サービスのAPIを実行する必要があったのですが、そのときにcookieを使うというオプションが存在しませんでした。
一方、HTTPヘッダを読み込んで使うことはできたので、必然的に自分でcookieを処理する動作を作り込む必要がありました。
その過程で初めてcookieのやり取りの詳細を知りましたので、今回はcurlを使ってそのやり取りを説明したいと思います。
wikipedia様によると、、
HTTP cookie(エイチティーティーピークッキー、単にクッキーとも表記される)は、マジッククッキーの一種であり、RFC 6265などで定義されたHTTPにおけるウェブサーバとウェブブラウザ間で状態を管理する通信プロトコル、またそこで用いられるウェブブラウザに保存された情報のことを指す。
ユーザ識別やセッション管理を実現する目的などに利用される。
とのことです。
重要なのは HTTPにおけるウェブサーバとウェブブラウザ間で状態を管理する通信プロトコル
の部分かと思います。
要するに、本来 ステートレス(状態の概念が存在しない)なHTTP というプロトコルにおいて、 状態を管理する機能を補完するための仕組み ということかと思います。
まずcookieは HTTPのヘッダ でやり取りされます。
実際に google.co.jp
にアクセスしてみます。
# ヘッダだけ表示する
curl -Ss -o /dev/null -D - https://www.google.co.jp
すると、ヘッダは以下のような内容が返ってきます。
HTTP/2 200
date: Mon, 27 May 2019 23:45:29 GMT
expires: -1
cache-control: private, max-age=0
content-type: text/html; charset=Shift_JIS
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
set-cookie: 1P_JAR=2019-05-27-23; expires=Wed, 26-Jun-2019 23:45:29 GMT; path=/; domain=.google.co.jp
set-cookie: NID=184=rA7ccN1BnJ2bixU7PmVQ8OUKzSbz9GHvvXa0JdMH2Uh0FHA3gCOu9sDg3jy9QzRgAlrTN9fsaK3QABOgjY7cGbJC7DEG5wKp3D7wYb4g7ftT9QJ_qleP9acX_Z4KoBhkXKF1LGmWALFgg--SEYBwYCbCL-7KgWDpdrEdb1TFqss; expires=Tue, 26-Nov-2019 23:45:29 GMT; path=/; domain=.google.co.jp; HttpOnly
alt-svc: quic=":443"; ma=2592000; v="46,44,43,39"
accept-ranges: none
vary: Accept-Encoding
この中の
set-cookie: 1P_JAR=2019-05-27-23; expires=Wed, 26-Jun-2019 23:45:29 GMT; path=/; domain=.google.co.jp
set-cookie: NID=184=rA7ccN1BnJ2bixU7PmVQ8OUKzSbz9GHvvXa0JdMH2Uh0FHA3gCOu9sDg3jy9QzRgAlrTN9fsaK3QABOgjY7cGbJC7DEG5wKp3D7wYb4g7ftT9QJ_qleP9acX_Z4KoBhkXKF1LGmWALFgg--SEYBwYCbCL-7KgWDpdrEdb1TFqss; expires=Tue, 26-Nov-2019 23:45:29 GMT; path=/; domain=.google.co.jp; HttpOnly
この部分がcookieの実体です。
もっと具体的に言うと、
1P_JAR=2019-05-27-23;
NID=184=rA7ccN1BnJ2bixU7PmVQ8OUKzSbz9GHvvXa0JdMH2Uh0FHA3gCOu9sDg3jy9QzRgAlrTN9fsaK3QABOgjY7cGbJC7DEG5wKp3D7wYb4g7ftT9QJ_qleP9acX_Z4KoBhkXKF1LGmWALFgg--SEYBwYCbCL-7KgWDpdrEdb1TFqss;
この部分こそがいわゆるcookieになります。一つ前の例では expires
や path
といった文字列も含まれていますが、それらは有効期限などの付加情報になります。
先程の例で少し触れていますが、HTTPサーバ <-> クライアント(ブラウザ) 間でcookieをやり取りする仕組みは以下の図のようになっています。
まずサーバ側から、「このcookieを付けてきてね」という情報が set-cookie
として渡されます。
次にクライアントがアクセスするときにはその情報を cookie
というヘッダに付けて送ります。
こうしてcookieのやり取りが行われるわけです。
では実際にgoogleの例を使ってcookieを手で付けてみます。
まずgoogleの検索画面にアクセスします。
(今回はわかりやすくするためヘッダのset-cookieだけを抽出)
curl -Ss -D - https://www.google.co.jp | grep 'set-cookie'
set-cookie: 1P_JAR=2019-05-28-23; expires=Thu, 27-Jun-2019 23:54:31 GMT; path=/; domain=.google.co.jp
set-cookie: NID=184=SWRLSIx1X4rTGnhMDk_VAZ8998GT9d7XHx99QbKm0wAER7FZNjvgeWLzPSDaOwwhXvCb9-gcxLeN-JrGepGZjcKcPwbfZhGuNlQkTZ4RRBiF5OOsTFh-beEAvB3zALqr1AxWjAcnXvbACQ7c2JW_p_TK9YxG_-akrrDExZLyU9I; expires=Wed, 27-Nov-2019 23:54:31 GMT; path=/; domain=.google.co.jp; HttpOnly
上記のようなレスポンスが返ってきます。
このレスポンスのうち、最初のカラムがcookieですので、そこだけを抜き出しておきます。
ちなみに上記のように複数 set-cookie
ヘッダが付いている場合がありますが、 全て有効です ので、今回も2行分のcookieを付ける必要があります。
次にアクセスするときは以下のようにcookieを付けます。
curl -H "Cookie: 1P_JAR=2019-05-28-23; NID=184=SW(長いので略)LyU9I" https://www.google.co.jp
このように、-H
オプションで Cookie
というヘッダを付与することができます。
また ;
で区切ることで複数のcookieをつけることができます。
これでcookieを使ったやり取りができるようになりました。
ちなみにcurlにはcookieを扱うオプションが用意されています。
cookieを受け取る場合
curl -c cookie.txt https://www.google.co.jp
# ./cookie.txt というファイルにcookie情報が格納される
cookieを使う場合
curl -b cookie.txt https://www.google.co.jp
# ./cookie.txt というファイルのcookie情報を使う
以上となります。
スポンサーリンク
2022/05/20 21:39:55
levitra ohne rezept levitra 20 mg 4 tablet https://xclevitradb.com/
2022/04/08 10:46:15
sildenafil tablets does sildenafil really work
2022/04/09 01:12:55
viagra cost viagra online canadian pharmacy
2022/04/09 04:20:16
tadalafil from nootropic review is tadalafil available in the united states
2022/04/09 08:51:18
buy viagra online without prescription buy female viagra viagra alternative buy cheap viagra
2022/04/09 16:35:16
tadalafil softsules tuf 20 tadalafil davis pdf
2022/04/10 05:31:06
buy tadalafil 20mg price cialis 20 mg
2022/04/12 07:25:01
my online pharmacy american rx pharmacy
2022/04/12 17:24:36
Was sind go to the happy hunting-grounds besten Gewinnchancen in einem Casino? "Einarmige Banditen"
Einarmige Banditen
Casinos werden in erster Linie mit Roulette und Poker in Verbindung gebracht, aber Statistiken zeigen, dass 61 % der Besucher von Spielhallen ihre Zeit damit verbringen, einarmige Banditen zu spielen (Daten von 2013 von der American Gaming Bonding). Die Regeln der Spielautomaten sind sehr einfach, und der niedrige Mindesteinsatz macht sie auch fur die armsten Spieler zuganglich.
https://bunkerhosting.de/sharing/robots/was_sind_die_besten_gewinnchancen_in_einem_casino__mathematik_in_casinos__volatilit_t_.html
http://ferienportugal.net/ice_media/robots/was_ist_der_beste_einsatz_bei_spielautomaten___10_wege_zur_erh_hung_ihrer_gewinnwahrscheinlichkeit.html
http://gsv-moers.de/wp-content/pages/casino_gewinne_1.html
https://welovetents.com/pag/was_sind_die_besten_gewinnchancen_in_einem_casino__eine_strategie_mit_positiver_progression.html
https://berliner-vv.de/articles/wie_man_sich_f_r_einen_casino_abend_kleidet.html
2022/04/12 20:01:54
order viagra viegra
2022/04/12 21:33:37
sildenafil for dogs sildenafil (viagra) prolongs erection by __________.
2022/04/13 04:49:06
tadalafil site:doctoroz.com online prescription tadalafil
2022/04/13 23:00:38
Kamagra order prescriptions online without doctor
2022/04/14 12:59:25
sildenafil and nitrates canada brand viagra
2022/04/14 18:07:39
top 10 pharmacy websites online pharmacy overnight delivery
2022/04/14 20:19:43
levitra samples levitra 10 mg precio mexico
2022/04/15 00:53:24
online pharmacies no prescription brand cialis
2022/04/15 18:02:23
cialis manufacturer coupon canadian cialis reviews
2022/04/17 21:57:15
penalty for mailing prescription drugs pfizer viagra online canadian pharmacy
2022/04/17 13:51:04
how to take tadalafil peptide pros tadalafil review
2022/04/17 19:47:40
cialis 20 cialis soft
2022/04/18 02:29:39
buy real viagra uk purchase viagra pills
2022/04/18 22:34:56
ivermectin for cows ivermectin dosage for lyme disease
2022/04/20 09:23:47
free cialis voucher viagra cialis samples
2022/04/20 21:32:53
cialis 10mg canada cheap cialis super active
2022/04/21 02:23:58
canada pharmacy online viagra prescription sildenafil over the counter canada
2022/04/21 04:29:16
viagra cialis levitra comparison levitra vs cialis vs viagra
2022/04/21 08:59:08
online pharmacy forums legitimate online pharmacy
2022/04/22 06:04:10
levitra side effects medications levitra viagra generic
2022/04/23 13:49:11
street names for prescription drugs online pharmacy australia free delivery
2022/04/24 02:01:10
how to start a pharmacy store Zyvox
2022/04/24 06:56:05
custom rx pharmacy kuna zithromax online pharmacy
2022/04/24 09:04:14
Brand Levitra rite aid pharmacy store locator
2022/04/24 13:30:23
canadian pharmacy viagra 200mg dominican republic pharmacy online
2022/04/25 10:04:21
canadian pharmacy antibiotics canada drugs wichita ks
2022/04/30 01:22:22
how to buy viagra from canada where can i buy viagra uk https://sgiviagraikn.com/
2022/04/30 13:20:34
where does sildenafil citrate come from sildenafil recommended dosage https://sildenafilkiol.com/
2022/04/30 18:06:31
Plendil canada pharmacy checker https://dtupharmyjn.com/
2022/04/30 20:11:55
cost of levitra 20 mg buy viagra levitra https://glevitrargu.com/
2022/05/01 00:40:30
milbemycin oxime vs ivermectin doramectin vs ivermectin https://stromectolujlo.com/
2022/05/01 20:44:56
business plan for pharmacy store cvs pharmacy refill online https://jzpharmacyukj.com/
2022/05/04 03:26:05
best online pharmacy navarro pharmacy store locator
2022/05/04 20:23:59
low dose cialis cialis soft tabs https://dccialisxvf.com/
2022/05/05 02:43:44
sildenafil india purchase can you purchase viagra in mexico https://aviagrasbt.com/
2022/05/05 21:29:06
where to buy generic levitra online levitra coupons https://xclevitradb.com/
2022/05/06 23:36:51
hcqs 400 plaquenil retinopathy
2022/05/09 21:21:17
lowest price canadian pharmacy us pharmacy no prior prescription
2022/05/10 08:56:13
buy sildenafil online sildenafil 100mg how long does it last https://sildenafilkiol.com/
2022/05/10 13:50:42
cialis dapoxetine 24 tab cialis,com https://dccialisxvf.com/
2022/05/10 15:35:25
levitra cvs levitra verses viagra https://glevitrargu.com/
2022/05/10 20:21:05
generic viagra for sale buy viagra over the counter australia https://aviagrasbt.com/
2022/05/11 13:05:21
order modafinil without prescription
2022/05/11 14:39:54
how long does levitra stay in your system levitra costs https://xclevitradb.com/
2022/05/13 18:48:12
dissertation uk help
dissertation consulting
dissertation help in los angeles
2022/05/14 18:11:39
dissertation help service quality
statistics help for dissertation
masters dissertation
2022/05/15 00:07:11
pricepro pharmacy, 102-17750 56 ave, surrey, bc v3s 1k4, canada pharmacy technician store
2022/05/15 11:50:12
side effects of sildenafil sildenafil and blood pressure https://sildenafilkiol.com/
2022/05/15 12:03:40
writing a dissertation in a week
dissertation help student room
dissertation custom writing
2022/05/15 18:40:59
dosage levitra buy levitra online with prescription https://glevitrargu.com/
2022/05/16 11:17:55
dissertation help leicester
masters dissertation writing services uk
art dissertation help
2022/05/18 09:00:13
order modafinil 200mg pill modafinil tablet
2022/05/18 12:48:18
order modafinil 200mg buy provigil 100mg pills provigil 200mg cheap
2022/05/18 14:28:44
2022/05/18 18:35:16
2022/05/19 03:12:32
marine pharmacy canada medication canadian pharmacy
2022/05/19 15:02:26
sildenafil masticables sildenafil citrate sublingual tablets 100mg https://sildenafilkiol.com/
2022/05/19 19:42:42
what does cialis look like cialis no perscrption https://dccialisxvf.com/
2022/05/19 21:43:55
levitra dosage recommended levitra coupon 2015 https://glevitrargu.com/
2022/05/19 23:37:00
dissertation template
dissertation hypothesis help
help with dissertation topic
2022/05/20 02:14:54
sildenafil buy over the counter cheap viagra 100mg uk https://aviagrasbt.com/
2022/05/20 07:27:50
provigil 200mg brand modafinil 100mg pill
2022/05/20 11:40:35
purchase modafinil provigil 100mg generic
2022/05/20 13:33:18
provigil 200mg ca order modafinil 100mg sale
2022/05/20 15:36:13
erythromycin eye ointment for newborns erythromycin overdose
2022/05/20 18:19:23
2022/05/20 18:39:37