[29] [DFN[[CODE(MIME)@en[[[multipart/x-mixed-replace]]]]]] は、
[[HTTP応答]]により[[サーバー]]が任意のタイミングで複数の[[文書]]を返し、
[[紙芝居]]的に[[レンダリング]]を切り替えさせるものです。

[30] 90年代に [[Netscape]] 社により [[server push]] の手法として実装されました。
現在でも [[MJPEG]] の実現手法として用いられています。

* 仕様書

[REFS[
- [9] [CITE@en-US-x-hixie[HTML Standard]] ([TIME[2012-03-10 00:31:33 +09:00]] 版) <http://www.whatwg.org/specs/web-apps/current-work/#read-multipart-x-mixed-replace>
- [10] [CITE@en-US-x-hixie[HTML Standard]] ([TIME[2012-03-10 00:31:33 +09:00]] 版) <http://www.whatwg.org/specs/web-apps/current-work/#multipart/x-mixed-replace>
]REFS]

* 構文

[34] [CODE(MIME)@en[[[multipart/mixed]]]] と同じとされています。

* 処理

[36] 基本的には [CODE(MIME)@en[[[multipart/mixed]]]] と同じように処理します。

[35] [[サーバー]]は適当なタイミングで順次新しい[[本体部分]]を送出していきますから、
[[クライアント]]は[[multipart実体]]全体の到着を待たずに受信した[[本体部分]]から[[レンダリング]]していく必要があります。

* MJPEG over HTTP

[27] [[JPEG]] を [CODE(MIME)@en[[[multipart/x-mixed-replace]]]] により [[HTTP]]
で返し、[[動画]]として[[レンダリング]]させるものを [DFN[[[MJPEG over HTTP]]]]
と呼ぶことがあります。単に [[MJPEG]] や [[Motion JPEG]] と呼ぶこともあるようです。

;; [[MJPEG]] も参照。

[33] いわゆる[[Webカメラ]]の [[Webブラウザー]]向け[[UI]]の実装などに使われています。

[37] 現在画像としての [CODE(MIME)@en[[[multipart/x-mixed-replace]]]] の処理方法を明確に規定した仕様書は存在していません。

* navigate における [CODE(MIME)@en[multipart/x-mixed-replace]]

[31] 元々の [[server push]] は [[MJPEG]] のような[[画像]]としての埋め込みだけでなく、
ページ全体にも適用されるものでした。すなわち [CODE(MIME)@en[[[multipart/x-mixed-replace]]]]
への [[navigate]] も実装されていました。

[32] 現在では [[JavaScript]] によりページの一部分を書き換えることができるため、
ページ全体を送りなおす必要がある本手法は使われなくなっています。
それに伴い [[Webブラウザー]]からも [[navigate]] における
[CODE(MIME)@en[[[multipart/x-mixed-replace]]]] 対応は削除されつつあります。

;; [[HTML Standard]] にはまだ [CODE(MIME)@en[[[multipart/x-mixed-replace]]]]
への [[navigate]] の規定が残っています。


* 例

[PRE[
 Content-type: multipart/x-mixed-replace;boundary=ThisRandomString
 
 --ThisRandomString
 Content-type: text/plain
 
 Data for the first object.
 
 --ThisRandomString
 Content-type: text/plain
 
 Data for the second and last object.
 
 --ThisRandomString--
]PRE]

* 関連

[48] データを直接送信する代わりに [[URL]] にしたものが [[HLS]]
といえなくもありません。 [[HLS]] で [CODE[data:]] [[URL]] を使えば実質同じことができます
(実装が対応しているのかどうかは謎)。

* 歴史

[8] 
Netscape 社の定義した、「Server Push」の一手法であり、
<http://www.netscape.com/assist/net_sites/pushpull.html> 
に説明があります。

Web browser は、新しい Multipart の part が送られてきたら、
閲覧中の part の''代わりに''新しい part を閲覧させます。

サーバー側は、 part を送り終わっても connection を張ったまま
にして、次に必要になったら新しい part を送りつけます。

結局のところ、普及せず標準化もされず、失敗に終わったと見られます。

けど、業界(どこ)ではそれなりに知名度があるらしく、
multipart/x-mixed-replace が IE では正しく表示されません、
てな質問を CGI 系 ML で稀に見かけるなあ。

接続が時間切れになる可能性をどうするのか、
時間切れが無いとしたら resource 喰うんじゃ? (keep-alive
よりたちが悪そう)、
boundary が part の entity body 中に現れないことを必ずしも
保証出来るのか (HTTP で multipart/* に CTE は使えるんだっけ?)、
必ず part ごとに全体を送りなおさないといけないから無駄が多いのでは、
なんていう疑問点が浮かびます。

→CTE は使えません。たぶん。
使えたとしても、対応してる実装はそう無いでしょう。そもそも、
multipart (byterange 以外) を理解する HTTP の実装って
稀稀なのでは?


- [1] 2002-11-02 (土) 20:40 ''[[名無しさん]]'': [[M$]] も存在は意識しているようで、たとえば[[レジストリ]] HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\"MimeExclusionListForCache" とかにでてきたりします。 ([[WinIE]]5.5 on [[WinMe]] で確認)
- [2] ''Dynamic Document Demonstration'' <http://wp.netscape.com/assist/net_sites/mozilla/> Server Error になるが。。。

[3]
多部分境界の先頭の [CODE[--]] を抜かしている解説書を見かけました。いくらなんでも動作確認くらいしているでしょうから (と考えるのは甘い?)、 Classic Mozilla は多部分境界の最初の [CODE[--]] がなくても動くのかもしれません (要確認)。
([[名無しさん]])

[4]
[CITE[Surfin’ Safari - Blog Archive &#187; WebKit Fixes in Safari 2.0.2 / Mac OS X 10.4.3]] ([CODE[2007-02-03 21:37:54 +09:00]] 版) <http://webkit.org/blog/?p=32>
([[名無しさん]])

[5]
[CITE[Bug 76816 &#8211; View Source tries to DL Bugzilla buglist as multipart/x-mixed-replace]] ([TIME[2007-02-10 13:35:44 +09:00]] 版) <https://bugzilla.mozilla.org/show_bug.cgi?id=76816>

[6] 
[CODE(HTMLe)@en[[[img]]]] [CODE(HTMLa)@en[[[src]]]] に指定することによって、[[画像]]を[[アニメーション]]させることができたようです。

;; まだ[[アニメーションGIF]] が無かった頃に使われていたようです。

[7] [CITE@en[Web Applications 1.0 r6596     Specify multipart/x-mixed-replace]]
( ([TIME[2011-09-29 09:21:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=6595&to=6596>

[11] [CITE[''''''[''''''ietf-types'''''']'''''' Registration for multipart/x-mixed-replace]]
( ([TIME[2012-08-08 01:20:11 +09:00]] 版))
<http://www.ietf.org/mail-archive/web/ietf-types/current/msg01710.html>

[12] [CITE@en[Motion JPEG - Wikipedia, the free encyclopedia]]
( ([TIME[2013-05-17 03:59:36 +09:00]] 版))
<http://en.wikipedia.org/wiki/Motion_JPEG>

[13] [CITE[An Exploration of Dynamic Documents]]
([TIME[2015-01-24 22:05:57 +09:00]] 版)
<http://web.archive.org/web/19970614044340/http://home.netscape.com/assist/net_sites/pushpull.html>

[FIG(quote)[
[FIGCAPTION[
[14] [CITE[IRC logs: freenode / #whatwg / 20150317]]
([TIME[2015-03-18 11:33:58 +09:00]] 版)
<http://krijnhoetmer.nl/irc-logs/whatwg/20150317#l-704>
]FIGCAPTION]

> '''['''22:19''']''' <seth_> in general support for multipart/x-mixed-replace images and documents seems to be shrinking everywhere, but i suspect we will end up being stuck with at least the subset necessary to display JPEG webcams

]FIG]


[15] [CITE[Issue 249132 - chromium - Remove support for multipart/x-mixed-replace main resources - An open-source project to help move the web forward. - Google Project Hosting]]
([TIME[2015-05-03 19:24:25 +09:00]] 版)
<https://code.google.com/p/chromium/issues/detail?id=249132>

[16] [CITE[flash - multipart/x-mixed-replace ActionScript3 and Google Chrome (and others as well) - Stack Overflow]]
([TIME[2015-05-03 19:25:24 +09:00]] 版)
<http://stackoverflow.com/questions/5129620/multipart-x-mixed-replace-actionscript3-and-google-chrome-and-others-as-well>

[17] [CITE[http - chrome multipart/x-mixed-replace connection not closed - Stack Overflow]]
([TIME[2015-05-03 19:27:24 +09:00]] 版)
<http://stackoverflow.com/questions/25479248/chrome-multipart-x-mixed-replace-connection-not-closed>

[18] [CITE[Issue 146972 - chromium - multipart/x-mixed-replace no longer working reliably - An open-source project to help move the web forward. - Google Project Hosting]]
([TIME[2015-05-03 19:29:10 +09:00]] 版)
<https://code.google.com/p/chromium/issues/detail?id=146972>

[19] [CITE@en[987135 – motion JPEG server push (multipart/x-mixed-replace image stream) flickers and sometimes stops]]
([TIME[2015-05-03 19:29:52 +09:00]] 版)
<https://bugzilla.mozilla.org/show_bug.cgi?id=987135>

[20] [CITE@en[Streaming no longer works using Google Chrome under Ubuntu 12.04 · Issue #8 · RobotWebTools/mjpeg_server]]
([TIME[2015-05-03 19:30:41 +09:00]] 版)
<https://github.com/RobotWebTools/mjpeg_server/issues/8>

[21] [CITE@en[Bug 82291 – Pages containing "multipart/x-mixed-replace" resources should not be cached.]]
([TIME[2015-05-03 19:31:25 +09:00]] 版)
<https://bugs.webkit.org/show_bug.cgi?id=82291>

[22] [CITE@en[Bug 47222 – Ability to pause and resume mjpeg image streams]]
([TIME[2015-05-03 19:31:41 +09:00]] 版)
<https://bugs.webkit.org/show_bug.cgi?id=47222>

[23] [CITE[''''''[''''''webkit-dev'''''']'''''' ''''''[''''''Proposal'''''']'''''' Remove support for 'multipart/x-mixed-replace' main resources]]
([TIME[2015-04-22 08:03:07 +09:00]] 版)
<https://lists.webkit.org/pipermail/webkit-dev/2015-April/027379.html>

[24] [CITE@en[Bug 143979 – Crash when showing Web Inspector on page with 'multipart/x-mixed-replace' main resource]]
([TIME[2015-05-03 19:34:10 +09:00]] 版)
<https://bugs.webkit.org/show_bug.cgi?id=143979>

[FIG(quote)[
[FIGCAPTION[
[25] [CITE@en-US[Site Compatibility for Firefox 22 - Mozilla | MDN]]
([TIME[2014-05-03 01:58:20 +09:00]] 版)
<https://developer.mozilla.org/en-US/Firefox/Releases/22/Site_compatibility#XHR_multipart_response_support_has_been_removed>
]FIGCAPTION]

> Support for the multipart property and multipart/x-mixed-replace responses has been removed from XMLHttpRequest. This was a Firefox-only feature that was never standardized. 

]FIG]


[26] [CITE@en[843508 – Remove support for multipart XHR responses]]
([TIME[2015-05-03 19:53:14 +09:00]] 版)
<https://bugzilla.mozilla.org/show_bug.cgi?id=843508>

[28] [CITE@en[625012 – Flickering Motion JPEG (mjpg)]]
([TIME[2015-05-03 20:02:58 +09:00]] 版)
<https://bugzilla.mozilla.org/show_bug.cgi?id=625012>

[FIG(quote)[
[FIGCAPTION[
[38] [CITE@ja[動画配信]]
([TIME[2014-03-08 16:02:46 +09:00]] 版)
<http://www2.wisnet.ne.jp/~daddy/LC3/Broadcast.html>
]FIGCAPTION]

> 実際のクライアント側の作り方に関してですが、Multipart間のヘッダーの仕様も様々なので、ちゃんとpartを理解して受信するプログラムを記述するよりも、JPEG画像の開始コードと終了コードを目安に受信した方が接続性は上がると思います。 

]FIG]


[39] [CITE@en[Navigate: remove "gone async" and define redirect handling]]
([[annevk]]著, [TIME[2016-07-04 17:38:20 +09:00]])
<https://github.com/whatwg/html/commit/8b630f5e4fa2ec8b0999470d09490bffe6e9a1e3>

[40] [CITE@en[Remove navigation support for multipart/x-mixed-replace resources by annevk · Pull Request #1353 · whatwg/html]]
([TIME[2016-07-05 15:47:00 +09:00]])
<https://github.com/whatwg/html/pull/1353>

[41] [CITE@en[249132 - Remove support for multipart/x-mixed-replace main resources - chromium - Monorail]]
([TIME[2016-12-29 17:11:11 +09:00]])
<https://bugs.chromium.org/p/chromium/issues/detail?id=249132>

[42] [CITE@en[Bug 14392 – Add support for multipart/x-mixed-replace to XMLHttpRequest]]
([TIME[2016-12-29 17:12:38 +09:00]])
<https://bugs.webkit.org/show_bug.cgi?id=14392>

[43] [CITE@en[237319 – Add support for server push using multipart/x-mixed-replace with XMLHttpRequest.]]
([TIME[2016-12-29 17:13:06 +09:00]])
<https://bugzilla.mozilla.org/show_bug.cgi?id=237319>

[44] [CITE@en[843508 – Remove support for multipart XHR responses]]
([TIME[2016-12-29 17:14:22 +09:00]])
<https://bugzilla.mozilla.org/show_bug.cgi?id=843508>

[45] [CITE@en-US[XMLHttpRequest.multipart - Web APIs | MDN]]
([TIME[2016-07-09 13:04:08 +09:00]])
<https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/multipart>

[46] [CITE@en[CSP not applied to pages sent with multipart/x-mixed-replace — Mozilla]]
([TIME[2016-12-29 07:44:25 +09:00]])
<https://www.mozilla.org/en-US/security/advisories/mfsa2016-45/>

[47] [CITE@en[Access Denied]]
([TIME[2016-12-29 17:18:45 +09:00]])
<https://bugzilla.mozilla.org/show_bug.cgi?id=1223743>