initial commit
[m6w6/m6w6.github.io] / _posts / 2006-09-16-httprequestdatashare.md
1 ---
2 title: HttpRequestDataShare
3 author: m6w6
4 tags:
5 - PHP
6 ---
7
8 There are some news to talk about development of
9 [pecl/http](http://pecl.php.net/package/pecl_http).
10
11 I recently implemented an interface to the [curl-
12 share](http://curl.haxx.se/libcurl/c/libcurl-share.html) functionality in form
13 of an HttpRequestDataShare class.
14
15 This is what [reflection](http://php.net/reflection) will tell you about it:
16
17 ```shell
18 mike@honeybadger:~/build/php-5.2-debug$ cli --rc HttpRequestDataShare
19 Class [ <internal:http> class HttpRequestDataShare implements Countable ] {
20
21 - Constants [0] {
22 }
23
24 - Static properties [1] {
25 Property [ private static $instance ]
26 }
27
28 - Static methods [1] {
29 Method [ <internal> static public method singleton ] {
30
31 - Parameters [1] {
32 Parameter #0 [ <optional> $global ]
33 }
34 }
35 }
36
37 - Properties [4] {
38 Property [ <default> public $cookie ]
39 Property [ <default> public $dns ]
40 Property [ <default> public $ssl ]
41 Property [ <default> public $connect ]
42 }
43
44 - Methods [5] {
45 Method [ <internal, dtor> public method __destruct ] {
46
47 - Parameters [0] {
48 }
49 }
50
51 Method [ <internal, prototype Countable> public method count ] {
52
53 - Parameters [0] {
54 }
55 }
56
57 Method [ <internal> public method attach ] {
58
59 - Parameters [1] {
60 Parameter #0 [ <required> HttpRequest $request ]
61 }
62 }
63
64 Method [ <internal> public method detach ] {
65
66 - Parameters [1] {
67 Parameter #0 [ <required> HttpRequest $request ]
68 }
69 }
70
71 Method [ <internal> public method reset ] {
72
73 - Parameters [0] {
74 }
75 }
76 }
77 }
78 ```
79
80 Using this class, you can save a fair amount of time with name lookups which
81 the following example shows:
82 ```php
83 $s = HttpRequestDataShare::singleton(true);
84 print_r($s);
85 for ($i = 0; $i < 10; ++$i) {
86 $r = new HttpRequest("http://www.google.com/");
87 $s->attach($r);
88 $r->send();
89 printf("%0.6fn", $r->getResponseInfo("namelookup_time"));
90 $s->detach($r);
91 }
92 ```
93
94 Executing this script without dns data sharing enabled gives the following
95 results:
96 ```shell
97 mike@honeybadger:~/build/php-5.2-debug$ cli -d"http.request.datashare.dns=0"
98 ~/devel/http_rshare.php
99 HttpRequestDataShare Object
100 (
101 [cookie] =>
102 [dns] =>
103 [ssl] =>
104 [connect] =>
105 )
106 0.071296
107 0.048798
108 0.049598
109 0.051545
110 0.046258
111 0.052318
112 0.043769
113 0.060753
114 0.049168
115 0.048568
116 ```
117
118 ...and with dns data sharing enabled:
119 ```shell
120 mike@honeybadger:~/build/php-5.2-debug$ cli -d"http.request.datashare.dns=1"
121 ~/devel/http_rshare.php
122 HttpRequestDataShare Object
123 (
124 [cookie] =>
125 [dns] => 1
126 [ssl] =>
127 [connect] =>
128 )
129 0.051945
130 0.000043
131 0.000041
132 0.000040
133 0.000039
134 0.000041
135 0.000041
136 0.000040
137 0.000040
138 0.000041
139 ```
140
141 **QED**
142
143 You can either use a per-process global datashare object created with
144 HttpRequestDataShare::singleton(true) or different instances for your
145 HttpRequest objects. Note that dns datasharing is used autmagically for
146 HttpRequestPool requests. Currently libcurl has implemented cookie and dns
147 data sharing only, trying to enable ssl session or connect sharing will raise
148 a warning.
149
150 Be sure to try it out; either directly from CVS or the next release, probably
151 being 1.3.0RC1.