summaryrefslogtreecommitdiff
path: root/tests/src/Http/HttpWorker.php
blob: 13fd6c27518fd45a731c60652f60e04dacd3a979 (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
96
97
98
99
100
101
102
103
<?php

/**
 * High-performance PHP process supervisor and load balancer written in Go
 *
 * @author Alex Bond
 */
declare(strict_types=1);

namespace Spiral\RoadRunner\Http;

use Spiral\RoadRunner\WorkerInterface;

class HttpWorker
{
    /** @var WorkerInterface */
    private WorkerInterface $worker;

    /**
     * @param WorkerInterface $worker
     */
    public function __construct(WorkerInterface $worker)
    {
        $this->worker = $worker;
    }

    /**
     * @return WorkerInterface
     */
    public function getWorker(): WorkerInterface
    {
        return $this->worker;
    }

    /**
     * Wait for incoming http request.
     *
     * @return Request|null
     */
    public function waitRequest(): ?Request
    {
        $payload = $this->getWorker()->waitPayload();
        if (empty($payload->body) && empty($payload->header)) {
            // termination request
            return null;
        }

        $request = new Request();
        $request->body = $payload->body;

        $context = json_decode($payload->header, true);
        if ($context === null) {
            // invalid context
            return null;
        }

        $this->hydrateRequest($request, $context);

        return $request;
    }

    /**
     * Send response to the application server.
     *
     * @param int        $status  Http status code
     * @param string     $body    Body of response
     * @param string[][] $headers An associative array of the message's headers. Each
     *                            key MUST be a header name, and each value MUST be an array of strings
     *                            for that header.
     */
    public function respond(int $status, string $body, array $headers = []): void
    {
        if ($headers === []) {
            // this is required to represent empty header set as map and not as array
            $headers = new \stdClass();
        }

        $this->getWorker()->send(
            $body,
            (string) json_encode(['status' => $status, 'headers' => $headers])
        );
    }

    /**
     * @param Request $request
     * @param array   $context
     */
    private function hydrateRequest(Request $request, array $context): void
    {
        $request->remoteAddr = $context['remoteAddr'];
        $request->protocol = $context['protocol'];
        $request->method = $context['method'];
        $request->uri = $context['uri'];
        $request->attributes = $context['attributes'];
        $request->headers = $context['headers'];
        $request->cookies = $context['cookies'];
        $request->uploads = $context['uploads'];
        parse_str($context['rawQuery'], $request->query);

        // indicates that body was parsed
        $request->parsed = $context['parsed'];
    }
}