/// GET: /broadcast
RequestResult Request_GET_Broadcast(Database* db, const std::string& uriparams, const std::string& qparams, const std::string& body) {
	RequestResult ret;
        auto qdict = parseQueryParams(qparams);
        if(qdict.size() < 2 || !db->validateSession(qdict["r_user"], qdict["token"])) {
        	ret.code = 401;
                ret.data = "{\"error\": \"token invalido\" }";
        }else{
                Broadcast bcast;
                if(!db->loadBroadcast(bcast)) {
                	ret.code = 500;
                        ret.data = "{ \"error\": \"error al cargar broadcast\" }";
                }else{
                        int lines;
                        try {
                                lines = std::stoi(qdict["lines"]);
                        }catch(...) {
                                lines = 0;
                        }
                        ret.data = bcast.asJson(lines);
			// Marco como leido
                        db->markBroadcastUnread(qdict["r_user"], false);
                }
        }
        return ret;
}
/// GET: /conversacion
RequestResult Request_GET_Conversacion(Database* db, const std::string& uriparams, const std::string& qparams, const std::string& body) {
	RequestResult ret;
	auto qdict = parseQueryParams(qparams);
	if(qdict.size() < 2 || !db->validateSession(qdict["r_user"], qdict["token"])) {
		ret.code = 401;
		ret.data = "{\"error\": \"token invalido\" }";
	}else{
		const auto& r_user = qdict["r_user"];
		const auto& t_user = uriparams;
		Conversacion conv;
		if(!db->loadConversacion(r_user, t_user, conv)) {
			ret.code = 500;
                        ret.data = "{ \"error\": \"error al cargar conversacion\" }";
		}else{
			int lines;
			try {
				lines = std::stoi(qdict["lines"]);
			}catch(...) {
				lines = 0;
			}
			ret.data = conv.asJson(lines);
			db->markRead(r_user, t_user);
		}
	}
	return ret;
}
/// POST: conversacion
RequestResult Request_POST_Conversacion(Database* db, const std::string& uriparams, const std::string& qparams, const std::string& body) {
	RequestResult ret;
	auto qdict = parseQueryParams(qparams);
	if(qdict.size() < 2 || !db->validateSession(qdict["r_user"], qdict["token"])) {
		ret.code = 401;
		ret.data = "{\"error\": \"token invalido\" }";
	}else{
		const auto& r_user = qdict["r_user"];
		const auto& t_user = uriparams;
		auto js = JSONParse(body);
                const auto& msg = js.get("mensaje", "").asString();
                if(msg.length() == 0) {
                	ret.code = 401;
                        ret.data = "{ \"error\": \"mensaje invalido\" }";
                }else if(!db->usuarioExists(t_user)){
			ret.code = 401;
                        ret.data = "{ \"error\": \"usuario de destino invalido\" }";
		}else if(!db->postearMensaje(r_user, t_user, msg)) {
			ret.code = 500;
                        ret.data = "{ \"error\": \"error al postear mensaje\" }";
		}else{
			// Todo ok
			ret.code = 201;
		}
	}
	return ret;
}
/// GET: /usuarios
RequestResult Request_GET_Usuarios(Database* db, const std::string& uriparams, const std::string& qparams, const std::string& body) {
        RequestResult ret;
	auto qdict = parseQueryParams(qparams);
	if(qdict.size() < 2 || !db->validateSession(qdict["r_user"], qdict["token"])) {
		ret.code = 401;
		ret.data = "{\"error\": \"token invalido\" }";
	}else{
		ret.data = db->getListaUsuariosJson(qdict["r_user"]);
	}
        return ret;
}
示例#5
0
void O2ReplyServer::onBytesReady() {
    QByteArray reply;
    QByteArray content;
    content.append("<HTML></HTML>");
    reply.append("HTTP/1.0 200 OK \r\n");
    reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n");
    reply.append(QString("Content-Length: %1\r\n\r\n").arg(content.size()));
    reply.append(content);
    socket->write(reply);

    QByteArray data = socket->readAll();
    QMap<QString, QString> queryParams = parseQueryParams(&data);
    socket->disconnectFromHost();
    close();
    emit verificationReceived(queryParams);
}
/// PUT: /usuario
RequestResult Request_PUT_Usuario(Database* db, const std::string& uriparams, const std::string& qparams, const std::string& body) {
	RequestResult ret;
	auto qdict = parseQueryParams(qparams);
	Usuario usr;
	// Debe pasar la clave tambien
	if(qdict.size() < 2 || !db->validateSession(qdict["r_user"], qdict["token"])) {
		ret.code = 401;
		ret.data = "{\"error\": \"token invalido\" }";
		return ret;
	}
	if(uriparams != qdict["r_user"]) {
		ret.code = 401;
		ret.data = "{\"error\": \"permiso denegado\" }";
		return ret;
	}
	if(!db->loadUsuario(uriparams, usr)) {
		ret.code = 500;
		ret.data = "{\"error\": \"error cargando usuario\" }";
		return ret;
	}
	if(qdict["password"] != usr.password) {
		ret.code = 401;
		ret.data = "{\"error\": \"password invalido\" }";
		return ret;
	}
	// Editamos los valores que nos pasaron (si los pasaron)
	auto js = JSONParse(body);
	usr.load(js);
	// Guardamos el usuario
	if(!db->saveUsuario(usr)) {
		// TODO: desdoblar en los distintos
		ret.code = 401;
		ret.data = "{ \"error\": \"atributos invalidos\" }";
	}else{
		ret.code = 201;
	}
	return ret;
}
示例#7
0
void O2ReplyServer::onBytesReady() {
    if (!isListening()) {
        // server has been closed, stop processing queued connections
        return;
    }
    qDebug() << "O2ReplyServer::onBytesReady: Processing request";
    // NOTE: on first call, the timeout timer is started
    QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
    if (!socket) {
        qWarning() << "O2ReplyServer::onBytesReady: No socket available";
        return;
    }
    QByteArray reply;
    reply.append("HTTP/1.0 200 OK \r\n");
    reply.append("Content-Type: text/html; charset=\"utf-8\"\r\n");
    reply.append(QString("Content-Length: %1\r\n\r\n").arg(replyContent_.size()).toLatin1());
    reply.append(replyContent_);
    socket->write(reply);
    qDebug() << "O2ReplyServer::onBytesReady: Sent reply";

    QByteArray data = socket->readAll();
    QMap<QString, QString> queryParams = parseQueryParams(&data);
    if (queryParams.isEmpty()) {
        if (tries_ < maxtries_ ) {
            qDebug() << "O2ReplyServer::onBytesReady: No query params found, waiting for more callbacks";
            ++tries_;
            return;
        } else {
            tries_ = 0;
            qWarning() << "O2ReplyServer::onBytesReady: No query params found, maximum callbacks received";
            closeServer(socket, false);
            return;
        }
    }
    qDebug() << "O2ReplyServer::onBytesReady: Query params found, closing server";
    closeServer(socket, true);
    Q_EMIT verificationReceived(queryParams);
}