| 136 | } |
| 137 | |
| 138 | void process(QHttpRequest* req, QHttpResponse* res) { |
| 139 | auto root = QJsonDocument::fromJson(req->collectedData()).object(); |
| 140 | |
| 141 | if ( root.isEmpty() || root.value("name").toString() != QLatin1Literal("add") ) { |
| 142 | const static char KMessage[] = "Invalid json format!"; |
| 143 | res->setStatusCode(qhttp::ESTATUS_BAD_REQUEST); |
| 144 | res->addHeader("connection", "close"); |
| 145 | res->addHeaderValue("content-length", strlen(KMessage)); |
| 146 | res->end(KMessage); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | int total = 0; |
| 151 | auto args = root.value("args").toArray(); |
| 152 | for ( const auto jv : args ) { |
| 153 | total += jv.toInt(); |
| 154 | } |
| 155 | root["args"] = total; |
| 156 | |
| 157 | QByteArray body = QJsonDocument(root).toJson(); |
| 158 | res->addHeader("connection", "keep-alive"); |
| 159 | res->addHeaderValue("content-length", body.length()); |
| 160 | res->setStatusCode(qhttp::ESTATUS_OK); |
| 161 | res->end(body); |
| 162 | } |
| 163 | }; // struct server |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | } // namespace server |