Make return in add location text input boxes commit
[loomclient.git] / Socket.php
blobc254cab4b9929c147a6b452991d876201f121fb5
1 <?php
2 /* $Id: socket.php,v 1.3 2002/09/08 19:25:02 shaggy Exp $ */
4 /*
5 Copyright (c) 2001, 2002 by Martin Tsachev. All rights reserved.
6 http://www.mtweb.org
8 Redistribution and use in source and binary forms,
9 with or without modification, are permitted provided
10 that the conditions available at
11 http://www.opensource.org/licenses/bsd-license.html
12 are met.
15 // From http://www.mtdev.com/download/2002/09/http-keep-alive-connections-in-php/socket.phps
17 Class Socket {
18 var $fp = null;
19 var $host;
20 var $ssl;
21 var $timeout = 30;
22 var $maxlen = 4096;
24 var $accept = 'text/plain'; // MIME types to accept
25 var $lang = 'en'; // Language to accept
27 var $headers; // response headers
28 var $body; // response body
30 var $status; // HTTP status
31 var $connection; // Connection type: close/keep-alive
32 var $te; // Transfer encoding
33 var $type; // returned MIME type
36 function Socket($host, $ssl=false) {
37 $this->host = $host;
38 $this->ssl = $ssl;
42 function setAccept($types) {
43 $this->accept = $types;
47 function connect() {
48 $port = 80;
49 $host = $this->host;
50 if ($this->ssl) {
51 $host = "ssl://$host";
52 $port = 443;
54 $this->fp = fsockopen($host, $port, $errno, $errstr, $this->timeout);
56 if (!$this->fp) {
57 return "Network error: $errstr ($errno)";
60 return 0;
64 function disconnect() {
65 fclose($this->fp);
69 function get($uri) {
70 return $this->getOrPost($uri, 'GET');
73 function post($uri) {
74 return $this->getOrPost($uri, 'POST');
77 function getOrPost($uri, $getOrPost) {
78 $request =
79 "$getOrPost $uri HTTP/1.1\r\n" .
80 "Host: $this->host\r\n" .
81 "Connection: Keep-Alive\r\n" .
82 //"Accept: $this->accept\r\n" .
83 //"Accept-Language: $this->lang\r\n" .
84 //"Accept-Encoding: chunked\r\n" .
85 //"User-Agent: PHP/4.2.1\r\n" .
86 "\r\n";
88 fputs($this->fp, $request);
90 $this->headers = fgets($this->fp, $this->maxlen);
91 if (!$this->headers) { // if disconnected meanwhile
92 $this->connect();
93 fputs($this->fp, $request);
94 $this->headers = fgets($this->fp, $this->maxlen);
97 preg_match('|^HTTP.+? (\d+?) |', $this->headers, $matches);
98 $this->status = $matches[1];
100 $this->type = '';
101 $this->connection = '';
102 $this->te = '';
104 while ($line = fgets($this->fp, $this->maxlen)) {
105 if ($line == "\r\n") { break; }
107 if (preg_match('/^Content-Length: (.+)/', $line, $matches)) {
108 $length = (int) trim($matches[1]);
111 if (preg_match('/^Content-Type: (.+)/', $line, $matches)) {
112 $this->type = strtolower(trim($matches[1]));
115 if (preg_match('/^Connection: (.+)/', $line, $matches)) {
116 $this->connection = strtolower(trim($matches[1]));
119 if (preg_match('/^Transfer-Encoding: (.+)/', $line, $matches)) {
120 $this->te = strtolower(trim($matches[1]));
123 $this->headers .= $line;
126 $this->body = '';
127 if ($this->connection == 'close') {
128 while (!feof($this->fp)) {
129 $this->body .= fread($this->fp, $this->maxlen);
131 return ;
134 if (isset($length) and strpos($this->te, 'chunked') === false) {
135 $this->body = fread($this->fp, $length);
136 return ;
139 // chunked encoding
140 $length = rtrim(fgets($this->fp, $this->maxlen));
141 $length = hexdec($length);
143 while (true) {
144 if ($length < 1) { break; }
145 $this->body .= fread($this->fp, $length);
147 fgets($this->fp, $this->maxlen);
148 $length = rtrim(fgets($this->fp, $this->maxlen));
149 $length = hexdec($length);
152 fgets($this->fp, $this->maxlen);
155 } // class Socket