Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Message.php « HTTP « CRT « AWS « src « aws-crt-php « aws - github.com/nextcloud/3rdparty.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8c151fe25407136a9cd0e6eaee2fe752a343b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
namespace AWS\CRT\HTTP;

use AWS\CRT\NativeResource;
use AWS\CRT\Internal\Encoding;

abstract class Message extends NativeResource {
    private $method;
    private $path;
    private $query;
    private $headers;

    public function __construct($method, $path, $query = [], $headers = []) {
        parent::__construct();
        $this->method = $method;
        $this->path = $path;
        $this->query = $query;
        $this->headers = new Headers($headers);
        $this->acquire(self::$crt->http_message_new_from_blob(self::marshall($this)));
    }

    public function __destruct() {
        self::$crt->http_message_release($this->release());
        parent::__destruct();
    }

    public function toBlob() {
        return self::$crt->http_message_to_blob($this->native);
    }

    protected static function marshall($msg) {
        $buf = "";
        $buf .= Encoding::encodeString($msg->method);
        $buf .= Encoding::encodeString($msg->pathAndQuery());
        $buf .= Headers::marshall($msg->headers);
        return $buf;
    }

    protected static function _unmarshall($buf, $class=Message::class) {
        $method = Encoding::readString($buf);
        $path_and_query = Encoding::readString($buf);
        $parts = explode("?", $path_and_query, 2);
        $path = isset($parts[0]) ? $parts[0] : "";
        $query = isset($parts[1]) ? $parts[1] : "";
        $headers = Headers::unmarshall($buf);

        // Turn query params back into a dictionary
        if (strlen($query)) {
            $query = rawurldecode($query);
            $query = explode("&", $query);
            $query = array_reduce($query, function($params, $pair) {
                list($param, $value) = explode("=", $pair, 2);
                $params[$param] = $value;
                return $params;
            }, []);
        } else {
            $query = [];
        }

        return new $class($method, $path, $query, $headers->toArray());
    }

    public function pathAndQuery() {
        $path = $this->path;
        $queries = [];
        foreach ($this->query as $param => $value) {
            $queries []= urlencode($param) . "=" . urlencode($value);
        }
        $query = implode("&", $queries);
        if (strlen($query)) {
            $path = implode("?", [$path, $query]);
        }
        return $path;
    }

    public function method() {
        return $this->method;
    }

    public function path() {
        return $this->path;
    }

    public function query() {
        return $this->query;
    }

    public function headers() {
        return $this->headers;
    }
}