Skip to content

Commit 657a583

Browse files
committed
Header plugin
1 parent 1989a3d commit 657a583

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

plugins/headers.rst

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
Header Plugins
2+
==============
3+
4+
Header plugins are useful to manage request headers. Many operations are possible:
5+
6+
Default headers values
7+
----------------------
8+
9+
The plugin Http\Plugins\HeaderDefaultPlugin allows to set default values for given headers.
10+
That means if the request hasn't already a given header it will be set,
11+
but if the request already has a value for the given header then the request wont be changed
12+
13+
.. code:: php
14+
15+
use Http\Discovery\HttpClientDiscovery;
16+
use Http\Plugins\PluginClient;
17+
use Http\Plugins\HeaderDefaultPlugin;
18+
19+
$defaultUserAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
20+
21+
$headerDefaultPlugin = new HeaderDefaultPlugin([
22+
'User-Agent' => $defaultUserAgent
23+
]);
24+
25+
$pluginClient = new PluginClient(
26+
HttpClientDiscovery::find(),
27+
[$headerDefaultPlugin]
28+
);
29+
30+
Mandatory headers values
31+
------------------------
32+
33+
The Http\Plugins\HeaderSetPlugin allows to fix values of given header. That mean that any request passing through
34+
this plugin will have the same values for given headers
35+
36+
.. code:: php
37+
38+
use Http\Discovery\HttpClientDiscovery;
39+
use Http\Plugins\PluginClient;
40+
use Http\Plugins\HeaderSetPlugin;
41+
42+
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
43+
44+
$headerSetPlugin = new HeaderSetPlugin([
45+
'User-Agent' => $userAgent,
46+
'Accept' => 'application/json'
47+
]);
48+
49+
$pluginClient = new PluginClient(
50+
HttpClientDiscovery::find(),
51+
[$headerSetPlugin]
52+
);
53+
54+
55+
56+
Removing headers
57+
----------------
58+
59+
The Http\Plugins\HeaderRemovePlugin allows to remove given headers from the request.
60+
That mean that any request passing through this plugin will be freed of the given headers.
61+
62+
.. code:: php
63+
64+
use Http\Discovery\HttpClientDiscovery;
65+
use Http\Plugins\PluginClient;
66+
use Http\Plugins\HeaderRemovePlugin;
67+
68+
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
69+
70+
$headerRemovePlugin = new HeaderRemovePlugin([
71+
'User-Agent'
72+
]);
73+
74+
$pluginClient = new PluginClient(
75+
HttpClientDiscovery::find(),
76+
[$headerRemovePlugin]
77+
);
78+
79+
80+
Appending header values
81+
-----------------------
82+
83+
The Http\Plugins\HeaderAppendPlugin allows to set headers or to add values to existing headers.
84+
That means if the request has already the given headers then the value will be appended to the current value
85+
but if the request hasn't already the given header the header will be added to the request with the given value.
86+
87+
.. note::
88+
89+
This header is very specific and is mostly useful for headers like "forwarded"
90+
91+
.. code:: php
92+
93+
use Http\Discovery\HttpClientDiscovery;
94+
use Http\Plugins\PluginClient;
95+
use Http\Plugins\HeaderAppendPlugin;
96+
97+
$myIp = '100.100.100.100';
98+
99+
$headerAppendPlugin = new HeaderAppendPlugin([
100+
'Forwarded' => 'for=' . $myIp
101+
]);
102+
103+
$pluginClient = new PluginClient(
104+
HttpClientDiscovery::find(),
105+
[$headerAppendPlugin]
106+
);
107+
108+
109+
Mixing operations
110+
-----------------
111+
112+
Different header operations can be mixed by using the together in the same header plugin
113+
but you can use the same plugin for identical operations
114+
115+
The following example will set the force the "User-Agent" and the "Accept" headers while removing cookies:
116+
117+
.. code:: php
118+
119+
use Http\Discovery\HttpClientDiscovery;
120+
use Http\Plugins\PluginClient;
121+
use Http\Plugins\HeaderSetPlugin;
122+
use Http\Plugins\HeaderRemovePlugin;
123+
124+
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
125+
126+
$headerSetPlugin = new HeaderSetPlugin([
127+
'User-Agent' => $userAgent,
128+
'Accept' => 'application/json'
129+
]);
130+
131+
$headerRemovePlugin = new HeaderRemovePlugin([
132+
'Cookie'
133+
]);
134+
135+
$pluginClient = new PluginClient(
136+
HttpClientDiscovery::find(),
137+
[
138+
$headerSetPlugin,
139+
$headerRemovePlugin
140+
]
141+
);
142+
143+

plugins/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ request or you can even start a completely new request. This gives you full cont
1515
cookie
1616
decoder
1717
error
18+
headers
1819
history
1920
logger
2021
redirect

0 commit comments

Comments
 (0)