@@ -32,6 +32,45 @@ public function fromGlobals(): Response
3232 }
3333
3434
35+ public function fromString (string $ message ): Response
36+ {
37+ $ response = new Response ;
38+ $ parts = explode ("\r\n\r\n" , $ message , 2 );
39+ $ headers = explode ("\r\n" , $ parts [0 ]);
40+ $ this ->parseStatus ($ response , array_shift ($ headers ));
41+ $ this ->parseHeaders ($ response , $ headers );
42+ $ response ->setBody ($ parts [1 ] ?? '' );
43+ return $ response ;
44+ }
45+
46+
47+ public function fromUrl (string $ url ): Response
48+ {
49+ $ response = new Response ;
50+ $ response ->setBody (file_get_contents ($ url ));
51+ $ headers = [];
52+ foreach ($ http_response_header as $ header ) {
53+ if (substr ($ header , 0 , 5 ) === 'HTTP/ ' ) {
54+ $ headers = [];
55+ }
56+ $ headers [] = $ header ;
57+ }
58+ $ this ->parseStatus ($ response , array_shift ($ headers ));
59+ $ this ->parseHeaders ($ response , $ headers );
60+ return $ response ;
61+ }
62+
63+
64+ private function parseStatus (Response $ response , string $ status ): void
65+ {
66+ if (!preg_match ('#^HTTP/([\d.]+) (\d+) (.+)$# ' , $ status , $ m )) {
67+ throw new Nette \InvalidArgumentException ("Invalid status line ' $ status'. " );
68+ }
69+ $ response ->setProtocolVersion ($ m [1 ]);
70+ $ response ->setCode ((int ) $ m [2 ], $ m [3 ]);
71+ }
72+
73+
3574 private function parseHeaders (Response $ response , array $ headers ): void
3675 {
3776 foreach ($ headers as $ header ) {
0 commit comments