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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
<?php
class MockCookie extends Cookie {
public function canServeDomain( $arg ) { return parent::canServeDomain( $arg ); }
public function canServePath( $arg ) { return parent::canServePath( $arg ); }
public function isUnExpired() { return parent::isUnExpired(); }
}
/**
* @group Broken
*/
class HttpTest extends MediaWikiTestCase {
static $content;
static $headers;
static $has_curl;
static $has_fopen;
static $has_proxy = false;
static $proxy = "http://hulk:8080/";
var $test_geturl = array(
"http://en.wikipedia.org/robots.txt",
"https://secure.wikimedia.org/",
"http://pecl.php.net/feeds/pkg_apc.rss",
"http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw",
"http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&format=php",
);
var $test_requesturl = array( "http://en.wikipedia.org/wiki/Special:Export/User:MarkAHershberger" );
var $test_posturl = array( "http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example" => "review=test" );
function setUp() {
putenv( "http_proxy" ); /* Remove any proxy env var, so curl doesn't get confused */
if ( is_array( self::$content ) ) {
return;
}
self::$has_curl = function_exists( 'curl_init' );
self::$has_fopen = wfIniGetBool( 'allow_url_fopen' );
if ( !file_exists( "/usr/bin/curl" ) ) {
$this->markTestIncomplete( "This test requires the curl binary at /usr/bin/curl. If you have curl, please file a bug on this test, or, better yet, provide a patch." );
}
$content = tempnam( wfTempDir(), "" );
$headers = tempnam( wfTempDir(), "" );
if ( !$content && !$headers ) {
die( "Couldn't create temp file!" );
}
// This probably isn't the best test for a proxy, but it works on my system!
system( "curl -0 -o $content -s " . self::$proxy );
$out = file_get_contents( $content );
if ( $out ) {
self::$has_proxy = true;
}
/* Maybe use wget instead of curl here ... just to use a different codebase? */
foreach ( $this->test_geturl as $u ) {
system( "curl -0 -s -D $headers '$u' -o $content" );
self::$content["GET $u"] = file_get_contents( $content );
self::$headers["GET $u"] = file_get_contents( $headers );
}
foreach ( $this->test_requesturl as $u ) {
system( "curl -0 -s -X POST -H 'Content-Length: 0' -D $headers '$u' -o $content" );
self::$content["POST $u"] = file_get_contents( $content );
self::$headers["POST $u"] = file_get_contents( $headers );
}
foreach ( $this->test_posturl as $u => $postData ) {
system( "curl -0 -s -X POST -d '$postData' -D $headers '$u' -o $content" );
self::$content["POST $u => $postData"] = file_get_contents( $content );
self::$headers["POST $u => $postData"] = file_get_contents( $headers );
}
unlink( $content );
unlink( $headers );
}
function testInstantiation() {
Http::$httpEngine = false;
$r = MWHttpRequest::factory( "http://www.example.com/" );
if ( self::$has_curl ) {
$this->assertThat( $r, $this->isInstanceOf( 'CurlHttpRequest' ) );
} else {
$this->assertThat( $r, $this->isInstanceOf( 'PhpHttpRequest' ) );
}
unset( $r );
if ( !self::$has_fopen ) {
$this->setExpectedException( 'MWException' );
}
Http::$httpEngine = 'php';
$r = MWHttpRequest::factory( "http://www.example.com/" );
$this->assertThat( $r, $this->isInstanceOf( 'PhpHttpRequest' ) );
unset( $r );
if ( !self::$has_curl ) {
$this->setExpectedException( 'MWException' );
}
Http::$httpEngine = 'curl';
$r = MWHttpRequest::factory( "http://www.example.com/" );
if ( self::$has_curl ) {
$this->assertThat( $r, $this->isInstanceOf( 'CurlHttpRequest' ) );
}
}
function runHTTPFailureChecks() {
// Each of the following requests should result in a failure.
$timeout = 1;
$start_time = time();
$r = Http::get( "http://www.example.com:1/", $timeout );
$end_time = time();
$this->assertLessThan( $timeout + 2, $end_time - $start_time,
"Request took less than {$timeout}s via " . Http::$httpEngine );
$this->assertEquals( $r, false, "false -- what we get on error from Http::get()" );
$r = Http::get( "http://www.mediawiki.org/xml/made-up-url", $timeout );
$this->assertFalse( $r, "False on 404s" );
$r = MWHttpRequest::factory( "http://www.mediawiki.org/xml/made-up-url" );
$er = $r->execute();
if ( $r instanceof PhpHttpRequest && version_compare( '5.2.10', phpversion(), '>' ) ) {
$this->assertRegexp( "/HTTP request failed/", $er->getWikiText() );
} else {
$this->assertRegexp( "/404 Not Found/", $er->getWikiText() );
}
}
function testFailureDefault() {
Http::$httpEngine = false;
$this->runHTTPFailureChecks();
}
function testFailurePhp() {
if ( !self::$has_fopen ) {
$this->markTestIncomplete( "This test requires allow_url_fopen=true." );
}
Http::$httpEngine = "php";
$this->runHTTPFailureChecks();
}
function testFailureCurl() {
if ( !self::$has_curl ) {
$this->markTestIncomplete( "This test requires curl." );
}
Http::$httpEngine = "curl";
$this->runHTTPFailureChecks();
}
/* ./phase3/includes/Import.php:1108: $data = Http::request( $method, $url ); */
/* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */
/* ./includes/Import.php:1134: return ImportStreamSource::newFromURL( $url, "POST" ); */
function runHTTPRequests( $proxy = null ) {
$opt = array();
if ( $proxy ) {
$opt['proxy'] = $proxy;
} elseif ( $proxy === false ) {
$opt['noProxy'] = true;
}
/* no postData here because the only request I could find in code so far didn't have any */
foreach ( $this->test_requesturl as $u ) {
$r = Http::request( "POST", $u, $opt );
$this->assertEquals( self::$content["POST $u"], "$r", "POST $u with " . Http::$httpEngine );
}
}
function testRequestDefault() {
Http::$httpEngine = false;
$this->runHTTPRequests();
}
function testRequestPhp() {
if ( !self::$has_fopen ) {
$this->markTestIncomplete( "This test requires allow_url_fopen=true." );
}
Http::$httpEngine = "php";
$this->runHTTPRequests();
}
function testRequestCurl() {
if ( !self::$has_curl ) {
$this->markTestIncomplete( "This test requires curl." );
}
Http::$httpEngine = "curl";
$this->runHTTPRequests();
}
function runHTTPGets( $proxy = null ) {
$opt = array();
if ( $proxy ) {
$opt['proxy'] = $proxy;
} elseif ( $proxy === false ) {
$opt['noProxy'] = true;
}
foreach ( $this->test_geturl as $u ) {
$r = Http::get( $u, 30, $opt ); /* timeout of 30s */
$this->assertEquals( self::$content["GET $u"], "$r", "Get $u with " . Http::$httpEngine );
}
}
function testGetDefault() {
Http::$httpEngine = false;
$this->runHTTPGets();
}
function testGetPhp() {
if ( !self::$has_fopen ) {
$this->markTestIncomplete( "This test requires allow_url_fopen=true." );
}
Http::$httpEngine = "php";
$this->runHTTPGets();
}
function testGetCurl() {
if ( !self::$has_curl ) {
$this->markTestIncomplete( "This test requires curl." );
}
Http::$httpEngine = "curl";
$this->runHTTPGets();
}
function runHTTPPosts( $proxy = null ) {
$opt = array();
if ( $proxy ) {
$opt['proxy'] = $proxy;
} elseif ( $proxy === false ) {
$opt['noProxy'] = true;
}
foreach ( $this->test_posturl as $u => $postData ) {
$opt['postData'] = $postData;
$r = Http::post( $u, $opt );
$this->assertEquals( self::$content["POST $u => $postData"], "$r",
"POST $u (postData=$postData) with " . Http::$httpEngine );
}
}
function testPostDefault() {
Http::$httpEngine = false;
$this->runHTTPPosts();
}
function testPostPhp() {
if ( !self::$has_fopen ) {
$this->markTestIncomplete( "This test requires allow_url_fopen=true." );
}
Http::$httpEngine = "php";
$this->runHTTPPosts();
}
function testPostCurl() {
if ( !self::$has_curl ) {
$this->markTestIncomplete( "This test requires curl." );
}
Http::$httpEngine = "curl";
$this->runHTTPPosts();
}
function runProxyRequests() {
if ( !self::$has_proxy ) {
$this->markTestIncomplete( "This test requires a proxy." );
}
$this->runHTTPGets( self::$proxy );
$this->runHTTPPosts( self::$proxy );
$this->runHTTPRequests( self::$proxy );
// Set false here to do noProxy
$this->runHTTPGets( false );
$this->runHTTPPosts( false );
$this->runHTTPRequests( false );
}
function testProxyDefault() {
Http::$httpEngine = false;
$this->runProxyRequests();
}
function testProxyPhp() {
if ( !self::$has_fopen ) {
$this->markTestIncomplete( "This test requires allow_url_fopen=true." );
}
Http::$httpEngine = 'php';
$this->runProxyRequests();
}
function testProxyCurl() {
if ( !self::$has_curl ) {
$this->markTestIncomplete( "This test requires curl." );
}
Http::$httpEngine = 'curl';
$this->runProxyRequests();
}
function testIsLocalUrl() {
}
/* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
function testUserAgent() {
}
function testIsValidUrl() {
}
/**
* @dataProvider cookieDomains
*/
function testValidateCookieDomain( $expected, $domain, $origin=null ) {
if ( $origin ) {
$ok = Cookie::validateCookieDomain( $domain, $origin );
$msg = "$domain against origin $origin";
} else {
$ok = Cookie::validateCookieDomain( $domain );
$msg = "$domain";
}
$this->assertEquals( $expected, $ok, $msg );
}
function cookieDomains() {
return array(
array( false, "org"),
array( false, ".org"),
array( true, "wikipedia.org"),
array( true, ".wikipedia.org"),
array( false, "co.uk" ),
array( false, ".co.uk" ),
array( false, "gov.uk" ),
array( false, ".gov.uk" ),
array( true, "supermarket.uk" ),
array( false, "uk" ),
array( false, ".uk" ),
array( false, "127.0.0." ),
array( false, "127." ),
array( false, "127.0.0.1." ),
array( true, "127.0.0.1" ),
array( false, "333.0.0.1" ),
array( true, "example.com" ),
array( false, "example.com." ),
array( true, ".example.com" ),
array( true, ".example.com", "www.example.com" ),
array( false, "example.com", "www.example.com" ),
array( true, "127.0.0.1", "127.0.0.1" ),
array( false, "127.0.0.1", "localhost" ),
);
}
function testSetCooke() {
$c = new MockCookie( "name", "value",
array(
"domain" => "ac.th",
"path" => "/path/",
) );
$this->assertFalse( $c->canServeDomain( "ac.th" ) );
$c = new MockCookie( "name", "value",
array(
"domain" => "example.com",
"path" => "/path/",
) );
$this->assertTrue( $c->canServeDomain( "example.com" ) );
$this->assertFalse( $c->canServeDomain( "www.example.com" ) );
$c = new MockCookie( "name", "value",
array(
"domain" => ".example.com",
"path" => "/path/",
) );
$this->assertFalse( $c->canServeDomain( "www.example.net" ) );
$this->assertFalse( $c->canServeDomain( "example.com" ) );
$this->assertTrue( $c->canServeDomain( "www.example.com" ) );
$this->assertFalse( $c->canServePath( "/" ) );
$this->assertFalse( $c->canServePath( "/bogus/path/" ) );
$this->assertFalse( $c->canServePath( "/path" ) );
$this->assertTrue( $c->canServePath( "/path/" ) );
$this->assertTrue( $c->isUnExpired() );
$this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.net" ) );
$this->assertEquals( "", $c->serializeToHttpRequest( "/", "www.example.com" ) );
$this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
$c = new MockCookie( "name", "value",
array(
"domain" => "www.example.com",
"path" => "/path/",
) );
$this->assertFalse( $c->canServeDomain( "example.com" ) );
$this->assertFalse( $c->canServeDomain( "www.example.net" ) );
$this->assertTrue( $c->canServeDomain( "www.example.com" ) );
$c = new MockCookie( "name", "value",
array(
"domain" => ".example.com",
"path" => "/path/",
"expires" => "-1 day",
) );
$this->assertFalse( $c->isUnExpired() );
$this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
$c = new MockCookie( "name", "value",
array(
"domain" => ".example.com",
"path" => "/path/",
"expires" => "+1 day",
) );
$this->assertTrue( $c->isUnExpired() );
$this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
}
function testCookieJarSetCookie() {
$cj = new CookieJar;
$cj->setCookie( "name", "value",
array(
"domain" => ".example.com",
"path" => "/path/",
) );
$cj->setCookie( "name2", "value",
array(
"domain" => ".example.com",
"path" => "/path/sub",
) );
$cj->setCookie( "name3", "value",
array(
"domain" => ".example.com",
"path" => "/",
) );
$cj->setCookie( "name4", "value",
array(
"domain" => ".example.net",
"path" => "/path/",
) );
$cj->setCookie( "name5", "value",
array(
"domain" => ".example.net",
"path" => "/path/",
"expires" => "-1 day",
) );
$this->assertEquals( "name4=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
$this->assertEquals( "name3=value", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
$this->assertEquals( "name=value; name3=value", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
$cj->setCookie( "name5", "value",
array(
"domain" => ".example.net",
"path" => "/path/",
"expires" => "+1 day",
) );
$this->assertEquals( "name4=value; name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
$cj->setCookie( "name4", "value",
array(
"domain" => ".example.net",
"path" => "/path/",
"expires" => "-1 day",
) );
$this->assertEquals( "name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
}
function testParseResponseHeader() {
$cj = new CookieJar;
$h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[0], "www.example.com" );
$this->assertEquals( "name4=value", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
$h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[1], "www.example.com" );
$this->assertEquals( "", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
$this->assertEquals( "name4=value2", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
$h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[2], "www.example.com" );
$this->assertEquals( "name4=value2; name5=value3", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
$h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[3], "www.example.com" );
$this->assertEquals( "", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
$h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[4], "www.example.net" );
$this->assertEquals( "", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
$h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
$cj->parseCookieResponseHeader( $h[5], "www.example.net" );
$this->assertEquals( "name6=value4", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
}
function runCookieRequests() {
$r = MWHttpRequest::factory( "http://www.php.net/manual", array( 'followRedirects' => true ) );
$r->execute();
$jar = $r->getCookieJar();
$this->assertThat( $jar, $this->isInstanceOf( 'CookieJar' ) );
$serialized = $jar->serializeToHttpRequest( "/search?q=test", "www.php.net" );
$this->assertRegExp( '/\bCOUNTRY=[^=;]+/', $serialized );
$this->assertRegExp( '/\bLAST_LANG=[^=;]+/', $serialized );
$this->assertEquals( '', $jar->serializeToHttpRequest( "/search?q=test", "www.php.com" ) );
}
function testCookieRequestDefault() {
Http::$httpEngine = false;
$this->runCookieRequests();
}
function testCookieRequestPhp() {
if ( !self::$has_fopen ) {
$this->markTestIncomplete( "This test requires allow_url_fopen=true." );
}
Http::$httpEngine = 'php';
$this->runCookieRequests();
}
function testCookieRequestCurl() {
if ( !self::$has_curl ) {
$this->markTestIncomplete( "This test requires curl." );
}
Http::$httpEngine = 'curl';
$this->runCookieRequests();
}
/**
* Test Http::isValidURI()
* @bug 27854 : Http::isValidURI is to lax
*@dataProvider provideURI */
function testIsValidUri( $expect, $URI, $message = '' ) {
$this->assertEquals(
$expect,
(bool) Http::isValidURI( $URI ),
$message
);
}
/**
* Feeds URI to test a long regular expression in Http::isValidURI
*/
function provideURI() {
/** Format: 'boolean expectation', 'URI to test', 'Optional message' */
return array(
array( false, '¿non sens before!! http://a', 'Allow anything before URI' ),
# (http|https) - only two schemes allowed
array( true, 'http://www.example.org/' ),
array( true, 'https://www.example.org/' ),
array( true, 'http://www.example.org', 'URI without directory' ),
array( true, 'http://a', 'Short name' ),
array( true, 'http://étoile', 'Allow UTF-8 in hostname' ), # 'étoile' is french for 'star'
array( false, '\\host\directory', 'CIFS share' ),
array( false, 'gopher://host/dir', 'Reject gopher scheme' ),
array( false, 'telnet://host', 'Reject telnet scheme' ),
# :\/\/ - double slashes
array( false, 'http//example.org', 'Reject missing colon in protocol' ),
array( false, 'http:/example.org', 'Reject missing slash in protocol' ),
array( false, 'http:example.org', 'Must have two slashes' ),
# Following fail since hostname can be made of anything
array( false, 'http:///example.org', 'Must have exactly two slashes, not three' ),
# (\w+:{0,1}\w*@)? - optional user:pass
array( true, 'http://user@host', 'Username provided' ),
array( true, 'http://user:@host', 'Username provided, no password' ),
array( true, 'http://user:pass@host', 'Username and password provided' ),
# (\S+) - host part is made of anything not whitespaces
array( false, 'http://!"èèè¿¿¿~~\'', 'hostname is made of any non whitespace' ),
array( false, 'http://exam:ple.org/', 'hostname can not use colons!' ),
# (:[0-9]+)? - port number
array( true, 'http://example.org:80/' ),
array( true, 'https://example.org:80/' ),
array( true, 'http://example.org:443/' ),
array( true, 'https://example.org:443/' ),
# Part after the hostname is / or / with something else
array( true, 'http://example/#' ),
array( true, 'http://example/!' ),
array( true, 'http://example/:' ),
array( true, 'http://example/.' ),
array( true, 'http://example/?' ),
array( true, 'http://example/+' ),
array( true, 'http://example/=' ),
array( true, 'http://example/&' ),
array( true, 'http://example/%' ),
array( true, 'http://example/@' ),
array( true, 'http://example/-' ),
array( true, 'http://example//' ),
array( true, 'http://example/&' ),
# Fragment
array( true, 'http://exam#ple.org', ), # This one is valid, really!
array( true, 'http://example.org:80#anchor' ),
array( true, 'http://example.org/?id#anchor' ),
array( true, 'http://example.org/?#anchor' ),
array( false, 'http://a ¿non !!sens after', 'Allow anything after URI' ),
);
}
}
|