Vincent Bernat: Mobile browsing & content optimization
Since my laptop features WWAN connectivity, I purchased a 3G mobile
broadband plan from SFR, a French mobile phone company. I took
the cheapest one available: 250 MB per month for 10 .
Optimizing transparent proxy by SFR
While browsing, I was curious about the fact that there were multiple
accesses to non-public IP, like 10.141.0.4. I was browsing the mobile
version of Le Monde, a French daily newspaper. I enabled
Chromium developer tools and watched what happened in the Network
tab. All images were served from IP like 10.141.0.4. Looking at the
HTML source code, I discovered that <img> tags were rewritten to
point to those images. The smaller one were identical to the original
one but the larger ones (more than 50 KB) were of reduced quality (and
therefore reduced size).
<img> tags were not the only one to be rewritten. <style> and
<script> tags that were pointing to external resources (with a src
attribute) were also rewritten to include the CSS or the javascript
inline. Before:
<script src="http://s1.lemde.fr/js/lib/jquery/1.5.1/jquery.js"></script>
After:
<script oldSrc="http://s1.lemde.fr/js/lib/jquery/1.5.1/jquery.js" NG="replaced">
/*!
* jQuery JavaScript Library v1.5.1
*
* Copyright 2011, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
* [...] */
</script>
This means that each page that includes jQuery, a popular
javascript library, as an external resource, will be turned into a page
that includes the whole jQuery code which is about 85 KB. Each page
will weight more than 85 KB instead of relying on an external and
cacheable resource.
Nowadays, most browsers support HTTP keep-alive which
allows to serve multiple resources into a single request. There are
two cases:
-
If the resource is served from the same server as the HTML page,
keep-alive would enable to get almost the same result without
hindering the cache: the browser would first request the HTML page,
then the javascript resource into the same TCP connection. There is a
small penalty of about 100 ms to let the browser request the second
resource. The browser may decide to open another connection because
the initial document is not loaded yet. Some browsers may even
negotiate this additional connection before needing it. In this case,
it can use this idle connection at no cost. Otherwise, we may fall in
the second case.
-
If the resource is served from another server (like in the example
above) or if the browser does not have an idle connection ready for
reuse, it would download the resource in parallel with the help of
another connection. It will only costs about 300 ms of additional
time due to the establishment of a new TCP connection with the
server. Those 300 ms are a very small price to pay compared to the 2
seconds needed to download 85 KB for each page. Moreover, other
static resources will likely be downloaded from the same server and
therefore, this is 300 ms the first time and about 100 ms for
additional resources (round-trip time for a new HTTP request into the
same TCP connection).
UPDATED: In a previous version of this article, I confused
pipelining and keep-alive mechanisms. While
keep-alive allows to reuse an existing connection to download a new
resource, only one request at a time by connection could be
issued. Pipelining allows to push additional requests in the same
TCP connection without waiting for answers. As noted by
ric Daspet in a comment, pipelining
is disabled for most browsers. However, keep-alive is well supported.
Searching on the web for oldSrc and NG="replaced", I find very little
information about what is used. It seems that such a feature may also
break some scripts, like TinyMCE, trying to get additional resources
relative to the path where the script is installed. It appears that
SFR is using a product from Flash Networks called
Harmony Gateway. I have asked SFR more info about this and
if it was possible to disable the use of this feature but got no
answer so far.
I have retried a bit later and there was no rewriting for Javascript
and CSS. Images were still degraded and served by a special
server. The next day, my requests did not travel through a transparent
proxy anymore (no more Via header in answers). Each try was done at
a different location. The proxies may be installed at some locations
only and configured differently. Another explanation is that SFR
silently disabled this functionality for my account.
If it was done correctly, such an accelerator service could be be
proposed as an opt-out option since most users would appreciate faster
page downloads. If you care about network neutrality,
you can disable this option. Of course, it needs to be properly
advertised, for example as a free option in your mobile plan.
Setting up our own optimizing proxy
Google provides mod_pagespeed as a solution to optimize
a website on the fly. This is an Apache module. It is targeted at
optimizing your own website but since Apache can act as a regular
proxy, it seems possible to
enable mod_pagespeed for any website.
That s pretty easy to setup if you don t mind using a precompiled
version of mod_pagespeed. Download mod_pagespeed. I have
downloaded the 64bit version for Debian.
# touch /etc/default/mod-pagespeed
# dpkg -i ~/download/mod-pagespeed-beta_current_amd64.deb
Selecting previously deselected package mod-pagespeed-beta.
(Reading database ... 277699 files and directories currently installed.)
Unpacking mod-pagespeed-beta (from .../mod-pagespeed-beta_current_amd64.deb) ...
dpkg: dependency problems prevent configuration of mod-pagespeed-beta:
mod-pagespeed-beta depends on apache2.2-common; however:
Package apache2.2-common is not installed.
dpkg: error processing mod-pagespeed-beta (--install):
dependency problems - leaving unconfigured
Errors were encountered while processing:
mod-pagespeed-beta
# apt-get install -f
[...]
Unpacking apache2.2-common (from .../apache2.2-common_2.2.19-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up apache2.2-common (2.2.19-1) ...
[...]
# apt-get install apache2-mpm-worker
[...]
Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.19-1_amd64.deb) ...
Setting up apache2-mpm-worker (2.2.19-1) ...
Starting web server: apache2.
# a2enmod proxy_http
Considering dependency proxy for proxy_http:
Enabling module proxy.
Enabling module proxy_http.
To to activate the new configuration, you need to run:
/etc/init.d/apache2 restart
Then, we modify /etc/apache2/mods-enabled/proxy.conf:
ProxyRequests On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Proxy>
Of course, this makes little sense to run the proxy on your
laptop since you don t want to download the non-optimized content over
your WWAN link. You need to run it on another server and therefore allow your
laptop to access it.
Also, we need to modify /etc/apache2/mods-enabled/pagespeed.conf to
add the directive ModPagespeedDomain * to let mod_pagespeed
rewrite any page. You should restart Apache after that. Now, check if
the proxy is working:
$ curl -six http://localhost:80 http://www.lemonde.fr/ grep ^X-Mod-Pagespeed
X-Mod-Pagespeed: 0.9.17.7-716
All is working! Let s try a very simple site, like
Debian Backports. It features one HTML
file and two CSS files:
We downloaded a total of 8 KB (compression included). Let s use our
proxy and try again:
mod_pagespeed succeeds in reducing the size of the HTML file and
combined the CSS files. Moreover, it minified the resulting CSS
file. There seems to be a bug in Chromium that display incorrect
transfert sizes (including in the timeline representation) when using
a proxy. I have checked with Wireshark that the overall quantity
transferred was about 8 KB. The gain is very thin here but you can
imagine that you will get better results on larger sites.
mod_pagespeed seems to handle well most sites. However, it only
optimizes pages that already are in its cache. The first time you
grab a page, you will get the unoptimized version. I don t know if it
is the expected behaviour.
<script src="http://s1.lemde.fr/js/lib/jquery/1.5.1/jquery.js"></script>
<script oldSrc="http://s1.lemde.fr/js/lib/jquery/1.5.1/jquery.js" NG="replaced"> /*! * jQuery JavaScript Library v1.5.1 * * Copyright 2011, John Resig * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license * [...] */ </script>
mod_pagespeed as a solution to optimize
a website on the fly. This is an Apache module. It is targeted at
optimizing your own website but since Apache can act as a regular
proxy, it seems possible to
enable mod_pagespeed for any website.
That s pretty easy to setup if you don t mind using a precompiled
version of mod_pagespeed. Download mod_pagespeed. I have
downloaded the 64bit version for Debian.
# touch /etc/default/mod-pagespeed # dpkg -i ~/download/mod-pagespeed-beta_current_amd64.deb Selecting previously deselected package mod-pagespeed-beta. (Reading database ... 277699 files and directories currently installed.) Unpacking mod-pagespeed-beta (from .../mod-pagespeed-beta_current_amd64.deb) ... dpkg: dependency problems prevent configuration of mod-pagespeed-beta: mod-pagespeed-beta depends on apache2.2-common; however: Package apache2.2-common is not installed. dpkg: error processing mod-pagespeed-beta (--install): dependency problems - leaving unconfigured Errors were encountered while processing: mod-pagespeed-beta # apt-get install -f [...] Unpacking apache2.2-common (from .../apache2.2-common_2.2.19-1_amd64.deb) ... Processing triggers for man-db ... Setting up apache2.2-common (2.2.19-1) ... [...] # apt-get install apache2-mpm-worker [...] Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.19-1_amd64.deb) ... Setting up apache2-mpm-worker (2.2.19-1) ... Starting web server: apache2. # a2enmod proxy_http Considering dependency proxy for proxy_http: Enabling module proxy. Enabling module proxy_http. To to activate the new configuration, you need to run: /etc/init.d/apache2 restart
/etc/apache2/mods-enabled/proxy.conf:
ProxyRequests On <Proxy *> AddDefaultCharset off Order deny,allow Deny from all Allow from 127.0.0.1 </Proxy>
/etc/apache2/mods-enabled/pagespeed.conf to
add the directive ModPagespeedDomain * to let mod_pagespeed
rewrite any page. You should restart Apache after that. Now, check if
the proxy is working:
$ curl -six http://localhost:80 http://www.lemonde.fr/ grep ^X-Mod-Pagespeed X-Mod-Pagespeed: 0.9.17.7-716
We downloaded a total of 8 KB (compression included). Let s use our
proxy and try again:
mod_pagespeed succeeds in reducing the size of the HTML file and
combined the CSS files. Moreover, it minified the resulting CSS
file. There seems to be a bug in Chromium that display incorrect
transfert sizes (including in the timeline representation) when using
a proxy. I have checked with Wireshark that the overall quantity
transferred was about 8 KB. The gain is very thin here but you can
imagine that you will get better results on larger sites.
mod_pagespeed seems to handle well most sites. However, it only
optimizes pages that already are in its cache. The first time you
grab a page, you will get the unoptimized version. I don t know if it
is the expected behaviour.
As usual many people requested travel sponsorship to attend DebConf 11 and
are now waiting eagerly on the outcome of their request. We are sorry,
that we can t give you much details yet. To give you an idea of the
time line, here is a small explanation of the process.
About 100 people requested travel sponsorship. In total that would about
50 000 Euro. Problem is currently we don t exactly know, how much money we
may spend on travel sponsorship ourselves (By the way: If a potential
sponsor is reading this, please
DECLARATIVE DIVERSIONS
======================
Introduction
A diversion is when it is possible to have dpkg not overwrite a file when it
reinstalls the package it belongs to, and to have it put the file from the
package somewhere else instead.
Declarative diversions involves a new control file with a declarative
syntax which dpkg will parse and process directly as part of the package unpack
and removal phases, eliminating the problems resulting from non-atomic
handling of diversions.
Topics
There are a number of topics involved in implementing this kind of project
* What syntax do we use for the new control file?
* What dpkg does with the control file
* How do we handle diversions to a non-existant directory?
* How do we order unpacking a new package which adds a diversion?
* How do we order unpacking a new package that removes a diversion?
* How do we order removing a package which had a diversion?
* How do we handle errors?
* What happens to dpkg-divert?
Details - Control File Syntax
-
It will conform to RFC2822 style with the following format:
* Divert-From:
* Divert-To:
* Blank lines and lines beginning with # will be comments
Divert-To will be optional and if it is ommitted then files being diverted
will have their filename changed to file.distrib
The above style has it s advantages, one of the main ones being that there is
no need to escape whitespace within filenames. It also allows for more commands
to be added at a later date.
Details - Control File Handling
-
Within control.tar.gz the file should be named diversions
This file is then copied to /var/lib/info/$package.diversions
We need to store the control file for the currently-installed package so that
we can detect declarative diversions that have been removed from the control
file between versions (On-disk version and the to-be-unpacked version).
If there is no previous version of a control file then all current diversions
within the package are left in place and any new ones declared within the
control file are added.
If there is a previous version of the control file then the diversions
listed within the file are compared with the diversions currently in place.
If a diversion exists that is no longer listed in the control file then it is
removed. If there is a diversion listed in the control file that doesn t
currently exist then it is added.
Details - Handling Diversions to non-existant directories
-
Diverting files to directories that don t exist can cause a number of problems.
If the package does not own the directory it may be left orphaned on
removal of the package
The package is responsible for ensuring the availability of the target
directory in the unpack phase.
If a declarative diversion attempts to divert a file to a folder that neither
exists already on the filesystem at unpack time, nor is shipped within
the package, dpkg will return an error at unpack time.
Details - Ordering Requirements
-
=>Unpacking a new package that adds a diversion
*1. Check if package has a previous diversion control file
*2. Copy control file to tmp directory
*3. Calculate diversions to be added
*4. Unpack folders
*5. Add diversions
*6. Unpack files
*7. Copy control file to /var/lib/dpkg/info/$package.diversion
*Performing diversions this way avoids trouble with creating folders and
accidentally overwriting the wrong version of files.
*Another major problem would be extracting a new version of the file and
overwriting the old one.
*This might cause the wrong version of the file to be diverted and the
package to break.
=>Unpacking a new package that removes a diversion
*1. Check if package has a previous diversion control file
*2. Copy control file to tmp directory
*3. Calculate diversions to be removed
*4. Check if dropped diversion results in a file conflict and abort the
unpack if it does.
*5. Unpack folders
*6. Unpack files
*7. Remove old files no longer needed in the package
*8. Remove diversions
*9. Copy control file to /var/lib/dpkg/info/$package.diversion
*If a package has no previous diversion control file then no diversions
will be removed and existing diversions can only be removed using
dpkg-divert.
*If removing a diversion results in a file conflict then dpkg will return
an error at unpack time before anything is unpacked and the unpack
is aborted.
=>Removing a package which had a diversion
*1. Remove files
*2. Remove diversions
*3. Remove folders
*4. Remove control file at /var/lib/dpkg/info/$package.diversion
*This ensures that all files, diversions and folders are removed correctly
Details - Error handling
Errors in diversions will have to handled with a great deal of care due to
the fact that if they are not the package could be broken.
This means that a great deal of checks must be done to ensure that all the files
can be diverted properly before any actual diverting takes place. If they can t
the package installation/update must be stopped and rolled back to avoid the
package being installed incorrectly or broken.
Details - dpkg-divert
-
When we impliment the new diversion method we should keep the current
dpkg-divert. This allows maintainers to catch up with the new method without
breaking their packages. It also allows maintainers to perform some operations
that aren t support by the new method.
All current functionality of dpkg-divert will be left intact for use by
sysadmins and for maintainers to catch up with the new system. The two
systems will work together without directly conflicting with each other.
Example Usage #1
The file to be diverted is /usr/share/foo
It needs to be moved to /usr/share/bar
The syntax of the control file would be:
#Start File
Divert-From: /usr/share/foo
Divert-To: /usr/share/bar
#End File
Example Usage #2
In this example the maintainer doesn t want to move the file to any specific
folder
The syntax of the control file would be:
#Start File
Divert-From: /usr/share/foo
#End File
This would divert the file to /usr/share/foo.distrib
Footnotes
-
RFC2822 Guide:
*
(First posting to Pl net Commandline! Tag: pcli)
Vutral asked in IRC how to synchronise two shells environment
while they re running. As you may know, POSIX systems cannot change
a process environment vector after it has been started, only the
process itself can. Well,

I'm trying to resume some activity in OSM maintenance.
As many people, I mapped most parts ofmy neighbourhood, as one can see
when looking at
As I write this, it's the morning of June 5th, 1981. A few people scattered
across the US are waking up, going in to work, sitting down at their terminal
with a coffee, and reading Usenet. Usenet is only getting a trickle of posts
each day -- it's still in that period where it's easy to read every message
posted to it.
Many things lie in Usenet's future. It's still running
This is for my own reference more than anything else; periodically I find myself needing to boot DOS. Usually because I want to upgrade a BIOS and the upgrade tool only works with Windows or DOS. Yes, I'm aware of
I have been
Doing some work towards Debian Rolling
At the start of the month, the discussions about Debian rolling were still very active on debian-devel. Declaring that testing would be rolling did not make it (as
So I just read that
You'd think it'd be easy to reboot a PC, wouldn't you? But then you'd also think that it'd be straightforward to convince people that at least making some effort to be nice to each other would be a mutually beneficial proposal, and look how well that's worked for us.