void Connection::readyRead() { TRACE; was_NetData = true; if( is_WaitingHeaders ) { is_WaitingHeaders = ! readHeaders(p_ClientConnection, m_Headers); if( !is_WaitingHeaders ) { // parse headers m_RequestHeaders = getHeaders(m_Headers); // prepare for reading body m_Buffer.close(); m_Buffer.open(QBuffer::ReadWrite); QString len = m_RequestHeaders["content-length"]; bool ok; m_BodyLength = len.toInt(&ok); m_BodyLength = readBody(p_ClientConnection, m_Buffer, m_BodyLength); } } else { m_BodyLength = readBody(p_ClientConnection, m_Buffer, m_BodyLength); } if( is_WaitingHeaders==false && m_BodyLength==0 ) if( !onEmptyLine() ) p_ClientConnection->close(); }
static int doRequest(HttpConn *conn, cchar *url, MprList *files) { MprTime mark, remaining; HttpLimits *limits; mprAssert(url && *url); limits = conn->limits; mprLog(MPR_DEBUG, "fetch: %s %s", app->method, url); mark = mprGetTime(); if (issueRequest(conn, url, files) < 0) { return MPR_ERR_CANT_CONNECT; } remaining = limits->requestTimeout; while (!conn->error && conn->state < HTTP_STATE_COMPLETE && remaining > 0) { remaining = mprGetRemainingTime(mark, limits->requestTimeout); httpWait(conn, 0, remaining); readBody(conn); } if (conn->state < HTTP_STATE_COMPLETE && !conn->error) { httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT, "Inactive request timed out, exceeded request timeout %d", app->timeout); } else { readBody(conn); } reportResponse(conn, url, mprGetTime() - mark); httpDestroyRx(conn->rx); httpDestroyTx(conn->tx); return 0; }
bool SimpleHttpClient::readHeader () { char* pos = (char*) memchr(_readBuffer.c_str(), '\n', _readBuffer.length()); while (pos) { // size_t is unsigned, should never get < 0 assert(pos >= _readBuffer.c_str()); size_t len = pos - _readBuffer.c_str(); string line(_readBuffer.c_str(), len); _readBuffer.erase_front(len + 1); //printf("found header line %s\n", line.c_str()); if (line == "\r" || line == "") { // end of header found if (_result->isChunked()) { _state = IN_READ_CHUNKED_HEADER; return readChunkedHeader(); } else if (! _result->hasContentLength()) { // no content-length header in response _state = IN_READ_BODY; return readBody(); } else if (_result->hasContentLength() && _result->getContentLength() > 0) { // found content-length header in response if (_result->getContentLength() > _maxPacketSize) { setErrorMessage("Content-Length > max packet size found", true); // reset connection this->close(); _state = DEAD; return false; } _state = IN_READ_BODY; return readBody(); } else { _result->setResultType(SimpleHttpResult::COMPLETE); _state = FINISHED; if (! _keepAlive) { _connection->disconnect(); } } break; } else { _result->addHeaderField(line); } pos = (char*) memchr(_readBuffer.c_str(), '\n', _readBuffer.length()); } return true; }
static int doRequest(HttpConn *conn, cchar *url, MprList *files) { MprTicks mark, remaining; HttpLimits *limits; MprFile *outFile; cchar *path; assert(url && *url); limits = conn->limits; mprTrace(4, "fetch: %s %s", app->method, url); mark = mprGetTicks(); if (issueRequest(conn, url, files) < 0) { return MPR_ERR_CANT_CONNECT; } remaining = limits->requestTimeout; if (app->outFilename) { path = app->loadThreads > 1 ? sfmt("%s-%s.tmp", app->outFilename, mprGetCurrentThreadName()): app->outFilename; if ((outFile = mprOpenFile(path, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) { mprError("Cannot open %s", path); return MPR_ERR_CANT_OPEN; } } else { outFile = mprGetStdout(); } mprAddRoot(outFile); while (!conn->tx->finalized && conn->state < HTTP_STATE_COMPLETE && remaining > 0) { remaining = mprGetRemainingTicks(mark, limits->requestTimeout); readBody(conn, outFile); httpWait(conn, 0, remaining); } if (conn->state < HTTP_STATE_COMPLETE && !conn->error) { httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT, "Inactive request timed out, exceeded request timeout %d", app->timeout); } else { readBody(conn, outFile); } if (app->outFilename) { mprCloseFile(outFile); } mprRemoveRoot(outFile); reportResponse(conn, url, mprGetTicks() - mark); httpDestroyRx(conn->rx); httpDestroyTx(conn->tx); return 0; }
void Level::read(std::fstream& stream, Prototype* game, int versionNumber) { //int bGFilenameSize; //char bGFilename[32]; //stream.read((char*)&bGFilenameSize, sizeof(int)); //stream.read(bGFilename, bGFilenameSize); //setBackGround(imageManager->getTexture(std::string(bGFilename, bGFilename + bGFilenameSize))); stream.read((char*)&bgPosition, sizeof(bgPosition)); backGround.read(stream, versionNumber); //int gGFilenameSize; //char gGFilename[32]; //stream.read((char*)&gGFilenameSize, sizeof(int)); //stream.read(bGFilename, gGFilenameSize); //setGameGround(imageManager->getTexture(std::string(gGFilename, gGFilename + gGFilenameSize))); gameGround.read(stream, versionNumber); readBody(stream, game); stream.read((char*)&size, sizeof(size)); }
static void readWriteBody ( int fd, /* read fd */ FILE *out, /* output fd */ char *path /* body path */ ) { char buf[BLOCK_BUF_SIZE]; ssize_t n; memset( buf, 0x00, sizeof( buf ) ); memset( &n, 0x00, sizeof( n ) ); for( ;; ) { n = readBody( fd, buf, path ); if( n == 0 ) { break; } writeBody( buf, n, out ); } }
PetscErrorCode SingleBodyPoints::init(const MPI_Comm &comm, const PetscInt &dim, const std::string &name, const std::string &filePath) { PetscErrorCode ierr; PetscFunctionBeginUser; // read the body coordinates from the given file ierr = readBody(filePath); CHKERRQ(ierr); // check if the dimension of coordinates matches if ((unsigned)dim != coords[0].size()) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_FILE_READ, "The dimension of Lagrangian points are different than that " "of the background mesh!\n"); // record the initial body coordinates coords0 = coords; // create a distributed 1D DMDA with DoF equal to dim; nLclPts, bgPt, edPt, // da, nLclAllProcs, and offsetsAllProcs are set up here ierr = createDMDA(); CHKERRQ(ierr); // initialize meshIdx, which only contains background mesh indices of local // Lagrangian points. The indices are defined by pressure cell. meshIdx = type::IntVec2D(nLclPts, type::IntVec1D(dim, 0)); // create info string ierr = createInfoString(); CHKERRQ(ierr); PetscFunctionReturn(0); } // init
AsfFile::AsfFile(const char *fileName, int memoryLimitInBytes) : _memoryLimitInBytes(memoryLimitInBytes) { char last = *std::string(fileName).rbegin(); if (last == 'z') { _stream = (std::istream*) new igzstream(fileName, std::ios::in | std::ios::binary); _isPositioningDenied = true; } else if (last == 'f') { _stream = new std::ifstream(fileName, std::ios::in | std::ios::binary); _isPositioningDenied = false; } else { THROW_EXCEPTION("Unknown file format."); } if (!_stream->good()) { THROW_EXCEPTION("Can't open ASF file."); } readHead(); readBody(); }
//============================================================================== bool PEFile::loadFromFile(char* filePath) { unloadFile(); return readFileData(filePath) && readHeaders() && readBody() && readImportTable(); }
void DocxReader::readDocument() { m_cursor.beginEditBlock(); while (m_xml.readNextStartElement()) { if (m_xml.qualifiedName() == "w:body") { readBody(); } else { m_xml.skipCurrentElement(); } } m_cursor.endEditBlock(); }
void *wb_vrepext::readAttribute(pwr_tStatus *sts, const wb_orep *o, pwr_eBix bix, size_t offset, size_t size, void *p) { void *body = readBody( sts, o, bix, 0); if ( EVEN(*sts)) return 0; if ( p) { memcpy( p, (char *)body + offset, size); return p; } return (void *)((char *)body + offset); }
static int doRequest(HttpConn *conn, cchar *url, MprList *files) { MprFile *outFile; cchar *path; assert(url && *url); if (issueRequest(conn, url, files) < 0) { if (conn->rx && conn->rx->status) { reportResponse(conn, url); } return MPR_ERR_CANT_CONNECT; } if (app->outFilename) { path = app->loadThreads > 1 ? sfmt("%s-%s.tmp", app->outFilename, mprGetCurrentThreadName()): app->outFilename; if ((outFile = mprOpenFile(path, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) { mprLog("error http", 0, "Cannot open %s", path); return MPR_ERR_CANT_OPEN; } } else { outFile = mprGetStdout(); } mprAddRoot(outFile); readBody(conn, outFile); while (conn->state < HTTP_STATE_COMPLETE && !httpRequestExpired(conn, -1)) { readBody(conn, outFile); httpWait(conn, 0, -1); } if (conn->state < HTTP_STATE_COMPLETE && !conn->error) { httpError(conn, HTTP_ABORT | HTTP_CODE_REQUEST_TIMEOUT, "Request timed out"); } if (app->outFilename) { mprCloseFile(outFile); } mprRemoveRoot(outFile); reportResponse(conn, url); httpDestroyRx(conn->rx); httpDestroyTx(conn->tx); return 0; }
AppReply::AppReply(const char* method, const QUrl &url, QIODevice *body_device, QObject *parent) : QNetworkReply(parent) { this->method = method; this->url = url; this->body_device = body_device; if (body_device) { qDebug() << "body_device connect"; connect(body_device, SIGNAL(readyRead()), this, SLOT(readBody())); } else { QTimer::singleShot(0, this, SLOT(parseResponse())); } }
void PacketSocket::socketReadable() { switch(_state) { case stateReadingHeader: readHeader(); break; case stateReadingBody: readBody(); break; } if (_socket->bytesAvailable() > 0) { socketReadable(); } }
bool SimpleHttpClient::readHeader () { char* pos = (char*) memchr(_readBuffer.c_str(), '\n', _readBuffer.length()); while (pos) { size_t len = pos - _readBuffer.c_str(); string line(_readBuffer.c_str(), len); _readBuffer.erase_front(len + 1); //printf("found header line %s\n", line.c_str()); if (line == "\r" || line == "") { // end of header found if (_result->isChunked()) { _state = IN_READ_CHUNKED_HEADER; return readChunkedHeader(); } else if (_result->getContentLength()) { if (_result->getContentLength() > 5000000) { _errorMessage = "Content length > 5000000 bytes found!"; LOGGER_ERROR << "Content length > 5000000 bytes found! Closing connection."; // reset connection close(); return false; } _state = IN_READ_BODY; return readBody(); } else { _result->setResultType(SimpleHttpResult::COMPLETE); _state = FINISHED; } break; } else { _result->addHeaderField(line); } pos = (char*) memchr(_readBuffer.c_str(), '\n', _readBuffer.length()); } return true; }
//处理读到body数据 void KAsyncFetchObject::handleReadBody(KHttpRequest *rq,int got) { assert(header); if (got<=0) { /////////[399] //assert(rq->send_ctx.body==NULL); /* 读body失败,对于未知长度,如果没有设置cache_no_length,则不缓存 */ lifeTime = -1; if (!TEST(rq->filter_flags,RF_CACHE_NO_LENGTH) || TEST(rq->ctx->obj->index.flags,(ANSW_CHUNKED|ANSW_HAS_CONTENT_LENGTH))) { SET(rq->ctx->obj->index.flags,FLAG_DEAD|OBJ_INDEX_UPDATE); } stage_rdata_end(rq,STREAM_WRITE_SUCCESS); return; } //解析body parseBody(rq,header,got); //解析完继续处理body数据 readBody(rq); }
bool SimpleHttpClient::read (SimpleHttpResult* httpResult, double timeout) { httpResult->clear(); double start = now(); // read the http header if (!readHeader(httpResult, timeout)) { return false; } double runtime = now() - start; if (runtime > timeout) { LOGGER_WARNING << "read timeout"; return false; } // read the body return readBody(httpResult, timeout - runtime); }
bool SimpleHttpClient::read () { if (!checkSocket()) { return false; } do { char buffer[READBUFFER_SIZE]; int len_read = ::read(_socket, buffer, READBUFFER_SIZE - 1); if (len_read <= 0) { // error: stop reading break; } _readBuffer.appendText(buffer, len_read); } while(readable()); switch (_state) { case (IN_READ_HEADER): readHeader(); break; case (IN_READ_BODY): readBody(); break; case (IN_READ_CHUNKED_HEADER): readChunkedHeader(); break; case (IN_READ_CHUNKED_BODY): readChunkedBody(); break; default: break; } return true; }
std::string ClientImpl::get(const std::string& url, std::size_t timeout, std::size_t connectTimeout) { Request request(url); execute(request, timeout, connectTimeout); return readBody(); }
SimpleHttpResult* SimpleHttpClient::request (rest::HttpRequest::HttpRequestType method, const string& location, const char* body, size_t bodyLength, const map<string, string>& headerFields) { assert(_result == 0); _result = new SimpleHttpResult; _errorMessage = ""; // set body to all connections setRequest(method, location, body, bodyLength, headerFields); double endTime = now() + _requestTimeout; double remainingTime = _requestTimeout; while (isWorking() && remainingTime > 0.0) { switch (_state) { case (IN_CONNECT): { handleConnect(); break; } case (IN_WRITE): { size_t bytesWritten = 0; TRI_set_errno(TRI_ERROR_NO_ERROR); if (! _connection->handleWrite(remainingTime, (void*) (_writeBuffer.c_str() + _written), _writeBuffer.length() - _written, &bytesWritten)) { setErrorMessage(TRI_last_error(), false); this->close(); } else { _written += bytesWritten; if (_written == _writeBuffer.length()) { _state = IN_READ_HEADER; } } break; } case (IN_READ_HEADER): case (IN_READ_BODY): case (IN_READ_CHUNKED_HEADER): case (IN_READ_CHUNKED_BODY): { TRI_set_errno(TRI_ERROR_NO_ERROR); if (_connection->handleRead(remainingTime, _readBuffer)) { switch (_state) { case (IN_READ_HEADER): readHeader(); break; case (IN_READ_BODY): readBody(); break; case (IN_READ_CHUNKED_HEADER): readChunkedHeader(); break; case (IN_READ_CHUNKED_BODY): readChunkedBody(); break; default: break; } } else { setErrorMessage(TRI_last_error(), false); this->close(); } break; } default: break; } remainingTime = endTime - now(); } if (isWorking() && _errorMessage == "" ) { setErrorMessage("Request timeout reached"); } // set result type in getResult() SimpleHttpResult* result = getResult(); _result = 0; return result; }
void protocol::recv(int socketFD, message &msg) { unsigned int totalSize = readHeader(socketFD); readBody(socketFD, msg, totalSize); }