<?php
/**
* * * * * * * *<4297088@qq.com>
* */
namespace cn\gz53\framework\sdk\http;
use cn\gz53\framework\sdk\Sdk;
class HttpResponseSdk extends Sdk
{
private $isTo = false;
private $response = null;
private $workerId;
private $pushCallback;
private $isWrite = false;
private $isSendFile = false;
private $content;
public function getOnResponseCallback(HttpResponseOnCallbackGetSdkI $po){
$po->setOnReponseCallback([$this, 'onResponse']);
$po->setOnEndCallback([$this, 'onEnd']);
return true;
}
public function getToResponseCallback(HttpResponseToCallbackGetSdkI $po){
$po->setToReponseCallback([$this, 'toResponse']);
$po->setToEndCallback([$this, 'toEnd']);
return true;
}
public function setPushCallback(HttpResponsePushCallbackSetSdkI $po){
$this->pushCallback = $po->getPushCallback();
return true;
}
public function toResponse(){
$this->isTo = true;
return true;
}
public function toEnd(){
$this->isTo = false;
file_put_contents('php://output', $this->content);
flush();
ob_flush();
return true;
}
public function onResponse($reponse, $worker_id){
printf("\n[%s]HttpResponseSdk onResponse, worker_id:%d...", date("Y-m-d H:i:s"), $worker_id);
$this->isTo = false;
$this->workerId = $worker_id;
$this->response = $reponse;
return true;
}
public function onEnd(){
if($this->isWrite){
$this->response->end();
$this->isWrite = false;
}elseif($this->isSendFile){
$this->isSendFile = false;
}else{
$this->response->end($this->content);
}
return true;
}
public function push(HttpResponsePushSdkI $po){
$message = $po->getMessage();
$fd = $po->getFd();
call_user_func($this->pushCallback, $fd, $message);
return true;
}
public function header(HttpResponseHeaderSetSdkI $po){
$ucwords = $po->getUcWords();
$key = $po->getHeaderKey();
$value = $po->getHeaderValue();
if($this->isTo == true){
header($key.': ' . $value);
}else{
$this->response->header($key, $value, $ucwords);
}
return true;
}
public function headers(HttpResponseHeadersSetSdkI $po){
$ucwords = $po->getUcWords();
$headers = $po->getHeaders();
if(is_array($headers)){
foreach ($headers as $key => $value){
if($this->isTo == true){
header($key.': ' . $value);
}else {
$this->response->header($key, $value, $ucwords);
}
}
}
return true;
}
public function cookie(HttpResponseCookieSetSdkI $po){
$key = $po->getCookieKey();
$value = $po->getCookieValue();
$expire = $po->getCookieExpire();
$path = $po->getCookiePath();
$domain = $po->getCookieDomain();
$secure = $po->getCookieSecure();
$http_only = $po->getCookieHttpOnly();
if($this->isTo == true){
setcookie($key, $value, $expire, $path, $domain, $secure, $http_only);
}else {
$this->response->cookie($key, $value, $expire, $path, $domain, $secure, $http_only);
}
return true;
}
public function cookies(HttpResponseCookiesSetSdkI $po){
$values = $po->getCookieValues();
$expire = $po->getCookieExpire();
$path = $po->getCookiePath();
$domain = $po->getCookieDomain();
$secure = $po->getCookieSecure();
$http_only = $po->getCookieHttpOnly();
if(is_array($values)){
foreach ($values as $key => $value) {
if($this->isTo == true){
setcookie($key, $value, $expire, $path, $domain, $secure, $http_only);
}else {
$this->response->cookie($key, $value, $expire, $path, $domain, $secure, $http_only);
}
}
}
return true;
}
public function status(HttpResponseStatusSetSdkI $po){
$status = $po->getStatus();
if($this->isTo == true) {
$this->sendHttpStatus($status);
}else {
$this->response->status($status);
}
return true;
}
public function redirect(HttpResponseRedirectSdkI $po){
$url = $po->getUrl();
$http_code = $po->getHttpCode();
if($this->isTo == true) {
header('Location: '. $url);
$this->sendHttpStatus($http_code);
}else {
$this->response->redirect($url, $http_code);
}
return true;
}
public function sendfile(HttpResponseSendfileSdkI $po){
$this->isSendFile = true;
$filename = $po->getFilename();
$offset = $po->getOffset();
$length = $po->getLength();
if($this->isTo == true) {
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $length);
readfile($filename);
}
}else {
$this->response->sendfile($filename, $offset, $length);
}
return true;
}
public function write(HttpResponseWriteSdkI $po){
$data = $po->getData();
$this->isWrite = true;
$split_length = $po->getSplitLength();
$list = str_split($data, $split_length);
if(!is_array($list)){
$this->sysLog(1, 'str_split list empty', null);
return false;
}
foreach ($list as $key => $str){
if($this->isTo == true) {
file_put_contents('php://output', $str);
flush();
ob_flush();
}else {
$this->response->write($str);
}
}
return true;
}
public function content(HttpResponseContentSdkI $po){
$this->content = $po->getHttpResponseContent();
return true;
}
private function sendHttpStatus($code) {
static $_status = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Moved Temporarily ', 303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
if(isset($_status[$code])) {
header('HTTP/1.1 '.$code.' '.$_status[$code]);
header('Status:'.$code.' '.$_status[$code]);
}
}
}