/[webpac]/openisis/0.9.9e/php/Isis/Http.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /openisis/0.9.9e/php/Isis/Http.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (hide annotations)
Mon Dec 27 21:49:01 2004 UTC (19 years, 4 months ago) by dpavlin
File size: 5622 byte(s)
import of new openisis release, 0.9.9e

1 dpavlin 604 <?php
2     /*
3     The Malete project - the Z39.2/Z39.50 database framework of OpenIsis.
4     Version 0.9.x (patchlevel see file Version)
5     Copyright (C) 2001-2004 by Erik Grziwotz, erik@openisis.org
6    
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Lesser General Public
9     License as published by the Free Software Foundation; either
10     version 2.1 of the License, or (at your option) any later version.
11    
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     Lesser General Public License for more details.
16    
17     You should have received a copy of the GNU Lesser General Public
18     License along with this library; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20    
21     see README for more information
22     EOH */
23    
24     // $Id: Http.php,v 1.2 2004/11/03 13:56:57 kripke Exp $
25    
26    
27     /**
28     This class represents the connection to a HTTP server.
29     This is not for general purpose, but to transfer Isis records over HTTP.
30    
31     @version $Revision: 1.2 $
32     @license LGPL
33     @package Isis
34     */
35     class Isis_Http extends Isis_Server {
36     var $head;
37     /**
38     whether we need PHP's nutty v%d[] on parameters
39     */
40     var $nuts = 0;
41    
42    
43     /** static
44     convert Isis_Rec to urlencoded parameters.
45     should be a method of Isis_Rec,
46     however, since feeble PHP has to parse all the cruft
47     on every single request, better limit the code to where it's needed.
48     */
49     function toUrl ( $rec, $nuts = 0 )
50     {
51     $res = '';
52     if ($i = $rec->len())
53     for ( $res = '', $t = reset($rec->tag), $v = reset($rec->val); ;
54     $t = next($rec->tag), $v = next($rec->val)
55     ) {
56     if ( $nuts )
57     // use v42[]: the [] so we can send multiple values,
58     // and the v cause numerical named params as []
59     // tend to crash PHP; GET always, POST sometimes works ...
60     // (it's such a feeble ugly pile of shirt)
61     $res .= 'v' . $t . '%5B%5D=' . urlencode($v);
62     else
63     $res .= $t . '=' . urlencode($v);
64     if ( !--$i ) break;
65     $res .= '&';
66     }
67     return $res;
68     }
69    
70    
71     /** static
72     we ARE on the nutty side -- try to turn http request into something usable
73     see toUrl nuts above for the format
74     UNFORTUNATELY we have no way to tell the original order of parameters.
75     hmm ... for get we could resort to parse QUERY_STRING manually
76     @param mixed $db_or_fdt a Isis_Db object or just it's fdt array
77     to look up names.
78     */
79     function fromReq ( $db_or_fdt = null, $server_map = null )
80     {
81     if ( is_object($db_or_fdt) ) {
82     $req = new Isis_Rec( '-db', $db_or_fdt );
83     $fdt = $db_or_fdt->fdt;
84     } else {
85     $req = new Isis_Rec;
86     $fdt = $db_or_fdt; // array or null
87     }
88     if ( $server_map )
89     foreach ($server_map as $k => $v)
90     if (isset($_SERVER[$k]))
91     $req->append( $v, $_SERVER[$k] );
92     foreach (array($_GET,$_POST) as $in)
93     foreach ($in as $k => $v) {
94     if ( $fdt ) {
95     if ( !is_int($k = $fdt[$k]) )
96     continue;
97     } elseif ( is_numeric($k) )
98     $k = (int)$k;
99     elseif ( sscanf( $k, 'v%d', &$i ) )
100     $k = (int)$i;
101     else
102     continue;
103     if (is_string($v))
104     $req->append($k, $v);
105     else foreach ($v as $w) // Wolfsburg
106     $req->append($k, $w);
107     }
108     return $req;
109     }
110    
111    
112     /**
113     pers 1: use pfsockopen and keep-alive
114     pers -1: only keep-alive (for multiple requests within one run)
115     pers 0: neither
116     */
117     function Isis_Http ( $host, $url, $php = 0, $port = 80, $pers = 0 )
118     {
119     $this->host = $host;
120     $this->port = $port;
121     $this->pers = 0 < $pers;
122     $this->head = 'POST '.$url." HTTP/1.1\r\n"
123     . 'Host: '.$host."\r\n"
124     . "User-Agent: Isis\r\n"
125     // try keep-alive. fnord does not support that with cgi
126     . ($pers ? "Connection: keep-alive\r\n" : '')
127     . "Content-Type: application/x-www-form-urlencoded\r\n"
128     . 'Content-Length: ';
129     $this->nuts = $php;
130     $this->open();
131     }
132    
133    
134    
135     function request ( $req )
136     {
137     if ( !$this->sock && !$this->open() )
138     return null;
139     $url = Isis_Http::toUrl( $req, $this->nuts );
140     fwrite($this->sock, $this->head . strlen($url) ."\r\n\r\n" . $url );
141     // as opposed to folklore (http://www.php.net/manual/en/ref.stream.php),
142     // there is NO trailing CRLF in 1.1 (RTFM err ... RFC 2068 4.1 Page 30)
143     fflush($this->sock);
144     $ka = 0; // keepalive -- default for 1.1, explicit for 1.0
145     $ok = ($line = fgets($this->sock))
146     && sscanf( $line, "HTTP/1.%d 200 OK", &$ka );
147     $chunk = 0;
148     while ( ($line = fgets($this->sock)) && trim($line) ) {
149     // echo "---$line---";
150     if ( !strncasecmp('connection: keep-alive', $line, 22) )
151     $ka = $ok;
152     elseif ( !strncasecmp('connection: close', $line, 17) )
153     $ka = 0;
154     elseif ( !strncasecmp('transfer-encoding: chunked', $line, 26) )
155     $chunk = 1;
156     }
157     if ( !$ok )
158     $res = null;
159     else {
160     $txt = '';
161     if ($chunk) {
162     // RFC 2068 3.6 Page 25, 19.4.6 Page 154
163     while ( ($line = fgets($this->sock)) && trim($line)
164     && sscanf( $line, "%x", &$chunk ) && $chunk
165     ) {
166     // error_log( "///chunk size $chunk///\n" );
167     $data = fread( $this->sock, $chunk );
168     $txt .= $data;
169     fread( $this->sock, 2 ); // a CRLF
170     }
171     while ( ($line = fgets($this->sock)) && trim($line) ) // eat footer
172     ; // echo "???$line???\n";
173     } else
174     // HERE we rely on our other end being cooperative,
175     // nicely ending response with blank line
176     while ( ($line = fgets($this->sock)) && trim($line) ) {
177     // echo "+++$line+++";
178     $txt .= $line;
179     }
180     $res = new Isis_Rec;
181     $res->parse($txt, ISIS_REC_TEXT);
182     }
183     if ( !$ka ) {
184     fclose( $this->sock );
185     $this->sock = 0;
186     }
187     return $res;
188     }
189    
190     } // class Isis_Http
191     ?>

  ViewVC Help
Powered by ViewVC 1.1.26