void Bdecoder::read(qulonglong *value, bool &bSigned) { if (m_pos >= m_len || m_buf[m_pos] != 'i') { setError("expected integer, got %c", char(m_buf[m_pos])); return; } m_pos++; int end = m_buf.indexOf('e', m_pos); if (end <= m_pos || end >= m_len) { setError("buffer overrun"); return; } if (value) { QByteArray s = m_buf.mid(m_pos, end - m_pos); if (!validateInt(s)) return; bool ok; if(bSigned = (s.left(1) == "-")) *value = s.toLongLong(&ok, 10); else *value = s.toULongLong(&ok, 10); if (!ok) { setError("Invalid integer (%s)", s.constData()); return; } } m_pos = end + 1; }
qint64 QHttpNetworkHeaderPrivate::contentLength() const { bool ok = false; QByteArray value = headerField("content-length"); qint64 length = value.toULongLong(&ok); if (ok) return length; return -1; // the header field is not set }
void WatchData::setHexAddress(const QByteArray &a) { bool ok; const qint64 av = a.toULongLong(&ok, 0); if (ok) { address = av; } else { qWarning("WatchData::setHexAddress(): Failed to parse address value '%s' for '%s', '%s'", a.constData(), iname.constData(), type.constData()); address = 0; } }
qint64 QHttpNetworkHeaderPrivate::contentLength() const { bool ok = false; // We are not using the headerField() method here because servers might send us multiple content-length // headers which is crap (see QTBUG-15311). Therefore just take the first content-length header field. QByteArray value; QList<QPair<QByteArray, QByteArray> >::ConstIterator it = fields.constBegin(), end = fields.constEnd(); for ( ; it != end; ++it) if (qstricmp("content-length", it->first) == 0) { value = it->second; break; } qint64 length = value.toULongLong(&ok); if (ok) return length; return -1; // the header field is not set }
quint64 getRecvFileSize(const QByteArray &data) { int nPos = 0; QByteArray temp; const char *conL="Content-Length:"; nPos = data.indexOf(conL); if(nPos != -1) { temp = data.mid(nPos + strlen(conL)); nPos = temp.indexOf("\n"); if(nPos != -1) { temp = temp.left(nPos); temp=temp.trimmed(); return (temp.toULongLong()); } } return -1; }
/*! \brief Read and parse data from the given socket. * * The HTTP method (e.g. GET / 200 OK), headers and content will be split out * for further processing. */ bool TorcHTTPReader::Read(QTcpSocket *Socket, int *Abort) { if (!Socket) return false; if (*Abort || Socket->state() != QAbstractSocket::ConnectedState) return false; // sanity check if (m_headersRead >= 200) { LOG(VB_GENERAL, LOG_ERR, "Read 200 lines of headers - aborting"); return false; } // read headers if (!m_headersComplete) { while (!(*Abort) && Socket->canReadLine() && m_headersRead < 200) { QByteArray line = Socket->readLine().trimmed(); if (line.isEmpty()) { m_headersRead = 0; m_headersComplete = true; break; } if (!m_requestStarted) { LOG(VB_NETWORK, LOG_DEBUG, line); m_method = line; } else { m_headersRead++; int index = line.indexOf(":"); if (index > 0) { QByteArray key = line.left(index).trimmed(); QByteArray value = line.mid(index + 1).trimmed(); if (key == "Content-Length") m_contentLength = value.toULongLong(); LOG(VB_NETWORK, LOG_DEBUG, QString("%1: %2").arg(key.data()).arg(value.data())); m_headers->insert(key, value); } } m_requestStarted = true; } } // loop if we need more header data if (!m_headersComplete) return true; // abort early if needed if (*Abort || Socket->state() != QAbstractSocket::ConnectedState) return false; // read content while (!(*Abort) && (m_contentReceived < m_contentLength) && Socket->bytesAvailable() && Socket->state() == QAbstractSocket::ConnectedState) { static quint64 MAX_CHUNK = 32 * 1024; m_content->append(Socket->read(qMax(m_contentLength - m_contentReceived, qMax(MAX_CHUNK, (quint64)Socket->bytesAvailable())))); m_contentReceived = m_content->size(); } // loop if we need more data if (m_contentReceived < m_contentLength) return true; // abort early if needed if (*Abort || Socket->state() != QAbstractSocket::ConnectedState) return false; m_ready = true; return true; }
void clientHandler::processClient(QTcpSocket *socket, QList<FileExec>fileExecList) { //QByteArray inData; //QString inStr; //QStringList list; //QStringList ops; bool getProcess; //bool processHeader; //QString resultStr; executeData ed; QByteArray inData; qint64 loc; qint64 contentLength; qint64 requestLength; qint64 headerLength; quint64 timeout; bool processHeader; bool dataRecieved; processHeader = false; this->clientTCP = socket; this->fileExecList = fileExecList; contentLength = 0; requestLength = 0; loc = 0; getProcess = false; dataRecieved = false; timeout = 0; connect(this->clientTCP,SIGNAL(disconnected()),this,SLOT(socketDisconnected())); if (!this->clientTCP->waitForReadyRead(100)){ this->clientTCP->disconnectFromHost(); this->clientTCP->deleteLater(); return; } inData.clear(); while(1){ if (timeout == 30000){ break; } //we need to read the first data and determined content length if (this->clientTCP->bytesAvailable() > 0){ qDebug() << this->clientTCP->bytesAvailable(); timeout = 0; inData.append(this->clientTCP->readAll()); QCoreApplication::processEvents(); if (!processHeader){ headerLength = inData.indexOf("\r\n\r\n",0); if (headerLength > 0){ processHeader = true; //find content length; loc = inData.indexOf("Content-Length:"); if (loc < 0){ contentLength = 0; } if (loc > 0){ //get the size; qint64 loc2 = inData.indexOf("\r\n",loc); if (loc2 < 0){ contentLength = 0; } else { QByteArray size; size.clear(); size.append(inData.mid(loc,loc2 - loc).replace("Content-Length:","").trimmed()); contentLength = size.toULongLong(); } } } } if (processHeader){ requestLength = headerLength + contentLength; if (inData.size() >= requestLength){ dataRecieved = true; break; } } } QCoreApplication::processEvents(); timeout++; } if (!dataRecieved){ this->clientTCP->disconnectFromHost(); this->clientTCP->deleteLater(); return; } //qDebug() << "DEBUG STOP" << requestLength << timeout; //qDebug() << "inData size" << inData.size(); ed = this->processClientRequest(&inData,headerLength); /* //read from client browser inStr = QString::fromLocal8Bit(this->clientTCP->readAll()); list = inStr.split('\n'); resultStr.clear(); resultStr = "<br><font size=1>Infoblox TIG DGA Web System<br>\n"; resultStr.append("Copyright (C) 2015 Azril arahimATinfobloxDOTcom</font><br><br><b><br>\n"); for (quint16 i =0;i < list.size();i++){ ops = list.at(i).split(QChar(32)); if (ops.at(0).contains("GET")){ ed = this->processGET(ops.at(1)); getProcess = true; break; } if (ops.at(0).contains("POST")){ QString url = this->getPOSTURL(list); qDebug() << "POST:" << url; //qDebug() << "ACTUAL RAW\n" << inStr << "\n\n"; ed = this->processPOST(url); getProcess = true; break; } } if(!getProcess){ //none get process by GET or POST //create error Message resultStr.append("Invalid Request. Unknown Request</b><br>\n"); } //qDebug() << resultStr << "\n"; */ this->clientTCP->write(this->produceHttpReply(ed)); this->clientTCP->flush(); this->clientTCP->waitForBytesWritten(); //close the socker this->clientTCP->disconnectFromHost(); this->clientTCP->abort(); return; }
int JSonScanner::yylex(YYSTYPE* yylval, yy::location *yylloc) { char ch; if (!m_io->isOpen()) { qCritical() << "JSonScanner::yylex - io device is not open"; return -1; } yylloc->step(); do { bool ret; if (m_io->atEnd()) { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::END"; return yy::json_parser::token::END; } else ret = m_io->getChar(&ch); if (!ret) { qCritical() << "JSonScanner::yylex - error reading from io device"; return -1; } qjsonDebug() << "JSonScanner::yylex - got |" << ch << "|"; yylloc->columns(); if (ch == '\n' || ch == '\r') yylloc->lines(); } while (m_quotmarkClosed && (isspace(ch) != 0)); if (m_quotmarkClosed && ((ch == 't') || (ch == 'T'))) { const QByteArray buf = m_io->peek(3).toLower(); if (buf == "rue") { m_io->read (3); yylloc->columns(3); qjsonDebug() << "JSonScanner::yylex - TRUE_VAL"; return yy::json_parser::token::TRUE_VAL; } } else if (m_quotmarkClosed && ((ch == 'n') || (ch == 'N'))) { const QByteArray buf = m_io->peek(3).toLower(); if (buf == "ull") { m_io->read (3); yylloc->columns(3); qjsonDebug() << "JSonScanner::yylex - NULL_VAL"; return yy::json_parser::token::NULL_VAL; } else if (buf.startsWith("an") && m_allowSpecialNumbers) { m_io->read(2); yylloc->columns(2); qjsonDebug() << "JSonScanner::yylex - NAN_VAL"; return yy::json_parser::token::NAN_VAL; } } else if (m_quotmarkClosed && ((ch == 'f') || (ch == 'F'))) { // check false value const QByteArray buf = m_io->peek(4).toLower(); if (buf.length() == 4) { if (buf == "alse") { m_io->read (4); yylloc->columns(4); qjsonDebug() << "JSonScanner::yylex - FALSE_VAL"; return yy::json_parser::token::FALSE_VAL; } } } else if (m_quotmarkClosed && ((ch == 'e') || (ch == 'E'))) { QByteArray ret(1, ch); const QByteArray buf = m_io->peek(1); if (!buf.isEmpty()) { if ((buf[0] == '+' ) || (buf[0] == '-' )) { ret += m_io->read (1); yylloc->columns(); } } *yylval = QVariant(QString::fromUtf8(ret)); return yy::json_parser::token::E; } else if (m_allowSpecialNumbers && m_quotmarkClosed && ((ch == 'I') || (ch == 'i'))) { QByteArray ret(1, ch); const QByteArray buf = m_io->peek(7); if (buf == "nfinity") { m_io->read(7); yylloc->columns(7); qjsonDebug() << "JSonScanner::yylex - INFINITY_VAL"; return yy::json_parser::token::INFINITY_VAL; } } if (ch != '"' && !m_quotmarkClosed) { // we're inside a " " block QByteArray raw; raw += ch; char prevCh = ch; bool escape_on = (ch == '\\') ? true : false; while ( true ) { char nextCh; qint64 ret = m_io->peek(&nextCh, 1); if (ret != 1) { if (m_io->atEnd()) return yy::json_parser::token::END; else return -1; } else if ( !escape_on && nextCh == '\"' ) { bool ok; const QString str = unescape( raw, &ok ); *yylval = ok ? str : QString(); return ok ? yy::json_parser::token::STRING : -1; } #if 0 if ( prevCh == '\\' && nextCh != '"' && nextCh != '\\' && nextCh != '/' && nextCh != 'b' && nextCh != 'f' && nextCh != 'n' && nextCh != 'r' && nextCh != 't' && nextCh != 'u') { qjsonDebug() << "Just read" << nextCh; qjsonDebug() << "JSonScanner::yylex - error decoding escaped sequence"; return -1; } #endif m_io->read(1); // consume raw += nextCh; prevCh = nextCh; if (escape_on) escape_on = false; else escape_on = (prevCh == '\\') ? true : false; #if 0 if (nextCh == '\\') { char buf; if (m_io->getChar (&buf)) { yylloc->columns(); if (((buf != '"') && (buf != '\\') && (buf != '/') && (buf != 'b') && (buf != 'f') && (buf != 'n') && (buf != 'r') && (buf != 't') && (buf != 'u'))) { qjsonDebug() << "Just read" << buf; qjsonDebug() << "JSonScanner::yylex - error decoding escaped sequence"; return -1; } } else { qCritical() << "JSonScanner::yylex - error decoding escaped sequence : io error"; return -1; } } #endif } } else if (isdigit(ch) != 0 && m_quotmarkClosed) { bool ok; QByteArray numArray = QByteArray::fromRawData( &ch, 1 * sizeof(char) ); qulonglong number = numArray.toULongLong(&ok); if (!ok) { //This shouldn't happen qCritical() << "JSonScanner::yylex - error while converting char to ulonglong, returning -1"; return -1; } if (number == 0) { // we have to return immediately otherwise numbers like // 2.04 will be converted to 2.4 *yylval = QVariant(number); qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::DIGIT"; return yy::json_parser::token::DIGIT; } char nextCh; qint64 ret = m_io->peek(&nextCh, 1); while (ret == 1 && isdigit(nextCh)) { m_io->read(1); //consume yylloc->columns(1); numArray = QByteArray::fromRawData( &nextCh, 1 * sizeof(char) ); number = number * 10 + numArray.toULongLong(&ok); if (!ok) { //This shouldn't happen qCritical() << "JSonScanner::yylex - error while converting char to ulonglong, returning -1"; return -1; } ret = m_io->peek(&nextCh, 1); } *yylval = QVariant(number); qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::DIGIT"; return yy::json_parser::token::DIGIT; } else if (isalnum(ch) != 0) { *yylval = QVariant(QString(QChar::fromLatin1(ch))); qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::WORD (" << ch << ")"; return yy::json_parser::token::STRING; } else if (ch == ':') { // set yylval qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::COLON"; return yy::json_parser::token::COLON; } else if (ch == '"') { // yy::json_parser::token::QUOTMARK (") // set yylval m_quotmarkCount++; if (m_quotmarkCount %2 == 0) { m_quotmarkClosed = true; m_quotmarkCount = 0; qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::QUOTMARKCLOSE"; return yy::json_parser::token::QUOTMARKCLOSE; } else { m_quotmarkClosed = false; qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::QUOTMARKOPEN"; return yy::json_parser::token::QUOTMARKOPEN; } } else if (ch == ',') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::COMMA"; return yy::json_parser::token::COMMA; } else if (ch == '.') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::DOT"; return yy::json_parser::token::DOT; } else if (ch == '-') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::MINUS"; return yy::json_parser::token::MINUS; } else if (ch == '[') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::SQUARE_BRACKET_OPEN"; return yy::json_parser::token::SQUARE_BRACKET_OPEN; } else if (ch == ']') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::SQUARE_BRACKET_CLOSE"; return yy::json_parser::token::SQUARE_BRACKET_CLOSE; } else if (ch == '{') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::CURLY_BRACKET_OPEN"; return yy::json_parser::token::CURLY_BRACKET_OPEN; } else if (ch == '}') { qjsonDebug() << "JSonScanner::yylex - yy::json_parser::token::CURLY_BRACKET_CLOSE"; return yy::json_parser::token::CURLY_BRACKET_CLOSE; } //unknown char! //TODO yyerror? qCritical() << "JSonScanner::yylex - unknown char, returning -1"; return -1; }