bool HttpClientHandler::sendFile(HttpResponseHeader & hdr,const QString & full_path) { // Out(SYS_WEB|LOG_DEBUG) << "Sending file " << full_path << endl; setResponseHeaders(hdr); // first look in cache MMapFile* c = srv->cacheLookup(full_path); if (!c) { // not in cache so load it c = new MMapFile(); if (!c->open(full_path,QIODevice::ReadOnly)) { delete c; Out(SYS_WEB|LOG_DEBUG) << "Failed to open file " << full_path << endl; return false; } srv->insertIntoCache(full_path,c); } // Out(SYS_WEB|LOG_DEBUG) << "HTTP header : " << endl; // Out(SYS_WEB|LOG_DEBUG) << hdr.toString() << endl; QByteArray data((const char*)c->getDataPointer(),c->getSize()); hdr.setValue("Content-Length",QString::number(data.size())); output_buffer.append(hdr.toString().toUtf8()); output_buffer.append(data); sendOutputBuffer(); // Out(SYS_WEB|LOG_DEBUG) << "Finished sending " << full_path << " (" << written << " bytes)" << endl; return true; }
void testMMapFile() { const int BUF_SIZE = 50000; // 50MB MMapFile mmap; mmap.resize(BUF_SIZE); CPPUNIT_ASSERT_EQUAL( BUF_SIZE, mmap.getsize() ); void *buf = mmap.get(0, BUF_SIZE); memset(buf, 0x85, BUF_SIZE); mmap.release(); srand(time(NULL)); for (size_t i = 0; i < 100; i++) { int offset1 = rand() % BUF_SIZE; int size1 = rand() % (BUF_SIZE - offset1); char *p1 = (char *) mmap.get(offset1, size1); int offset2 = rand() % BUF_SIZE; int size2 = rand() % (BUF_SIZE - offset2); char *p2 = (char *) mmap.getmore(offset2, size2); int minsize = min(size1, size2); for (int j = 0; j < minsize; j++) { CPPUNIT_ASSERT_EQUAL( p1[j], p2[j] ); } mmap.release(); mmap.release(p2, size2); } }
void testMMapFile() { size_t i; const int BUF_SIZE = 50000; // 50MB // resize MMapFile mmap; mmap.resize(BUF_SIZE); CPPUNIT_ASSERT_EQUAL( BUF_SIZE, mmap.getsize() ); // set content char *buf = (char *) mmap.get(0, BUF_SIZE); for (i = 0; i < BUF_SIZE; i++) { buf[i] = i % 256; } mmap.release(); // test content and get(), getmore() srand(time(NULL)); for (i = 0; i < 100; i++) { int offset1 = rand() % BUF_SIZE; int size1 = rand() % (BUF_SIZE - offset1); char *p1 = (char *) mmap.get(offset1, size1); int offset2 = rand() % BUF_SIZE; int size2 = rand() % (BUF_SIZE - offset2); char *p2 = (char *) mmap.getmore(offset2, size2); int j; for (j = 0; j < size1; j++) { CPPUNIT_ASSERT_EQUAL( p1[j], char((offset1 + j) % 256) ); } for (j = 0; j < size2; j++) { CPPUNIT_ASSERT_EQUAL( p2[j], char((offset2 + j) % 256) ); } mmap.release(); mmap.release(p2, size2); } }
bool HttpClientHandler::sendFile(HttpResponseHeader & hdr,const QString & full_path) { // Out(SYS_WEB|LOG_DEBUG) << "Sending file " << full_path << endl; // first look in cache MMapFile* c = srv->cacheLookup(full_path); if (!c) { // not in cache so load it c = new MMapFile(); if (!c->open(full_path,MMapFile::READ)) { delete c; Out(SYS_WEB|LOG_DEBUG) << "Failed to open file " << full_path << endl; return false; } srv->insertIntoCache(full_path,c); } hdr.setValue("Content-Length",QString::number(c->getSize())); // Out(SYS_WEB|LOG_DEBUG) << "HTTP header : " << endl; // Out(SYS_WEB|LOG_DEBUG) << hdr.toString() << endl; QCString d = hdr.toString().utf8(); client->writeBlock(d.data(),d.length()); Uint32 written = 0; Uint32 total = c->getSize(); const char* data = (const char*)c->getDataPointer(); while (written < total) { Uint32 w = client->writeBlock(data + written,total - written); written += w; } client->flush(); // Out(SYS_WEB|LOG_DEBUG) << "Finished sending " << full_path << " (" << written << " bytes)" << endl; return true; }
void release() { mmap_.munmap(); }