第6回の内容
• コンテントネゴシエーション
• キャッシュ制御
• HTTP認証
リソースの表現
リソース
英語
HTML文書
日本語
プレーンテキスト
英語
PNG画像
日本語
⋯ バリアント
コンテントネゴシエーション
• HTTPリクエストメッセージのヘッダで、
希望する表現をサーバに通知
• 複数の候補を優先度付きで指定可
関連するヘッダフィールド
ヘッダ
説明
Accept
希望するメディアタイプを伝える
Accept-Language 希望する(自然)言語を伝える
優先度
q=
X
•
X
は0から1の数字 (小数点以下最大3桁)
• 既定値は1
Acceptヘッダ
• 希望するメディアタイプを伝える
タイプ
/サブタイプ
書式
意味
タイプ
/* あるタイプすべて
例:
image/* ⋯ 画像のタイプすべて
*/*
すべてのメディアタイプ
Acceptヘッダ
• 希望するメディアタイプを伝える
Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8
Acceptヘッダ
• 希望するメディアタイプを伝える
Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8
メディアタイプ
優先度
text/html
1 (既定値)
Acceptヘッダ
• 希望するメディアタイプを伝える
Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8
メディアタイプ
優先度
text/html
1 (既定値)
application/xhtml+xml 1 (既定値)
Acceptヘッダ
• 希望するメディアタイプを伝える
Accept: text/html,application/xhtml+xml,
application/xml;
q=0.9
,*/*;q=0.8
メディアタイプ
優先度
text/html
1 (既定値)
application/xhtml+xml 1 (既定値)
application/xml
0.9
Acceptヘッダ
• 希望するメディアタイプを伝える
Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;
q=0.8
メディアタイプ
優先度
text/html
1 (既定値)
application/xhtml+xml 1 (既定値)
application/xml
0.9
*/*
0.8
Acceptヘッダ
• 希望するメディアタイプを伝える
• HTML文書(text/html)かXHTML文書(application/
xhtml+xml)をもっとも希望する。(優先度1)
• 次にXML文書(application/xml)を希望する。(優先度0.9)
• それ以外(*/*)でもよい。(優先度0.8)
Accept: text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8
Accept-Languageヘッダ
• 希望する言語を伝える
言語タグ 意味
ja
日本語
ja-JP
日本で話される日本語
en
英語
en-US
アメリカ英語
en-CA
カナダ英語
fr-CA
カナダで話されるフランス語
書式:
X
または
X
-
Y
X
⋯ 言語
Y
⋯ 国や地域
言語タグ
Accept-Languageヘッダ
• 希望する言語を伝える
Accept-Language: ja,en-us;q=0.7,en;q=0.3
• 日本語(ja)をもっとも希望する。(優先度1)
• 次にアメリカ英語(en-us)を希望する。(優先度0.7)
• 次に国や地域を指定せずに英語(en)を希望する。(優先度0.3)
コンテントネゴシエーションの例 (1)
リクエスト
GET /manual HTTP/1.1
Accept: text/html, application/pdf;q=0.5
レスポンス
HTTP/1.1 200 OK
Content-Type:
text/html
リクエスト
GET /manual HTTP/1.1
Accept: application/pdf, text/html;q=0.5
レスポンス
HTTP/1.1 200 OK
コンテントネゴシエーションの例 (2)
リクエスト
レスポンス
GET /greeting HTTP/1.1
Accept:
text/html
, */*;q=0.5
Accept-Language:
en
, ja;q=0.5
HTTP/1.1 200 OK
Content-Type:
text/html
<!DOCTYPE html>
<html lang="en">
<head><title>Greeting</title></head>
<body><p>
Hello!
</p></body>
コンテントネゴシエーションの例 (2)
リクエスト
レスポンス
GET /greeting HTTP/1.1
Accept:
text/plain
, */*;q=0.5
Accept-Language:
ja
, en;q=0.5
HTTP/1.1 200 OK
Content-Type:
text/plain
こんにちは
Accept-Languageヘッダ
Accept-Languageヘッダ
Accept-Language:
ja
,
en-us
;q=0.7,
en
;q=0.3
コンテントネゴシエーションの実例
ステータスコード
406
100 Continue
101 Switching Protocols
200 OK
201 Created
202 Accepted
203 Non-Authoritative
Information
204 No Content
205 Reset Content
206 Partial Content
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
305 Use Proxy
307 Temporary Redirect
400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication
Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Payload Too Large
414 URI Too Long
415 Unsupported Media
Type
416 Range Not Satisfiable
417 Expectation Failed
426 Upgrade Required
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not
Supported
ステータスコード
406
リクエスト
レスポンス
HTTP/1.1 406 Not AcceptableContent-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
<title>406 Not Acceptable</title> </head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /manual could not be found on this server.</p>
Available variants: <ul>
<li><a href="manual.pdf">manual.pdf</a> , type application/pdf</li> <li><a href="manual.html">manual.html</a> , type text/html</li> </ul>
</body></html>
GET /manual HTTP/1.1
ステータスコード
406
リクエスト
レスポンス
HTTP/1.1 406 Not AcceptableContent-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
<title>406 Not Acceptable</title> </head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /manual could not be found on this server.</p>
Available variants: <ul>
<li><a href="manual.pdf">manual.pdf</a> , type application/pdf</li> <li><a href="manual.html">manual.html</a> , type text/html</li> </ul>
</body></html>
GET /manual HTTP/1.1
ステータスコード
406
リクエスト
レスポンス
HTTP/1.1 406 Not AcceptableContent-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
<title>406 Not Acceptable</title> </head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /manual could not be found on this server.</p>
Available variants: <ul>
<li><a href="manual.pdf">manual.pdf</a> , type application/pdf</li> <li><a href="manual.html">manual.html</a> , type text/html</li> </ul>
</body></html>
GET /manual HTTP/1.1
ステータスコード
406
リクエスト
レスポンス
HTTP/1.1 406 Not AcceptableContent-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head>
<title>406 Not Acceptable</title> </head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /manual could not be found on this server.</p>
Available variants:
<ul>
<li><a href="manual.pdf">manual.pdf</a> , type application/pdf</li> <li><a href="manual.html">manual.html</a> , type text/html</li>
</ul>
</body></html>
GET /manual HTTP/1.1