parse_response - problems parsing HTTP 100 Continue with extra header info
Closed this issue · 4 comments
Thanks for creating this very helpful class!
This is a build on issue #14.
I need to connect to an API that is responding with HTML header information in between the HTTP 100 Continue and the HTTP 200 OK response lines. This results in the $api->header info being incomplete, the $api->response field containing header information and my xml decoder failing for multiple reasons.
I've recommended a potential fix for this issue. Could the parse_response method be updated to handle this peculiar case?
-- Problematic Header --
HTTP/1.1 100 Continue
Via: 1.1 ID-0000605166710030 uproxy-3
HTTP/1.1 200 OK
Date: Wed, 24 Oct 2018 18:20:07 GMT
Accept-Ranges: bytes
Server: Apache
Content-Type: text/xml
Transfer-Encoding: chunked
Connection: close
Via: 1.1 ID-0000605166710030 uproxy-2
-- Potential Fix -- see 3 lines with $http_continue
public function parse_response($response){
$headers = [];
$this->response_status_lines = [];
$http_continue=false;
$line = strtok($response, "\n");
do {
if(strlen(trim($line)) == 0){
// Since we tokenize on \n, use the remaining \r to detect empty lines.
if((count($headers) > 0) && (!$http_continue)) break; // Must be the newline after headers, move on to response body
}
elseif(strpos($line, 'HTTP') === 0){
// One or more HTTP status lines
$this->response_status_lines[] = trim($line);
//HANDLE HTTP 100 Continue
$http_continue = (strpos($line, '100 Continue') !== false );
}
I'm having the same problem when posting messages using Slacks api, it sometimes also return a 100 Continue before the 200 OK.
Hola
L
This was solved with the incorporation of support for multiple HTTP status lines:
https://github.com/tcdent/php-restclient#multiple-http-status-lines