Пример #1
0
		void readCB(bool success) {
			if(unlikely(!success)) {
				destruct();
				return;
			}
			//if((sp=thr._stringPoolPool.tryGet())==nullptr) sp=new StringPool();
			if((resp=thr._responsePool.tryGet())) resp->init(this->s,&sp);
			else resp=new Response(this->s,&sp);
			
			//keepAlive=true;
			auto it=req.headers.find("connection");
			if(it!=req.headers.end() && (*it).value=="close")keepAlive=false;
			else keepAlive=true;
			resp->headers.insert({"Connection", keepAlive?"keep-alive":"close"});
			
			/*char* date=sp.beginAdd(32);
			tm time;
			gmtime_r(&thr.curClockTime.tv_sec,&time);
			int l=rfctime(time,date);
			if(l>32)l=32;
			sp.endAdd(l);
			*/
			resp->headers.insert({"Date", sp.addString(thr.curRFCTime)});
			
			thr.performanceCounters.totalRequestsReceived++;
			try {
				thr.handleRequest(req,*resp,{&handler::finalize,this});
			} catch(exception& ex) {
				thr.handleError(req,*resp,ex,{&handler::finalize,this});
			}
			
		}
Пример #2
0
		void handleStatic(staticPage* Sp) {
			Response& resp(*this->resp);
			(_staticPage=Sp)->retain();
			try {
				int bufferL = resp.buffer.length();
				if(Sp->mime.length()>0)resp.headers["Content-Type"]=Sp->mime;
				{
					char* tmps = sp.beginAdd(22);
					int l = itoa64(Sp->fileLen, tmps);
					sp.endAdd(l);
					resp.headers.insert({"Content-Length", { tmps, l }});
					StreamWriter sw(resp.buffer);
					resp.serializeHeaders(sw);
				}
				if(Sp->fileLen>=CPPSP_SENDFILE_MIN_SIZE) {
					_sendFileOffset=0;
					s.sendAll(resp.buffer.data()+bufferL,resp.buffer.length()-bufferL,
						MSG_MORE, { &handler::sendHeadersCB, this });
				} else {
					String data=Sp->data;
					iov[0]= {resp.buffer.data()+bufferL, (size_t)(resp.buffer.length()-bufferL)};
					iov[1]= {data.data(), (size_t)data.length()};
					resp.outputStream->writevAll(iov, data.length()<=0?1:2, { &handler::writevCB, this });
				}
			} catch(exception& ex) {
				Sp->release();
				server->handleError(req,resp,ex,{&handler::finalize,this});
			}
		}
Пример #3
0
		//deallocate resources after a request has been completed
		void cleanup() {
			server->performanceCounters.totalRequestsFinished++;
			thr.performanceCounters.totalRequestsFinished++;
			req.reset();
			resp->reset();
			thr._responsePool.put(resp);
			resp=nullptr;
			sp.clear();
		}
Пример #4
0
		void finalize() {
			thr.performanceCounters.totalRequestsFinished++;
			if(resp->closed) {
				destruct(); return;
			}
			req.reset();
			resp->reset();
			thr._responsePool.put(resp);
			resp=nullptr;
			sp.clear();
			if(keepAlive) {
				req.init(s,&sp);
				if(req.readRequest({&handler::readCB,this})) readCB(true);
			} else {
				s.shutdown(SHUT_WR);
				buf=(uint8_t*)malloc(4096);
				s.repeatRead(buf,4096,{&handler::sockReadCB,this});
			}
		}
Пример #5
0
		void handleStatic(staticPage* Sp) {
			Response& resp(*this->resp);
			try {
				String data=Sp->data;
				int bufferL = resp.buffer.length();
				if(Sp->mime.length()>0)resp.headers["Content-Type"]=Sp->mime;
				{
					char* tmps = sp.beginAdd(16);
					int l = itoa(data.length(), tmps);
					sp.endAdd(l);
					resp.headers.insert({"Content-Length", { tmps, l }});
					StreamWriter sw(resp.buffer);
					resp.serializeHeaders(sw);
				}
				iov[0]= {resp.buffer.data()+bufferL, (size_t)(resp.buffer.length()-bufferL)};
				iov[1]= {data.data(), (size_t)data.length()};
				resp.outputStream->writevAll(iov, 2, { &handler::writevCB, this });
			} catch(exception& ex) {
				thr.handleError(req,resp,ex,{&handler::finalize,this});
			}
		}