blob: b7eaa07fa4ad8cdaaae8184ece7fe3677be2ad33 [file] [log] [blame]
Roger Meier33b720a2012-01-24 18:42:46 +00001<?php
2
3/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 *
21 * @package thrift.protocol
22 */
23
24/**
25 * JSON implementation of thrift protocol, ported from Java.
26 */
27class TJSONProtocol extends TProtocol
28{
29 const COMMA = ',';
30 const COLON = ':';
31 const LBRACE = '{';
32 const RBRACE = '}';
33 const LBRACKET = '[';
34 const RBRACKET = ']';
35 const QUOTE = '"';
36 const BACKSLASH = '\\';
37 const ZERO = '0';
38 const ESCSEQ = '\\';
39 const DOUBLEESC = '__DOUBLE_ESCAPE_SEQUENCE__';
40
41 const VERSION = 1;
42
43 public static $JSON_CHAR_TABLE = array(
44 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
45 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, // 0
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
47 1, 1, '"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2
48 );
49
50 public static $ESCAPE_CHARS = array('"', '\\', "b", "f", "n", "r", "t");
51
52 public static $ESCAPE_CHAR_VALS = array(
53 '"', '\\', "\x08", "\f", "\n", "\r", "\t",
54 );
55
56 const NAME_BOOL = "tf";
57 const NAME_BYTE = "i8";
58 const NAME_I16 = "i16";
59 const NAME_I32 = "i32";
60 const NAME_I64 = "i64";
61 const NAME_DOUBLE = "dbl";
62 const NAME_STRUCT = "rec";
63 const NAME_STRING = "str";
64 const NAME_MAP = "map";
65 const NAME_LIST = "lst";
66 const NAME_SET = "set";
67
68 private function getTypeNameForTypeID($typeID)
69 {
70 switch ($typeID) {
71 case TType::BOOL:
72 return self::NAME_BOOL;
73 case TType::BYTE:
74 return self::NAME_BYTE;
75 case TType::I16:
76 return self::NAME_I16;
77 case TType::I32:
78 return self::NAME_I32;
79 case TType::I64:
80 return self::NAME_I64;
81 case TType::DOUBLE:
82 return self::NAME_DOUBLE;
83 case TType::STRING:
84 return self::NAME_STRING;
85 case TType::STRUCT:
86 return self::NAME_STRUCT;
87 case TType::MAP:
88 return self::NAME_MAP;
89 case TType::SET:
90 return self::NAME_SET;
91 case TType::LST:
92 return self::NAME_LIST;
93 default:
94 throw new TProtocolException("Unrecognized type", TProtocolException::UNKNOWN);
95 }
96 }
97
98 private function getTypeIDForTypeName($name)
99 {
100 $result = TType::STOP;
101
102 if (strlen($name) > 1) {
103 switch (substr($name, 0, 1)) {
104 case 'd':
105 $result = TType::DOUBLE;
106 break;
107 case 'i':
108 switch (substr($name, 1, 1)) {
109 case '8':
110 $result = TType::BYTE;
111 break;
112 case '1':
113 $result = TType::I16;
114 break;
115 case '3':
116 $result = TType::I32;
117 break;
118 case '6':
119 $result = TType::I64;
120 break;
121 }
122 break;
123 case 'l':
124 $result = TType::LST;
125 break;
126 case 'm':
127 $result = TType::MAP;
128 break;
129 case 'r':
130 $result = TType::STRUCT;
131 break;
132 case 's':
133 if (substr($name, 1, 1) == 't') {
134 $result = TType::STRING;
135 }
136 else if (substr($name, 1, 1) == 'e') {
137 $result = TType::SET;
138 }
139 break;
140 case 't':
141 $result = TType::BOOL;
142 break;
143 }
144 }
145 if ($result == TType::STOP) {
146 throw new TProtocolException("Unrecognized type", TProtocolException::INVALID_DATA);
147 }
148 return $result;
149 }
150
151 public $contextStack_ = array();
152 public $context_;
153 public $reader_;
154
155 private function pushContext($c) {
156 array_push($this->contextStack_, $this->context_);
157 $this->context_ = $c;
158 }
159
160 private function popContext() {
161 $this->context_ = array_pop($this->contextStack_);
162 }
163
164 public function __construct($trans) {
165 parent::__construct($trans);
166 $this->context_ = new TJSONProtocol_JSONBaseContext();
167 $this->reader_ = new TJSONProtocol_LookaheadReader($this);
168 }
169
170 public function reset() {
171 $this->contextStack_ = array();
172 $this->context_ = new TJSONProtocol_JSONBaseContext();
173 $this->reader_ = new TJSONProtocol_LookaheadReader($this);
174 }
175
176 private $tmpbuf_ = array(4);
177
178 public function readJSONSyntaxChar($b) {
179 $ch = $this->reader_->read();
180
181 if (substr($ch, 0, 1) != $b) {
182 throw new TProtocolException("Unexpected character: " . $ch, TProtocolException::INVALID_DATA);
183 }
184 }
185
186 private function hexVal($s) {
187 for ($i = 0; $i < strlen($s); $i++) {
188 $ch = substr($s, $i, 1);
189
190 if (!($ch >= "a" && $ch <= "f") && !($ch >= "0" && $ch <= "9")) {
191 throw new TProtocolException("Expected hex character " . $ch, TProtocolException::INVALID_DATA);
192 }
193 }
194
195 return hexdec($s);
196 }
197
198 private function hexChar($val) {
199 return dechex($val);
200 }
201
202 private function writeJSONString($b) {
203 $this->context_->write();
204
205 if (is_numeric($b) && $this->context_->escapeNum()) {
206 $this->trans_->write(self::QUOTE);
207 }
208
209 $this->trans_->write(json_encode($b));
210
211 if (is_numeric($b) && $this->context_->escapeNum()) {
212 $this->trans_->write(self::QUOTE);
213 }
214 }
215
216 private function writeJSONInteger($num) {
217 $this->context_->write();
218
219 if ($this->context_->escapeNum()) {
220 $this->trans_->write(self::QUOTE);
221 }
222
223 $this->trans_->write($num);
224
225 if ($this->context_->escapeNum()) {
226 $this->trans_->write(self::QUOTE);
227 }
228 }
229
230 private function writeJSONDouble($num) {
231 $this->context_->write();
232
233 if ($this->context_->escapeNum()) {
234 $this->trans_->write(self::QUOTE);
235 }
236
237 $this->trans_->write(json_encode($num));
238
239 if ($this->context_->escapeNum()) {
240 $this->trans_->write(self::QUOTE);
241 }
242 }
243
244 private function writeJSONBase64($data) {
245 $this->context_->write();
246 $this->trans_->write(self::QUOTE);
247 $this->trans_->write(json_encode(base64_encode($data)));
248 $this->trans_->write(self::QUOTE);
249 }
250
251 private function writeJSONObjectStart() {
252 $this->context_->write();
253 $this->trans_->write(self::LBRACE);
254 $this->pushContext(new TJSONProtocol_JSONPairContext($this));
255 }
256
257 private function writeJSONObjectEnd() {
258 $this->popContext();
259 $this->trans_->write(self::RBRACE);
260 }
261
262 private function writeJSONArrayStart() {
263 $this->context_->write();
264 $this->trans_->write(self::LBRACKET);
265 $this->pushContext(new TJSONProtocol_JSONListContext($this));
266 }
267
268 private function writeJSONArrayEnd() {
269 $this->popContext();
270 $this->trans_->write(self::RBRACKET);
271 }
272
273 private function readJSONString($skipContext) {
274 if (!$skipContext) {
275 $this->context_->read();
276 }
277
278 $jsonString = '';
279 $lastChar = NULL;
280 while (true) {
281 $ch = $this->reader_->read();
282 $jsonString .= $ch;
283 if ($ch == self::QUOTE &&
284 $lastChar !== NULL &&
285 $lastChar !== self::ESCSEQ) {
286 break;
287 }
288 if ($ch == self::ESCSEQ && $lastChar == self::ESCSEQ) {
289 $lastChar = self::DOUBLEESC;
290 } else {
291 $lastChar = $ch;
292 }
293 }
294 return json_decode($jsonString);
295 }
296
297 private function isJSONNumeric($b) {
298 switch ($b) {
299 case '+':
300 case '-':
301 case '.':
302 case '0':
303 case '1':
304 case '2':
305 case '3':
306 case '4':
307 case '5':
308 case '6':
309 case '7':
310 case '8':
311 case '9':
312 case 'E':
313 case 'e':
314 return true;
315 }
316 return false;
317 }
318
319 private function readJSONNumericChars() {
320 $strbld = array();
321
322 while (true) {
323 $ch = $this->reader_->peek();
324
325 if (!$this->isJSONNumeric($ch)) {
326 break;
327 }
328
329 $strbld[] = $this->reader_->read();
330 }
331
332 return implode("", $strbld);
333 }
334
335 private function readJSONInteger() {
336 $this->context_->read();
337
338 if ($this->context_->escapeNum()) {
339 $this->readJSONSyntaxChar(self::QUOTE);
340 }
341
342 $str = $this->readJSONNumericChars();
343
344 if ($this->context_->escapeNum()) {
345 $this->readJSONSyntaxChar(self::QUOTE);
346 }
347
348 if (!is_numeric($str)) {
349 throw new TProtocolException("Invalid data in numeric: " . $str, TProtocolException::INVALID_DATA);
350 }
351
352 return intval($str);
353 }
354
355 /**
356 * Identical to readJSONInteger but without the final cast.
357 * Needed for proper handling of i64 on 32 bit machines. Why a
358 * separate function? So we don't have to force the rest of the
359 * use cases through the extra conditional.
360 */
361 private function readJSONIntegerAsString() {
362 $this->context_->read();
363
364 if ($this->context_->escapeNum()) {
365 $this->readJSONSyntaxChar(self::QUOTE);
366 }
367
368 $str = $this->readJSONNumericChars();
369
370 if ($this->context_->escapeNum()) {
371 $this->readJSONSyntaxChar(self::QUOTE);
372 }
373
374 if (!is_numeric($str)) {
375 throw new TProtocolException("Invalid data in numeric: " . $str, TProtocolException::INVALID_DATA);
376 }
377
378 return $str;
379 }
380
381 private function readJSONDouble() {
382 $this->context_->read();
383
384 if (substr($this->reader_->peek(), 0, 1) == self::QUOTE) {
385 $arr = $this->readJSONString(true);
386
387 if ($arr == "NaN") {
388 return NAN;
389 } else if ($arr == "Infinity") {
390 return INF;
391 } else if (!$this->context_->escapeNum()) {
392 throw new TProtocolException("Numeric data unexpectedly quoted " . $arr,
393 TProtocolException::INVALID_DATA);
394 }
395
396 return floatval($arr);
397 } else {
398 if ($this->context_->escapeNum()) {
399 $this->readJSONSyntaxChar(self::QUOTE);
400 }
401
402 return floatval($this->readJSONNumericChars());
403 }
404 }
405
406 private function readJSONBase64() {
407 $arr = $this->readJSONString(false);
408 $data = base64_decode($arr, true);
409
410 if ($data === false) {
411 throw new TProtocolException("Invalid base64 data " . $arr, TProtocolException::INVALID_DATA);
412 }
413
414 return $data;
415 }
416
417 private function readJSONObjectStart() {
418 $this->context_->read();
419 $this->readJSONSyntaxChar(self::LBRACE);
420 $this->pushContext(new TJSONProtocol_JSONPairContext($this));
421 }
422
423 private function readJSONObjectEnd() {
424 $this->readJSONSyntaxChar(self::RBRACE);
425 $this->popContext();
426 }
427
428 private function readJSONArrayStart()
429 {
430 $this->context_->read();
431 $this->readJSONSyntaxChar(self::LBRACKET);
432 $this->pushContext(new TJSONProtocol_JSONListContext($this));
433 }
434
435 private function readJSONArrayEnd() {
436 $this->readJSONSyntaxChar(self::RBRACKET);
437 $this->popContext();
438 }
439
440 /**
441 * Writes the message header
442 *
443 * @param string $name Function name
444 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
445 * @param int $seqid The sequence id of this message
446 */
447 public function writeMessageBegin($name, $type, $seqid) {
448 $this->writeJSONArrayStart();
449 $this->writeJSONInteger(self::VERSION);
450 $this->writeJSONString($name);
451 $this->writeJSONInteger($type);
452 $this->writeJSONInteger($seqid);
453 }
454
455 /**
456 * Close the message
457 */
458 public function writeMessageEnd() {
459 $this->writeJSONArrayEnd();
460 }
461
462 /**
463 * Writes a struct header.
464 *
465 * @param string $name Struct name
466 * @throws TException on write error
467 * @return int How many bytes written
468 */
469 public function writeStructBegin($name) {
470 $this->writeJSONObjectStart();
471 }
472
473 /**
474 * Close a struct.
475 *
476 * @throws TException on write error
477 * @return int How many bytes written
478 */
479 public function writeStructEnd() {
480 $this->writeJSONObjectEnd();
481 }
482
483 public function writeFieldBegin($fieldName, $fieldType, $fieldId) {
484 $this->writeJSONInteger($fieldId);
485 $this->writeJSONObjectStart();
486 $this->writeJSONString($this->getTypeNameForTypeID($fieldType));
487 }
488
489 public function writeFieldEnd() {
490 $this->writeJsonObjectEnd();
491 }
492
493 public function writeFieldStop() {
494 }
495
496 public function writeMapBegin($keyType, $valType, $size) {
497 $this->writeJSONArrayStart();
498 $this->writeJSONString($this->getTypeNameForTypeID($keyType));
499 $this->writeJSONString($this->getTypeNameForTypeID($valType));
500 $this->writeJSONInteger($size);
501 $this->writeJSONObjectStart();
502 }
503
504 public function writeMapEnd() {
505 $this->writeJSONObjectEnd();
506 $this->writeJSONArrayEnd();
507 }
508
509 public function writeListBegin($elemType, $size) {
510 $this->writeJSONArrayStart();
511 $this->writeJSONString($this->getTypeNameForTypeID($elemType));
512 $this->writeJSONInteger($size);
513 }
514
515 public function writeListEnd() {
516 $this->writeJSONArrayEnd();
517 }
518
519 public function writeSetBegin($elemType, $size) {
520 $this->writeJSONArrayStart();
521 $this->writeJSONString($this->getTypeNameForTypeID($elemType));
522 $this->writeJSONInteger($size);
523 }
524
525 public function writeSetEnd() {
526 $this->writeJSONArrayEnd();
527 }
528
529 public function writeBool($bool) {
530 $this->writeJSONInteger($bool ? 1 : 0);
531 }
532
533 public function writeByte($byte) {
534 $this->writeJSONInteger($byte);
535 }
536
537 public function writeI16($i16) {
538 $this->writeJSONInteger($i16);
539 }
540
541 public function writeI32($i32) {
542 $this->writeJSONInteger($i32);
543 }
544
545 public function writeI64($i64) {
546 $this->writeJSONInteger($i64);
547 }
548
549 public function writeDouble($dub) {
550 $this->writeJSONDouble($dub);
551 }
552
553 public function writeString($str) {
554 $this->writeJSONString($str);
555 }
556
557 /**
558 * Reads the message header
559 *
560 * @param string $name Function name
561 * @param int $type message type TMessageType::CALL or TMessageType::REPLY
562 * @parem int $seqid The sequence id of this message
563 */
564 public function readMessageBegin(&$name, &$type, &$seqid) {
565 $this->readJSONArrayStart();
566
567 if ($this->readJSONInteger() != self::VERSION) {
568 throw new TProtocolException("Message contained bad version", TProtocolException::BAD_VERSION);
569 }
570
571 $name = $this->readJSONString(false);
572 $type = $this->readJSONInteger();
573 $seqid = $this->readJSONInteger();
574
575 return true;
576 }
577
578 /**
579 * Read the close of message
580 */
581 public function readMessageEnd() {
582 $this->readJSONArrayEnd();
583 }
584
585 public function readStructBegin(&$name) {
586 $this->readJSONObjectStart();
587 return 0;
588 }
589
590 public function readStructEnd() {
591 $this->readJSONObjectEnd();
592 }
593
594 public function readFieldBegin(&$name, &$fieldType, &$fieldId) {
595 $ch = $this->reader_->peek();
596 $name = "";
597
598 if (substr($ch, 0, 1) == self::RBRACE) {
599 $fieldType = TType::STOP;
600 } else {
601 $fieldId = $this->readJSONInteger();
602 $this->readJSONObjectStart();
603 $fieldType = $this->getTypeIDForTypeName($this->readJSONString(false));
604 }
605 }
606
607 public function readFieldEnd() {
608 $this->readJSONObjectEnd();
609 }
610
611 public function readMapBegin(&$keyType, &$valType, &$size) {
612 $this->readJSONArrayStart();
613 $keyType = $this->getTypeIDForTypeName($this->readJSONString(false));
614 $valType = $this->getTypeIDForTypeName($this->readJSONString(false));
615 $size = $this->readJSONInteger();
616 $this->readJSONObjectStart();
617 }
618
619 public function readMapEnd() {
620 $this->readJSONObjectEnd();
621 $this->readJSONArrayEnd();
622 }
623
624 public function readListBegin(&$elemType, &$size) {
625 $this->readJSONArrayStart();
626 $elemType = $this->getTypeIDForTypeName($this->readJSONString(false));
627 $size = $this->readJSONInteger();
628 return true;
629 }
630
631 public function readListEnd() {
632 $this->readJSONArrayEnd();
633 }
634
635 public function readSetBegin(&$elemType, &$size) {
636 $this->readJSONArrayStart();
637 $elemType = $this->getTypeIDForTypeName($this->readJSONString(false));
638 $size = $this->readJSONInteger();
639 return true;
640 }
641
642 public function readSetEnd() {
643 $this->readJSONArrayEnd();
644 }
645
646 public function readBool(&$bool) {
647 $bool = $this->readJSONInteger() == 0 ? false : true;
648 return true;
649 }
650
651 public function readByte(&$byte) {
652 $byte = $this->readJSONInteger();
653 return true;
654 }
655
656 public function readI16(&$i16) {
657 $i16 = $this->readJSONInteger();
658 return true;
659 }
660
661 public function readI32(&$i32) {
662 $i32 = $this->readJSONInteger();
663 return true;
664 }
665
666 public function readI64(&$i64) {
667 if ( PHP_INT_SIZE === 4 ) {
668 $i64 = $this->readJSONIntegerAsString();
669 } else {
670 $i64 = $this->readJSONInteger();
671 }
672 return true;
673 }
674
675 public function readDouble(&$dub) {
676 $dub = $this->readJSONDouble();
677 return true;
678 }
679
680 public function readString(&$str) {
681 $str = $this->readJSONString(false);
682 return true;
683 }
684}
685
686/**
687 * JSON Protocol Factory
688 */
689class TJSONProtocolFactory implements TProtocolFactory
690{
691 public function __construct()
692 {
693 }
694
695 public function getProtocol($trans)
696 {
697 return new TJSONProtocol($trans);
698 }
699}
700
701class TJSONProtocol_JSONBaseContext
702{
703 function escapeNum()
704 {
705 return false;
706 }
707
708 function write()
709 {
710 }
711
712 function read()
713 {
714 }
715}
716
717class TJSONProtocol_JSONListContext extends TJSONProtocol_JSONBaseContext
718{
719 private $first_ = true;
720 private $p_;
721
722 public function __construct($p) {
723 $this->p_ = $p;
724 }
725
726 public function write() {
727 if ($this->first_) {
728 $this->first_ = false;
729 } else {
730 $this->p_->getTransport()->write(TJSONProtocol::COMMA);
731 }
732 }
733
734 public function read() {
735 if ($this->first_) {
736 $this->first_ = false;
737 } else {
738 $this->p_->readJSONSyntaxChar(TJSONProtocol::COMMA);
739 }
740 }
741}
742
743class TJSONProtocol_JSONPairContext extends TJSONProtocol_JSONBaseContext {
744 private $first_ = true;
745 private $colon_ = true;
746 private $p_ = null;
747
748 public function __construct($p) {
749 $this->p_ = $p;
750 }
751
752 public function write() {
753 if ($this->first_) {
754 $this->first_ = false;
755 $this->colon_ = true;
756 } else {
757 $this->p_->getTransport()->write($this->colon_ ? TJSONProtocol::COLON : TJSONProtocol::COMMA);
758 $this->colon_ = !$this->colon_;
759 }
760 }
761
762 public function read() {
763 if ($this->first_) {
764 $this->first_ = false;
765 $this->colon_ = true;
766 } else {
767 $this->p_->readJSONSyntaxChar($this->colon_ ? TJSONProtocol::COLON : TJSONProtocol::COMMA);
768 $this->colon_ = !$this->colon_;
769 }
770 }
771
772 public function escapeNum() {
773 return $this->colon_;
774 }
775}
776
777class TJSONProtocol_LookaheadReader
778{
779 private $hasData_ = false;
780 private $data_ = array();
781 private $p_;
782
783 public function __construct($p)
784 {
785 $this->p_ = $p;
786 }
787
788 public function read() {
789 if ($this->hasData_) {
790 $this->hasData_ = false;
791 } else {
792 $this->data_ = $this->p_->getTransport()->readAll(1);
793 }
794
795 return substr($this->data_, 0, 1);
796 }
797
798 public function peek() {
799 if (!$this->hasData_) {
800 $this->data_ = $this->p_->getTransport()->readAll(1);
801 }
802
803 $this->hasData_ = true;
804 return substr($this->data_, 0, 1);
805 }
806}
807
808