Add -fno-strict-aliasing to prevent compile warnings on some systems.
[polipo.git] / polipo.texi
blob0ea611c919db3c48498a59c908335487c8a8ed3f
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename polipo.info
4 @settitle The Polipo Manual
5 @afourpaper
6 @c %**end of header
8 @dircategory Network Applications
9 @direntry
10 * Polipo: (polipo).                     The Polipo caching web proxy.
11 @end direntry
13 @copying
14 Copyright @copyright{} 2003 -- 2010 by Juliusz Chroboczek.
15 @end copying
17 @titlepage
18 @title The Polipo Manual
19 @author Juliusz Chroboczek
20 @page
21 @vskip 0pt plus 1fill
22 Polipo is a caching web proxy designed to be used as a personal
23 cache or a cache shared among a few users.
24 @vskip 0pt plus 1fill
26 @insertcopying
28 @end titlepage
30 @contents
32 @ifnottex
33 @node Top, Background, (dir), (dir)
34 @top Polipo
36 Polipo is a caching web proxy designed to be used as a personal
37 cache or a cache shared among a few users.
39 @ifhtml
40 The latest version of Polipo can be found on 
41 @uref{http://www.pps.jussieu.fr/~jch/software/polipo/,the Polipo web page}.
42 @end ifhtml
44 This manual was written by
45 @uref{http://www.pps.jussieu.fr/~jch/,,Juliusz Chroboczek}.
47 @end ifnottex
49 @menu
50 * Background::                  Background information.
51 * Running::                     Running Polipo
52 * Network::                     Polipo and the network.
53 * Caching::                     Caching.
54 * Memory usage::                Limiting Polipo's memory usage.
55 * Copying::                     Your rights and mine.
56 * Variable index::              Variable index.
57 * Concept index::               Concept index.
58 @end menu
60 @node Background, Running, Top, Top
61 @chapter Background
63 @menu
64 * The web::                     The web and HTTP.
65 * Proxies and caches::          Proxies and caches.
66 * Latency and throughput::      Optimise latency, not throughput.
67 * Network traffic::             Be nice to the net.
68 * Partial instances::           Don't discard data.
69 * POST and PUT::                Other requests
70 * Other HTTP proxies::          Why did I write Polipo from scratch?
71 @end menu
73 @node The web, Proxies and caches, Background, Background
74 @section The web and HTTP
75 @cindex URL
76 @cindex resource
77 @cindex instance
78 @cindex entity
79 @cindex HTTP
81 The web is a wide-scale decentralised distributed hypertext system,
82 something that's obviously impossible to achieve reliably.
84 The web is a collection of @dfn{resources} which are identified by
85 @dfn{URLs}, strings starting with @code{http://}.  At any point in
86 time, a resource has a certain value, which is called an
87 @dfn{instance} of the resource.
89 The fundamental protocol of the web is HTTP, a simple request/response
90 protocol.  With HTTP, a client can make a request for a resource to a
91 server, and the server replies with an @dfn{entity}, which is an
92 on-the-wire representation of an instance or of a fragment thereof.
94 @node Proxies and caches, Latency and throughput, The web, Background
95 @section Proxies and caches
96 @cindex proxy
97 @cindex caching
99 A proxy is a program that acts as both a client and a server.  It
100 listens for client requests and forwards them to servers, and forwards
101 the servers' replies to clients.
103 An HTTP proxy can optimise web traffic away by @dfn{caching} server
104 replies, storing them in memory in case they are needed again.  If a
105 reply has been cached, a later client request may, under some
106 conditions, be satisfied without going to the source again.
108 In addition to taking the shortcuts made possible by caching, proxies
109 can improve performance by generating better network traffic than the
110 client applications would do.
112 Proxies are also useful in ways unrelated to raw performance.  A proxy
113 can be used to contact a server that are not visible to the browser,
114 for example because there is a firewall in the way (@pxref{Parent
115 proxies}), or because the client and the server use different lower
116 layer protocols (for example IPv4 and IPv6).  Another common
117 application of proxies is to modify the data sent to servers and
118 returned to clients, for example by censoring headers that expose too
119 much about the client's identity (@pxref{Censoring headers}) or
120 removing advertisements from the data returned by the server
121 (@pxref{Forbidden}).
123 Polipo is a caching HTTP proxy that was originally designed as
124 a @dfn{personal} proxy, i.e.@: a proxy that is used by a single user
125 or a small group of users.
127 @node Latency and throughput, Network traffic, Proxies and caches, Background
128 @section Latency and throughput
129 @cindex throughput
130 @cindex latency
132 Most network benchmarks consider @dfn{throughput}, or the average
133 amount of data being pushed around per unit of time.  While important
134 for batch applications (for example benchmarks), average throughput is
135 mostly irrelevant when it comes to interactive web usage.  What is more
136 important is a transaction's median @dfn{latency}, or whether the data
137 starts to trickle down before the user gets annoyed.
139 Typical web caches optimise for throughput --- for example, by
140 consulting sibling caches before accessing a remote resource.  By
141 doing so, they significantly add to the median latency, and therefore
142 to the average user frustration.
144 Polipo was designed to minimise latency.
146 @node Network traffic, Partial instances, Latency and throughput, Background
147 @section Network traffic
149 The web was developed by people who were interested in text processing
150 rather than in networking and, unsurprisingly enough, the first
151 versions of the HTTP protocol did not make very good use of network
152 resources.  The main problem in HTTP/0.9 and early versions of
153 HTTP/1.0 was that a separate TCP connection (``virtual circuit'' for
154 them telecom people) was created for every entity transferred.
156 Opening multiple TCP connections has significant performance
157 implications.  Obviously, connection setup and teardown require
158 additional packet exchanges which increase network usage and, more
159 importantly, latency.
161 Less obviously, TCP is not optimised for that sort of usage.  TCP aims
162 to avoid network @dfn{congestion}, a situation in which the network
163 becomes unusable due to overly aggressive traffic patterns.  A correct
164 TCP implementation will very carefully probe the network at the
165 beginning of every connection, which means that a TCP connection is
166 very slow during the first couple of kilobytes transferred, and only
167 gets up to speed later.  Because most HTTP entities are small (in the
168 1 to 10 kilobytes range), HTTP/0.9 uses TCP where it is most inefficient.
170 @menu
171 * Persistent connections::      Don't shut connections down.
172 * Pipelining::                  Send a bunch of requests at once.
173 * Poor Mans Multiplexing::      Split requests.
174 @end menu
176 @node Persistent connections, Pipelining, Network traffic, Network traffic
177 @subsection Persistent connections
178 @cindex persistent connection
179 @cindex keep-alive connection
181 Later HTTP versions allow the transfer of multiple entities on a
182 single connection.  A connection that carries multiple entities is
183 said to be @dfn{persistent} (or sometimes @dfn{keep-alive}).
184 Unfortunately, persistent connections are an optional feature of HTTP,
185 even in version 1.1.
187 Polipo will attempt to use persistent connections on the server side,
188 and will honour persistent connection requests from clients.
190 @node Pipelining, Poor Mans Multiplexing, Persistent connections, Network traffic
191 @subsection Pipelining
192 @cindex Pipelining
194 With persistent connections it becomes possible to @dfn{pipeline} or
195 @dfn{stream} requests, i.e. to send multiple requests on a single
196 connection without waiting for the replies to come back.  Because this
197 technique gets the requests to the server faster, it reduces latency.
198 Additionally, because multiple requests can often be sent in a single
199 packet, pipelining reduces network traffic.
201 Pipelining is a fairly common technique@footnote{The X11 protocol
202 fundamentally relies on pipelining.  NNTP does support pipelining.
203 SMTP doesn't, while ESMTP makes it an option.  FTP does support
204 pipelining on the control connection.}, but it is not supported by
205 HTTP/1.0.  HTTP/1.1 makes pipelining support compulsory in every
206 server implementation that can use persistent connections, but there
207 are a number of buggy servers that claim to implement HTTP/1.1 but
208 don't support pipelining.
210 Polipo carefully probes for pipelining support in a server and uses
211 pipelining if it believes that it is reliable.  Polipo also deeply
212 enjoys being pipelined at by a client@footnote{Other client-side
213 implementations of HTTP that make use of pipelining include
214 @uref{http://www.opera.com/,,Opera}, recent versions of
215 @uref{http://www.mozilla.org,,Mozilla}, APT (the package downloader
216 used by @uref{http://www.debian.org,,Debian} GNU/Linux) and LFTP.}.
218 @node Poor Mans Multiplexing,  , Pipelining, Network traffic
219 @subsection Poor Man's Multiplexing
220 @cindex Poor Man's Multiplexing
221 @cindex multiplexing
223 A major weakness of the HTTP protocol is its inability to share a
224 single connection between multiple simultaneous transactions --- to
225 @dfn{multiplex} a number of transactions over a single connection.  In
226 HTTP, a client can either request all instances sequentially, which
227 significantly increases latency, or else open multiple concurrent
228 connections, with all the problems that this implies
229 (@pxref{Persistent connections}).
231 Poor Man's Multiplexing (PMM) is a technique that simulates
232 multiplexing by requesting an instance in multiple segments; because
233 the segments are fetched in independent transactions, they can be
234 interleaved with requests for other resources.
236 Obviously, PMM only makes sense in the presence of persistent
237 connections; additionally, it is only effective in the presence of
238 pipelining (@pxref{Pipelining}).
240 PMM poses a number of reliability issues.  If the resource being
241 fetched is dynamic, it is quite possible that it will change between
242 segments; thus, an implementation making use of PMM needs to be able
243 to switch to full-resource retrieval when it detects a dynamic
244 resource.
246 Polipo supports PMM, but it is disabled it by default (@pxref{PMM}).
248 @node Partial instances, POST and PUT, Network traffic, Background
249 @section Caching partial instances
250 @cindex partial instance
251 @cindex range request
253 A partial instance is an instance that is being cached but only part
254 of which is available in the local cache.  There are three ways in
255 which partial instances can arise: client applications requesting only
256 part of an instance (Adobe's Acrobat Reader plugin is famous for
257 that), a server dropping a connection mid-transfer (because it is
258 short on resources, or, surprisingly often, because it is buggy), a
259 client dropping a connection (usually because the user pressed
260 @emph{stop}).
262 When an instance is requested that is only partially cached, it is
263 possible to request just the missing data by using a feature of HTTP
264 known as a @dfn{range} request.  While support for range requests is
265 optional, most servers honour them in case of static data (data that
266 are stored on disk, rather then being generated on the fly e.g.@: by a
267 CGI script).
269 Caching partial instances has a number of positive effects.  Obviously,
270 it reduces the amount of data transmitted as the available data
271 needn't be fetched again.  Because it prevents partial data from being
272 discarded, it makes it reasonable for a proxy to unconditionally abort
273 a download when requested by the user, and therefore reduces network
274 traffic.
276 Polipo caches arbitrary partial instances in its in-memory cache.  It
277 will only store the initial segment of a partial instance (from its
278 beginning up to its first hole) in its on-disk cache, though.  In
279 either case, it will attempt to use range requests to fetch the
280 missing data.
282 @node POST and PUT, Other HTTP proxies, Partial instances, Background
283 @section Other requests
284 @cindex GET request
285 @cindex HEAD request
286 @cindex PUT request
287 @cindex POST request
288 @cindex OPTIONS request
289 @cindex PROPFIND request
291 The previous sections pretend that there is only one kind of request
292 in HTTP --- the @samp{GET} request.  In fact, there are some others.
294 The @samp{HEAD} request method retrieves data about an resource.  Polipo
295 does not normally use @samp{HEAD}, but will fall back to using it for
296 validation it if finds that a given server fails to cooperate with its
297 standard validation methods (@pxref{Cache transparency}).  Polipo will
298 correctly reply to a client's @samp{HEAD} request.
300 The @samp{POST} method is used to request that the server should do
301 something rather than merely sending an entity; it is usually used
302 with HTML forms that have an effect@footnote{HTML forms should use the
303 @samp{GET} method when the form has no side-effect as this makes the
304 results cacheable.}.  The @samp{PUT} method is used to replace an
305 resource with a different instance; it is typically used by web
306 publishing applications.
308 @samp{POST} and @samp{PUT} requests are handled by Polipo pretty much
309 like @samp{GET} and @samp{HEAD}; however, for various reasons, some
310 precautions must be taken.  In particular, any cached data for the
311 resource they refer to must be discarded, and they can never be
312 pipelined.
314 Finally, HTTP/1.1 includes a convenient backdoor with the
315 @samp{CONNECT} method.  For more information, please see
316 @ref{Tunnelling connections}.
318 Polipo does not currently handle the more exotic methods such as
319 @samp{OPTIONS} and @samp{PROPFIND}.
321 @node Other HTTP proxies,  , POST and PUT, Background
322 @section Other HTTP proxies
323 @cindex proxy
325 I started writing Polipo because the weather was bad.  But also
326 because I wanted to implement some features that other web proxies
327 don't have.
329 @menu
330 * Harvest and Squid::           Historic proxies.
331 * Apache::                      The web server has a proxy.
332 * WWWOFFLE::                    A personal proxy.
333 * Junkbuster::                  Get rid of ads.
334 * Privoxy::                     Junkbuster on speed.
335 * Oops::                        A multithreaded cache.
336 @end menu
338 @node Harvest and Squid, Apache, Other HTTP proxies, Other HTTP proxies
339 @subsection Harvest and Squid
340 @cindex Harvest
341 @cindex Squid
343 Harvest, the grandfather of all web caches, has since evolved into
344 @uref{http://www.squid-cache.org/,,Squid}.
346 Squid sports an elegant single-threaded non-blocking architecture and
347 multiplexes multiple clients in a single process.  It also features
348 almost complete support for HTTP/1.1, although for some reason it
349 doesn't currently advertise it.
351 Squid is designed as a large-scale shared proxy running on a dedicated
352 machine, and therefore carries certain design decisions which make it
353 difficult to use as a personal proxy.  Because Squid keeps all
354 resource meta-data in memory, it requires a fair amount of RAM in
355 order to manipulate a reasonably sized cache.
357 Squid doesn't cache partial instances, and has trouble with instances
358 larger than available memory@footnote{Recent versions of Squid support
359 instances larger than available memory by using a hack that the
360 authors call a ``sliding window algorithm''.}.  If a client connection
361 is interrupted, Squid has to decide whether to continue fetching the
362 resource (and possibly waste bandwidth) or discard what it already has
363 (and possibly waste bandwidth).
365 Some versions of squid would, under some circumstances, pipeline up to
366 two outgoing requests on a single connection.  At the time of writing,
367 this feature appears to have been disabled in the latest version.
369 Squid's developers have decided to re-write it in C++.
371 @node Apache, WWWOFFLE, Harvest and Squid, Other HTTP proxies
372 @subsection The Apache proxy
373 @cindex Apache
375 The @uref{http://www.apache.org/,,Apache web server} includes a
376 complete HTTP/1.1 proxy.
378 The Apache web server was designed to maximise ease of programming ---
379 a decision which makes Apache immensely popular for deploying
380 web-based applications.  Of course, this ease of programming comes at
381 a cost, and Apache is not the most lightweight proxy available.
383 As cheaper caching proxies are available, Apache is not useful as a
384 standalone proxy.  The main application of Apache's proxy is to join
385 multiple web servers' trees into a single hierarchy.
387 The Apache proxy doesn't cache partial instances and doesn't pipeline
388 multiple outgoing requests.
390 @node WWWOFFLE, Junkbuster, Apache, Other HTTP proxies
391 @subsection WWWOFFLE
392 @cindex WWWOFFLE
394 @uref{http://www.gedanken.demon.co.uk/wwwoffle/,,WWWOFFLE}, an elegant
395 personal proxy, is the primary model for Polipo.
397 WWWOFFLE has more features than can be described here.  It will censor
398 banner ads, clean your HTML, decorate it with random colours, schedule
399 fetches for off-peak hours.
401 Unfortunately, the HTTP traffic that WWWOFFLE generates is disgusting.
402 It will open a connection for every fetch, and forces the client to do
403 the same.
405 WWWOFFLE only caches complete instances.
407 I used WWWOFFLE for many years, and frustration with WWWOFFLE's
408 limitations is the main reason why I started Polipo in the first
409 place.
411 @node Junkbuster, Privoxy, WWWOFFLE, Other HTTP proxies
412 @subsection Junkbuster
413 @cindex Junkbuster
415 @uref{http://internet.junkbuster.com/,,Junkbuster} is a simple
416 non-caching web proxy designed to remove banner ads and cookies.  It
417 was the main model for WWWOFFLE's (and therefore Polipo's) header and
418 ad-removing features.
420 Junkbuster's HTTP support is very simple (some would say broken): it
421 doesn't do persistent connections, and it breaks horribly if the
422 client tries pipelining.  Junkbuster is no longer being maintained,
423 and has evolved into Privoxy.
425 @node Privoxy, Oops, Junkbuster, Other HTTP proxies
426 @subsection Privoxy
427 @cindex Privoxy
429 @uref{http://www.privoxy.org/,,Privoxy} is the current incarnation of
430 Junkbuster.  Privoxy has the ability to randomly modify web pages
431 before sending them to the browser --- for example, remove
432 @samp{<blink>} or @samp{<img>} tags.
434 Just like its parent, Privoxy cannot do persistent connections.  Under
435 some circumstances, it will also buffer whole pages before sending
436 them to the client, which significantly adds to its latency.  However,
437 this is difficult to avoid given the kinds of rewriting it attempts to
438 perform.
440 @node Oops,  , Privoxy, Other HTTP proxies
441 @subsection Oops
442 @cindex Oops
444 @uref{http://zipper.paco.net/~igor/oops.eng/,,Oops} is a caching web
445 proxy that uses one thread (lightweight process) for every connection.
446 This technique does cost additional memory, but allows good
447 concurrency of requests while avoiding the need for complex
448 non-blocking programming.  Oops was apparently designed as a
449 wide-scale shared proxy.
451 Although Oops' programming model makes it easy to implement persistent
452 connections, Oops insists on opening a separate connection to the
453 server for every single resource fetch, which disqualifies it from
454 production usage.
456 @node Running, Network, Background, Top
457 @chapter Running Polipo
459 @menu
460 * Polipo Invocation::           Starting Polipo.
461 * Browser configuration::       Configuring your browser.
462 * Stopping::                    Stopping and refreshing Polipo.
463 * Local server::                The local web server and web interface.
464 @end menu
466 @node Polipo Invocation, Browser configuration, Running, Running
467 @section Starting Polipo
468 @cindex invocation
470 By default, Polipo runs as a normal foreground job in a terminal in
471 which it can log random ``How do you do?'' messages.  With the right
472 configuration options, Polipo can run as a daemon.
474 Polipo is run with the following command line:
475 @example
476 $ polipo [ -h ] [ -v ] [ -x ] [ -c @var{config} ] [ @var{var}=@var{val}... ]
477 @end example
478 All flags are optional.  The flag @option{-h} causes Polipo to print a
479 short help message and to quit.  The flag @option{-v} causes Polipo to
480 list all of its configuration variables and quit.  The flag
481 @option{-x} causes Polipo to purge its on-disk cache and then quit
482 (@pxref{Purging}).  The flag @option{-c} specifies the configuration
483 file to use (by default @file{~/.polipo} or
484 @file{/etc/polipo/config}).  Finally, Polipo's configuration can be
485 changed on the command line by assigning values to given configuration
486 variables.
488 @menu
489 * Configuring Polipo::          Plenty of options.
490 * Daemon::                      Running in the background.
491 * Logging::                     Funnelling status messages.
492 @end menu
494 @node Configuring Polipo, Daemon, Polipo Invocation, Polipo Invocation
495 @subsection Configuration
496 @cindex runtime configuration
497 @cindex variable
498 @cindex configuration variable
499 @cindex configuration file
501 There is a number of variables that you can tweak in order to
502 configure Polipo, and they should all be described in this manual
503 (@pxref{Variable index}).  You can display the complete, most
504 up-to-date list of configuration variables by using the @option{-v}
505 command line flag or by accessing the ``current configuration'' page
506 of Polipo's web interface (@pxref{Web interface}).  Configuration
507 variables can be set either on the command line or else in the
508 configuration file given by the @option{-c} command-line flag.
510 Configuration variables are typed, and @option{-v} will display their
511 types.  The type can be of one of the following:
512 @itemize @bullet
513 @item
514 @samp{integer} or @samp{float}: a numeric value;
516 @item
517 @samp{boolean}: a truth value, one of @samp{true} or @samp{false};
519 @item
520 @samp{tristate}: one of @samp{false}, @samp{maybe} or @samp{true};
522 @item
523 @samp{4-state}, one of @samp{false}, @samp{reluctantly},
524 @samp{happily} or @samp{true};
526 @item
527 @samp{5-state}, one of @samp{false}, @samp{reluctantly}, @samp{maybe},
528 @samp{happily} or @samp{true};
530 @item
531 @samp{atom}, a string written within double quotes @samp{"});
533 @item
534 @samp{list}, a comma-separated list of strings;
536 @item
537 @samp{intlist}, a comma-separated list of integers and ranges of
538 integers (of the form `@var{n}--@var{m}').
539 @end itemize
541 The configuration file has a very simple syntax.  All blank lines are
542 ignored, as are lines starting with a hash sign @samp{#}.  Other lines
543 must be of the form
544 @example
545 @var{var} = @var{val}
546 @end example
547 where @var{var} is a variable to set and @var{val} is the value to set
548 it to.
550 It is possible to change the configuration of a running polipo by
551 using the local configuration interface (@pxref{Web interface}).
553 @node Daemon, Logging, Configuring Polipo, Polipo Invocation
554 @subsection Running as a daemon
555 @cindex daemon
556 @cindex terminal
557 @cindex pid
558 @vindex daemonise
559 @vindex pidFile
561 If the configuration variable @code{daemonise} is set to true, Polipo
562 will run as a daemon: it will fork and detach from its controlling
563 terminal (if any).  The variable @code{daemonise} defaults to false.
565 When Polipo is run as a daemon, it can be useful to get it to
566 atomically write its @emph{pid} to a file.  If the variable
567 @code{pidFile} is defined, it should be the name of a file where
568 Polipo will write its @emph{pid}.  If the file already exists when it
569 is started, Polipo will refuse to run.
571 @node Logging,  , Daemon, Polipo Invocation
572 @subsection Logging
573 @cindex logging
574 @vindex logLevel
575 @vindex logFile
576 @vindex logFilePermissions
577 @vindex logSyslog
578 @vindex logFacility
579 @vindex scrubLogs
581 When it encounters a difficulty, Polipo will print a friendly message.
582 The location where these messages go is controlled by the
583 configuration variables @code{logFile} and @code{logSyslog}.
584 If @code{logSyslog} is @code{true}, error messages go to the system log
585 facility given by @code{logFacility}.  If @code{logFile} is set, it is
586 the name of a file where all output will accumulate.  If @code{logSyslog}
587 is @code{false} and @code{logFile} is empty, messages go to the error
588 output of the process (normally the terminal).
590 The variable @code{logFile} defaults to empty if @code{daemonise} is
591 false, and to @samp{/var/log/polipo} otherwise.  The variable
592 @code{logSyslog} defaults to @code{false}, and @code{logFacility}
593 defaults to @samp{user}.
595 If @code{logFile} is set, then the variable @code{logFilePermissions}
596 controls the Unix permissions with which the log file will be created if
597 it doesn't exist.  It defaults to 0640.
599 The amount of logging is controlled by the variable @code{logLevel}.
600 Please see the file @samp{log.h} in the Polipo sources for the
601 possible values of @code{logLevel}.
603 Keeping extensive logs on your users browsing habits is probably
604 a serere violation of their privacy.  If the variable @code{scrubLogs}
605 is set, then Polipo will scrub most, if not all, private information
606 from its logs.
608 @node Browser configuration, Stopping, Polipo Invocation, Running
609 @section Configuring your browser
610 @cindex browser configuration
611 @cindex user-agent configuration
613 Telling your user-agent (web browser) to use Polipo is an operation
614 that depends on the browser.  Many user-agents will transparently use
615 Polipo if the environment variable @samp{http_proxy} points at it;
616 e.g.@:
617 @example
618 $ export http_proxy=http://localhost:8123/
619 @end example
620 Netscape Navigator, Mozilla, Mozilla Firefox, KDE's Konqueror and
621 probably other browsers require that you configure them manually
622 through their @emph{Preferences} or @emph{Configure} menu.
624 If your user-agent sports such options, tell it to use persistent
625 connections when speaking to proxies, to speak HTTP/1.1 and to use
626 HTTP/1.1 pipelining.
628 @node Stopping, Local server, Browser configuration, Running
629 @section Stopping Polipo and getting it to reload
630 @cindex signals
631 @cindex shutting down
632 @cindex stopping
634 Polipo will shut down cleanly if it receives @code{SIGHUP},
635 @code{SIGTERM} or @code{SIGINT} signals; this will normally happen
636 when a Polipo in the foreground receives a @code{^C} key press, when
637 your system shuts down, or when you use the @code{kill} command with
638 no flags.  Polipo will then write-out all its in-memory data to disk
639 and quit.
641 If Polipo receives the @code{SIGUSR1} signal, it will write out all
642 the in-memory data to disk (but won't discard them), reopen the log
643 file, and then reload the forbidden URLs file (@pxref{Forbidden}).
645 Finally, if Polipo receives the @code{SIGUSR2} signal, it will write
646 out all the in-memory data to disk and discard as much of the memory
647 cache as possible.  It will then reopen the log file and reload the
648 forbidden URLs file.
650 @node Local server,  , Stopping, Running
651 @section The local web server
652 @vindex localDocumentRoot
653 @vindex disableProxy
654 @cindex web server
655 @cindex local server
657 Polipo includes a local web server, which is accessible on the same
658 port as the one the proxy listens to.  Therefore, by default you can
659 access Polipo's local web server as @samp{http://localhost:8123/}.
661 The data for the local web server can be configured by setting
662 @code{localDocumentRoot}, which defaults to
663 @file{/usr/share/polipo/www/}.  Setting this variable to @samp{""}
664 will disable the local server.
666 Polipo assumes that the local web tree doesn't change behind its back.
667 If you change any of the local files, you will need to notify Polipo
668 by sending it a @code{SIGUSR2} signal (@pxref{Stopping}).
670 If you use polipo as a publicly accessible web server, you might want
671 to set the variable @code{disableProxy}, which will prevent it from
672 acting as a web proxy.  (You will also want to set
673 @code{disableLocalInterface} (@pxref{Web interface}), and perhaps run
674 Polipo in a @emph{chroot} jail.)
676 @menu
677 * Web interface::               The web interface.
678 @end menu
680 @node Web interface,  , Local server, Local server
681 @subsection The web interface
682 @cindex runtime configuration
683 @cindex web interface
684 @vindex disableLocalInterface
685 @vindex disableConfiguration
686 @vindex disableServersList
688 The subtree of the local web space rooted at
689 @samp{http://localhost:8123/polipo/} is treated specially: URLs under
690 this root do not correspond to on-disk files, but are generated by
691 Polipo on-the-fly.  We call this subtree Polipo's @dfn{local web
692 interface}.
694 The page @samp{http://localhost:8123/polipo/config?} contains the
695 values of all configuration variables, and allows setting most of them.
697 The page @samp{http://localhost:8123/polipo/status?} provides a summary
698 status report about the running Polipo, and allows performing a number
699 of actions on the proxy, notably flushing the in-memory cache.
701 The page @samp{http://localhost:8123/polipo/servers?} contains the list
702 of known servers, and the statistics maintained about them
703 (@pxref{Server statistics}).
705 The pages starting with @samp{http://localhost:8123/polipo/index?}
706 contain indices of the disk cache.  For example, the following page
707 contains the index of the cached pages from the server of some random
708 company:
709 @example
710 http://localhost:8123/polipo/index?http://www.microsoft.com/
711 @end example
712 The pages starting with
713 @samp{http://localhost:8123/polipo/recursive-index?} contain recursive
714 indices of various servers.  This functionality is disabled by
715 default, and can be enabled by setting the variable
716 @code{disableIndexing}.
718 If you have multiple users, you will probably want to disable the
719 local interface by setting the variable @code{disableLocalInterface}.
720 You may also selectively control setting of variables, indexing and
721 listing known servers by setting the variables
722 @code{disableConfiguration}, @code{disableIndexing} and
723 @code{disableServersList}.
725 @node Network, Caching, Running, Top
726 @chapter Polipo and the network
728 @menu
729 * Client connections::          Speaking to clients
730 * Contacting servers::          Contacting servers.
731 * HTTP tuning::                 Tuning at the HTTP level.
732 * Offline browsing::            Browsing with poor connectivity.
733 * Server statistics::           Polipo keeps statistics about servers.
734 * Server-side behaviour::       Tuning the server-side behaviour.
735 * PMM::                         Poor Man's Multiplexing.
736 * Forbidden::                   You can forbid some URLs.
737 * DNS::                         How Polipo finds hosts.
738 * Parent proxies::              Fetching data from other proxies.
739 * Tuning POST and PUT::         Tuning POST and PUT requests.
740 * Tunnelling connections::      Tunnelling foreign protocols and https.
741 @end menu
743 @node Client connections, Contacting servers, Network, Network
744 @section Client connections
746 @vindex proxyAddress
747 @vindex proxyPort
748 @vindex proxyName
749 @cindex address
750 @cindex port
751 @cindex IPv6
752 @cindex proxy loop
753 @cindex loop
754 @cindex proxy name
755 @cindex via
756 @cindex loopback address
757 @cindex security
759 There are three fundamental values that control how Polipo speaks to
760 clients.  The variable @code{proxyAddress}, defines the IP address on
761 which Polipo will listen; by default, its value is the @dfn{loopback
762 address} @code{"127.0.0.1"}, meaning that Polipo will listen on the
763 IPv4 loopback interface (the local host) only.  By setting this
764 variable to a global IP address or to one of the special values
765 @code{"::"} or @code{"0.0.0.0"}, it is possible to allow Polipo to
766 serve remote clients.  This is likely to be a security hole unless you
767 set @code{allowedClients} to a reasonable value (@pxref{Access control}).
769 Note that the type of address that you specify for @code{proxyAddress}
770 will determine whether Polipo listens to IPv4 or IPv6.  Currently, the
771 only way to have Polipo listen to both protocols is to specify the
772 IPv6 unspecified address (@code{"::"}) for @code{proxyAddress}.
774 The variable @code{proxyPort}, by default 8123, defines the TCP port
775 on which Polipo will listen.
777 The variable @code{proxyName}, which defaults to the host name of the
778 machine on which Polipo is running, defines the @dfn{name} of the
779 proxy.  This can be an arbitrary string that should be unique among
780 all instances of Polipo that you are running.  Polipo uses it in error
781 messages (Tor users, @pxref{Preventing information leaks}) and optionally
782 for detecting proxy loops (by using the @samp{Via} HTTP header,
783 @pxref{Censoring headers}).
785 @menu
786 * Access control::                 Deciding who can connect.
787 * Preventing information leaks::   Notes for Tor users who run scripts.
788 @end menu
790 @node Access control, Preventing information leaks, Client connections, Client connections
791 @subsection Access control
792 @vindex proxyAddress
793 @vindex authCredentials
794 @vindex authRealm
795 @vindex allowedClients
796 @cindex access control
797 @cindex authentication
798 @cindex loopback address
799 @cindex security
800 @cindex username
801 @cindex password
803 By making it possible to have Polipo listen on a non-routable address
804 (for example the loopback address @samp{127.0.0.1}), the variable
805 @code{proxyAddress} provides a very crude form of @dfn{access
806 control}: the ability to decide which hosts are allowed to connect.
808 A finer form of access control can be implemented by specifying
809 explicitly a number of client addresses or ranges of addresses
810 (networks) that a client is allowed to connect from.  This is done
811 by setting the variable @code{allowedClients}.
813 Every entry in @code{allowedClients} can be an IP address, for example
814 @samp{134.157.168.57} or @samp{::1}.  It can also be a network
815 address, i.e.@: an IP address and the number of bits in the network
816 prefix, for example @samp{134.157.168.0/24} or
817 @samp{2001:660:116::/48}.  Typical uses of @samp{allowedClients}
818 variable include
819 @example
820 allowedClients = 127.0.0.1, ::1, 134.157.168.0/24, 2001:660:116::/48
821 @end example
822 or, for an IPv4-only version of Polipo,
823 @example
824 allowedClients = 127.0.0.1, 134.157.168.0/24
825 @end example
827 A different form of access control can be implemented by requiring
828 each client to @dfn{authenticate}, i.e.@: to prove its identity before
829 connecting.  Polipo currently only implements the most insecure form
830 of authentication, @dfn{HTTP basic authentication}, which sends
831 usernames and passwords in clear over the network.  HTTP basic
832 authentication is required when the variable @code{authCredentials} is
833 not null; its value should be of the form @samp{username:password}.
835 Note that both IP-based authentication and HTTP basic authentication
836 are insecure: the former is vulnerable to IP address spoofing, the
837 latter to replay attacks.  If you need to access Polipo over the
838 public Internet, the only secure option is to have it listen over the
839 loopback interface only and use an ssh tunnel (@pxref{Parent
840 proxies})@footnote{It is not quite clear to me whether HTTP digest
841 authentication is worth implementing.  On the one hand, if implemented
842 correctly, it appears to provide secure authentication; on the other
843 hand, and unlike ssh or SSL, it doesn't make any attempt at ensuring
844 privacy, and its optional integrity guarantees are impossible to
845 implement without significantly impairing latency.}.
847 @node Preventing information leaks,  , Access control, Client connections
848 @subsection Preventing information leaks
849 @vindex dontIdentifyToClients
850 @vindex proxyAddress
851 @vindex proxyPort
852 @vindex proxyName
853 @cindex address
854 @cindex port
855 @cindex proxy name
856 @cindex security
857 @cindex privacy
859 When Polipo is used with @uref{http://tor.eff.org,,Tor}, it is desirable to
860 prevent client-side scripts from accessing sensitive information, because
861 scripts may interact with remote sites. Even when
862 @uref{https://www.torproject.org/torbutton/,,Torbutton} is used, scripts
863 may still glean details about a user from Polipo's error messages and headers.
865 By default, Polipo sends the contents of @code{proxyName} and @code{proxyPort},
866 as well as the local timezone, in a trailer beneath its error messages. In
867 addition, Polipo sends @code{proxyName} in Warning headers (see RFC@tie{}2616,
868 14.46).
870 The variable @code{dontIdentifyToClients} can be set to @code{true} to avoid
871 sending the trailer in error messages and send @code{polipo} as the pseudonym
872 in Warning headers.
874 It makes sense set @code{disableLocalInterface} (@pxref{Web interface})
875 along with @code{dontIdentifyToClients}. 
877 To prevent information leaks to servers, @pxref{Censoring headers}.
879 @node Contacting servers, HTTP tuning, Client connections, Network
880 @section Contacting servers
882 @cindex multiple addresses
883 @cindex IPv6
884 @vindex useTemporarySourceAddress
886 A server can have multiple addresses, for example if it is
887 @dfn{multihomed} (connected to multiple networks) or if it can speak
888 both IPv4 and IPv6.  Polipo will try all of a hosts addresses in turn;
889 once it has found one that works, it will stick to that address until
890 it fails again.
892 If connecting via IPv6 there is the possibility to use temporary
893 source addresses to increase privacy (RFC@tie{}3041). The variable
894 @code{useTemporarySourceAddress} controls the use of temporary
895 addresses for outgoing connections; if set to @code{true}
896 temporary addresses are preferred, if set to @code{false} static addresses
897 are used and if set to @code{maybe} (the default) the operation
898 system default is in effect. This setting is not available
899 on all operation systems.
901 @menu
902 * Allowed ports::               Where the proxy is allowed to connect.
903 @end menu
905 @node Allowed ports,  , Contacting servers, Contacting servers
906 @subsection Allowed ports
908 @cindex Allowed ports
909 @cindex Forbidden ports
910 @cindex ports
911 @vindex allowedPorts
913 A TCP service is identified not only by the IP address of the machine
914 it is running on, but also by a small integer, the TCP @dfn{port} it
915 is @dfn{listening} on.  Normally, web servers listen on port 80, but
916 it is not uncommon to have them listen on different ports; Polipo's
917 internal web server, for example, listens on port 8123 by default.
919 The variable @code{allowedPorts} contains the list of ports that
920 Polipo will accept to connect to on behalf of clients; it defaults to
921 @samp{80-100, 1024-65535}.  Set this variable to @samp{1-65535} if your
922 clients (and the web pages they consult!) are fully trusted.  (The
923 variable @code{allowedPorts} is not considered for tunnelled
924 connections; @pxref{Tunnelling connections}).
926 @node HTTP tuning, Offline browsing, Contacting servers, Network
927 @section Tuning at the HTTP level
928 @cindex HTTP
929 @cindex headers
931 @menu
932 * Tuning the HTTP parser::      Tuning parsing of HTTP headers.
933 * Censoring headers::           Censoring HTTP headers.
934 * Intermediate proxies::        Adjusting intermediate proxy behaviour.
935 @end menu
937 @node Tuning the HTTP parser, Censoring headers, HTTP tuning, HTTP tuning
938 @subsection Tuning the HTTP parser
939 @vindex laxHttpParser
940 @vindex bigBufferSize
942 As a number of HTTP servers and CGI scripts serve incorrect HTTP
943 headers, Polipo uses a @emph{lax} parser, meaning that incorrect HTTP
944 headers will be ignored (a warning will be logged by default).  If the
945 variable @code{laxHttpParser} is not set (it is set by default),
946 Polipo will use a @emph{strict} parser, and refuse to serve an
947 instance unless it could parse all the headers.
949 When the amount of headers exceeds one chunk's worth (@pxref{Chunk
950 memory}), Polipo will allocate a @dfn{big buffer} in order to store
951 the headers.  The size of big buffers, and therefore the maximum
952 amount of headers Polipo can parse, is specified by the variable
953 @code{bigBufferSize} (32@dmn{kB} by default).
955 @node Censoring headers, Intermediate proxies, Tuning the HTTP parser, HTTP tuning
956 @subsection Censoring headers
957 @cindex privacy
958 @cindex anonymity
959 @cindex Referer
960 @cindex cookies
961 @vindex censorReferer
962 @vindex censoredHeaders
963 @vindex proxyName
964 @vindex disableVia
966 Polipo offers the option to censor given HTTP headers in both client
967 requests and server replies.  The main application of this feature is
968 to very slightly improve the user's privacy by eliminating cookies and
969 some content-negotiation headers.
971 It is important to understand that these features merely make it
972 slightly more difficult to gather statistics about the user's
973 behaviour.  While they do not actually prevent such statistics from
974 being collected, they might make it less cost-effective to do so.
976 The general mechanism is controlled by the variable
977 @code{censoredHeaders}, the value of which is a case-insensitive list
978 of headers to unconditionally censor.  By default, it is empty, but
979 I recommend that you set it to @samp{From, Accept-Language}.  Adding
980 headers such as @samp{Set-Cookie}, @samp{Set-Cookie2}, @samp{Cookie},
981 @samp{Cookie2} or @samp{User-Agent} to this list will probably break
982 many web sites.
984 The case of the @samp{Referer}@footnote{HTTP contains many mistakes
985 and even one spelling error.} header is treated specially because many
986 sites will refuse to serve pages when it is not provided.  If
987 @code{censorReferer} is @code{false} (the default), @samp{Referer}
988 headers are passed unchanged to the server.  If @code{censorReferer}
989 is @code{maybe}, @samp{Referer} headers are passed to the server only
990 when they refer to the same host as the resource being fetched.  If
991 @code{censorReferer} is @code{true}, all @samp{Referer} headers are
992 censored.  I recommend setting @code{censorReferer} to @code{maybe}.
994 Another header that can have privacy implications is the @samp{Via}
995 header, which is used to specify the chain of proxies through which
996 a given request has passed.  Polipo will generate @samp{Via} headers
997 if the variable @code{disableVia} is @code{false} (it is true by
998 default).  If you choose to generate @samp{Via} headers, you may want
999 to set the @code{proxyName} variable to some innocuous string
1000 (@pxref{Client connections}).
1002 @menu
1003 * Censor Accept-Language::      Why Accept-Language is evil.
1004 @end menu
1006 @node Censor Accept-Language,  , Censoring headers, Censoring headers
1007 @subsubsection Why censor Accept-Language
1008 @cindex negotiation
1009 @cindex content negotiation
1010 @cindex Accept-Language
1012 Recent versions of HTTP include a mechanism known as @dfn{content
1013 negotiation} which allows a user-agent and a server to negotiate the
1014 best representation (instance) for a given resource.  For example, a
1015 server that provides both PNG and GIF versions of an image will serve
1016 the PNG version to user-agents that support PNG, and the GIF version
1017 to Internet Explorer.
1019 Content negotiation requires that a client should send with every
1020 single request a number of headers specifying the user's cultural and
1021 technical preferences.  Most of these headers do not expose sensitive
1022 information (who cares whether your browser supports PNG?).  The
1023 @samp{Accept-Language} header, however, is meant to convey the user's
1024 linguistic preferences.  In some cases, this information is sufficient
1025 to pinpoint with great precision the user's origins and even his
1026 political or religious opinions; think, for example, of the
1027 implications of sending @samp{Accept-Language: yi} or @samp{ar_PS}.
1029 At any rate, @samp{Accept-Language} is not useful.  Its design is
1030 based on the assumption that language is merely another representation
1031 for the same information, and @samp{Accept-Language} simply carries a
1032 prioritised list of languages, which is not enough to usefully
1033 describe a literate user's preferences.  A typical French user, for
1034 example, will prefer an English-language original to a French
1035 (mis-)translation, while still wanting to see French language texts
1036 when they are original.  Such a situation cannot be described by the
1037 simple-minded @samp{Accept-Language} header.
1039 @node Intermediate proxies,  , Censoring headers, HTTP tuning
1040 @subsection Adjusting intermediate proxy behaviour
1041 @vindex alwaysAddNoTransform
1042 @cindex intermediate proxies
1044 Implementors of intermediate caches (proxies) have found it useful to
1045 convert the media type of certain entity bodies. A non-transparent
1046 proxy might, for example, convert between image formats in order to
1047 save cache space or to reduce the amount of traffic on a slow link.
1049 If @code{alwaysAddNoTransform} is true (it is false by default),
1050 Polipo will add a 'no-transform' cache control directive to all
1051 outgoing requests. This directive forbids (compliant) intermediate
1052 caches from responding with an object that was compressed or
1053 transformed in any way.
1055 @node Offline browsing, Server statistics, HTTP tuning, Network
1056 @section Offline browsing
1057 @vindex proxyOffline
1058 @cindex offline browsing
1059 @cindex browsing offline
1060 @cindex connectivity
1061 @cindex warning
1062 @cindex shift-click
1064 In an ideal world, all machines would have perfect connectivity to the
1065 network at all times and servers would never crash.  In the real
1066 world, it may be necessary to avoid hitting the network and have
1067 Polipo serve stale objects from its cache.
1069 Setting @code{proxyOffline} to @code{true} prevents Polipo from
1070 contacting remote servers, no matter what.  This setting is suitable
1071 when you have no network connection whatsoever.
1073 If @code{proxyOffline} is false, Polipo's caching behaviour is
1074 controlled by a number of variables documented in @ref{Tweaking validation}.
1076 @node Server statistics, Server-side behaviour, Offline browsing, Network
1077 @section Server statistics
1078 @vindex serverExpireTime
1079 @cindex server statistics
1080 @cindex round-trip time
1081 @cindex transfer rate
1083 In order to decide when to pipeline requests (@pxref{Pipelining}) and
1084 whether to perform Poor Man's Multiplexing 
1085 (@pxref{Poor Mans Multiplexing}), Polipo needs to keep statistics
1086 about servers.  These include the server's ability to handle
1087 persistent connections, the server's ability to handle pipelined
1088 requests, the round-trip time to the server, and the server's transfer
1089 rate.  The statistics are accessible from Polipo's web interface
1090 (@pxref{Web interface}). 
1092 The variable @samp{serverExpireTime} (default 1 day) specifies how
1093 long such information remains valid.  If a server has not been
1094 accessed for a time interval of at least @code{serverExpireTime},
1095 information about it will be discarded.
1097 As Polipo will eventually recover from incorrect information about a
1098 server, this value can be made fairly large.  The reason why it exists
1099 at all is to limit the amount of memory used up by information about
1100 servers.
1102 @node Server-side behaviour, PMM, Server statistics, Network
1103 @section Tweaking server-side behaviour
1104 @vindex serverSlots
1105 @vindex serverSlots1
1106 @vindex serverMaxSlots
1107 @vindex smallRequestTime
1108 @vindex replyUnpipelineTime
1109 @vindex replyUnpipelineSize
1110 @vindex maxPipelineTrain
1111 @vindex pipelineAdditionalRequests
1112 @vindex maxSideBuffering
1113 @cindex small request
1114 @cindex large request
1115 @cindex breaking pipelines
1117 The most important piece of information about a server is whether it
1118 supports persistent connections.  If this is the case, Polipo will
1119 open at most @code{serverSlots} connections to that server
1120 (@code{serverSlots1} if the server only implements HTTP/1.0), and
1121 attempt to pipeline; if not, Polipo will hit the server harder,
1122 opening up to @code{serverMaxSlots} connections.
1124 Another use of server information is to decide whether to pipeline
1125 additional requests on a connection that already has in-flight
1126 requests.  This is controlled by the variable
1127 @code{pipelineAdditionalRequests}; if it is @code{false}, no
1128 additional requests will be pipelined.  If it is @code{true},
1129 additional requests will be pipelined whenever possible.  If it is
1130 @code{maybe} (the default), additional requests will only be pipelined
1131 following @dfn{small} requests, where a small request one whose
1132 download is estimated to take no more than @code{smallRequestTime}
1133 (default 5@dmn{s}).
1135 Sometimes, a request has been pipelined after a request that prompts a
1136 very large reply from the server; when that happens, the pipeline
1137 needs be broken in order to reduce latency.  A reply is @dfn{large}
1138 and will cause a pipeline to be broken if either its size is at least
1139 @code{replyUnpipelineSize} (default one megabyte) or else the server's
1140 transfer rate is known and the body is expected to take at least
1141 @code{replyUnpipelineTime} to download (default 15@dmn{s}).
1143 The variable @code{maxPipelineTrain} defines the maximum number of
1144 requests that will be pipelined in a single write (default 10).
1145 Setting this variable to a very low value might (or might not) fix
1146 interaction with some unreliable servers that the normal heuristics
1147 are unable to detect.
1149 The variable @code{maxSideBuffering} specifies how much data will be
1150 buffered in a PUT or POST request; it defaults to 1500 bytes.  Setting
1151 this variable to 0 may cause some media players that abuse the HTTP
1152 protocol to work.
1154 @node PMM, Forbidden, Server-side behaviour, Network
1155 @section Poor Man's Multiplexing
1156 @cindex Poor Man's Multiplexing
1157 @cindex multiplexing
1158 @vindex pmmSize
1159 @vindex pmmFirstSize
1161 By default, Polipo does not use Poor Man's Multiplexing (@pxref{Poor
1162 Mans Multiplexing}).  If the variable @code{pmmSize} is set to a
1163 positive value, Polipo will use PMM when speaking to servers that are
1164 known to support pipelining.  It will request resources by segments of
1165 @code{pmmSize} bytes.  The first segment requested has a size of
1166 @code{pmmFirstSize}, which defaults to twice @code{pmmSize}.
1168 PMM is an intrinsically unreliable technique.  Polipo makes heroic
1169 efforts to make it at least usable, requesting that the server disable
1170 PMM when not useful (by using the @samp{If-Range} header) and
1171 disabling it on its own if a resource turns out to be dynamic.
1172 Notwithstanding these precautions, unless the server
1173 cooperates@footnote{More precisely, unless CGI scripts cooperate.},
1174 you will see failures when using PMM, which will usually result in
1175 blank pages and broken image icons; hitting @emph{Reload} on your
1176 browser will usually cause Polipo to notice that something went wrong
1177 and correct the problem.
1179 @node Forbidden, DNS, PMM, Network
1180 @section Forbidden and redirected URLs
1181 @cindex forbidden
1182 @cindex redirect
1183 @cindex web counter
1184 @cindex counter
1185 @cindex web bug
1186 @cindex bug
1187 @cindex advertisement
1188 @cindex web ad
1189 @cindex banner ad
1191 The web contains advertisements that a user-agent is supposed to
1192 download together with the requested pages.  Not only do
1193 advertisements pollute the user's brain, pushing them around takes
1194 time and uses up network bandwidth.
1196 Many so-called content providers also track user activities by using
1197 @dfn{web bugs}, tiny embedded images that cause a server to log where
1198 they are requested from.  Such images can be detected because they are
1199 usually uncacheable (@pxref{Cache transparency}) and therefore logged
1200 by Polipo by default.
1202 Polipo can be configured to prevent certain URLs from reaching the
1203 browser, either by returning a @emph{forbidden} error message to the
1204 user, or by @emph{redirecting} such URLs to some other URL.
1206 @menu
1207 * Internal forbidden list::     Specifying forbidden URLs.
1208 * External redirectors::        Using an external redirector.
1209 @end menu
1211 @node Internal forbidden list, External redirectors, Forbidden, Forbidden
1212 @subsection Internal forbidden list
1213 @cindex forbidden
1214 @cindex redirect
1215 @vindex forbiddenFile
1216 @vindex forbiddenUrl
1217 @vindex forbiddenRedirectCode
1219 The file pointed at by the variable @code{forbiddenFile} (defaults to
1220 @file{~/.polipo-forbidden} or @file{/etc/polipo/forbidden}, whichever
1221 exists) specifies the set of URLs that should never be fetched.  If
1222 @code{forbiddenFile} is a directory, it will be recursively searched
1223 for files with forbidden URLs.
1225 Every line in a file listing forbidden URLs can either be a domain
1226 name --- a string that doesn't contain any of @samp{/}, @samp{*} or
1227 @samp{\} ---, or a POSIX extended regular expression.  Blank lines are
1228 ignored, as are those that start with a hash sign @samp{#}.
1230 By default, whenever it attempts to fetch a forbidden URL, the browser
1231 will receive a @emph{403 forbidden} error from Polipo.  Some users
1232 prefer to have the browser display a different page or an image.
1234 If @code{forbiddenUrl} is not null, it should represent a URL to which
1235 all forbidden URLs will be redirected.  The kind of redirection used
1236 is specified by @code{forbiddenRedirectCode}; if this is 302 (the
1237 default) the redirection will be marked as temporary, if 301 it will
1238 be a permanent one.
1240 @node External redirectors,  , Internal forbidden list, Forbidden
1241 @subsection External redirectors
1242 @cindex forbidden
1243 @cindex redirect
1244 @cindex redirector
1245 @cindex Squid-style redirector
1246 @cindex Adzapper
1247 @vindex redirector
1248 @vindex redirectorRedirectCode
1250 Polipo can also use an external process (a @dfn{Squid-style
1251 redirector}) to determine which URLs should be redirected.  The name
1252 of the redirector binary is determined from the variable
1253 @code{redirector}, and the kind of redirection generated is specified
1254 by @code{redirectorRedirectCode}, which should be 302 (the default) or
1255 301.
1257 For example, to use Adzapper to redirect ads to an innocuous image, just set
1258 @example
1259 redirector = /usr/bin/adzapper
1260 @end example
1262 @node DNS, Parent proxies, Forbidden, Network
1263 @section The domain name service
1264 @cindex DNS
1265 @cindex name server
1266 @cindex gethostbyname
1267 @cindex resolver
1268 @cindex IPv6
1269 @vindex dnsMaxTimeout
1270 @vindex dnsUseGethostbyname
1271 @vindex dnsNameServer
1272 @vindex dnsNegativeTtl
1273 @vindex dnsGethostbynameTtl
1274 @vindex dnsQueryIPv6
1276 The low-level protocols beneath HTTP identify machines by IP
1277 addresses, sequences of four 8-bit integers such as
1278 @samp{199.232.41.10}@footnote{Or sequences of eight 16-bit integers if
1279 you are running IPv6.}.  HTTP, on the other hand, and most application 
1280 protocols, manipulate host names, strings such as @samp{www.polipo.org}.
1282 The @dfn{domain name service} (DNS) is a distributed database that
1283 maps host names to IP addresses.  When an application wants to make
1284 use of the DNS, it invokes a @dfn{resolver}, a local library or
1285 process that contacts remote name servers.
1287 Polipo usually tries to speak the DNS protocol itself rather than
1288 using the system resolver@footnote{The Unix interface to the resolver
1289 is provided by the @code{gethostbyname}(3) library call
1290 (@code{getaddrinfo}(3) on recent systems), which was designed at
1291 a time when a host lookup consisted in searching for one of five hosts
1292 in a @samp{HOSTS.TXT} file.  The @code{gethostbyname} call is
1293 @dfn{blocking}, meaning that all activity must cease while a host
1294 lookup is in progress.  When the call eventually returns, it doesn't
1295 provide a @dfn{time to live} (TTL) value to indicate how long the
1296 address may be cached.  For these reasons, @code{gethostbyname} is
1297 hardly useful for programs that need to contact more than a few hosts.
1298 (Recent systems replace @code{gethostbyname}(3) by
1299 @code{getaddrinfo}(3), which is reentrant.  While this removes one
1300 important problem that multi-threaded programs encounter, it doesn't
1301 solve any of the other issues with @code{gethostbyname}.)}.  Its
1302 precise behaviour is controlled by the value of
1303 @code{dnsUseGethostbyname}.  If @code{dnsUseGethostbyname} is
1304 @code{false}, Polipo never uses the system resolver.  If it is
1305 @code{reluctantly} (the default), Polipo tries to speak DNS and falls
1306 back to the system resolver if a name server could not be contacted.
1307 If it is @code{happily}, Polipo tries to speak DNS, and falls back to
1308 the system resolver if the host couldn't be found for any reason (this
1309 is not a good idea for shared proxies).  Finally, if
1310 @code{dnsUseGethostbyname} is @code{true}, Polipo never tries to speak
1311 DNS itself and uses the system resolver straight away (this is not
1312 recommended).
1314 If the internal DNS support is used, Polipo must be given a recursive
1315 name server to speak to.  By default, this information is taken from
1316 the @samp{/etc/resolv.conf} file; however, if you wish to use
1317 a different name server, you may set the variable @code{dnsNameServer}
1318 to an IP address@footnote{While Polipo does its own caching of DNS
1319 data, I recommend that you run a local caching name server.  I am very
1320 happy with @uref{http://home.t-online.de/home/Moestl/,,@code{pdnsd}},
1321 notwithstanding its somewhat bizarre handling of TCP connections.}.
1323 When the reply to a DNS request is late to come, Polipo will retry
1324 multiple times using an exponentially increasing timeout.  The maximum
1325 timeout used before Polipo gives up is defined by @code{dnsMaxTimeout}
1326 (default 60@dmn{s}); the total time before Polipo gives up on a DNS
1327 query will be roughly twice @code{dnsMaxTimeout}.
1329 The variable @code{dnsNegativeTtl} specifies the time during which
1330 negative DNS information (information that a host @emph{doesn't}
1331 exist) will be cached; this defaults to 120@dmn{s}.  Increasing this
1332 value reduces both latency and network traffic but may cause a failed
1333 host not to be noticed when it comes back up.
1335 The variable @code{dnsQueryIPv6} specifies whether to query for IPv4
1336 or IPv6 addresses.  If @code{dnsQueryIPv6} is @code{false}, only IPv4
1337 addresses are queried.  If @code{dnsQueryIPv6} is @code{reluctantly},
1338 both types of addresses are queried, but IPv4 addresses are preferred.
1339 If @code{dnsQueryIPv6} is @code{happily} (the default), IPv6 addresses
1340 are preferred.  Finally, if @code{dnsQueryIPv6} is @code{true}, only
1341 IPv6 addresses are queried.
1343 If the system resolver is used, the value @code{dnsGethostbynameTtl}
1344 specifies the time during which a @code{gethostbyname} reply will be
1345 cached (default 5 minutes).
1347 @node Parent proxies, Tuning POST and PUT, DNS, Network
1348 @section Parent proxies
1350 Polipo will usually fetch instances directly from source servers as
1351 this configuration minimises latency.  In some cases, however, it may
1352 be useful to have Polipo fetch instances from a @dfn{parent} proxy.
1354 Polipo can use two protocols to speak to a parent proxy: HTTP and
1355 SOCKS.  When configured to use both HTTP and SOCKS proxying, Polipo
1356 will contact an HTTP proxy over SOCKS --- in other words, SOCKS is
1357 considered as being at a lower (sub)layer than HTTP.
1359 @menu
1360 * HTTP parent proxies::         Using an HTTP parent proxy.
1361 * SOCKS parent proxies::        Using a SOCKS4a parent proxy.
1362 @end menu
1364 @node HTTP parent proxies, SOCKS parent proxies, Parent proxies, Parent proxies
1365 @subsection HTTP parent proxies
1366 @vindex parentProxy
1367 @vindex parentAuthCredentials
1368 @cindex parent proxy
1369 @cindex upstream proxy
1370 @cindex firewall
1371 @cindex authentication
1373 The variable @code{parentProxy} specifies the hostname and port number
1374 of an HTTP parent proxy; it should have the form @samp{host:port}.
1376 If the parent proxy requires authorisation, the username and password
1377 should be specified in the variable @code{parentAuthCredentials} in
1378 the form @samp{username:password}.  Only @emph{Basic} authentication
1379 is supported, which is vulnerable to replay attacks.
1381 The main application of the parent proxy support is to cross
1382 firewalls.  Given a machine, say @code{trurl}, with unrestricted
1383 access to the web, the following evades a firewall by using an
1384 encrypted compressed @code{ssh} link:
1385 @example
1386 $ ssh -f -C -L 8124:localhost:8123 trurl polipo
1387 $ polipo parentProxy=localhost:8124
1388 @end example
1390 @node SOCKS parent proxies,  , HTTP parent proxies, Parent proxies
1391 @subsection SOCKS parent proxies
1392 @cindex SOCKS
1393 @vindex socksParentProxy
1394 @vindex socksUserName
1395 @vindex socksProxyType
1397 The variable @code{socksParentProxy} specifies the hostname and port
1398 number of a SOCKS parent proxy; it should have the form
1399 @samp{host:port}.  The variant of the SOCKS protocol being used is
1400 defined by @code{socksProxyType}, which can be either @samp{socks4a}
1401 or @samp{socks5}; the latter value specifies ``SOCKS5 with
1402 hostnames'', and is the default.
1404 The user name passed to the SOCKS4a proxy is defined by the variable
1405 @code{socksUserName}.  This value is currently ignored with a
1406 SOCKS5 proxy.
1408 The main application of the SOCKS support is to use
1409 @uref{http://tor.eff.org,,Tor} to evade overly restrictive or
1410 misconfigured firewalls.  Assuming you have a Tor client running on
1411 the local host listening on the default port (9050), the following
1412 uses Tor for all outgoing HTTP traffic:
1413 @example
1414 $ polipo socksParentProxy=localhost:9050
1415 @end example
1417 @node Tuning POST and PUT, Tunnelling connections, Parent proxies, Network
1418 @section Tuning POST and PUT requests
1419 @cindex POST request
1420 @cindex PUT request
1421 @vindex expectContinue
1423 The main assumption behind the design of the HTTP protocol is that
1424 requests are idempotent: since a request can be repeated by a client,
1425 a server is allowed to drop a connection at any time.  This fact, more
1426 than anything else, explains the amazing scalability of the protocol.
1428 This assumption breaks down in the case of POST requests.  Indeed, a
1429 POST request usually causes some action to be performed (a page to be
1430 printed, a significant amount of money to be transferred from your
1431 bank account, or, in Florida, a vote to be registered), and such a
1432 request should not be repeated.
1434 The only solution to this problem is to reserve HTTP to idempotent
1435 activities, and use reliable protocols for action-effecting ones.
1436 Notwithstanding that, HTTP/1.1 makes a weak attempt at making POST
1437 requests slightly more reliable and efficient than they are in
1438 HTTP/1.0.
1440 When speaking to an HTTP/1.1 server, an HTTP client is allowed to
1441 request that the server check @emph{a priori} whether it intends to
1442 honour a POST request.  This is done by sending @dfn{an expectation},
1443 a specific header with the request, @samp{Expect: 100-continue}, and
1444 waiting for either an error message or a @samp{100 Continue} reply
1445 from the server.  If the latter arrives, the client is welcome to send
1446 the rest of the POST request@footnote{This, of course, is only part of
1447 the story.  Additionally, the server is not required to reply with
1448 @samp{100 Continue}, hence the client must implement a timeout.
1449 Furthermore, according to the obsolete RFC2068, the server is
1450 allowed to spontaneously send @samp{100 Continue}, so the client must
1451 be prepared to ignore such a reply at any time.}.
1453 Polipo's behaviour w.r.t.@: client expectations is controlled by the
1454 variable @code{expectContinue}.  If this variable is false, Polipo
1455 will never send an expectation to the server; if a client sends an
1456 expectation, Polipo will fail the expectation straight away, causing
1457 the client (if correctly implemented) to retry with no expectation.
1458 If @code{expectContinue} is @code{maybe} (the default), Polipo will
1459 behave in a standards-compliant manner: it will forward expectations
1460 to the server when allowed to do so, and fail client expectations
1461 otherwise.  Finally, if @code{expectContinue} is @code{true}, Polipo
1462 will always send expectations when it is reasonable to do so; this
1463 violates the relevant standards and will break some websites, but
1464 might decrease network traffic under some circumstances.
1466 @node Tunnelling connections,  , Tuning POST and PUT, Network
1467 @section Tunnelling connections
1468 @cindex tunnel
1469 @cindex tunnelling proxy
1470 @cindex https
1471 @cindex HTTP/SSL
1472 @cindex rsync
1473 @cindex CONNECT
1474 @vindex tunnelAllowedPorts
1476 Polipo is an HTTP proxy; it proxies HTTP traffic, and clients using
1477 other protocols should either establish a direct connection to the
1478 server or use an @emph{ad hoc} proxy.
1480 In many circumstances, however, it is not possible to establish
1481 a direct connection to the server, for example due to mis-configured
1482 firewalls or when trying to access the IPv4 Internet from an IPv6-only
1483 host.  In such situations, it is possible to have Polipo behave as
1484 a @emph{tunnelling} proxy --- a proxy that merely forwards traffic
1485 between the client and the server without understanding it.  Polipo
1486 enters tunnel mode when the client requests it by using the HTTP
1487 @samp{CONNECT} method.
1489 Most web browsers will use this technique for HTTP over SSL if
1490 configured to use Polipo as their `https proxy'.  More generally, the
1491 author has successfully used it to cross mis-configured firewalls
1492 using OpenSSH, rsync, Jabber, IRC, etc.
1494 The variable @code{tunnelAllowedPorts} specifies the set of ports that
1495 Polipo will accept to tunnel traffic to.  It defaults to allowing ssh,
1496 HTTP, https, rsync, IMAP, imaps, POP, pops, Jabber, CVS and Git traffic.
1498 @node Caching, Memory usage, Network, Top
1499 @chapter Caching
1501 @menu
1502 * Cache transparency::          Fresh and stale data.
1503 * Memory cache::                The in-memory cache.
1504 * Disk cache::                  The on-disk cache.
1505 @end menu
1507 @node Cache transparency, Memory cache, Caching, Caching
1508 @section Cache transparency and validation
1509 @cindex transparent cache
1510 @cindex cache transparency
1511 @cindex out-of-date instances
1512 @cindex validation
1513 @cindex revalidation
1514 @cindex expire
1515 @cindex stale
1516 @cindex fresh
1518 If resources on a server change, it is possible for a cached instance
1519 to become out-of date.  Ideally, a cache would be perfectly
1520 @dfn{transparent}, meaning that it never serves an out-of-date
1521 instance; in a universe with a finite speed of signal propagation,
1522 however, this ideal is impossible to achieve.
1524 If a caching proxy decides that a cached instance is new enough to
1525 likely still be valid, it will directly serve the instance to the
1526 client; we then say that the cache decided that the instance is
1527 @dfn{fresh}.  When an instance is @dfn{stale} (not fresh), the cache
1528 will check with the upstream server whether the resource has changed;
1529 we say that the cached instance is being @dfn{revalidated}.
1531 In HTTP/1.1, responsibility for revalidation is shared between the
1532 client, the server and the proxy itself.  The client can override
1533 revalidation policy by using the @samp{Cache-Control}
1534 header@footnote{Or the obsolete @samp{Pragma} header.}; for example,
1535 some user-agents will request end-to-end revalidation in this way when
1536 the user shift-clicks on @emph{reload}.  The server may choose to
1537 specify revalidation policy by using the @samp{Expires} and
1538 @samp{Cache-Control} headers.  As to the proxy, it needs to choose a
1539 revalidation policy for instances with neither server- nor client-side
1540 cache control information.  Of course, nothing (except the HTTP/1.1
1541 spec, but that is easily ignored) prevents a proxy from overriding the
1542 client's and server's cache control directives.
1544 @menu
1545 * Tuning validation::           Tuning Polipo's validation behaviour.
1546 * Tweaking validation::         Further tweaking of validation.
1547 @end menu
1549 @node Tuning validation, Tweaking validation, Cache transparency, Cache transparency
1550 @subsection Tuning validation behaviour
1551 @cindex age
1552 @vindex maxAge
1553 @vindex maxAgeFraction
1554 @vindex maxExpiresAge
1555 @vindex maxNoModifiedAge
1557 Polipo's revalidation behaviour is controlled by a number of
1558 variables.  In the following, an resource's @dfn{age} is the time since
1559 it was last validated, either because it was fetched from the server
1560 or because it was revalidated.
1562 The policy defining when cached instances become stale in the absence
1563 of server-provided information is controlled by the variables
1564 @code{maxAge}, @code{maxAgeFraction}, @code{maxExpiresAge} and
1565 @code{maxNoModifiedAge}.  If an instance has an @samp{Expires} header,
1566 it becomes stale at the date given by that header, or when its age
1567 becomes larger than @code{maxExpiresAge}, whichever happens first.  If
1568 an instance has no @samp{Expires} header but has a @samp{LastModified}
1569 header, it becomes stale when its age reaches either
1570 @code{maxAgeFraction} of the time since it was last modified or else
1571 the absolute value @code{maxAge}, whichever happens first.  Finally,
1572 if an instance has neither @samp{Expires} nor @samp{Last-Modified}, it
1573 will become stale when its age reaches @code{maxNoModifiedAge}.
1575 @node Tweaking validation,  , Tuning validation, Cache transparency
1576 @subsection Further tweaking of validation behaviour
1577 @cindex uncachable
1578 @cindex vary
1579 @vindex cacheIsShared
1580 @vindex mindlesslyCacheVary
1581 @vindex uncachableFile
1582 @vindex dontCacheCookies
1583 @vindex dontCacheRedirects
1584 @vindex dontTrustVaryETag
1586 If @code{cacheIsShared} is false (it is true by default), Polipo will
1587 ignore the server-side @samp{Cache-Control} directives @samp{private},
1588 @samp{s-maxage} and @samp{proxy-must-revalidate}.  This is highly
1589 desirable behaviour when the proxy is used by just one user, but might
1590 break some sites if the proxy is shared.
1592 When connectivity is very poor, the variable @code{relaxTransparency}
1593 can be used to cause Polipo to serve stale instances under some
1594 circumstances.  If @code{relaxTransparency} is @code{false} (the
1595 default), all stale instances are validated (@pxref{Cache
1596 transparency}), and failures to connect are reported to the client.
1597 This is the default mode of operation of most other proxies, and the
1598 least likely to surprise the user.
1600 If @code{relaxTransparency} is @code{maybe}, all stale instances are
1601 still validated, but a failure to connect is only reported as an error
1602 if no data is available in the cache.  If a connection fails and stale
1603 data is available, it is served to the client with a suitable HTTP/1.1
1604 @samp{Warning} header.  Current user-agents do not provide visible
1605 indication of such warnings, however, and this setting will typically
1606 cause the browser to display stale data with no indication that
1607 anything went wrong.  It is useful when you are consulting a live web
1608 site but don't want to be bothered with failed revalidations.
1610 If @code{relaxTransparency} is @code{true}, missing data is fetched
1611 from remote servers, but stale data are unconditionally served with no
1612 validation.  Client-side @samp{Cache-Control} directives are still
1613 honoured, which means that you can force an end-to-end revalidation
1614 from the browser's interface (typically by shift-clicking on
1615 ``reload'').  This setting is only useful if you have very bad network
1616 connectivity or are consulting a very slow web site or one that
1617 provides incorrect cache control information@footnote{This is for
1618 example the case of @code{www.microsoft.com}, and also of websites
1619 generated by a popular Free content management system written in
1620 Python.} and are willing to manually revalidate pages that you suspect
1621 are stale.
1623 If @code{mindlesslyCacheVary} is true, the presence of a @samp{Vary}
1624 header (which indicates that content-negotiation occurred,
1625 @pxref{Censor Accept-Language}) is ignored, and cached negotiated
1626 instances are mindlessly returned to the client.  If it is false (the
1627 default), negotiated instances are revalidated on every client
1628 request.
1630 Unfortunately, a number of servers (most notably some versions of
1631 Apache's @code{mod_deflate} module) send objects with a @samp{ETag}
1632 header that will confuse Polipo in the presence of a @samp{Vary}
1633 header.  Polipo will make a reasonable check for consistency if
1634 @samp{dontTrustVaryETag} is set to @samp{maybe} (the default); it will
1635 systematically ignore @samp{ETag} headers on objects with @samp{Vary}
1636 headers if it is set to @samp{true}.
1638 A number of websites incorrectly mark variable resources as cachable;
1639 such issues can be worked around in polipo by manually marking given
1640 categories of objects as uncachable.  If @code{dontCacheCookies} is
1641 true, all pages carrying HTTP cookies will be treated as uncachable.
1642 If @code{dontCacheRedirects} is true, all redirects (301 and 302) will
1643 be treated as uncachable.  Finally, if everything else fails, a list
1644 of uncachable URLs can be given in the file specified by
1645 @code{uncachableFile}, which has the same format as the
1646 @code{forbiddenFile} (@pxref{Internal forbidden list}).  If not
1647 specified, its location defaults to @samp{~/.polipo-uncachable} or
1648 @samp{/etc/polipo/uncachable}, whichever exists.
1650 @node Memory cache, Disk cache, Cache transparency, Caching
1651 @section The in-memory cache
1653 The in-memory cache consists of a list of HTTP and DNS objects
1654 maintained in least-recently used order.  An index to the in-memory
1655 cache is maintained as a (closed) hash table.
1657 When the in-memory cache grows beyond a certain size (controlled by a
1658 number of variables, @pxref{Memory usage}), or when a hash table
1659 collision occurs, resources are written out to disk.
1661 @node Disk cache,  , Memory cache, Caching
1662 @section The on-disk cache
1663 @cindex filesystem
1664 @cindex NFS
1665 @vindex diskCacheRoot
1666 @vindex maxDiskEntries
1667 @vindex diskCacheWriteoutOnClose
1668 @vindex diskCacheFilePermissions
1669 @vindex diskCacheDirectoryPermissions
1670 @vindex maxDiskCacheEntrySize
1672 The on-disk cache consists in a filesystem subtree rooted at
1673 a location defined by the variable @code{diskCacheRoot}, by default
1674 @code{"/var/cache/polipo/"}.  This directory should normally be
1675 writeable, readable and seekable by the user running Polipo.  While it
1676 is best to use a local filesystem for the on-disk cache, a NFSv3- or
1677 AFS-mounted filesystem should be safe in most implementations.  Do not
1678 use NFSv2, as it will cause cache corruption @footnote{Polipo assumes
1679 that @samp{open(O_CREAT | O_EXCL)} works reliably.}.
1681 If @code{diskCacheRoot} is an empty string, no disk cache is used.
1683 The value @code{maxDiskEntries} (32 by default) is the absolute
1684 maximum of file descriptors held open for on-disk objects.  When this
1685 limit is reached, Polipo will close descriptors on
1686 a least-recently-used basis.  This value should be set to be slightly
1687 larger than the number of resources that you expect to be live at
1688 a single time; defining the right notion of liveness is left as an
1689 exercise for the interested reader.
1691 The value @code{diskCacheWriteoutOnClose} (64@dmn{kB} by default) is
1692 the amount of data that Polipo will write out when closing a disk
1693 file.  Writing out data when closing a file can avoid subsequently
1694 reopening it, but causes unnecessary work if the instance is later
1695 superseded.
1697 The integers @code{diskCacheDirectoryPermissions} and
1698 @code{diskCacheFilePermissions} are the Unix filesystem permissions
1699 with which files and directories are created in the on-disk cache;
1700 they default to @samp{0700} and @samp{0600} respectively.
1702 The variable @code{maxDiskCacheEntrySize} specifies the maximum size,
1703 in bytes, of an instance that is stored in the on-disk cache.  If set
1704 to -1 (the default), all objects are stored in the on-disk cache,
1706 @menu
1707 * Asynchronous writing::        Writing out data when idle.
1708 * Purging::                     Purging the on-disk cache.
1709 * Disk format::                 Format of the on-disk cache.
1710 * Modifying the on-disk cache::  
1711 @end menu
1713 @node Asynchronous writing, Purging, Disk cache, Disk cache
1714 @subsection Asynchronous writing
1715 @vindex idleTime
1716 @vindex maxObjectsWhenIdle
1717 @vindex maxWriteoutWhenIdle
1719 When Polipo runs out of memory (@pxref{Limiting memory usage}), it
1720 will start discarding instances from its memory cache.  If a disk
1721 cache has been configured, it will write out any instance that it
1722 discards.  Any memory allocation that prompted the purge must then
1723 wait for the write to complete.
1725 In order to avoid the latency hit that this causes, Polipo will
1726 preemptively write out instances to the disk cache whenever it is
1727 idle.  The integer @code{idleTime} specifies the time during which
1728 Polipo will remain idle before it starts writing out random objects to
1729 the on-disk cache; this value defaults to 20@dmn{s}.  You may want to
1730 decrease this value for a busy cache with little memory, or increase
1731 it if your cache is often idle and has a lot of memory.
1733 The value @code{maxObjectsWhenIdle} (default 32) specifies the maximum
1734 number of instances that an idle Polipo will write out without
1735 checking whether there's any new work to do.  The value
1736 @code{maxWriteoutWhenIdle} specifies the maximum amount of data
1737 (default 64@dmn{kB}) that Polipo will write out without checking for
1738 new activity.  Increasing these values will make asynchronous
1739 write-out slightly faster, at the cost of possibly increasing Polipo's
1740 latency in some rare circumstances.
1742 @node Purging, Disk format, Asynchronous writing, Disk cache
1743 @subsection Purging the on-disk cache
1744 @cindex purging
1745 @vindex diskCacheUnlinkTime
1746 @vindex diskCacheTruncateTime
1747 @vindex diskCacheTruncateSize
1748 @vindex preciseExpiry
1750 Polipo never removes a file in its on-disk cache, except when it finds
1751 that the instance that it represents has been superseded by a newer
1752 version.  In order to keep the on-disk cache from growing without
1753 bound, it is necessary to @dfn{purge} it once in a while.  Purging the
1754 cache typically consists in removing some files, truncating large
1755 files (@pxref{Partial instances}) or moving them to off-line storage.
1757 Polipo itself can be used to purge its on-disk cache; this is done by
1758 invoking Polipo with the @option{-x} flag.  This can safely be done
1759 when Polipo is running (@pxref{Modifying the on-disk cache}).
1761 For a purge to be effective, it is necessary to cause Polipo to
1762 write-out its in-memory cache to disk (@pxref{Stopping}).
1763 Additionally, Polipo will not necessarily notice the changed files
1764 until it attempts to access them; thus, you will want it to discard
1765 its in-memory cache after performing the purge.  The safe way to
1766 perform a purge is therefore:
1767 @example
1768 $ kill -USR1 @var{polipo-pid}
1769 $ sleep 1
1770 $ polipo -x
1771 $ kill -USR2 @var{polipo-pid}
1772 @end example
1774 The behaviour of the @option{-x} flag is controlled by three
1775 configuration variables.  The variable @code{diskCacheUnlinkTime}
1776 specifies the time during which an on-disk entry should remain unused
1777 before it is eligible for removal; it defaults to 32 days.  
1779 The variable @code{diskCacheTruncateTime} specifies the time for which
1780 an on-disk entry should remain unused before it is eligible for
1781 truncation; it defaults to 4 days and a half.  The variable
1782 @code{diskCacheTruncateSize} specifies the size at which files are
1783 truncated after they have not been accessed for
1784 @code{diskCacheTruncateTime}; it defaults to 1@dmn{MB}.
1786 Usually, Polipo uses a file's modification time in order to determine
1787 whether it is old enough to be expirable.  This heuristic can be
1788 disabled by setting the variable @code{preciseExpiry} to true.
1790 @node Disk format, Modifying the on-disk cache, Purging, Disk cache
1791 @subsection Format of the on-disk cache
1792 @vindex DISK_CACHE_BODY_OFFSET
1793 @cindex on-disk file
1794 @cindex on-disk cache
1796 The on-disk cache consists of a collection of files, one per instance.
1797 The format of an on-disk resource is similar to that of an HTTP
1798 message: it starts with an HTTP status line, followed by HTTP headers,
1799 followed by a blank line (@samp{\r\n\r\n}).  The blank line is
1800 optionally followed by a number of binary zeroes.  The body of the
1801 instance follows.
1803 The headers of an on-disk file have a few minor differences with HTTP
1804 messages.  Obviously, there is never a @samp{Transfer-Encoding} line.
1805 A few additional headers are used by Polipo for its internal
1806 bookkeeping:
1807 @itemize
1808 @item 
1809 @samp{X-Polipo-Location}: this is the URL of the resource stored in this
1810 file.  This is always present.
1812 @item
1813 @samp{X-Polipo-Date}: this is Polipo's estimation of the date at which
1814 this instance was last validated, and is used for generating the
1815 @samp{Age} header of HTTP messages.  This is optional, and only stored
1816 if different from the instance's date.
1818 @item
1819 @samp{X-Polipo-Access}: this is the date when the instance was last
1820 accessed by Polipo, and is used for cache purging (@pxref{Purging}).
1821 This is optional, and is absent if the instance was never accessed.
1823 @item
1824 @samp{X-Polipo-Body-Offset}: the presence of this line indicates that
1825 the blank line following the headers is followed by a number of zero
1826 bytes.  Its value is an integer, which indicates the offset since the
1827 beginning of the file at which the instance body actually starts.
1828 This line is optional, and if absent the body starts immediately after
1829 the blank line.
1831 @end itemize
1833 @node Modifying the on-disk cache,  , Disk format, Disk cache
1834 @subsection Modifying the on-disk cache
1835 @cindex on-disk cache
1837 It is safe to modify the on-disk cache while Polipo is running as long
1838 as no file is ever modified in place.  More precisely, the only safe
1839 operations are to unlink (remove, delete) files in the disk cache, or
1840 to atomically add new files to the cache (by performing an exclusive
1841 open, or by using one of the @samp{link} or @samp{rename} system
1842 calls).  It is @emph{not} safe to truncate a file in place.
1844 @node Memory usage, Copying, Caching, Top
1845 @chapter Memory usage
1846 @cindex memory
1848 Polipo uses two distinct pools of memory, the @dfn{chunk pool} and
1849 the @dfn{malloc pool}.
1851 @menu
1852 * Chunk memory::                Chunk memory.
1853 * Malloc memory::               Malloc memory.
1854 * Limiting memory usage::       Limiting Polipo's memory usage.
1855 @end menu
1857 @node Chunk memory, Malloc memory, Memory usage, Memory usage
1858 @section Chunk memory
1860 @vindex CHUNK_SIZE
1861 @vindex MALLOC_CHUNKS
1862 @cindex chunk
1863 @cindex memory
1865 Most of the memory used by Polipo is stored in chunks, fixed-size
1866 blocks of memory; the size of a chunk is defined by the compile-time
1867 constant @code{CHUNK_SIZE}, and defaults to 4096 bytes on 32-bit
1868 platforms, 8192 on 64-bit ones.  Chunks are used for storing object
1869 data (bodies of instances) and for temporary I/O buffers.  Increasing
1870 the chunk size increases performance somewhat, but at the cost of
1871 larger granularity of allocation and hence larger memory usage.
1873 By default, Polipo uses a hand-crafted memory allocator based on
1874 @code{mmap}(2) (@code{VirtualAlloc} under Windows) for allocating
1875 chunks; while this is very slightly faster than the stock memory
1876 allocator, its main benefit is that it limits memory fragmentation.
1877 It is possible to disable the chunk allocator, and use
1878 @code{malloc}(3) for all memory allocation, by defining
1879 @code{MALLOC_CHUNKS} at compile time; this is probably only useful for
1880 debugging.
1882 There is one assumption made about @code{CHUNK_SIZE}:
1883 @code{CHUNK_SIZE} multiplied by the number of bits in an
1884 @code{unsigned long} (actually in a @code{ChunkBitmap} --- see
1885 @file{chunk.c}) must be a multiple of the page size, which is 4096 on
1886 most systems (8192 on Alpha, and apparently 65536 on Windows).
1888 As all network I/O will be performed in units of one to two chunks,
1889 @code{CHUNK_SIZE} should be at least equal to your network interface's
1890 MTU (typically 1500 bytes).  Additionally, as much I/O will be done at
1891 @code{CHUNK_SIZE}-aligned addresses, @code{CHUNK_SIZE} should ideally
1892 be a multiple of the page size.
1894 In summary, 2048, 4096, 8192 and 16384 are good choices for
1895 @code{CHUNK_SIZE}.
1897 @node Malloc memory, Limiting memory usage, Chunk memory, Memory usage
1898 @section Malloc allocation
1899 @cindex malloc
1900 @cindex memory
1902 Polipo uses the standard @code{malloc}(3) memory allocator for
1903 allocating small data structures (up to 100 bytes), small strings and
1904 atoms (unique strings).
1906 @node Limiting memory usage,  , Malloc memory, Memory usage
1907 @section Limiting Polipo's memory usage
1908 @cindex limiting memory
1909 @cindex memory
1911 Polipo is designed to work well when given little memory, but will
1912 happily scale to larger configurations.  For that reason, you need to
1913 inform it of the amount of memory it can use.
1915 @menu
1916 * Limiting chunk usage::        Discard objects when low on chunks.
1917 * Limiting object usage::       Limit the number of objects.
1918 * OS usage limits::             Don't impose OS limits.
1919 @end menu
1921 @node Limiting chunk usage, Limiting object usage, Limiting memory usage, Limiting memory usage
1922 @subsection Limiting chunk usage
1924 @vindex chunkHighMark
1925 @vindex chunkCriticalMark
1926 @vindex chunkLowMark
1927 @vindex CHUNK_SIZE
1928 @cindex memory
1929 @cindex chunk
1931 You can limit Polipo's usage of chunk memory by setting
1932 @code{chunkHighMark} and @code{chunkLowMark}.
1934 The value @code{chunkHighMark} is the absolute maximum number of bytes
1935 of allocated chunk memory.  When this value is reached, Polipo will try
1936 to purge objects from its in-memory cache; if that fails to free memory,
1937 Polipo will start dropping connections.  This value defaults to
1938 24@dmn{MB} or one quarter of the machine's physical memory, whichever is
1939 less.
1941 When chunk usage falls back below @code{chunkLowMark}, Polipo will
1942 stop discarding in-memory objects.  The value
1943 @code{chunkCriticalMark}, which should be somewhere between
1944 @code{chunkLowMark} and @code{chunkHighMark}, specifies the value
1945 above which Polipo will make heroic efforts to free memory, including
1946 punching holes in the middle of instances, but without dropping
1947 connections.
1949 Unless set explicitly, both @code{chunkLowMark} and
1950 @code{chunkCriticalMark} are computed automatically from
1951 @code{chunkHighMark}.
1953 @node Limiting object usage, OS usage limits, Limiting chunk usage, Limiting memory usage
1954 @subsection Limiting object usage
1956 @vindex objectHighMark
1957 @vindex publicObjectLowMark
1958 @vindex objectHashTableSize
1960 Besides limiting chunk usage, it is possible to limit Polipo's memory
1961 usage by bounding the number of objects it keeps in memory at any given
1962 time.  This is done with @code{objectHighMark} and
1963 @code{publicObjectLowMark}.
1965 The value @code{objectHighMark} is the absolute maximum of objects
1966 held in memory (including resources and server addresses).  When the
1967 number of in-memory objects that haven't been superseded yet falls
1968 below @code{publicObjectLowMark}, Polipo will stop writing out objects
1969 to disk (superseded objects are discarded as soon as possible).
1971 On 32-bit architectures, every object costs 108 bytes of memory, plus
1972 storage for every globally unique header that is not handled specially
1973 (hopefully negligible), plus an overhead of one word (4 bytes) for
1974 every chunk of data in the object.
1976 You may also want to change @code{objectHashTableSize}.  This is the
1977 size of the hash table used for holding objects; it should be a power
1978 of two and defaults to eight times @code{objectHighMark}.  Increasing
1979 this value will reduce the number of objects being written out to disk
1980 due to hash table collisions.  Every hash table entry costs one word.
1982 @node OS usage limits,  , Limiting object usage, Limiting memory usage
1983 @subsection OS usage limits
1984 @cindex usage limit
1985 @cindex ulimit
1986 @cindex OOM killer
1988 Many operating systems permit limiting a process' memory usage by
1989 setting a @dfn{usage limit}; on most Unix-like systems, this is done
1990 with the @option{-v} option to the @command{ulimit} command.
1991 Typically, the effect is to cause calls to the @code{malloc} and
1992 @code{mmap} library functions to fail.
1994 Polipo will usually react gracefully to failures to allocate
1995 memory@footnote{There are exactly three places in the code where
1996 Polipo will give up and exit if out of memory; all three are extremely
1997 unlikely to happen in practice.}.  Nonetheless, you should avoid using
1998 OS limits to limit Polipo's memory usage: when it hits an OS limit,
1999 Polipo cannot allocate the memory needed to schedule recovery from the
2000 out-of-memory condition, and has no choice other than to drop a
2001 connection.
2003 Unfortunately, some operating system kernels (notably certain Linux
2004 releases) fail to fail an allocation if no usage limit is given;
2005 instead, they either crash when memory is exhausted, or else start
2006 killing random processes with no advance warning@footnote{How I wish
2007 for a @samp{SIGXMEM} signal.}.  On such systems, imposing an
2008 (unrealistically large) usage limit on Polipo is the safe thing to do.
2010 @node Copying, Variable index, Memory usage, Top
2011 @unnumbered Copying
2012 You are allowed to do anything you wish with Polipo as long as you
2013 don't deny my right to be recognised as its author and you don't blame
2014 me if anything goes wrong.
2016 More formally, Polipo is distributed under the following terms:
2018 @quotation
2019 Copyright @copyright{} 2003--2010 by Juliusz Chroboczek@*
2020 Copyright @copyright{} 2009--2010 by Christopher Davis
2022 Permission is hereby granted, free of charge, to any person obtaining a copy
2023 of this software and associated documentation files (the "Software"), to deal
2024 in the Software without restriction, including without limitation the rights
2025 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2026 copies of the Software, and to permit persons to whom the Software is
2027 furnished to do so, subject to the following conditions:
2029 The above copyright notice and this permission notice shall be included in
2030 all copies or substantial portions of the Software.
2032 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2033 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2034 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2035 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2036 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2037 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2038 THE SOFTWARE.
2039 @end quotation
2040 The last sentence is what happens when you allow lawyers to have it
2041 their way with a language.
2043 @node Variable index, Concept index, Copying, Top
2044 @unnumbered Variable index
2045 @printindex vr
2047 @node Concept index,  , Variable index, Top
2048 @unnumbered Concept index
2049 @printindex cp
2051 @bye