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 /* $Id: CurlHttpResponse.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class CurlHttpResponse implements HttpResponse
00017     {
00018         private $headerParser       = null;
00019         private $body               = null;
00020         private $status             = null;
00021         private $maxFileSize        = null;
00022         private $currentFileSize    = null;
00023         
00024         public function __construct()
00025         {
00026             $this->headerParser = HeaderParser::create();
00027             $this->currentFileSize = 0;
00028         }
00029         
00033         public static function create()
00034         {
00035             return new self;
00036         }
00037         
00041         public function writeHeader($resource, $line)
00042         {
00043             $this->headerParser->doLine($line);
00044             
00045             if (
00046                 $this->maxFileSize !== null
00047                 && $this->headerParser->hasHeader('Content-Length')
00048                 && $this->headerParser->getHeader('Content-Length')
00049                     > $this->maxFileSize
00050             )
00051                 return -1; // see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html CURLOPT_HEADERFUNCTION
00052             else
00053                 return strlen($line);
00054         }
00055         
00059         public function writeBody($resource, $body)
00060         {
00061             $this->body .= $body;
00062             $obtained = strlen($body);
00063             
00064             if (
00065                 $this->maxFileSize !== null
00066                 && $this->currentFileSize + $obtained > $this->maxFileSize
00067             ) {
00068                 return -1;
00069             } else {
00070                 $this->currentFileSize += $obtained;
00071                 return $obtained;
00072             }
00073         }
00074         
00079         public function setMaxFileSize($maxFileSize)
00080         {
00081             $this->maxFileSize = $maxFileSize;
00082             return $this;
00083         }
00084         
00088         public function setStatus(HttpStatus $status)
00089         {
00090             $this->status = $status;
00091             return $this;
00092         }
00093         
00097         public function getStatus()
00098         {
00099             return $this->status;
00100         }
00101         
00102         public function getReasonPhrase()
00103         {
00104             throw new UnsupportedMethodException();
00105         }
00106         
00110         public function getHeaders()
00111         {
00112             return $this->headerParser->getHeaders();
00113         }
00114         
00115         public function hasHeader($name)
00116         {
00117             return $this->headerParser->hasHeader($name);
00118         }
00119         
00120         public function getHeader($name)
00121         {
00122             return $this->headerParser->getHeader($name);
00123         }
00124         
00125         public function getBody()
00126         {
00127             return $this->body;
00128         }
00129     }
00130 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4