~ubuntu-branches/ubuntu/wily/phabricator/wily

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

/**
 * Channel to a Mercurial "cmdserver" client. For a detailed description of the
 * "cmdserver" protocol, see @{class:ArcanistHgServerChannel}. This channel
 * implements the other half of the protocol: it decodes messages from the
 * client and encodes messages from the server.
 *
 * Because the proxy server speaks the exact same protocol that Mercurial
 * does and fully decodes both sides of the protocol, we need this half of the
 * decode/encode to talk to clients. Without it, we wouldn't be able to
 * determine when a client request had completed and was ready for transmission
 * to the Mercurial server.
 *
 * (Technically, we could get away without re-encoding messages from the
 * server, but the serialization is not complicated and having a general
 * implementation of encoded/decode for both the client and server dialects
 * seemed useful.)
 *
 * @task protocol Protocol Implementation
 */
final class ArcanistHgClientChannel extends PhutilProtocolChannel {

  const MODE_COMMAND    = 'command';
  const MODE_LENGTH     = 'length';
  const MODE_ARGUMENTS  = 'arguments';

  private $command;
  private $byteLengthOfNextChunk;

  private $buf = '';
  private $mode = self::MODE_COMMAND;


/* -(  Protocol Implementation  )-------------------------------------------- */


  /**
   * Encode a message for transmission to the client. The message should be
   * a pair with the channel name and the a block of data, like this:
   *
   *   array('o', '<some data...>');
   *
   * We encode it like this:
   *
   *   o
   *   1234                # Length, as a 4-byte unsigned long.
   *   <data: 1234 bytes>
   *
   * For a detailed description of the cmdserver protocol, see
   * @{class:ArcanistHgServerChannel}.
   *
   * @param pair<string,string> The <channel, data> pair to encode.
   * @return string Encoded string for transmission to the client.
   *
   * @task protocol
   */
  protected function encodeMessage($argv) {
    if (!is_array($argv) || count($argv) !== 2) {
      throw new Exception('Message should be <channel, data>.');
    }

    $channel = head($argv);
    $data    = last($argv);

    $len = strlen($data);
    $len = pack('N', $len);

    return "{$channel}{$len}{$data}";
  }


  /**
   * Decode a message received from the client. The message looks like this:
   *
   *   runcommand\n
   *   8                   # Length, as a 4-byte unsigned long.
   *   log\0
   *   -l\0
   *   5
   *
   * We decode it into a list in PHP, which looks like this:
   *
   *   array(
   *     'runcommand',
   *     'log',
   *     '-l',
   *     '5',
   *   );
   *
   * @param string Bytes from the server.
   * @return list<list<string>> Zero or more complete commands.
   *
   * @task protocol
   */
  protected function decodeStream($data) {
    $this->buf .= $data;

    // The first part is terminated by "\n", so we don't always know how many
    // bytes we need to look for. This makes parsing a bit of a pain.

    $messages = array();

    do {
      $continue_parsing = false;

      switch ($this->mode) {
        case self::MODE_COMMAND:
          // We're looking for "\n", which indicates the end of the command
          // name, like "runcommand". Next, we'll expect a length.

          $pos = strpos($this->buf, "\n");
          if ($pos === false) {
            break;
          }

          $this->command = substr($this->buf, 0, $pos);
          $this->buf = substr($this->buf, $pos + 1);
          $this->mode = self::MODE_LENGTH;

          $continue_parsing = true;
          break;
        case self::MODE_LENGTH:
          // We're looking for a byte length, as a 4-byte big-endian unsigned
          // integer. Next, we'll expect that many bytes of data.

          if (strlen($this->buf) < 4) {
            break;
          }

          $len = substr($this->buf, 0, 4);
          $len = unpack('N', $len);
          $len = head($len);

          $this->buf = substr($this->buf, 4);

          $this->mode = self::MODE_ARGUMENTS;
          $this->byteLengthOfNextChunk = $len;

          $continue_parsing = true;
          break;
        case self::MODE_ARGUMENTS:
          // We're looking for the data itself, which is a block of bytes
          // of the given length. These are arguments delimited by "\0". Next
          // we'll expect another command.

          if (strlen($this->buf) < $this->byteLengthOfNextChunk) {
            break;
          }

          $data = substr($this->buf, 0, $this->byteLengthOfNextChunk);
          $this->buf = substr($this->buf, $this->byteLengthOfNextChunk);

          $message = array_merge(array($this->command), explode("\0", $data));

          $this->mode = self::MODE_COMMAND;
          $this->command = null;
          $this->byteLengthOfNextChunk = null;

          $messages[] = $message;

          $continue_parsing = true;
          break;
      }
    } while ($continue_parsing);

    return $messages;
  }

}