<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'peer.http.HttpConnection',
    'peer.http.UnexpectedResponseException'
  );
  
  define('REDIRECT_LIMIT',      0xA);

  /**
   * The HttpUtil class provides an easy way to retrieve a complete 
   * URL's contents into a string.
   *
   * Example:
   * <code>
   *   uses('peer.http.HttpUtil');
   *
   *   try {
   *     $buf= HttpUtil::get(new HttpConnection('http://localhost/'));
   *   } catch(UnexpectedResponseException $e) {
   *     $e->printStackTrace();
   *     exit(-1);
   *   }
   *
   *   echo $buf;
   * </code>
   *
   * @see      xp://peer.http.HttpConnection
   * @purpose  Utility class
   */
  class HttpUtil extends Object {
  
    /**
     * Fetch an URL's content. Follows redirects up until the defined 
     * constant REDIRECT_LIMIT times.
     *
     * @param   peer.http.HttpConnection connection
     * @param   array params default array()
     * @param   array headers default array()
     * @return  string
     * @throws  peer.http.UnexpectedResponseException
     */
    public static function get($connection, $params= array(), $headers= array()) {
      $redirected= 0;
      do {
        try {
          $response= $connection->get($params, $headers);
        } catch (Exception $e) {
          throw new UnexpectedResponseException(
            $e->getMessage(),
            -1
          );
        }

        // Check return code
        switch ($sc= $response->getStatusCode()) {
          case 200:             // 200 OK - fetch data
            $content= '';
            while (FALSE !== ($buf= $response->readData())) {
              $content.= $buf;
            }      
            return $content;

          case 301:             // 301 Moved permanently or
          case 302:             // 302 Moved temporarily - redirect
            if (!($loc= $response->getHeader('Location'))) {
              throw new UnexpectedResponseException(
                'Redirect status '.$sc.', but no location header in '.$response->toString(),
                $sc
              );
            }
            if ($redirected >= REDIRECT_LIMIT) {
              throw new UnexpectedResponseException(
                'Redirection limit ('.REDIRECT_LIMIT.') reached @ '.$loc,
                $sc
              );
            }
            $redirected++;
            $connection->request= HttpRequestFactory::factory(new URL($loc));
            break;

          default:              // Any other code
            throw new UnexpectedResponseException(
              'Unexpected answer '.$response->toString(),
              $sc
            );
        }
      } while ($redirected < REDIRECT_LIMIT + 1);
    }
  }
?>
<commit_msg>- Exception -> Throwable<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'peer.http.HttpConnection',
    'peer.http.UnexpectedResponseException'
  );
  
  define('REDIRECT_LIMIT',      0xA);

  /**
   * The HttpUtil class provides an easy way to retrieve a complete 
   * URL's contents into a string.
   *
   * Example:
   * <code>
   *   uses('peer.http.HttpUtil');
   *
   *   try {
   *     $buf= HttpUtil::get(new HttpConnection('http://localhost/'));
   *   } catch(UnexpectedResponseException $e) {
   *     $e->printStackTrace();
   *     exit(-1);
   *   }
   *
   *   echo $buf;
   * </code>
   *
   * @see      xp://peer.http.HttpConnection
   * @purpose  Utility class
   */
  class HttpUtil extends Object {
  
    /**
     * Fetch an URL's content. Follows redirects up until the defined 
     * constant REDIRECT_LIMIT times.
     *
     * @param   peer.http.HttpConnection connection
     * @param   array params default array()
     * @param   array headers default array()
     * @return  string
     * @throws  peer.http.UnexpectedResponseException
     */
    public static function get($connection, $params= array(), $headers= array()) {
      $redirected= 0;
      do {
        try {
          $response= $connection->get($params, $headers);
        } catch (Throwable $e) {
          throw new UnexpectedResponseException(
            $e->getMessage(),
            -1
          );
        }

        // Check return code
        switch ($sc= $response->getStatusCode()) {
          case 200:             // 200 OK - fetch data
            $content= '';
            while (FALSE !== ($buf= $response->readData())) {
              $content.= $buf;
            }      
            return $content;

          case 301:             // 301 Moved permanently or
          case 302:             // 302 Moved temporarily - redirect
            if (!($loc= $response->getHeader('Location'))) {
              throw new UnexpectedResponseException(
                'Redirect status '.$sc.', but no location header in '.$response->toString(),
                $sc
              );
            }
            if ($redirected >= REDIRECT_LIMIT) {
              throw new UnexpectedResponseException(
                'Redirection limit ('.REDIRECT_LIMIT.') reached @ '.$loc,
                $sc
              );
            }
            $redirected++;
            $connection->request= HttpRequestFactory::factory(new URL($loc));
            break;

          default:              // Any other code
            throw new UnexpectedResponseException(
              'Unexpected answer '.$response->toString(),
              $sc
            );
        }
      } while ($redirected < REDIRECT_LIMIT + 1);
    }
  }
?>

{'fraction_non_alphanumeric': 0.12416168207359073, 'fraction_numerical': 0.008337864781584194, 'mean_word_length': 1.6201329534662867, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1994234', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1474, 'n_words': 378}