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

  uses(
    'peer.Socket',
    'peer.URL',
    'peer.http.HttpResponse',
    'peer.http.RequestData',
    'peer.Header'
  );

  define('HTTP_GET',     'GET');
  define('HTTP_POST',    'POST');
  define('HTTP_HEAD',    'HEAD');
  
  
  
  /**
   * Wrap HTTP/1.0 and HTTP/1.1 requests (used internally by the 
   * HttpConnection class)
   *
   * @see      xp://peer.http.HttpConnection
   * @see      rfc://2616
   * @purpose  HTTP request
   */
  class HttpRequest extends Object {
    public
      $url        = NULL,
      $method     = HTTP_GET,
      $version    = '1.1',
      $headers    = array(
        'Connection' => 'close'
      ),
      $parameters = array();
      
    /**
     * Constructor
     *
     * @access  public
     * @param   &peer.URL url object
     */
    public function __construct(URL $url) {
      $this->url= $url;
      if ($url->getUser() && $url->getPassword()) {
        $this->headers['Authorization']= 'Basic '.base64_encode($url->getUser().':'.$url->getPassword());
      }
    }
    
    /**
     * Set request method
     *
     * @access  public
     * @param   string method request method, e.g. HTTP_GET
     */
    public function setMethod($method) {
      $this->method= $method;
    }

    /**
     * Set request parameters
     *
     * @access  public
     * @param   mixed p either a string, a PostData object or an associative array
     */
    public function setParameters($p) {
      if (is_a($p, 'RequestData')) {
        $this->parameters= $p;
      } elseif (is_string($p)) {
        parse_str($p, $this->parameters); 
      } else {
        $this->parameters= array_merge($this->url->getParams(), $p);
      }
    }
    
    /**
     * Set header
     *
     * @access  public
     * @param   string k header name
     * @param   string v header value
     */
    public function setHeader($k, $v) {
      $this->headers[$k]= $v;
    }

    /**
     * Add headers
     *
     * @access  public
     * @param   array headers
     */
    public function addHeaders($headers) {
      $this->headers= array_merge($this->headers, $headers);
    }
    
    /**
     * Get request string
     *
     * @access  public
     * @return  string
     */
    public function getRequestString() {
      if (is_a($this->parameters, 'RequestData')) {
        $query= "\0".$this->parameters->getData();
      } else {
        $query= '';
        if (is_array($this->parameters)) foreach ($this->parameters as $k => $v) {
          $query.= '&'.$k.'='.urlencode($v);
        }
      }
      $target= $this->url->getPath('/');
      
      // Which HTTP method? GET and HEAD use query string, POST etc. use
      // body for passing parameters
      switch ($this->method) {
        case HTTP_HEAD:
        case HTTP_GET:
          $target.= empty($query) ? '' : '?'.substr($query, 1);
          $body= '';
          break;
          
        case HTTP_POST:
        default:
          $body= substr($query, 1);
          if (NULL !== $this->url->getQuery()) $target.= '?'.$this->url->getQuery();
          $this->headers['Content-Length']= strlen($body);
          if (empty($this->headers['Content-Type'])) {
            $this->headers['Content-Type']= 'application/x-www-form-urlencoded';
          }
          break;
      }
      
      $request= sprintf(
        "%s %s HTTP/%s\r\nHost: %s\r\n",
        $this->method,
        $target,
        $this->version,
        $this->url->getHost()
      );
      
      // Add request headers
      foreach ($this->headers as $k => $v) {
        $request.= (is_a($v, 'Header') 
          ? $v->toString() 
          : $k.': '.$v
        )."\r\n";
      }
      
      return $request."\r\n".$body;
    }
    
    /**
     * Send request
     *
     * @access  public
     * @return  &peer.http.HttpResponse response object
     */<commit_msg>- Use instanceof keyword instead of is_a() function calls<commit_after><?php
/* This class is part of the XP framework
 * 
 * $Id$
 */

  uses(
    'peer.Socket',
    'peer.URL',
    'peer.http.HttpResponse',
    'peer.http.RequestData',
    'peer.Header'
  );

  define('HTTP_GET',     'GET');
  define('HTTP_POST',    'POST');
  define('HTTP_HEAD',    'HEAD');
  
  
  
  /**
   * Wrap HTTP/1.0 and HTTP/1.1 requests (used internally by the 
   * HttpConnection class)
   *
   * @see      xp://peer.http.HttpConnection
   * @see      rfc://2616
   * @purpose  HTTP request
   */
  class HttpRequest extends Object {
    public
      $url        = NULL,
      $method     = HTTP_GET,
      $version    = '1.1',
      $headers    = array(
        'Connection' => 'close'
      ),
      $parameters = array();
      
    /**
     * Constructor
     *
     * @access  public
     * @param   &peer.URL url object
     */
    public function __construct(URL $url) {
      $this->url= $url;
      if ($url->getUser() && $url->getPassword()) {
        $this->headers['Authorization']= 'Basic '.base64_encode($url->getUser().':'.$url->getPassword());
      }
    }
    
    /**
     * Set request method
     *
     * @access  public
     * @param   string method request method, e.g. HTTP_GET
     */
    public function setMethod($method) {
      $this->method= $method;
    }

    /**
     * Set request parameters
     *
     * @access  public
     * @param   mixed p either a string, a PostData object or an associative array
     */
    public function setParameters($p) {
      if ($p instanceof RequestData) {
        $this->parameters= $p;
      } elseif (is_string($p)) {
        parse_str($p, $this->parameters); 
      } else {
        $this->parameters= array_merge($this->url->getParams(), $p);
      }
    }
    
    /**
     * Set header
     *
     * @access  public
     * @param   string k header name
     * @param   string v header value
     */
    public function setHeader($k, $v) {
      $this->headers[$k]= $v;
    }

    /**
     * Add headers
     *
     * @access  public
     * @param   array headers
     */
    public function addHeaders($headers) {
      $this->headers= array_merge($this->headers, $headers);
    }
    
    /**
     * Get request string
     *
     * @access  public
     * @return  string
     */
    public function getRequestString() {
      if ($this->parameters instanceof RequestData) {
        $query= "\0".$this->parameters->getData();
      } else {
        $query= '';
        if (is_array($this->parameters)) foreach ($this->parameters as $k => $v) {
          $query.= '&'.$k.'='.urlencode($v);
        }
      }
      $target= $this->url->getPath('/');
      
      // Which HTTP method? GET and HEAD use query string, POST etc. use
      // body for passing parameters
      switch ($this->method) {
        case HTTP_HEAD:
        case HTTP_GET:
          $target.= empty($query) ? '' : '?'.substr($query, 1);
          $body= '';
          break;
          
        case HTTP_POST:
        default:
          $body= substr($query, 1);
          if (NULL !== $this->url->getQuery()) $target.= '?'.$this->url->getQuery();
          $this->headers['Content-Length']= strlen($body);
          if (empty($this->headers['Content-Type'])) {
            $this->headers['Content-Type']= 'application/x-www-form-urlencoded';
          }
          break;
      }
      
      $request= sprintf(
        "%s %s HTTP/%s\r\nHost: %s\r\n",
        $this->method,
        $target,
        $this->version,
        $this->url->getHost()
      );
      
      // Add request headers
      foreach ($this->headers as $k => $v) {
        $request.= ($v instanceof Header ? $v->toString() : $k.': '.$v)."\r\n";
      }
      
      return $request."\r\n".$body;
    }
    
    /**
     * Send request
     *
     * @access  public
     * @return  &peer.http.HttpResponse response object
     */
{'fraction_non_alphanumeric': 0.16520076481835563, 'fraction_numerical': 0.0038240917782026767, 'mean_word_length': 1.7568517217146873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '975148', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2454, 'n_words': 548}