Skip to content

Commit f9f769d

Browse files
IMSoPnikic
authored andcommitted
Make http stream wrapper advertise HTTP/1.1 by default
In practice, we always act as an HTTP/1.1 client, for compatibility with servers which ignore protocol version. Sending the version in the request will avoid problems with servers which don't ignore it. HTTP/1.0 can still be forced using a stream context option. Closes GH-5899.
1 parent 9b975fe commit f9f769d

File tree

12 files changed

+77
-69
lines changed

12 files changed

+77
-69
lines changed

UPGRADING

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ PHP 8.0 UPGRADE NOTES
558558
accept parameters by reference will now warn if a callback with reference
559559
parameters is used. Examples include array_filter() and array_reduce().
560560
This was already the case for most, but not all, functions previously.
561+
. The HTTP stream wrapper as used by functions like file_get_contents()
562+
now advertises HTTP/1.1 rather than HTTP/1.0 by default. This does not
563+
change the behaviour of the client, but may cause servers to respond
564+
differently. To retain the old behaviour, set the 'protocol_version'
565+
stream context option, e.g.
566+
567+
$ctx = stream_context_create(['http' => ['protocol_version' => '1.0']]);
568+
echo file_get_contents('http://example.org', false, $ctx);
561569

562570
- Sysvmsg:
563571
. msg_get_queue() will now return an SysvMessageQueue object rather than a

ext/standard/http_fopen_wrapper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
419419
smart_str_appends(&req_buf, "\r\n");
420420
efree(protocol_version);
421421
} else {
422-
smart_str_appends(&req_buf, " HTTP/1.0\r\n");
422+
smart_str_appends(&req_buf, " HTTP/1.1\r\n");
423423
}
424424

425425
if (context && (tmpzval = php_stream_context_get_option(context, "http", "header")) != NULL) {

ext/standard/tests/http/bug38802.phpt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ function do_test($context_options) {
1313
$context = stream_context_create(array('http' => $context_options));
1414

1515
$responses = array(
16-
"data://text/plain,HTTP/1.0 302 Moved Temporarily\r\nLocation: http://127.0.0.1:12342/foo/bar2\r\n\r\n1",
17-
"data://text/plain,HTTP/1.0 301 Moved Permanently\r\nLocation: http://127.0.0.1:12342/foo/bar3\r\n\r\n",
18-
"data://text/plain,HTTP/1.0 302 Moved Temporarily\r\nLocation: http://127.0.0.1:12342/foo/bar4\r\n\r\n3",
19-
"data://text/plain,HTTP/1.0 200 OK\r\n\r\ndone.",
16+
"data://text/plain,HTTP/1.1 302 Moved Temporarily\r\nLocation: http://127.0.0.1:12342/foo/bar2\r\n\r\n1",
17+
"data://text/plain,HTTP/1.1 301 Moved Permanently\r\nLocation: http://127.0.0.1:12342/foo/bar3\r\n\r\n",
18+
"data://text/plain,HTTP/1.1 302 Moved Temporarily\r\nLocation: http://127.0.0.1:12342/foo/bar4\r\n\r\n3",
19+
"data://text/plain,HTTP/1.1 200 OK\r\n\r\ndone.",
2020
);
2121

2222
$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);
@@ -71,34 +71,34 @@ do_test(array('max_redirects' => 2, 'ignore_errors' => 1), 2);
7171
resource(%d) of type (stream)
7272
array(7) {
7373
[0]=>
74-
string(30) "HTTP/1.0 302 Moved Temporarily"
74+
string(30) "HTTP/1.1 302 Moved Temporarily"
7575
[1]=>
7676
string(41) "Location: http://127.0.0.1:12342/foo/bar2"
7777
[2]=>
78-
string(30) "HTTP/1.0 301 Moved Permanently"
78+
string(30) "HTTP/1.1 301 Moved Permanently"
7979
[3]=>
8080
string(41) "Location: http://127.0.0.1:12342/foo/bar3"
8181
[4]=>
82-
string(30) "HTTP/1.0 302 Moved Temporarily"
82+
string(30) "HTTP/1.1 302 Moved Temporarily"
8383
[5]=>
8484
string(41) "Location: http://127.0.0.1:12342/foo/bar4"
8585
[6]=>
86-
string(15) "HTTP/1.0 200 OK"
86+
string(15) "HTTP/1.1 200 OK"
8787
}
8888
string(5) "done."
89-
string(%d) "GET /foo/bar HTTP/1.0
89+
string(%d) "GET /foo/bar HTTP/1.1
9090
Host: 127.0.0.1:12342
9191
Connection: close
9292

93-
GET /foo/bar2 HTTP/1.0
93+
GET /foo/bar2 HTTP/1.1
9494
Host: 127.0.0.1:12342
9595
Connection: close
9696

97-
GET /foo/bar3 HTTP/1.0
97+
GET /foo/bar3 HTTP/1.1
9898
Host: 127.0.0.1:12342
9999
Connection: close
100100

101-
GET /foo/bar4 HTTP/1.0
101+
GET /foo/bar4 HTTP/1.1
102102
Host: 127.0.0.1:12342
103103
Connection: close
104104

@@ -107,11 +107,11 @@ Connection: close
107107

108108
Warning: fopen(http://127.0.0.1:12342/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
109109
bool(false)
110-
string(%d) "GET /foo/bar HTTP/1.0
110+
string(%d) "GET /foo/bar HTTP/1.1
111111
Host: 127.0.0.1:12342
112112
Connection: close
113113

114-
GET /foo/bar2 HTTP/1.0
114+
GET /foo/bar2 HTTP/1.1
115115
Host: 127.0.0.1:12342
116116
Connection: close
117117

@@ -120,7 +120,7 @@ Connection: close
120120

121121
Warning: fopen(http://127.0.0.1:12342/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
122122
bool(false)
123-
string(%d) "GET /foo/bar HTTP/1.0
123+
string(%d) "GET /foo/bar HTTP/1.1
124124
Host: 127.0.0.1:12342
125125
Connection: close
126126

@@ -129,7 +129,7 @@ Connection: close
129129

130130
Warning: fopen(http://127.0.0.1:12342/foo/bar): Failed to open stream: Redirection limit reached, aborting in %s
131131
bool(false)
132-
string(%d) "GET /foo/bar HTTP/1.0
132+
string(%d) "GET /foo/bar HTTP/1.1
133133
Host: 127.0.0.1:12342
134134
Connection: close
135135

@@ -138,12 +138,12 @@ Connection: close
138138
resource(%d) of type (stream)
139139
array(2) {
140140
[0]=>
141-
string(30) "HTTP/1.0 302 Moved Temporarily"
141+
string(30) "HTTP/1.1 302 Moved Temporarily"
142142
[1]=>
143143
string(41) "Location: http://127.0.0.1:12342/foo/bar2"
144144
}
145145
string(1) "1"
146-
string(%d) "GET /foo/bar HTTP/1.0
146+
string(%d) "GET /foo/bar HTTP/1.1
147147
Host: 127.0.0.1:12342
148148
Connection: close
149149

@@ -152,12 +152,12 @@ Connection: close
152152
resource(%d) of type (stream)
153153
array(2) {
154154
[0]=>
155-
string(30) "HTTP/1.0 302 Moved Temporarily"
155+
string(30) "HTTP/1.1 302 Moved Temporarily"
156156
[1]=>
157157
string(41) "Location: http://127.0.0.1:12342/foo/bar2"
158158
}
159159
string(1) "1"
160-
string(%d) "GET /foo/bar HTTP/1.0
160+
string(%d) "GET /foo/bar HTTP/1.1
161161
Host: 127.0.0.1:12342
162162
Connection: close
163163

@@ -166,20 +166,20 @@ Connection: close
166166
resource(%d) of type (stream)
167167
array(4) {
168168
[0]=>
169-
string(30) "HTTP/1.0 302 Moved Temporarily"
169+
string(30) "HTTP/1.1 302 Moved Temporarily"
170170
[1]=>
171171
string(41) "Location: http://127.0.0.1:12342/foo/bar2"
172172
[2]=>
173-
string(30) "HTTP/1.0 301 Moved Permanently"
173+
string(30) "HTTP/1.1 301 Moved Permanently"
174174
[3]=>
175175
string(41) "Location: http://127.0.0.1:12342/foo/bar3"
176176
}
177177
string(0) ""
178-
string(%d) "GET /foo/bar HTTP/1.0
178+
string(%d) "GET /foo/bar HTTP/1.1
179179
Host: 127.0.0.1:12342
180180
Connection: close
181181

182-
GET /foo/bar2 HTTP/1.0
182+
GET /foo/bar2 HTTP/1.1
183183
Host: 127.0.0.1:12342
184184
Connection: close
185185

ext/standard/tests/http/bug48929.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function do_test($context_options) {
1313
$context = stream_context_create(array('http' => $context_options));
1414

1515
$responses = array(
16-
"data://text/plain,HTTP/1.0 200 OK\r\n\r\n",
16+
"data://text/plain,HTTP/1.1 200 OK\r\n\r\n",
1717
);
1818

1919
$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);
@@ -41,7 +41,7 @@ do_test(array('header' => "X-Foo: bar\r\nContent-Type: text/plain", 'method' =>
4141
?>
4242
--EXPECTF--
4343
-- Test: requests with 'header' as array --
44-
string(%d) "POST / HTTP/1.0
44+
string(%d) "POST / HTTP/1.1
4545
Host: 127.0.0.1:12342
4646
Connection: close
4747
Content-Length: 4
@@ -50,7 +50,7 @@ Content-Type: text/plain
5050

5151
ohai"
5252
-- Test: requests with 'header' as string --
53-
string(%d) "POST / HTTP/1.0
53+
string(%d) "POST / HTTP/1.1
5454
Host: 127.0.0.1:12342
5555
Connection: close
5656
Content-Length: 4

ext/standard/tests/http/bug53198.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require 'server.inc';
1212
function do_test() {
1313

1414
$responses = array(
15-
"data://text/plain,HTTP/1.0 200 OK\r\n\r\n",
15+
"data://text/plain,HTTP/1.1 200 OK\r\n\r\n",
1616
);
1717

1818
$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);
@@ -43,14 +43,14 @@ do_test();
4343
?>
4444
--EXPECTF--
4545
-- Test: leave default --
46-
string(%d) "GET / HTTP/1.0
46+
string(%d) "GET / HTTP/1.1
4747
4848
Host: 127.0.0.1:12342
4949
Connection: close
5050

5151
"
5252
-- Test: after ini_set --
53-
string(%d) "GET / HTTP/1.0
53+
string(%d) "GET / HTTP/1.1
5454
5555
Host: 127.0.0.1:12342
5656
Connection: close

ext/standard/tests/http/bug61548.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,84 +42,84 @@ do_test("First:1\nContent-type:text/plain\nSecond:2\nThird:");
4242
?>
4343
Done
4444
--EXPECT--
45-
POST / HTTP/1.0
45+
POST / HTTP/1.1
4646
Host: 127.0.0.1:12342
4747
Connection: close
4848
First:1
4949
Second:2
5050
Content-type: text/plain
5151

52-
GET /foo HTTP/1.0
52+
GET /foo HTTP/1.1
5353
Host: 127.0.0.1:12342
5454
Connection: close
5555
First:1
5656
Second:2
5757

5858

59-
POST / HTTP/1.0
59+
POST / HTTP/1.1
6060
Host: 127.0.0.1:12342
6161
Connection: close
6262
First:1
6363
Second:2
6464
Content-type: text/plain
6565

66-
GET /foo HTTP/1.0
66+
GET /foo HTTP/1.1
6767
Host: 127.0.0.1:12342
6868
Connection: close
6969
First:1
7070
Second:2
7171

7272

73-
POST / HTTP/1.0
73+
POST / HTTP/1.1
7474
Host: 127.0.0.1:12342
7575
Connection: close
7676
First:1
7777
Second:2
7878
Content-type: text/plain
7979
Third:
8080

81-
GET /foo HTTP/1.0
81+
GET /foo HTTP/1.1
8282
Host: 127.0.0.1:12342
8383
Connection: close
8484
First:1
8585
Second:2
8686
Third:
8787

88-
POST / HTTP/1.0
88+
POST / HTTP/1.1
8989
Host: 127.0.0.1:12342
9090
Connection: close
9191
First:1
9292
Content-type:text/plain
9393
Second:2
9494

95-
GET /foo HTTP/1.0
95+
GET /foo HTTP/1.1
9696
Host: 127.0.0.1:12342
9797
Connection: close
9898
First:1
9999
Second:2
100100

101-
POST / HTTP/1.0
101+
POST / HTTP/1.1
102102
Host: 127.0.0.1:12342
103103
Connection: close
104104
First:1
105105
Content-type:text/plain
106106
Second:2
107107

108-
GET /foo HTTP/1.0
108+
GET /foo HTTP/1.1
109109
Host: 127.0.0.1:12342
110110
Connection: close
111111
First:1
112112
Second:2
113113

114-
POST / HTTP/1.0
114+
POST / HTTP/1.1
115115
Host: 127.0.0.1:12342
116116
Connection: close
117117
First:1
118118
Content-type:text/plain
119119
Second:2
120120
Third:
121121

122-
GET /foo HTTP/1.0
122+
GET /foo HTTP/1.1
123123
Host: 127.0.0.1:12342
124124
Connection: close
125125
First:1

ext/standard/tests/http/bug67430.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ do_test(false);
3737
?>
3838
Done
3939
--EXPECT--
40-
POST / HTTP/1.0
40+
POST / HTTP/1.1
4141
Host: 127.0.0.1:12342
4242
Connection: close
4343

44-
GET /foo HTTP/1.0
44+
GET /foo HTTP/1.1
4545
Host: 127.0.0.1:12342
4646
Connection: close
4747

48-
POST / HTTP/1.0
48+
POST / HTTP/1.1
4949
Host: 127.0.0.1:12342
5050
Connection: close
5151

ext/standard/tests/http/bug79265.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ allow_url_fopen=1
99
require 'server.inc';
1010

1111
$responses = array(
12-
"data://text/plain,HTTP/1.0 200 OK\r\n\r\n",
12+
"data://text/plain,HTTP/1.1 200 OK\r\n\r\n",
1313
);
1414

1515
$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);
@@ -32,7 +32,7 @@ http_server_kill($pid);
3232

3333
?>
3434
--EXPECT--
35-
GET / HTTP/1.0
35+
GET / HTTP/1.1
3636
Connection: close
3737
RandomHeader: localhost:8080
3838
Cookie: foo=bar

ext/standard/tests/http/bug79265_2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ allow_url_fopen=1
99
require 'server.inc';
1010

1111
$responses = array(
12-
"data://text/plain,HTTP/1.0 200 OK\r\n\r\n",
12+
"data://text/plain,HTTP/1.1 200 OK\r\n\r\n",
1313
);
1414

1515
$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);
@@ -31,7 +31,7 @@ http_server_kill($pid);
3131

3232
?>
3333
--EXPECT--
34-
GET / HTTP/1.0
34+
GET / HTTP/1.1
3535
Host: 127.0.0.1:12342
3636
Connection: close
3737
RandomHeader: host:8080

0 commit comments

Comments
 (0)