Project

General

Profile

« Previous | Next » 

Revision 2fe78f3c

Added by Andreas Kohlbecker almost 11 years ago

more consistent use of drupal_http_request() and proxy_content() responds always with HTTP headers + gzip compression

View differences:

7.x/modules/cdm_dataportal/cdm_api/cdm_api.module
680 680
    $post_data = implode(',', $post_data);
681 681

  
682 682
    // For testing.
683
    // TODO use cdm_http_request(..) instead; ( CDM_HTTP_REQUEST_TIMEOUT is already set in there )
683 684
    $data = drupal_http_request($uri, array('headers' => "POST", 'method' => $post_data, 'timeout' => CDM_HTTP_REQUEST_TIMEOUT));
684 685
    // print $data;
685 686
  } else {
......
690 691
    if (empty($hook)) {
691 692
      // simply return the webservice response
692 693
      // Print out JSON, the cache cannot be used since it contains objects.
693
      $http_response = drupal_http_request($uri, array('timeout' => CDM_HTTP_REQUEST_TIMEOUT));
694
      $http_response = cdm_http_request($uri);
694 695
      if (isset($http_response->headers)) {
695 696
        foreach ($http_response->headers as $hname => $hvalue) {
696 697
          drupal_add_http_header($hname, $hvalue);
......
698 699
      }
699 700
      if (isset($http_response->data)) {
700 701
        print $http_response->data;
702
        flush();
701 703
      }
702
      exit();
704
      exit(); // leave drupal here
703 705
    } else {
704 706
      // $hook has been supplied
705 707
      // handle $hook either as compose ot theme hook
......
716 718
      }
717 719

  
718 720
      $obj = cdm_ws_get($uri, NULL, NULL, NULL, TRUE);
721

  
719 722
      $theme_result = NULL;
720 723

  
721 724
      if (function_exists('compose_' . $hook)){
......
767 770
            break;
768 771
        } // END of theme hook switch
769 772
      } // END of tread as theme hook
770
      print $theme_result;
773

  
774
      $compressed = gzencode($theme_result, 2, FORCE_GZIP);
775
      drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
776
      drupal_add_http_header('Content-Length', strlen($compressed));
777
      drupal_add_http_header('Content-Encoding', 'gzip');
778
      print $compressed;
771 779
    } // END of handle $hook either as compose ot theme hook
772 780
  }
773 781
}
......
1408 1416
    //
1409 1417
    $time_get_start = microtime(TRUE);
1410 1418
    // Request data from webservice JSON or XML.
1411
    $datastr = cdm_http_request($uri, $method);
1419
    $response = cdm_http_request($uri, $method);
1420
    $datastr = NULL;
1421
    if (isset($response->data)) {
1422
      $datastr = $response->data;
1423
    }
1412 1424
    $time_get = microtime(TRUE) - $time_get_start;
1413

  
1414 1425
    $time_parse_start = microtime(TRUE);
1415 1426

  
1416 1427
    // Parse data and create object.
......
1529 1540
}
1530 1541

  
1531 1542
/**
1532
 * Do a http request to a CDM webservice.
1543
 * Do a http request to a CDM RESTful web service.
1533 1544
 *
1534 1545
 * @param string $uri
1535 1546
 *   The webservice url.
......
1541 1552
 * @param array $header
1542 1553
 *   The header to include in the request.
1543 1554
 *
1544
 * @return
1545
 *   The response data from the request.
1546
 */
1547
function cdm_http_request($uri, $method = "GET", $parameters = array(), $header = array()) {
1555
 * @return object
1556
 *   The object as returned by drupal_http_request():
1557
 *   An object that can have one or more of the following components:
1558
 *   - request: A string containing the request body that was sent.
1559
 *   - code: An integer containing the response status code, or the error code
1560
 *     if an error occurred.
1561
 *   - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
1562
 *   - status_message: The status message from the response, if a response was
1563
 *     received.
1564
 *   - redirect_code: If redirected, an integer containing the initial response
1565
 *     status code.
1566
 *   - redirect_url: If redirected, a string containing the URL of the redirect
1567
 *     target.
1568
 *   - error: If an error occurred, the error message. Otherwise not set.
1569
 *   - headers: An array containing the response headers as name/value pairs.
1570
 *     HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
1571
 *     easy access the array keys are returned in lower case.
1572
 *   - data: A string containing the response body that was received.
1573
 */
1574
function cdm_http_request($uri, $method = "GET", $parameters = array(), $header = array(), $options = NULL) {
1548 1575
  static $acceptLanguage = NULL;
1549 1576

  
1550 1577
  if (!$acceptLanguage) {
......
1570 1597
    $header['Accept-Charset'] = 'UTF-8';
1571 1598
  }
1572 1599

  
1573
  $response = drupal_http_request($uri, array('headers' => $header, 'method' => $method, 'timeout' => CDM_HTTP_REQUEST_TIMEOUT));
1574
  if (isset($response->data)) {
1575
      return $response->data;
1576
  }
1600
  return drupal_http_request($uri, array(
1601
      'headers' => $header,
1602
      'method' => $method,
1603
      'timeout' => CDM_HTTP_REQUEST_TIMEOUT
1604
      )
1605
   );
1577 1606
}
1578 1607

  
1579 1608
/**

Also available in: Unified diff