Ejemplo n.º 1
0
 /**
  * Read and validate the request.
  *
  * @return bool TRUE if valid.
  */
 public function validate()
 {
     $requestLine = $this->httpRequest->getRequestLine();
     if (preg_match(',^GET /[^ ]* HTTP/1.1$,', $requestLine) !== 1) {
         $this->httpResponse->setStatus("400 Bad Request");
     } else {
         if (stristr($this->httpRequest->getHeader("Upgrade"), "websocket") === false) {
             $this->reject("400 Unrecognised Upgrade header");
         } else {
             if (stristr($this->httpRequest->getHeader("Connection"), "Upgrade") === false) {
                 $this->reject("400 Unrecognised Connection header");
             } else {
                 if ($this->httpRequest->getHeader("Sec-WebSocket-Version") !== "13") {
                     $this->reject("400 Unrecognised Sec-WebSocket-Version header");
                 } else {
                     if (($this->key = $this->httpRequest->getHeader("Sec-WebSocket-Key")) === null) {
                         $this->reject("400 Unrecognised Sec-WebSocket-Key");
                     } else {
                         if (($protocols = $this->httpRequest->getHeader("Sec-WebSocket-Protocol")) === null) {
                             $this->reject("400 Unrecognised Sec-WebSocket-Protocol");
                         } else {
                             $this->protocols = explode(',', $protocols);
                             $this->protocol = $this->protocols[0];
                             $this->httpResponse->setStatus("101 Switching Protocols");
                             return true;
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
Ejemplo n.º 2
0
 /** 	 
  * @param HttpRequest $req
  */
 public function applyCookie($req)
 {
     // fetch cookies
     $host = $req->getHeader('host');
     $path = $req->getUrlParam('path');
     $cookies = $this->fetchCookieToSend($host, $path);
     if ($this !== $req) {
         $cookies = array_merge($cookies, $req->fetchCookieToSend($host, $path));
     }
     // add to header
     $req->setHeader('cookie', null);
     foreach (array_chunk(array_values($cookies), 3) as $chunk) {
         $req->addHeader('cookie', implode('; ', $chunk));
     }
 }
Ejemplo n.º 3
0
 private function request($path, $method, $data = "")
 {
     $data = trim($data);
     $httpRequest = new HttpRequest();
     $httpRequest->setOptions(array("useragent" => "Scalr (http://scalr.com)"));
     $fullUrl = "{$this->chefServerUrl}{$path}";
     $chunks = parse_url($fullUrl);
     if ($method == 'POST' && $data) {
         if (is_array($data)) {
             $httpRequest->setPostFields($data);
         } else {
             $httpRequest->setBody($data);
         }
     }
     if ($method == 'PUT' && $data) {
         $httpRequest->setPutData($data);
     }
     $httpRequest->setUrl($fullUrl);
     $httpRequest->setMethod(constant("HTTP_METH_{$method}"));
     $tz = @date_default_timezone_get();
     date_default_timezone_set("UTC");
     $timestamp = date("Y-m-d\\TH:i:s\\Z");
     date_default_timezone_set($tz);
     $chunks['path'] = str_replace('//', '/', $chunks['path']);
     $hashedPath = base64_encode(sha1($chunks['path'], true));
     $hashedBody = base64_encode(sha1($data, true));
     $userId = $this->username;
     $str = "Method:{$method}\n" . "Hashed Path:{$hashedPath}\n" . "X-Ops-Content-Hash:{$hashedBody}\n" . "X-Ops-Timestamp:{$timestamp}\n" . "X-Ops-UserId:{$userId}";
     $headers = array('x-ops-sign' => "algorithm=sha1;version=1.0", 'x-chef-version' => "0.10.8", 'x-ops-userid' => $userId, 'x-ops-timestamp' => $timestamp, 'x-ops-content-hash' => $hashedBody, 'content-type' => 'application/json', 'accept' => 'application/json');
     $r = array_merge($headers, $this->sign($str));
     $httpRequest->addHeaders($r);
     $httpRequest->send();
     if ($httpRequest->getResponseCode() == 401) {
         throw new Exception("Failed to authenticate as {$userId}. Ensure that your node_name and client key are correct.");
     }
     if ($httpRequest->getResponseCode() == 404) {
         throw new Exception("Client not found or parameters are not valid");
     } else {
         if ($httpRequest->getResponseCode() <= 205) {
             $data = $httpRequest->getResponseData();
             $retval = $data['body'] ? json_decode($data['body']) : true;
         } else {
             if ($httpRequest->getResponseCode() >= 300 && $httpRequest->getResponseCode() < 400) {
                 throw new Exception("Request to chef server failed. Chef server returned code {$httpRequest->getResponseCode()}. Redirect URL: {$httpRequest->getHeader("Location")}");
             } else {
                 if ($httpRequest->getResponseCode() > 400) {
                     $data = $httpRequest->getResponseData();
                     $msg = $data['body'] ? json_decode($data['body']) : "";
                     if (is_array($msg->error)) {
                         $msg = $msg->error[0];
                     } elseif ($msg->error) {
                         $msg = $msg->error;
                     } else {
                         $msg = "Unknown error. Error code: {$httpRequest->getResponseCode()}";
                     }
                     throw new Exception("Request to chef server failed with error: {$msg} ({$method} {$path})");
                 } else {
                     throw new Exception("Unexpected situation. Response code {$httpRequest->getResponseCode()}");
                 }
             }
         }
     }
     return $retval;
 }