Debian Codegraph @

lib/3rdParty/composer/aws/aws-crt-php/src/AWS/CRT/HTTP/Message.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 * SPDX-License-Identifier: Apache-2.0.
5 */
6 namespace AWS\CRT\HTTP;
7
8 use AWS\CRT\NativeResource;
9 use AWS\CRT\Internal\Encoding;
10
11 abstract class Message extends NativeResource {
12 private $method;
13 private $path;
14 private $query;
15 private $headers;
16
17 public function __construct($method, $path, $query = [], $headers = []) {
18 parent::__construct();
19 $this->method = $method;
20 $this->path = $path;
21 $this->query = $query;
22 $this->headers = new Headers($headers);
23 $this->acquire(self::$crt->http_message_new_from_blob(self::marshall($this)));
24 }
25
26 public function __destruct() {
27 self::$crt->http_message_release($this->release());
28 parent::__destruct();
29 }
30
31 public function toBlob() {
32 return self::$crt->http_message_to_blob($this->native);
33 }
34
35 protected static function marshall($msg) {
36 $buf = "";
37 $buf .= Encoding::encodeString($msg->method);
38 $buf .= Encoding::encodeString($msg->pathAndQuery());
39 $buf .= Headers::marshall($msg->headers);
40 return $buf;
41 }
42
43 protected static function _unmarshall($buf, $class=Message::class) {
44 $method = Encoding::readString($buf);
45 $path_and_query = Encoding::readString($buf);
46 $parts = explode("?", $path_and_query, 2);
47 $path = isset($parts[0]) ? $parts[0] : "";
48 $query = isset($parts[1]) ? $parts[1] : "";
49 $headers = Headers::unmarshall($buf);
50
51 // Turn query params back into a dictionary
52 if (strlen($query)) {
53 $query = rawurldecode($query);
54 $query = explode("&", $query);
55 $query = array_reduce($query, function($params, $pair) {
56 list($param, $value) = explode("=", $pair, 2);
57 $params[$param] = $value;
58 return $params;
59 }, []);
60 } else {
61 $query = [];
62 }
63
64 return new $class($method, $path, $query, $headers->toArray());
65 }
66
67 public function pathAndQuery() {
68 $path = $this->path;
69 $queries = [];
70 foreach ($this->query as $param => $value) {
71 $queries []= urlencode($param) . "=" . urlencode($value);
72 }
73 $query = implode("&", $queries);
74 if (strlen($query)) {
75 $path = implode("?", [$path, $query]);
76 }
77 return $path;
78 }
79
80 public function method() {
81 return $this->method;
82 }
83
84 public function path() {
85 return $this->path;
86 }
87
88 public function query() {
89 return $this->query;
90 }
91
92 public function headers() {
93 return $this->headers;
94 }
95 }