Cannot play cached audio from service worker - caching

Summary: Unable to cache and use the cached audio via a Service Worker.
Trying to: cache relatively small audio files (2-3mb) using service worker and cache API. These are loaded/played via a HTML Audio element, which usually sets a 'range' header.
Problem: Service worker responds with the whole content, ignoring the range header, and the Audio element ignores the content and does not play.
Without service worker: server obeys the range header, sends partial content, Audio element is happy and plays the audio.
I am using Google Chrome 52.
Note: Solution appears to have been noted by mnot "Serve range requests from a complete cache entry" but has any browser implemented this? Or, is there any workaround??
Technical info
Chrome terminal stderr: [1:1:0603/164806:ERROR:render_media_log.cc(23)] MediaEvent: PIPELINE_ERROR demuxer: could not open
Chrome console error: Uncaught (in promise) DOMException: The element has no supported sources.
Chrome headers (with SW, 2 parts):
PART#1
GENERAL
Request URL:http://localhost:3333/audio.m4a
Request Method:GET
Status Code:200 OK (from ServiceWorker)
Remote Address:127.0.0.1:3333
RESPONSE
accept-ranges:bytes
connection:keep-alive
content-length:2449048
content-type:audio/x-m4a
date:Fri, 03 Jun 2016 10:14:02 GMT
etag:"56f8f953-255e98"
last-modified:Mon, 28 Mar 2016 09:28:51 GMT
server:nginx
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
REQUEST
Provisional headers are shown
Accept-Encoding:identity;q=1, *;q=0
Range:bytes=0-
Referer:http://localhost:3333/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.10 Safari/537.36
PART #2
GENERAL
Request URL:http://localhost:3333/audio.m4a
Request Method:GET
Status Code:200 OK (from ServiceWorker)
Remote Address:127.0.0.1:3333
RESPONSE
accept-ranges:bytes
connection:keep-alive
content-length:2449048
content-type:audio/x-m4a
date:Fri, 03 Jun 2016 10:14:02 GMT
etag:"56f8f953-255e98"
last-modified:Mon, 28 Mar 2016 09:28:51 GMT
server:nginx
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
REQUEST
Provisional headers are shown
Accept-Encoding:identity;q=1, *;q=0
Range:bytes=2392064-
Referer:http://localhost:3333/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.10 Safari/537.36
Chrome headers (without SW, 3 parts!):
PART #1
GENERAL
Request URL:http://localhost:3333/audio.m4a
Request Method:GET
Status Code:206 Partial Content
Remote Address:127.0.0.1:3333
RESPONSE
Connection:keep-alive
Content-Length:2449048
Content-Range:bytes 0-2449047/2449048
Content-Type:audio/x-m4a
Date:Fri, 03 Jun 2016 10:24:32 GMT
ETag:"56f8f953-255e98"
Last-Modified:Mon, 28 Mar 2016 09:28:51 GMT
Server:nginx
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block
REQUEST
Accept:*/*
Accept-Encoding:identity;q=1, *;q=0
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Host:localhost:3333
Range:bytes=0-
Referer:http://localhost:3333/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.10 Safari/537.36
PART #2
GENERAL
Request URL:http://localhost:3333/audio.m4a
Request Method:GET
Status Code:206 Partial Content
Remote Address:127.0.0.1:3333
RESPONSE
Connection:keep-alive
Content-Length:56984
Content-Range:bytes 2392064-2449047/2449048
Content-Type:audio/x-m4a
Date:Fri, 03 Jun 2016 10:24:32 GMT
ETag:"56f8f953-255e98"
Last-Modified:Mon, 28 Mar 2016 09:28:51 GMT
Server:nginx
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block
REQUEST
Accept:*/*
Accept-Encoding:identity;q=1, *;q=0
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Host:localhost:3333
If-Range:"56f8f953-255e98"
Range:bytes=2392064-2449047
Referer:http://localhost:3333/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.10 Safari/537.36
PART #3
GENERAL
Request URL:http://localhost:3333/audio.m4a
Request Method:GET
Status Code:206 Partial Content
Remote Address:127.0.0.1:3333
RESPONSE
Content-Length:2121368
Content-Range:bytes 327680-2449047/2449048
Content-Type:audio/x-m4a
Date:Fri, 03 Jun 2016 10:24:32 GMT
ETag:"56f8f953-255e98"
Last-Modified:Mon, 28 Mar 2016 09:28:51 GMT
Server:nginx
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block
REQUEST
Provisional headers are shown
Accept-Encoding:identity;q=1, *;q=0
Range:bytes=327680-
Referer:http://localhost:3333/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.10 Safari/537.36

Check out https://samdutton.github.io/samples/service-worker/prefetch-video/ which works around this issue by manually created ranged responses.
Fixing this is gated on figuring out what browsers should be doing here, and updating the service worker spec if needed.

I used IndexedDB to implement this functionality and results are pretty impressive. You can use localForage to store the audio file in database and later retrieve it in offline mode via blob src url.
request = new XMLHttpRequest();
request.open('GET', audioURL, true);
request.responseType = 'blob';
request.onreadystatechange = function(){
if (request.readyState === 4 && request.status === 200){
localforage.setItem('KeyName', request.response).then(function(savedAudioBlob) { console.log('Saved Blob URL ' + savedAudioBlob); }); }

Related

Can't download a file, error 500

I'm trying to download a zipped file from Amazon Datafeed url and then decompress it.
This is my code:
open('public/files/amazon_ce.xml', 'w') do |local_file|
open('https://assoc-datafeeds-eu.amazon.com/datafeed/it_amazon_ce.xml.gz', :http_basic_authentication=>[USERNAME, PASSWORD]) do |remote_file|
local_file.write(Zlib::GzipReader.new(remote_file).read)
end
end
If I try with another file everything is ok, but not with this Amazon file: the error is:
OpenURI::HTTPError: 500 Internal Server Error
I logged the request when I download the same file using the browser...
GET /datafeed/getFeed?filename=it_amazon_ce.xml.gz HTTP/1.1
Host: assoc-datafeeds-eu.amazon.com:443
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: xxxxx
Referer: https://assoc-datafeeds-eu.amazon.com/datafeed/listFeeds?format=text/html
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
HTTP/1.1 302 Moved Temporarily
Cache-Control: no-cache
Content-Length: 0
Date: Wed, 30 Sep 2015 21:24:22 GMT
Expires: Thu, 01 Jan 1970 00:00:00 UTC
Location: http://prod-deamazon.outputs.datafeeds.s3.amazonaws.com/it_amazon_dvd.xml.gz?Signature=xxxx&AWSAccessKeyId=xxxx&Expires=xxxx
Pragma: No-cache
Server: Apache-Coyote/1.1
Any idea?
Your browser is already authenticated and has a cookie which allows you to download the file.
You need to make sure that your Rails app has proper credentials to download the file (I can't in my browser, it asks for username/password)

WebSocket 'Connection' header is missing

I am trying to connect to Intersystems Cache CSP Websocket with the following line from the client:
var ws = new WebSocket("ws://" + window.location.host + "/path/ClassName.cls");
And I'm getting this error in chrome console:
WebSocket connection to 'ws://<server address>' failed: Error during WebSocket handshake: 'Connection' header is missing
The request headers:
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,he;q=0.6
Cache-Control:no-cache
Connection:Upgrade
Cookie:CSPWSERVERID=Cache for Windows (x86-64) 2013.1.1 (Build 501_1_13062) Sun Aug 11 2013 23:20:33 EDT
Host:<host-ip>
Origin:http://<host-ip>
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:<the key>
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36
And the response headers:
Date:Tue, 04 Aug 2015 11:45:54 GMT
Sec-WebSocket-Accept:<some key>
Sec-WebSocket-Protocol:chat
Server:Microsoft-IIS/7.5
Transfer-Encoding:chunked
Upgrade:websocket
X-Powered-By:ASP.NET
The server is using IIS 7.5, which technically doesn't support WebSocket, but it seems that all the websocket headers are indeed in place, except one: "Connection":"Upgrade".
Is there any way to inject the "connection" header into the handshake response? Is there some client configuration that can disregard the missing header?
Thanks.
Unfortunately for WebSockets support you need IIS 8 + Windows 8 and Windows Server 2012
http://docs.intersystems.com/cache20152/csp/docbook/DocBook.UI.Page.cls?KEY=GCGI_oper_config#GCGI_websockets

Why is the browser not caching my images?

I am using nginx and Adaptive Images to deliver dynamically sized images based on device resolution. The response headers being set by the adaptive-images.php file are shown below but every time I refresh the page the browser requests the images again. Why is the browser not caching these images? The browser is Google Chrome and it seems to be setting max-age=0 in the request headers no matter how I refresh. I've tried F5, Ctrl+F5 and entering the URL in the address bar and hitting Enter.
Request Headers:
GET /img/photos/p8.jpg HTTP/1.1
Host: example.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: image/webp,*/*;q=0.8
Pragma: no-cache
If-Modified-Since: Wed, 16 Jul 2014 12:01:31 GMT
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Referer: http://example.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6
Response Headers:
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 16 Jul 2014 12:08:55 GMT
Content-Type: image/jpeg
Content-Length: 391104
X-Powered-By: PHP/5.4.30
Cache-Control: private, max-age=604800
Expires: Wed, 23 Jul 2014 12:08:55 GMT
Last-Modified: Wed, 16 Jul 2014 12:08:55 GMT
Connection: Keep-Alive
It turns out that this seems to be a Chrome feature
See this other SO answer for why: Chrome doesn't cache images/js/css
Don't use control shift r when testing this (reload)

Jmeter- HTTP Cache Manager, Unable to cache everything what it is being cached by Browser

I used HTTP Chache Manager to Cache files which are being cached in browser. I am successful of doing it for some of the pages. Number of files being cached in Jmeter is equal to Number of files being cached by browser.
But in some cases :
I found number files being cached is lesser than the files being cached by browser.
Using Jmeter I found only 5 files are being cached but in real browser 12 files are getting cached.
Header for one file which is cached in Chrome but not in Jmeter
Header in Chrome Browser:
Remote Address:
Request URL:
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:image/webp,/;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8,it-CH;q=0.6,it;q=0.4,ar;q=0.2
Cache-Control:max-age=0
Connection:keep-alive
Cookie:
Host:
If-Modified-Since:Thu, 16 Jan 2014 16:38:32 GMT
If-None-Match:W/"1242-1389890312000"
Referer:
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Response Headersview source
Cache-Control:private
Connection:keep-alive
Date:Wed, 11 Jun 2014 09:57:49 GMT
ETag:W/"1242-1389890312000"
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Server:
Header in JMeter:
Thread Name: Thread Group 1-2
Sample Start: 2014-06-11 15:18:56 IST
Load time: 326
Latency: 326
Size in bytes: 1541
Headers size in bytes: 299
Body size in bytes: 1242
Sample Count: 1
Error Count: 0
Response code: 200
Response message: OK
Response headers:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private
Content-Type: image/png
Date: Wed, 11 Jun 2014 09:48:53 GMT
ETag: W/"1242-1389890312000"
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified: Thu, 16 Jan 2014 16:38:32 GMT
Server:
Content-Length: 1242
Connection: keep-alive
Thanks in advance
Have you tried to tick Use Cache Control/Expires header when processing GET requests box which simulates real browser behavior and matching content is returned immediately without actual request being made.
Another possible reason is exceeding Max Number of elements in cache threshold which defaults to 5000.
See Using the HTTP Cache Manager guide for further explanations and recommendations.

MVC3 project has Ajax calls failed after deploying to IIS7.5

I have read almost all of the questions on this, but found no solution. The error I got is 500 Internal Service Error which points to a specific error: CS0103: The name 'ViewBag' does not exist in the current context.
The strange thing is this error only occurs when an ajax call is made. When there is no ajax call involved, the same cshtml page works fine.
Here are the two responses with one that works(first) and the other fails(second):
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
Response Headersview source
Access-Control-Allow-Origin:Access-Control-Allow-Origin
Cache-Control:private
Content-Encoding:gzip
Content-Length:7033
Content-Type:text/html; charset=utf-8
Date:Mon, 10 Feb 2014 19:25:49 GMT
Server:Microsoft-IIS/7.5
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
Request Method:GET
Status Code:500 Internal Server Error
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
quarter:1
year:1
Response Headersview source
Access-Control-Allow-Origin:Access-Control-Allow-Origin
Cache-Control:private
Content-Length:48466
Content-Type:text/html; charset=utf-8
Date:Mon, 10 Feb 2014 19:28:29 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
Please help!

Resources