Refbase update_2021-01-28_15_58
This commit is contained in:
173
includes/classes/org/active-link/net/HTTPClient.php
Normal file
173
includes/classes/org/active-link/net/HTTPClient.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This file is part of ActiveLink PHP NET Package (www.active-link.com).
|
||||
Copyright (c) 2002-2004 by Zurab Davitiani
|
||||
|
||||
You can contact the author of this software via E-mail at
|
||||
hattrick@mailcan.com
|
||||
|
||||
ActiveLink PHP NET Package is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ActiveLink PHP NET Package is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with ActiveLink PHP NET Package; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/*
|
||||
* requires Socket class
|
||||
*/
|
||||
import("org.active-link.net.Socket");
|
||||
|
||||
/**
|
||||
* HTTPClient class provides HTTP request functionality and ability to retrieve response
|
||||
* @class HTTPClient
|
||||
* @package org.active-link.net
|
||||
* @author Zurab Davitiani
|
||||
* @version 0.4.0
|
||||
* @extends Socket
|
||||
* @requires Socket
|
||||
* @see Socket
|
||||
*/
|
||||
|
||||
class HTTPClient extends Socket {
|
||||
|
||||
// protected properties
|
||||
var $defaultRequestMethod;
|
||||
var $defaultRequestURI;
|
||||
var $defaultRequestVersion;
|
||||
var $defaultRequestUserAgent;
|
||||
var $defaultRequestBody;
|
||||
var $requestMethod;
|
||||
var $requestURI;
|
||||
var $requestVersion;
|
||||
var $requestUserAgent;
|
||||
var $requestHeaders;
|
||||
|
||||
/**
|
||||
* HTTP client class constructor accepts host (required) and port (optional, default 80) arguments
|
||||
* @method HTTPClient
|
||||
* @param string host
|
||||
* @param optional int port
|
||||
*/
|
||||
function HTTPClient($host, $port = 80) {
|
||||
$this->Socket($host, $port);
|
||||
$this->defaultRequestMethod = "GET";
|
||||
$this->defaultRequestURI = "/";
|
||||
$this->defaultRequestVersion = "HTTP/1.0";
|
||||
$this->defaultRequestUserAgent = "ActiveLink NET Object/0.3.3";
|
||||
$this->defaultRequestBody = "";
|
||||
$this->requestMethod = $this->defaultRequestMethod;
|
||||
$this->requestURI = $this->defaultRequestURI;
|
||||
$this->requestVersion = $this->defaultRequestVersion;
|
||||
$this->requestUserAgent = $this->defaultRequestUserAgent;
|
||||
$this->requestBody = $this->defaultRequestBody;
|
||||
$this->requestHeaders = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a supplied raw header to the internal header array
|
||||
* @method addRequestHeaderRaw
|
||||
* @param string header
|
||||
* @returns none
|
||||
*/
|
||||
function addRequestHeaderRaw($header) {
|
||||
$this->requestHeaders[] = $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a string containing all HTTP request headers in their raw form
|
||||
* @method getRequestHeaders
|
||||
* @returns string request HTTP headers
|
||||
*/
|
||||
function getRequestHeaders() {
|
||||
$headers = $this->requestMethod . " " . $this->requestURI . " " . $this->requestVersion . "\r\n";
|
||||
$headers .= "User-Agent: " . $this->requestUserAgent . "\r\n";
|
||||
$headers .= "Host: " . $this->host . "\r\n";
|
||||
foreach($this->requestHeaders as $header) {
|
||||
$headers .= $header . "\r\n";
|
||||
}
|
||||
if($this->requestMethod == "POST") {
|
||||
$contentLength = strlen($this->requestBody);
|
||||
$headers .= "Content-length: " . $contentLength . "\r\n";
|
||||
}
|
||||
$headers .= "Connection: close\r\n\r\n";
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP request body/payload, used only when request method is POST
|
||||
* @method setRequestBody
|
||||
* @param string body
|
||||
* @returns none
|
||||
*/
|
||||
function setRequestBody($body) {
|
||||
$this->requestBody = $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP request method, GET or POST
|
||||
* @method setRequestMethod
|
||||
* @param string method
|
||||
* @returns none
|
||||
*/
|
||||
function setRequestMethod($method) {
|
||||
$this->requestMethod = strtoupper($method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets request URI, if not set here, default will be /
|
||||
* @method setRequestURI
|
||||
* @param string uri
|
||||
* @returns none
|
||||
*/
|
||||
function setRequestURI($uri) {
|
||||
$this->requestURI = $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP request User-Agent to send to the server, default is "ActiveLink NET Object/version"
|
||||
* @method setRequestUserAgent
|
||||
* @param string userAgent
|
||||
* @returns none
|
||||
*/
|
||||
function setRequestUserAgent($userAgent) {
|
||||
$this->setRequestUserAgent = $userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets HTTP protocol version to be used, default is "HTTP/1.0"
|
||||
* @method setRequestVersion
|
||||
* @param string version
|
||||
* @returns none
|
||||
*/
|
||||
function setRequestVersion($version) {
|
||||
$this->requestVersion = $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* After all settings are complete, send the request to the server
|
||||
* @method sendRequest
|
||||
* @returns string server response if successful, false otherwise
|
||||
*/
|
||||
function sendRequest() {
|
||||
$response = false;
|
||||
$request = $this->getRequestHeaders();
|
||||
$request .= $this->requestBody;
|
||||
$success = $this->connect();
|
||||
if($success) {
|
||||
$response = $this->sendReceive($request);
|
||||
$this->disconnect();
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
50
includes/classes/org/active-link/net/HTTPServer.php
Normal file
50
includes/classes/org/active-link/net/HTTPServer.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This file is part of ActiveLink PHP NET Package (www.active-link.com).
|
||||
Copyright (c) 2002-2004 by Zurab Davitiani
|
||||
|
||||
You can contact the author of this software via E-mail at
|
||||
hattrick@mailcan.com
|
||||
|
||||
ActiveLink PHP NET Package is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ActiveLink PHP NET Package is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with ActiveLink PHP NET Package; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/*
|
||||
* requires Socket class
|
||||
*/
|
||||
import("org.active-link.net.Socket");
|
||||
|
||||
/**
|
||||
* HTTPServer class provides functionality to receive HTTP requests and serve responses
|
||||
* @class HTTPServer
|
||||
* @package org.active-link.net
|
||||
* @author Zurab Davitiani
|
||||
* @version 0.4.0
|
||||
* @extends Socket
|
||||
* @requires Socket
|
||||
* @see Socket
|
||||
*/
|
||||
|
||||
class HTTPServer extends Socket {
|
||||
|
||||
// protected properties
|
||||
var $defaultServer;
|
||||
|
||||
function HTTPServer () {
|
||||
$this->defaultServer = "ActiveLink NET Object/0.1";
|
||||
}
|
||||
|
||||
}
|
162
includes/classes/org/active-link/net/Socket.php
Normal file
162
includes/classes/org/active-link/net/Socket.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This file is part of ActiveLink PHP NET Package (www.active-link.com).
|
||||
Copyright (c) 2002-2004 by Zurab Davitiani
|
||||
|
||||
You can contact the author of this software via E-mail at
|
||||
hattrick@mailcan.com
|
||||
|
||||
ActiveLink PHP NET Package is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
ActiveLink PHP NET Package is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with ActiveLink PHP NET Package; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/**
|
||||
* Socket class provides a basic network socket functionality
|
||||
* @class Socket
|
||||
* @package org.active-link.net
|
||||
* @author Zurab Davitiani
|
||||
* @version 0.4.0
|
||||
*/
|
||||
|
||||
class Socket {
|
||||
|
||||
// protected properties
|
||||
var $host;
|
||||
var $port;
|
||||
var $connected;
|
||||
var $connectionID;
|
||||
|
||||
/**
|
||||
* Constructor, accepts host and port, initializes object
|
||||
* @method Socket
|
||||
* @param host
|
||||
* @param port
|
||||
*/
|
||||
function Socket($host, $port) {
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->connected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to host with specified settings, accepts connection timeout (optional, default 30)
|
||||
* @method connect
|
||||
* @param optional int connectionTimeout
|
||||
* @returns true if successful, false otherwise
|
||||
*/
|
||||
function connect($connectTimeout = 30) {
|
||||
$this->connectionID = fsockopen($this->host, $this->port, $errorID, $errorDesc, $connectTimeout);
|
||||
if($this->connectionID === false) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
$this->connected = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects if already connected
|
||||
* @method disconnect
|
||||
* @returns true if successful, false otherwise
|
||||
*/
|
||||
function disconnect() {
|
||||
$success = fclose($this->connectionID);
|
||||
if($success)
|
||||
$this->connected = false;
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives data through connected socket, accepts chunk size (optional, default 4096)
|
||||
* @method receive
|
||||
* @param optional int chunkSize
|
||||
* @returns string received data if successful, false otherwise
|
||||
*/
|
||||
function receive($chunkSize = 4096) {
|
||||
$receivedString = "";
|
||||
$success = false;
|
||||
if($this->connected) {
|
||||
while(!feof($this->connectionID)) {
|
||||
$receivedString .= fgets($this->connectionID, $chunkSize);
|
||||
}
|
||||
$success = true;
|
||||
}
|
||||
if($success)
|
||||
return $receivedString;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends data through connected socket
|
||||
* @method send
|
||||
* @param string sendString
|
||||
* @returns true if successful, false otherwise
|
||||
*/
|
||||
function send($sendString) {
|
||||
$success = false;
|
||||
if($this->connected)
|
||||
$success = fwrite($this->connectionID, $sendString);
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Combination of send and receive methods in one
|
||||
* @method sendReceive
|
||||
* @param sendString
|
||||
* @param optional int connectionTimeout
|
||||
* @returns string received data if successful, false otherwise
|
||||
*/
|
||||
function sendReceive($sendString, $receiveChunkSize = 4096) {
|
||||
$success = true;
|
||||
$receivedString = "";
|
||||
if($this->connected) {
|
||||
$bytesSent = $this->send($sendString);
|
||||
if($bytesSent === false)
|
||||
$success = false;
|
||||
if($success) {
|
||||
$receivedString = $this->receive($receiveChunkSize);
|
||||
if($receivedString === false)
|
||||
$success = false;
|
||||
}
|
||||
}
|
||||
if($success)
|
||||
return $receivedString;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets host to make a connection to
|
||||
* @method setHost
|
||||
* @param string host
|
||||
* @returns none
|
||||
*/
|
||||
function setHost($host) {
|
||||
$this->host = $host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets port to use for the connection
|
||||
* @method setPort
|
||||
* @param int port
|
||||
* @returns none
|
||||
*/
|
||||
function setPort($port) {
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user