ども、かっぱです。

tl;dr

リダイレクトがあったり Cookie をセットしたりする Web サイトを調査する際に利用する curl のオプションをいくつか試してみることにした。

メモ

アプリケーション

以下のような sinatra アプリケーションを用意して確認する。

require 'sinatra'
require 'sinatra/cookies'
require 'sinatra/reloader'

get '/' do
  cookies[:fortune] = 'cookie'
  redirect to('/fortune')
  p 'set cookie'
end

get '/fortune' do
  if request.cookies['fortune'] == 'cookie'
    p 'has cookie'
  else
    p 'no cookie'
  end
end

以下のように起動する。

$ ruby app.rb &
[1] 25386
t$ == Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop

普通にアクセス

普通に / にアクセスすると以下のように。

$ curl -v localhost:4567/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4567 (#0)
>127.0.0.1 - - [21/Feb/2016:21:35:51 +0900] "GET / HTTP/1.1" 302 - 0.0006
 GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:4567
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Content-Type: text/html;charset=utf-8
< Set-Cookie: fortune=cookie; path=/; HttpOnly
< Location: http://localhost:4567/fortune
< Content-Length: 0
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Connection: keep-alive
* Server thin is not blacklisted                                                                                                                                                                                                               < Server: thin                                                                                                                                                                                                                                 <
* Connection #0 to host localhost left intact

リダイレクト先がレスポンスヘッダの Location: にセットされていることが判る。また、Set-Cookie によって fortune=cookie がセットされていることも判る。

リダイレクト先までアクセスする

--location オプション(-L オプション)を利用することでリダイレクト先までアクセスすることが出来る。

$ curl -i --location localhost:4567/
127.0.0.1 - - [21/Feb/2016:21:41:32 +0900] "GET / HTTP/1.1" 302 - 0.0007
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html;charset=utf-8
Set-Cookie: fortune=cookie; path=/; HttpOnly
Location: http://localhost:4567/fortune
Content-Length: 0
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

"no cookie"
127.0.0.1 - - [21/Feb/2016:21:41:32 +0900] "GET /fortune HTTP/1.1" 200 9 0.0004                                                                                                                                                                tHTTP/1.1 200 OK                                                                                                                                                                                                                                tContent-Type: text/html;charset=utf-8
Content-Length: 9
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

cookie を保持したままリダイレクト先までアクセスする

cookie を保存する場合には --cookie オプション(-c オプション)を利用する。

$ curl -i -c cookie_saved --location localhost:4567/
127.0.0.1 - - [21/Feb/2016:21:44:12 +0900] "GET / HTTP/1.1" 302 - 0.0006
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html;charset=utf-8
Set-Cookie: fortune=cookie; path=/; HttpOnly
Location: http://localhost:4567/fortune
Content-Length: 0
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

"has cookie"
127.0.0.1 - - [21/Feb/2016:21:44:12 +0900] "GET /fortune HTTP/1.1" 200 10 0.0006
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Content-Length: 10
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

cookie_saved は以下のような内容になっている。

$ cat cookie_saved
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_localhost     FALSE   /       FALSE   0       fortune cookie

リダイレクト先を増やしてみて cookie が保持されているかを確認する

以下のようにリダイレクト追加。

require 'sinatra'
require 'sinatra/cookies'
require 'sinatra/reloader'

get '/' do
  cookies[:fortune] = 'cookie'
  redirect to('/fortune')
  p 'set cookie'
end

get '/fortune' do
  if request.cookies['fortune'] == 'cookie'
    p 'has cookie'
    p request.cookies['fortune']
    redirect to('/koisuru')
  else
    p 'no cookie'
  end
end

get '/koisuru' do
  if request.cookies['fortune'] == 'cookie'
    p 'has cookie'
    p request.cookies['fortune']
  else
    p 'no cookie'
  end
end

以下のように出力されて cookie が保持されていることが判る。

$ curl -i --cookie cookie_saved --location localhost:4567/
127.0.0.1 - - [21/Feb/2016:21:50:59 +0900] "GET / HTTP/1.1" 302 - 0.0006
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html;charset=utf-8
Set-Cookie: fortune=cookie; path=/; HttpOnly
Location: http://localhost:4567/fortune
Content-Length: 0
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

"has cookie"
"cookie"
127.0.0.1 - - [21/Feb/2016:21:50:59 +0900] "GET /fortune HTTP/1.1" 302 - 0.0005
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html;charset=utf-8
Location: http://localhost:4567/koisuru
Content-Length: 0
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

"has cookie"
"cookie"
127.0.0.1 - - [21/Feb/2016:21:50:59 +0900] "GET /koisuru HTTP/1.1" 200 6 0.0006
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Content-Length: 6
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Connection: keep-alive
Server: thin

以上

curl にはたくさんオプションがあって驚いた。

元記事はこちら

おやつの curl メモ(リダイレクト、cookie とか)