CurlHttpResponse.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Anton E. Lebedevich                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class CurlHttpResponse implements HttpResponse
00016     {
00017         private $headerParser       = null;
00018         private $body               = null;
00019         private $status             = null;
00020         private $maxFileSize        = null;
00021         private $currentFileSize    = null;
00022         
00023         public function __construct()
00024         {
00025             $this->headerParser = HeaderParser::create();
00026             $this->currentFileSize = 0;
00027         }
00028         
00032         public static function create()
00033         {
00034             return new self;
00035         }
00036         
00040         public function writeHeader($resource, $line)
00041         {
00042             $this->headerParser->doLine($line);
00043             
00044             if (
00045                 $this->maxFileSize !== null
00046                 && $this->headerParser->hasHeader('Content-Length')
00047                 && $this->headerParser->getHeader('Content-Length')
00048                     > $this->maxFileSize
00049             )
00050                 return -1; // see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html CURLOPT_HEADERFUNCTION
00051             else
00052                 return strlen($line);
00053         }
00054         
00058         public function writeBody($resource, $body)
00059         {
00060             $this->body .= $body;
00061             $obtained = strlen($body);
00062             
00063             if (
00064                 $this->maxFileSize !== null
00065                 && $this->currentFileSize + $obtained > $this->maxFileSize
00066             ) {
00067                 return -1;
00068             } else {
00069                 $this->currentFileSize += $obtained;
00070                 return $obtained;
00071             }
00072         }
00073         
00078         public function setMaxFileSize($maxFileSize)
00079         {
00080             $this->maxFileSize = $maxFileSize;
00081             return $this;
00082         }
00083         
00087         public function setStatus(HttpStatus $status)
00088         {
00089             $this->status = $status;
00090             return $this;
00091         }
00092         
00096         public function getStatus()
00097         {
00098             return $this->status;
00099         }
00100         
00101         public function getReasonPhrase()
00102         {
00103             throw new UnsupportedMethodException();
00104         }
00105         
00109         public function getHeaders()
00110         {
00111             return $this->headerParser->getHeaders();
00112         }
00113         
00114         public function hasHeader($name)
00115         {
00116             return $this->headerParser->hasHeader($name);
00117         }
00118         
00119         public function getHeader($name)
00120         {
00121             return $this->headerParser->getHeader($name);
00122         }
00123         
00124         public function getBody()
00125         {
00126             return $this->body;
00127         }
00128     }
00129 ?>