Move busui to seperate repository
[bus.git] / test_http.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/*
 * test_http.php
 *
 * @(#) $Header: /home/mlemos/cvsroot/http/test_http.php,v 1.18 2008/02/24 05:06:30 mlemos Exp $
 *
 */
 
?><HTML>
<HEAD>
<TITLE>Test for Manuel Lemos' PHP HTTP class</TITLE>
</HEAD>
<BODY>
<H1><CENTER>Test for Manuel Lemos' PHP HTTP class</CENTER></H1>
<HR>
<UL>
<?php
        require("http.php");
 
        /* Uncomment the line below when accessing Web servers or proxies that
         * require authentication.
         */
        /*
        require("sasl.php");
        */
 
        set_time_limit(0);
        $http=new http_class;
 
        /* Connection timeout */
        $http->timeout=0;
 
        /* Data transfer timeout */
        $http->data_timeout=0;
 
        /* Output debugging information about the progress of the connection */
        $http->debug=1;
 
        /* Format dubug output to display with HTML pages */
        $http->html_debug=1;
 
 
        /*
         *  Need to emulate a certain browser user agent?
         *  Set the user agent this way:
         */
        $http->user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
 
        /*
         *  If you want to the class to follow the URL of redirect responses
         *  set this variable to 1.
         */
        $http->follow_redirect=1;
 
        /*
         *  How many consecutive redirected requests the class should follow.
         */
        $http->redirection_limit=5;
 
        /*
         *  If your DNS always resolves non-existing domains to a default IP
         *  address to force the redirection to a given page, specify the
         *  default IP address in this variable to make the class handle it
         *  as when domain resolution fails.
         */
        $http->exclude_address="";
 
        /*
         *  If you want to establish SSL connections and you do not want the
         *  class to use the CURL library, set this variable to 0 .
         */
        $http->prefer_curl=0;
 
        /*
         *  If basic authentication is required, specify the user name and
         *  password in these variables.
         */
 
        $user="";
        $password="";
        $realm="";       /* Authentication realm or domain      */
        $workstation=""; /* Workstation for NTLM authentication */
        $authentication=(strlen($user) ? UrlEncode($user).":".UrlEncode($password)."@" : "");
 
/*
        Do you want to access a page via SSL?
        Just specify the https:// URL.
        $url="https://www.openssl.org/";
*/
 
        $url="http://".$authentication."www.php.net/";
 
        /*
         *  Generate a list of arguments for opening a connection and make an
         *  HTTP request from a given URL.
         */
        $error=$http->GetRequestArguments($url,$arguments);
 
        if(strlen($realm))
                $arguments["AuthRealm"]=$realm;
 
        if(strlen($workstation))
                $arguments["AuthWorkstation"]=$workstation;
 
        $http->authentication_mechanism=""; // force a given authentication mechanism;
 
        /*
         *  If you need to access a site using a proxy server, use these
         *  arguments to set the proxy host and authentication credentials if
         *  necessary.
         */
        /*
        $arguments["ProxyHostName"]="127.0.0.1";
        $arguments["ProxyHostPort"]=3128;
        $arguments["ProxyUser"]="proxyuser";
        $arguments["ProxyPassword"]="proxypassword";
        $arguments["ProxyRealm"]="proxyrealm";  // Proxy authentication realm or domain
        $arguments["ProxyWorkstation"]="proxyrealm"; // Workstation for NTLM proxy authentication
        $http->proxy_authentication_mechanism=""; // force a given proxy authentication mechanism;
        */
 
        /*
         *  If you need to access a site using a SOCKS server, use these
         *  arguments to set the SOCKS host and port.
         */
        /*
        $arguments["SOCKSHostName"]='127.0.0.1';
        $arguments["SOCKSHostPort"]=1080;
        $arguments["SOCKSVersion"]='5';
        */
 
        /* Set additional request headers */
        $arguments["Headers"]["Pragma"]="nocache";
/*
        Is it necessary to specify a certificate to access a page via SSL?
        Specify the certificate file this way.
        $arguments["SSLCertificateFile"]="my_certificate_file.pem";
        $arguments["SSLCertificatePassword"]="some certificate password";
*/
 
/*
        Is it necessary to preset some cookies?
        Just use the SetCookie function to set each cookie this way:
 
        $cookie_name="LAST_LANG";
        $cookie_value="de";
        $cookie_expires="2010-01-01 00:00:00"; // "" for session cookies
        $cookie_uri_path="/";
        $cookie_domain=".php.net";
        $cookie_secure=0; // 1 for SSL only cookies
        $http->SetCookie($cookie_name, $cookie_value, $cookie_expiry, $cookie_uri_path, $cookie_domain, $cookie_secure);
*/
 
        echo "<H2><LI>Opening connection to:</H2>\n<PRE>",HtmlEntities($arguments["HostName"]),"</PRE>\n";
        flush();
        $error=$http->Open($arguments);
 
        if($error=="")
        {
                echo "<H2><LI>Sending request for page:</H2>\n<PRE>";
                echo HtmlEntities($arguments["RequestURI"]),"\n";
                if(strlen($user))
                        echo "\nLogin:    ",$user,"\nPassword: ",str_repeat("*",strlen($password));
                echo "</PRE>\n";
                flush();
                $error=$http->SendRequest($arguments);
 
                if($error=="")
                {
                        echo "<H2><LI>Request:</LI</H2>\n<PRE>\n".HtmlEntities($http->request)."</PRE>\n";
                        echo "<H2><LI>Request headers:</LI</H2>\n<PRE>\n";
                        for(Reset($http->request_headers),$header=0;$header<count($http->request_headers);Next($http->request_headers),$header++)
                        {
                                $header_name=Key($http->request_headers);
                                if(GetType($http->request_headers[$header_name])=="array")
                                {
                                        for($header_value=0;$header_value<count($http->request_headers[$header_name]);$header_value++)
                                                echo $header_name.": ".$http->request_headers[$header_name][$header_value],"\r\n";
                                }
                                else
                                        echo $header_name.": ".$http->request_headers[$header_name],"\r\n";
                        }
                        echo "</PRE>\n";
                        flush();
 
                        $headers=array();
                        $error=$http->ReadReplyHeaders($headers);
                        if($error=="")
                        {
                                echo "<H2><LI>Response status code:</LI</H2>\n<P>".$http->response_status;
                                switch($http->response_status)
                                {
                                        case "301":
                                        case "302":
                                        case "303":
                                        case "307":
                                                echo " (redirect to <TT>".$headers["location"]."</TT>)<BR>\nSet the <TT>follow_redirect</TT> variable to handle redirect responses automatically.";
                                                break;
                                }
                                echo "</P>\n";
                                echo "<H2><LI>Response headers:</LI</H2>\n<PRE>\n";
                                for(Reset($headers),$header=0;$header<count($headers);Next($headers),$header++)
                                {
                                        $header_name=Key($headers);
                                        if(GetType($headers[$header_name])=="array")
                                        {
                                                for($header_value=0;$header_value<count($headers[$header_name]);$header_value++)
                                                        echo $header_name.": ".$headers[$header_name][$header_value],"\r\n";
                                        }
                                        else
                                                echo $header_name.": ".$headers[$header_name],"\r\n";
                                }
                                echo "</PRE>\n";
                                flush();
 
                                echo "<H2><LI>Response body:</LI</H2>\n<PRE>\n";
                                for(;;)
                                {
                                        $error=$http->ReadReplyBody($body,1000);
                                        if($error!=""
                                        || strlen($body)==0)
                                                break;
                                        echo HtmlSpecialChars($body);
                                }
                                echo "</PRE>\n";
                                flush();
                        }
                }
                $http->Close();
        }
        if(strlen($error))
                echo "<CENTER><H2>Error: ",$error,"</H2><CENTER>\n";
?>
</UL>
<HR>
</BODY>
</HTML>