WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#11555 closed defect (bug) (fixed)

HTTP Streams transport returns duplicate headers during redirections

Reported by: dd32 Owned by: dd32
Milestone: 3.0 Priority: normal
Severity: normal Version: 2.9
Component: HTTP API Keywords: has-patch
Focuses: Cc:

Description

When using the Streams transport, The Headers returned become doubled up if a redirection takes place.

Example in the case of 3 redirects:

<?php

$url  = "http://localhost/redirect/?rt=2";

$html = wp_remote_get($url, array('max-redirects' => 7));

echo '<pre>';
var_dump($html);

?> 
array(4) {
  ["headers"]=>
  array(7) {
    ["date"]=>
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(29) "Tue, 22 Dec 2009 06:56:52 GMT"
        [1]=>
        string(29) "Tue, 22 Dec 2009 06:56:52 GMT"
      }
      [1]=>
      string(29) "Tue, 22 Dec 2009 06:56:52 GMT"
    }
    ["server"]=>
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(31) "Apache/2.2.14 (Win32) PHP/5.3.0"
        [1]=>
        string(31) "Apache/2.2.14 (Win32) PHP/5.3.0"
      }
      [1]=>
      string(31) "Apache/2.2.14 (Win32) PHP/5.3.0"
    }
    ["x-powered-by"]=>
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(9) "PHP/5.3.0"
        [1]=>
        string(9) "PHP/5.3.0"
      }
      [1]=>
      string(9) "PHP/5.3.0"
    }
    ["location"]=>
    array(2) {
      [0]=>
      string(19) "/redirect/?rt=2&r=1"
      [1]=>
      string(19) "/redirect/?rt=2&r=2"
    }
    ["content-length"]=>
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(2) "15"
        [1]=>
        string(2) "15"
      }
      [1]=>
      string(2) "24"
    }
    ["connection"]=>
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(5) "close"
        [1]=>
        string(5) "close"
      }
      [1]=>
      string(5) "close"
    }
    ["content-type"]=>
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(9) "text/html"
        [1]=>
        string(9) "text/html"
      }
      [1]=>
      string(9) "text/html"
    }
  }
  ["body"]=>
  string(24) "Redirect 2 of 2 is FINAL"
  ["response"]=>
  array(2) {
    ["code"]=>
    string(3) "200"
    ["message"]=>
    string(2) "OK"
  }
  ["cookies"]=>
  array(0) {
  }
}

The first Array element of the response for each header is the previous redirects header... I'll work on a patch in a moment.

Attachments (1)

11555.diff (689 bytes) - added by dd32 4 years ago.

Download all attachments as: .zip

Change History (6)

dd324 years ago

comment:1 dd324 years ago

  • Keywords has-patch added; needs-patch removed

Attahed patch takes an array such as this, returned from $wraper_data, and pulls out the final group of headers:

From:

array(23) {
  [0]=>
  string(18) "HTTP/1.1 302 Found"
  [1]=>
  string(35) "Date: Tue, 22 Dec 2009 07:15:43 GMT"
  [2]=>
  string(39) "Server: Apache/2.2.14 (Win32) PHP/5.3.0"
  [3]=>
  string(23) "X-Powered-By: PHP/5.3.0"
  [4]=>
  string(29) "Location: /redirect/?rt=2&r=1"
  [5]=>
  string(18) "Content-Length: 15"
  [6]=>
  string(17) "Connection: close"
  [7]=>
  string(23) "Content-Type: text/html"
  [8]=>
  string(18) "HTTP/1.1 302 Found"
  [9]=>
  string(35) "Date: Tue, 22 Dec 2009 07:15:43 GMT"
  [10]=>
  string(39) "Server: Apache/2.2.14 (Win32) PHP/5.3.0"
  [11]=>
  string(23) "X-Powered-By: PHP/5.3.0"
  [12]=>
  string(29) "Location: /redirect/?rt=2&r=2"
  [13]=>
  string(18) "Content-Length: 15"
  [14]=>
  string(17) "Connection: close"
  [15]=>
  string(23) "Content-Type: text/html"
  [16]=>
  string(15) "HTTP/1.1 200 OK"
  [17]=>
  string(35) "Date: Tue, 22 Dec 2009 07:15:43 GMT"
  [18]=>
  string(39) "Server: Apache/2.2.14 (Win32) PHP/5.3.0"
  [19]=>
  string(23) "X-Powered-By: PHP/5.3.0"
  [20]=>
  string(18) "Content-Length: 24"
  [21]=>
  string(17) "Connection: close"
  [22]=>
  string(23) "Content-Type: text/html"
}

To:

array(7) {
  [0]=>
  string(15) "HTTP/1.1 200 OK"
  [1]=>
  string(35) "Date: Tue, 22 Dec 2009 07:16:53 GMT"
  [2]=>
  string(39) "Server: Apache/2.2.14 (Win32) PHP/5.3.0"
  [3]=>
  string(23) "X-Powered-By: PHP/5.3.0"
  [4]=>
  string(18) "Content-Length: 24"
  [5]=>
  string(17) "Connection: close"
  [6]=>
  string(23) "Content-Type: text/html"
}

Whilst the headers of the in-between requests can be useful sometimes, The WordPress HTTP API currently doesnt expose them, so discarding these is "OK"

comment:2 dd324 years ago

  • Component changed from General to HTTP

comment:3 hakre4 years ago

  • Keywords reporter-feedback added

dd32 can you just attach your redirect testing script so that other devs can easier test this?

comment:4 dd324 years ago

  • Keywords reporter-feedback removed

You can test this against anything which redirects. that being said, A test redirector is at http://tools.dd32.id.au/redirect/

comment:5 dd324 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [12746]) Fix WP_Http_Streams to return a single set of headers in the event of redirections. Fixes #11555

Note: See TracTickets for help on using tickets.