1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
23 #include "configreader.h"
25 #include "inspsocket.h"
29 /* $ModDesc: Provides a JSON-RPC interface for modules using m_httpd.so */
31 class ModuleRpcJson : public Module
33 void MthModuleVersion (HTTPRequest *http, json::Value &request, json::Value &response)
35 std::string result = "GetVersion().ToString()";
36 response["result"] = result;
39 void system_list_methods (HTTPRequest *http, json::Value &request, json::Value &response)
42 json::Value method_list (json::arrayValue);
44 json::rpc::method_map::iterator it;
45 for (it = json::rpc::methods.begin(); it != json::rpc::methods.end(); ++it)
47 method_list[i] = json::Value (it->first);
51 response["result"] = method_list;
55 ModuleRpcJson(InspIRCd* Me) : Module(Me)
57 ServerInstance->PublishInterface("JSON-RPC", this);
58 json::rpc::add_method ("system.listMethods", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::system_list_methods);
59 json::rpc::add_method ("ircd.moduleVersion", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::MthModuleVersion);
62 void OnEvent(Event* event)
64 std::stringstream data("");
66 if (event->GetEventID() == "httpd_url")
68 HTTPRequest* http = (HTTPRequest*)event->GetData();
70 if (http->GetURI() == "/jsonrpc" && http->GetType() == "POST")
74 std::string response_text;
75 json::rpc::process (http, response_text, http->GetPostData().c_str());
76 data << response_text;
78 catch (std::runtime_error &)
80 data << "{ \"result\": \"JSON Fault\", \"error\": \"Invalid RPC call\", \"id\": 1}";
83 /* Send the document back to m_httpd */
84 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_rpc_json.so\r\n"
85 "Content-Type: application/json; charset=iso-8859-1\r\n");
86 Request req((char*)&response, (Module*)this, event->GetSource());
92 void Implements(char* List)
97 virtual ~ModuleRpcJson()
99 ServerInstance->UnpublishInterface("JSON-RPC", this);
102 virtual Version GetVersion()
104 return Version(0, 1, 0, 0, VF_VENDOR, API_VERSION);
110 ValueIteratorBase::ValueIteratorBase ()
115 ValueIteratorBase::ValueIteratorBase (const Value::ObjectValues::iterator ¤t)
121 ValueIteratorBase::deref () const
123 return current_->second;
128 ValueIteratorBase::increment ()
135 ValueIteratorBase::decrement ()
141 ValueIteratorBase::difference_type
142 ValueIteratorBase::computeDistance (const SelfType &other) const
144 return difference_type (std::distance (current_, other.current_));
149 ValueIteratorBase::isEqual (const SelfType &other) const
151 return current_ == other.current_;
156 ValueIteratorBase::copy (const SelfType &other)
158 current_ = other.current_;
163 ValueIteratorBase::key () const
165 const Value::CZString czstring = (*current_).first;
166 if (czstring.c_str ())
168 if (czstring.isStaticString ())
169 return Value (StaticString (czstring.c_str ()));
170 return Value (czstring.c_str ());
172 return Value (czstring.index ());
177 ValueIteratorBase::index () const
179 const Value::CZString czstring = (*current_).first;
180 if (!czstring.c_str ())
181 return czstring.index ();
182 return unsigned (-1);
187 ValueIteratorBase::memberName () const
189 char const *name = (*current_).first.c_str ();
190 return name ? name : "";
194 ValueConstIterator::ValueConstIterator ()
199 ValueConstIterator::ValueConstIterator (const Value::ObjectValues::iterator ¤t)
200 : ValueIteratorBase (current)
205 ValueConstIterator::operator = (const ValueIteratorBase &other)
212 ValueIterator::ValueIterator ()
217 ValueIterator::ValueIterator (const Value::ObjectValues::iterator ¤t)
218 : ValueIteratorBase (current)
222 ValueIterator::ValueIterator (const ValueConstIterator &other)
223 : ValueIteratorBase (other)
227 ValueIterator::ValueIterator (const ValueIterator &other)
228 : ValueIteratorBase (other)
233 ValueIterator::operator = (const SelfType &other)
243 in (char c, char c1, char c2, char c3, char c4)
245 return c == c1 || c == c2 || c == c3 || c == c4;
249 in (char c, char c1, char c2, char c3, char c4, char c5)
251 return c == c1 || c == c2 || c == c3 || c == c4 || c == c5;
260 Reader::parse (const std::string &document,
263 document_ = document;
264 char const *begin = document_.c_str ();
265 char const *end = begin + document_.length ();
266 return parse (begin, end, root);
270 Reader::parse (std::istream& sin,
274 std::getline (sin, doc, (char)EOF);
275 return parse (doc, root);
279 Reader::parse (char const *beginDoc, char const *endDOc,
288 while (!nodes_.empty ())
292 bool successful = readValue ();
303 while (token.type_ == tokenComment);
304 bool successful = true;
308 case tokenObjectBegin:
309 successful = readObject ();
311 case tokenArrayBegin:
312 successful = readArray ();
315 successful = decodeNumber (token);
318 successful = decodeString (token);
321 currentValue () = true;
324 currentValue () = false;
327 currentValue () = Value ();
330 return addError ("Syntax error: value, object or array expected.", token);
338 Reader::expectToken (TokenType type, Token &token, char const *message)
341 if (token.type_ != type)
342 return addError (message, token);
348 Reader::readToken (Token &token)
351 token.start_ = current_;
352 char c = getNextChar ();
357 token.type_ = tokenObjectBegin;
360 token.type_ = tokenObjectEnd;
363 token.type_ = tokenArrayBegin;
366 token.type_ = tokenArrayEnd;
369 token.type_ = tokenString;
377 case '0': case '1': case '2': case '3':
378 case '4': case '5': case '6': case '7':
382 token.type_ = tokenNumber;
386 token.type_ = tokenTrue;
387 ok = match ("rue", 3);
390 token.type_ = tokenFalse;
391 ok = match ("alse", 4);
394 token.type_ = tokenNull;
395 ok = match ("ull", 3);
398 token.type_ = tokenArraySeparator;
401 token.type_ = tokenMemberSeparator;
404 token.type_ = tokenEndOfStream;
411 token.type_ = tokenError;
412 token.end_ = current_;
418 Reader::skipSpaces ()
420 while (current_ != end_)
423 if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
432 Reader::match (Location pattern, int patternLength)
434 if (end_ - current_ < patternLength)
436 int index = patternLength;
438 if (current_[index] != pattern[index])
440 current_ += patternLength;
446 Reader::readNumber ()
448 while (current_ != end_)
450 if (!(*current_ >= '0' && *current_ <= '9') &&
451 !in (*current_, '.', 'e', 'E', '+', '-'))
458 Reader::readString ()
461 while (current_ != end_)
474 Reader::readObject ()
478 currentValue () = Value (objectValue);
479 while (readToken (tokenName))
481 if (tokenName.type_ == tokenObjectEnd && name.empty ()) // empty object
483 if (tokenName.type_ != tokenString)
487 if (!decodeString (tokenName, name))
488 return recoverFromError (tokenObjectEnd);
491 if (!readToken (colon) || colon.type_ != tokenMemberSeparator)
493 return addErrorAndRecover ("Missing ':' after object member name",
497 Value &value = currentValue ()[ name ];
498 nodes_.push (&value);
499 bool ok = readValue ();
501 if (!ok) // error already set
502 return recoverFromError (tokenObjectEnd);
505 if (!readToken (comma)
506 || (comma.type_ != tokenObjectEnd &&
507 comma.type_ != tokenArraySeparator))
509 return addErrorAndRecover ("Missing ',' or '}' in object declaration",
513 if (comma.type_ == tokenObjectEnd)
516 return addErrorAndRecover ("Missing '}' or object member name",
525 currentValue () = Value (arrayValue);
527 if (*current_ == ']') // empty array
530 readToken (endArray);
536 Value &value = currentValue ()[ index++ ];
537 nodes_.push (&value);
538 bool ok = readValue ();
540 if (!ok) // error already set
541 return recoverFromError (tokenArrayEnd);
544 if (!readToken (token)
545 || (token.type_ != tokenArraySeparator &&
546 token.type_ != tokenArrayEnd))
548 return addErrorAndRecover ("Missing ',' or ']' in array declaration",
552 if (token.type_ == tokenArrayEnd)
560 Reader::decodeNumber (Token &token)
562 bool isDouble = false;
563 for (Location inspect = token.start_; inspect != token.end_; ++inspect)
566 || in (*inspect, '.', 'e', 'E', '+')
567 || (*inspect == '-' && inspect != token.start_);
570 return decodeDouble (token);
571 Location current = token.start_;
572 bool isNegative = *current == '-';
575 unsigned threshold = (isNegative ? unsigned (-Value::minInt)
576 : Value::maxUInt) / 10;
578 while (current < token.end_)
581 if (c < '0' || c > '9')
582 return addError ("'" + std::string (token.start_, token.end_) + "' is not a number.", token);
583 if (value >= threshold)
584 return decodeDouble (token);
585 value = value * 10 + unsigned (c - '0');
588 currentValue () = -int (value);
589 else if (value <= unsigned (Value::maxInt))
590 currentValue () = int (value);
592 currentValue () = value;
598 Reader::decodeDouble (Token &token)
601 const int bufferSize = 32;
603 int length = int (token.end_ - token.start_);
604 if (length <= bufferSize)
606 char buffer[bufferSize];
607 memcpy (buffer, token.start_, length);
609 count = sscanf (buffer, "%lf", &value);
613 std::string buffer (token.start_, token.end_);
614 count = sscanf (buffer.c_str (), "%lf", &value);
618 return addError ("'" + std::string (token.start_, token.end_) + "' is not a number.", token);
619 currentValue () = value;
625 Reader::decodeString (Token &token)
628 if (!decodeString (token, decoded))
630 currentValue () = decoded;
636 Reader::decodeString (Token &token, std::string &decoded)
638 Location current = token.start_ + 1; // skip '"'
639 Location end = token.end_ - 1; // do not include '"'
640 decoded.reserve (long (end - current));
642 while (current != end)
645 if (expect_false (c == '"'))
647 else if (expect_false (c == '\\'))
649 if (expect_false (current == end))
650 return addError ("Empty escape sequence in string", token, current);
651 char escape = *current++;
656 case '\\': decoded += escape; break;
658 case 'b': decoded += '\010'; break;
659 case 't': decoded += '\011'; break;
660 case 'n': decoded += '\012'; break;
661 case 'f': decoded += '\014'; break;
662 case 'r': decoded += '\015'; break;
666 if (!decodeUnicodeEscapeSequence (token, current, end, unicode))
668 // @todo encode unicode as utf8.
669 // @todo remember to alter the writer too.
673 return addError ("Bad escape sequence in string", token, current);
687 Reader::decodeUnicodeEscapeSequence (Token &token,
692 if (end - current < 4)
693 return addError ("Bad unicode escape sequence in string: four digits expected.", token, current);
695 for (int index = 0; index < 4; ++index)
699 if (c >= '0' && c <= '9')
701 else if (c >= 'a' && c <= 'f')
702 unicode += c - 'a' + 10;
703 else if (c >= 'A' && c <= 'F')
704 unicode += c - 'A' + 10;
706 return addError ("Bad unicode escape sequence in string: hexadecimal digit expected.", token, current);
713 Reader::addError (const std::string &message,
719 info.message_ = message;
721 errors_.push_back (info);
727 Reader::recoverFromError (TokenType skipUntilToken)
729 int errorCount = int (errors_.size ());
733 if (!readToken (skip))
734 errors_.resize (errorCount); // discard errors caused by recovery
735 if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream)
738 errors_.resize (errorCount);
744 Reader::addErrorAndRecover (const std::string &message,
746 TokenType skipUntilToken)
748 addError (message, token);
749 return recoverFromError (skipUntilToken);
754 Reader::currentValue ()
756 return *(nodes_.top ());
761 Reader::getNextChar ()
763 if (current_ == end_)
770 Reader::getLocationLineAndColumn (Location location,
774 Location current = begin_;
775 Location lastLineStart = current;
777 while (current < location && current != end_)
782 if (*current == '\n')
784 lastLineStart = current;
789 lastLineStart = current;
793 // column & line start at 1
794 column = int (location - lastLineStart) + 1;
800 Reader::getLocationLineAndColumn (Location location) const
803 getLocationLineAndColumn (location, line, column);
804 char buffer[18+16+16+1];
805 sprintf (buffer, "Line %d, Column %d", line, column);
811 Reader::error_msgs () const
813 std::string formattedMessage;
814 for (Errors::const_iterator itError = errors_.begin ();
815 itError != errors_.end ();
818 const ErrorInfo &error = *itError;
819 formattedMessage += "* " + getLocationLineAndColumn (error.token_.start_) + "\n";
820 formattedMessage += " " + error.message_ + "\n";
822 formattedMessage += "See " + getLocationLineAndColumn (error.extra_) + " for detail.\n";
824 return formattedMessage;
831 unreachable_internal (char const *file, int line, char const *function)
834 snprintf (buf, 1024, "%s (%d) [%s] critical: Unreachable line reached.",
835 file, line, function);
837 throw std::runtime_error (buf);
841 throw_unless_internal (char const *file, int line, char const *function, char const *condition)
844 snprintf (buf, 1024, "%s (%d) [%s] critical: Assertion `%s' failed.",
845 file, line, function, condition);
847 throw std::runtime_error (buf);
851 throw_msg_unless_internal (char const *file, int line, char const *function, char const *message)
854 snprintf (buf, 1024, "%s (%d) [%s] critical: %s.",
855 file, line, function, message);
857 throw std::runtime_error (buf);
860 #define throw_unreachable unreachable_internal (__FILE__, __LINE__, CURFUNC)
861 #define throw_unless(condition) if (!expect_false (condition)) throw_unless_internal (__FILE__, __LINE__, CURFUNC, #condition)
862 #define throw_msg_unless(condition, message) if (!expect_false (condition)) throw_msg_unless_internal (__FILE__, __LINE__, CURFUNC, message)
864 const Value Value::null;
865 const int Value::minInt = int (~ (unsigned (-1)/2));
866 const int Value::maxInt = int (unsigned (-1)/2);
867 const unsigned Value::maxUInt = unsigned (-1);
869 ValueAllocator::~ValueAllocator ()
873 class DefaultValueAllocator : public ValueAllocator
876 virtual ~DefaultValueAllocator ()
880 virtual char *makeMemberName (char const *memberName)
882 return duplicateStringValue (memberName);
885 virtual void releaseMemberName (char *memberName)
887 releaseStringValue (memberName);
890 virtual char *duplicateStringValue (char const *value, unsigned length = unknown)
892 //@todo invesgate this old optimization
894 if (!value || value[0] == 0)
898 if (length == unknown)
899 length = (unsigned)strlen (value);
900 char *newString = static_cast<char *> (malloc (length + 1));
901 memcpy (newString, value, length);
902 newString[length] = 0;
906 virtual void releaseStringValue (char *value)
913 static ValueAllocator *&valueAllocator ()
915 static DefaultValueAllocator defaultAllocator;
916 static ValueAllocator *valueAllocator = &defaultAllocator;
917 return valueAllocator;
920 static struct DummyValueAllocatorInitializer {
921 DummyValueAllocatorInitializer ()
923 valueAllocator (); // ensure valueAllocator () statics are initialized before main ().
925 } dummyValueAllocatorInitializer;
929 Value::CZString::CZString (int index)
935 Value::CZString::CZString (char const *cstr, DuplicationPolicy allocate)
936 : cstr_ (allocate == duplicate ? valueAllocator()->makeMemberName (cstr)
942 Value::CZString::CZString (const CZString &other)
943 : cstr_ (other.index_ != noDuplication && other.cstr_ != 0
944 ? valueAllocator()->makeMemberName (other.cstr_)
946 , index_ (other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
951 Value::CZString::~CZString ()
953 if (cstr_ && index_ == duplicate)
954 valueAllocator()->releaseMemberName (const_cast<char *> (cstr_));
958 Value::CZString::swap (CZString &other)
960 std::swap (cstr_, other.cstr_);
961 std::swap (index_, other.index_);
965 Value::CZString::operator = (const CZString &other)
967 CZString temp (other);
973 Value::CZString::operator < (const CZString &other) const
976 return strcmp (cstr_, other.cstr_) < 0;
977 return index_ < other.index_;
981 Value::CZString::operator == (const CZString &other) const
984 return strcmp (cstr_, other.cstr_) == 0;
985 return index_ == other.index_;
990 Value::CZString::index () const
997 Value::CZString::c_str () const
1003 Value::CZString::isStaticString () const
1005 return index_ == noDuplication;
1009 // class Value::Value
1011 Value::Value (ValueType type)
1031 value_.map_ = new ObjectValues ();
1034 value_.bool_ = false;
1042 Value::Value (int value)
1045 value_.int_ = value;
1049 Value::Value (unsigned value)
1052 value_.uint_ = value;
1055 Value::Value (double value)
1058 value_.real_ = value;
1061 Value::Value (char const *value)
1062 : type_ (stringValue)
1065 value_.string_ = valueAllocator()->duplicateStringValue (value);
1068 Value::Value (const std::string &value)
1069 : type_ (stringValue)
1072 value_.string_ = valueAllocator()->duplicateStringValue (value.c_str (), (unsigned)value.length ());
1075 Value::Value (const StaticString &value)
1076 : type_ (stringValue)
1077 , allocated_ (false)
1079 value_.string_ = const_cast<char *> (value.c_str ());
1083 Value::Value (bool value)
1084 : type_ (booleanValue)
1086 value_.bool_ = value;
1090 Value::Value (const Value &other)
1091 : type_ (other.type_)
1100 value_ = other.value_;
1103 if (other.value_.string_)
1105 value_.string_ = valueAllocator()->duplicateStringValue (other.value_.string_);
1113 value_.map_ = new ObjectValues (*other.value_.map_);
1133 valueAllocator()->releaseStringValue (value_.string_);
1145 Value::operator = (const Value &other)
1153 Value::swap (Value &other)
1155 ValueType temp = type_;
1156 type_ = other.type_;
1158 std::swap (value_, other.value_);
1159 int temp2 = allocated_;
1160 allocated_ = other.allocated_;
1161 other.allocated_ = temp2;
1165 Value::type () const
1171 Value::operator < (const Value &other) const
1173 int typeDelta = type_ - other.type_;
1175 return typeDelta < 0 ? true : false;
1181 return value_.int_ < other.value_.int_;
1183 return value_.uint_ < other.value_.uint_;
1185 return value_.real_ < other.value_.real_;
1187 return value_.bool_ < other.value_.bool_;
1189 return (value_.string_ == 0 && other.value_.string_)
1190 || (other.value_.string_
1192 && strcmp (value_.string_, other.value_.string_) < 0);
1196 int delta = int (value_.map_->size () - other.value_.map_->size ());
1199 return (*value_.map_) < (*other.value_.map_);
1204 return 0; // unreachable
1208 Value::operator <= (const Value &other) const
1210 return !(other > *this);
1214 Value::operator >= (const Value &other) const
1216 return !(*this < other);
1220 Value::operator > (const Value &other) const
1222 return other < *this;
1226 Value::operator == (const Value &other) const
1228 if (type_ != other.type_)
1236 return value_.int_ == other.value_.int_;
1238 return value_.uint_ == other.value_.uint_;
1240 return value_.real_ == other.value_.real_;
1242 return value_.bool_ == other.value_.bool_;
1244 return (value_.string_ == other.value_.string_)
1245 || (other.value_.string_
1247 && strcmp (value_.string_, other.value_.string_) == 0);
1250 return value_.map_->size () == other.value_.map_->size ()
1251 && (*value_.map_) == (*other.value_.map_);
1255 return 0; // unreachable
1259 Value::operator != (const Value &other) const
1261 return !(*this == other);
1264 Value::operator char const * () const
1266 throw_unless (type_ == stringValue);
1267 return value_.string_;
1271 Value::operator std::string () const
1278 return value_.string_ ? value_.string_ : "";
1280 return value_.bool_ ? "true" : "false";
1286 throw_msg_unless (false, "Type is not convertible to string");
1290 return ""; // unreachable
1293 Value::operator int () const
1302 throw_msg_unless (value_.uint_ < (unsigned)maxInt, "integer out of signed integer range");
1303 return value_.uint_;
1305 throw_msg_unless (value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range");
1306 return int (value_.real_);
1308 return value_.bool_ ? 1 : 0;
1312 throw_msg_unless (false, "Type is not convertible to int");
1316 return 0; // unreachable;
1319 Value::operator unsigned () const
1326 throw_msg_unless (value_.int_ >= 0, "Negative integer can not be converted to unsigned integer");
1329 return value_.uint_;
1331 throw_msg_unless (value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range");
1332 return unsigned (value_.real_);
1334 return value_.bool_ ? 1 : 0;
1338 throw_msg_unless (false, "Type is not convertible to uint");
1342 return 0; // unreachable;
1345 Value::operator double () const
1354 return value_.uint_;
1356 return value_.real_;
1358 return value_.bool_ ? 1.0 : 0.0;
1362 throw_msg_unless (false, "Type is not convertible to double");
1366 return 0; // unreachable;
1369 Value::operator bool () const
1377 return value_.int_ != 0;
1379 return value_.real_ != 0.0;
1381 return value_.bool_;
1383 return value_.string_ && value_.string_[0] != 0;
1386 return value_.map_->size () != 0;
1390 return false; // unreachable;
1395 Value::isConvertibleTo (ValueType other) const
1402 return (other == nullValue && value_.int_ == 0)
1403 || other == intValue
1404 || (other == uintValue && value_.int_ >= 0)
1405 || other == realValue
1406 || other == stringValue
1407 || other == booleanValue;
1409 return (other == nullValue && value_.uint_ == 0)
1410 || (other == intValue && value_.uint_ <= (unsigned)maxInt)
1411 || other == uintValue
1412 || other == realValue
1413 || other == stringValue
1414 || other == booleanValue;
1416 return (other == nullValue && value_.real_ == 0.0)
1417 || (other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt)
1418 || (other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt)
1419 || other == realValue
1420 || other == stringValue
1421 || other == booleanValue;
1423 return (other == nullValue && value_.bool_ == false)
1424 || other == intValue
1425 || other == uintValue
1426 || other == realValue
1427 || other == stringValue
1428 || other == booleanValue;
1430 return other == stringValue
1431 || (other == nullValue && (!value_.string_ || value_.string_[0] == 0));
1433 return other == arrayValue
1434 || (other == nullValue && value_.map_->size () == 0);
1436 return other == objectValue
1437 || (other == nullValue && value_.map_->size () == 0);
1441 return false; // unreachable;
1445 /// Number of values in array or object
1447 Value::size () const
1458 case arrayValue: // size of the array is highest index + 1
1459 if (!value_.map_->empty ())
1461 ObjectValues::const_iterator itLast = value_.map_->end ();
1463 return itLast->first.index ()+1;
1467 return int (value_.map_->size ());
1471 return 0; // unreachable;
1476 Value::empty () const
1478 if (isNull () || isArray () || isObject ())
1479 return size () == 0u;
1486 Value::operator ! () const
1495 throw_unless (type_ == nullValue || type_ == arrayValue || type_ == objectValue);
1501 value_.map_->clear ();
1509 Value::resize (unsigned newSize)
1511 throw_unless (type_ == nullValue || type_ == arrayValue);
1512 if (type_ == nullValue)
1513 *this = Value (arrayValue);
1514 unsigned oldSize = size ();
1517 else if (newSize > oldSize)
1518 (*this)[ newSize - 1 ];
1521 for (unsigned index = newSize; index < oldSize; ++index)
1522 value_.map_->erase (index);
1523 throw_unless (size () == newSize);
1529 Value::operator [] (int index)
1531 return operator [] (static_cast<unsigned> (index));
1536 Value::operator [] (unsigned index)
1538 throw_unless (type_ == nullValue || type_ == arrayValue);
1539 if (type_ == nullValue)
1540 *this = Value (arrayValue);
1541 CZString key (index);
1542 ObjectValues::iterator it = value_.map_->lower_bound (key);
1543 if (it != value_.map_->end () && it->first == key)
1546 ObjectValues::value_type defaultValue (key, null);
1547 it = value_.map_->insert (it, defaultValue);
1553 Value::operator [] (int index) const
1555 return operator [] (static_cast<unsigned> (index));
1560 Value::operator [] (unsigned index) const
1562 throw_unless (type_ == nullValue || type_ == arrayValue);
1563 if (type_ == nullValue)
1565 CZString key (index);
1566 ObjectValues::const_iterator it = value_.map_->find (key);
1567 if (it == value_.map_->end ())
1574 Value::operator [] (char const *key)
1576 return resolveReference (key, false);
1581 Value::resolveReference (char const *key, bool isStatic)
1583 throw_unless (type_ == nullValue || type_ == objectValue);
1584 if (type_ == nullValue)
1585 *this = Value (objectValue);
1586 CZString actualKey (key, isStatic ? CZString::noDuplication
1587 : CZString::duplicateOnCopy);
1588 ObjectValues::iterator it = value_.map_->lower_bound (actualKey);
1589 if (it != value_.map_->end () && it->first == actualKey)
1592 ObjectValues::value_type defaultValue (actualKey, null);
1593 it = value_.map_->insert (it, defaultValue);
1594 Value &value = it->second;
1600 Value::get (int index, const Value &defaultValue) const
1602 return get (static_cast<unsigned> (index), defaultValue);
1607 Value::get (unsigned index, const Value &defaultValue) const
1609 const Value *value = &((*this)[index]);
1610 return value == &null ? defaultValue : *value;
1615 Value::isValidIndex (int index) const
1617 return isValidIndex (static_cast<unsigned> (index));
1622 Value::isValidIndex (unsigned index) const
1624 return index < size ();
1630 Value::operator [] (char const *key) const
1632 throw_unless (type_ == nullValue || type_ == objectValue);
1633 if (type_ == nullValue)
1635 CZString actualKey (key, CZString::noDuplication);
1636 ObjectValues::const_iterator it = value_.map_->find (actualKey);
1637 if (it == value_.map_->end ())
1644 Value::operator [] (const std::string &key)
1646 return (*this)[ key.c_str () ];
1651 Value::operator [] (const std::string &key) const
1653 return (*this)[ key.c_str () ];
1657 Value::operator [] (const StaticString &key)
1659 return resolveReference (key, true);
1664 Value::append (const Value &value)
1666 return (*this)[size ()] = value;
1671 Value::get (char const *key, const Value &defaultValue) const
1673 const Value *value = &((*this)[key]);
1674 return value == &null ? defaultValue : *value;
1679 Value::get (const std::string &key, const Value &defaultValue) const
1681 return get (key.c_str (), defaultValue);
1685 Value::removeMember (char const *key)
1687 throw_unless (type_ == nullValue || type_ == objectValue);
1688 if (type_ == nullValue)
1690 CZString actualKey (key, CZString::noDuplication);
1691 ObjectValues::iterator it = value_.map_->find (actualKey);
1692 if (it == value_.map_->end ())
1694 Value old (it->second);
1695 value_.map_->erase (it);
1700 Value::removeMember (const std::string &key)
1702 return removeMember (key.c_str ());
1706 Value::isMember (char const *key) const
1708 const Value *value = &((*this)[key]);
1709 return value != &null;
1714 Value::isMember (const std::string &key) const
1716 return isMember (key.c_str ());
1721 Value::getMemberNames () const
1723 throw_unless (type_ == nullValue || type_ == objectValue);
1724 if (type_ == nullValue)
1725 return Value::Members ();
1727 members.reserve (value_.map_->size ());
1728 ObjectValues::const_iterator it;
1729 ObjectValues::const_iterator itEnd = value_.map_->end ();
1730 for (it = value_.map_->begin (); it != itEnd; ++it)
1731 members.push_back (std::string (it->first.c_str()));
1736 Value::isNull () const
1738 return type_ == nullValue;
1743 Value::isBool () const
1745 return type_ == booleanValue;
1750 Value::isInt () const
1752 return type_ == intValue;
1757 Value::isUInt () const
1759 return type_ == uintValue;
1764 Value::isIntegral () const
1766 return type_ == intValue
1767 || type_ == uintValue
1768 || type_ == booleanValue;
1773 Value::isDouble () const
1775 return type_ == realValue;
1780 Value::isNumeric () const
1782 return isIntegral () || isDouble ();
1787 Value::isString () const
1789 return type_ == stringValue;
1794 Value::isArray () const
1796 return type_ == nullValue || type_ == arrayValue;
1801 Value::isObject () const
1803 return type_ == nullValue || type_ == objectValue;
1807 Value::const_iterator
1808 Value::begin () const
1815 return const_iterator (value_.map_->begin ());
1820 return const_iterator ();
1823 Value::const_iterator
1831 return const_iterator (value_.map_->end ());
1836 return const_iterator ();
1848 return iterator (value_.map_->begin ());
1864 return iterator (value_.map_->end ());
1875 static void uintToString (unsigned value,
1881 *--current = (value % 10) + '0';
1887 std::string valueToString (int value)
1890 char *current = buffer + sizeof (buffer);
1891 bool isNegative = value < 0;
1894 uintToString (unsigned (value), current);
1897 throw_unless (current >= buffer);
1902 std::string valueToString (unsigned value)
1905 char *current = buffer + sizeof (buffer);
1906 uintToString (value, current);
1907 throw_unless (current >= buffer);
1911 std::string valueToString (double value)
1914 sprintf (buffer, "%.16g", value);
1919 std::string valueToString (bool value)
1921 return value ? "true" : "false";
1924 std::string valueToQuotedString (char const *value)
1926 // Not sure how to handle unicode...
1927 if (std::strpbrk (value, "\"\\\b\f\n\r\t") == NULL)
1928 return std::string ("\"") + value + "\"";
1929 // We have to walk value and escape any special characters.
1930 // Appending to std::string is not efficient, but this should be rare.
1931 // (Note: forward slashes are *not* rare, but I am not escaping them.)
1932 unsigned maxsize = strlen (value) * 2 + 3; // allescaped+quotes+NULL
1934 result.reserve (maxsize); // to avoid lots of mallocs
1936 for (char const* c=value; *c != 0; ++c){
1960 // Even though \/ is considered a legal escape in JSON, a bare
1961 // slash is also legal, so I see no reason to escape it.
1962 // (I hope I am not misunderstanding something.)
1973 Writer::write (const Value &root)
1983 Writer::writeValue (const Value &value)
1985 switch (value.type ())
1988 document_ += "null";
1991 document_ += valueToString (static_cast<int> (value));
1994 document_ += valueToString (static_cast<unsigned> (value));
1997 document_ += valueToString (static_cast<double> (value));
2000 document_ += valueToQuotedString (static_cast<char const *> (value));
2003 document_ += valueToString (static_cast<bool> (value));
2008 int size = value.size ();
2009 for (int index = 0; index < size; ++index)
2013 writeValue (value[index]);
2020 Value::Members members (value.getMemberNames ());
2022 for (Value::Members::iterator it = members.begin ();
2023 it != members.end ();
2026 const std::string &name = *it;
2027 if (it != members.begin ())
2029 document_ += valueToQuotedString (name.c_str ());
2031 writeValue (value[name]);
2051 add_method (char *name, Module const *mod, method mth)
2053 mfp m = { mod, mth };
2058 service (HTTPRequest *http, Value &request, Value &response)
2060 char const *methodName = static_cast<char const *> (request["method"]);
2062 method_map::iterator mthit = methods.find (methodName);
2063 if (mthit != methods.end ())
2065 mfp m = mthit->second;
2066 Module *mod = new Module (*m.mod);
2068 (mod->*mth) (http, request, response);
2074 process (HTTPRequest *http, std::string &response_text, char const *request_text)
2078 Value request (objectValue);
2079 Value response (objectValue);
2083 parse_success = r.parse (request_text, request_text + strlen (request_text), request);
2085 service (http, request, response);
2087 text = w.write (response);
2089 response_text = text.c_str ();
2096 MODULE_INIT(ModuleRpcJson)