Skip to content

Commit c4a7fea

Browse files
committed
Header plugin
1 parent 1989a3d commit c4a7fea

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

plugins/header.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Header Plugin
2+
=============
3+
4+
Header plugins are useful to manage Request headers. Many operations are possible:
5+
6+
Default headers values
7+
----------------------
8+
9+
The HeaderDefaultPlugin allows to set default values for given headers. That mean if the request hasn't
10+
already a given header it will be set, but if the request already has a value for the given header
11+
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+
26+
// If a request handled by this client doesn't have a 'User-Agent' header,
27+
// then $defaultUserAgent will be used
28+
// If a request handled by this client has already a 'User-Agent' header,
29+
// then the request wont be changed
30+
$pluginClient = new PluginClient(
31+
HttpClientDiscovery::find(),
32+
[$headerDefaultPlugin]
33+
);
34+
35+
Mandatory headers values
36+
------------------------
37+
38+
The Http\Plugins\HeaderSetPlugin allows to fix values of given header. That mean that any request passing through
39+
this plugin will have the same values for given headers
40+
41+
.. code:: php
42+
43+
use Http\Discovery\HttpClientDiscovery;
44+
use Http\Plugins\PluginClient;
45+
use Http\Plugins\HeaderSetPlugin;
46+
47+
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
48+
49+
$headerSetPlugin = new HeaderSetPlugin([
50+
'User-Agent' => $userAgent,
51+
'Accept' => 'application/json'
52+
]);
53+
54+
55+
// Every request handled by this client will have the given 'User-Agent' and 'Accept' headers values
56+
// If the request already has one of these header then it will be overridden with the given value
57+
$pluginClient = new PluginClient(
58+
HttpClientDiscovery::find(),
59+
[$headerSetPlugin]
60+
);
61+
62+
63+
64+
Removing headers
65+
----------------
66+
67+
The Http\Plugins\HeaderRemovePlugin allows to remove given headers from the request.
68+
That mean that any request passing through this plugin will be freed of the given headers
69+
70+
.. code:: php
71+
72+
use Http\Discovery\HttpClientDiscovery;
73+
use Http\Plugins\PluginClient;
74+
use Http\Plugins\HeaderRemovePlugin;
75+
76+
$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1';
77+
78+
$headerRemovePlugin = new HeaderRemovePlugin([
79+
'User-Agent' => $userAgent
80+
]);
81+
82+
83+
// If a request handled by this client has a 'User-Agent' header,
84+
// then the 'User-Agent' header will be removed from the request
85+
$pluginClient = new PluginClient(
86+
HttpClientDiscovery::find(),
87+
[$headerRemovePlugin]
88+
);
89+
90+
91+
Appending header values
92+
-----------------------
93+
94+
The Http\Plugins\HeaderAppendPlugin allows to set headers or to add values to existing headers.
95+
96+
.. note::
97+
98+
This header is very specific and is mostly useful for headers like "forwarded"
99+
100+
.. code:: php
101+
102+
use Http\Discovery\HttpClientDiscovery;
103+
use Http\Plugins\PluginClient;
104+
use Http\Plugins\HeaderAppendPlugin;
105+
106+
$myIp = '100.100.100.100';
107+
108+
$headerAppendPlugin = new HeaderAppendPlugin([
109+
'X-Forwarded-For' => $myIp
110+
]);
111+
112+
113+
// If a request handled by this client has already a 'X-Forwarded-For' header,
114+
// then $myIp will be appended to the current 'X-Forwarded-For' header's value
115+
// If a request handled by this client hasn't already a 'X-Forwarded-For' header,
116+
// then the header 'X-Forwarded-For' header will be added to the request with $myIp as the value
117+
$pluginClient = new PluginClient(
118+
HttpClientDiscovery::find(),
119+
[$headerAppendPlugin]
120+
);
121+

0 commit comments

Comments
 (0)