//------------------------------------------------------------------------------ void ofxIpVideoServerRouteHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { if(isValidRequest(settings.route,request,response)) { MediaType mediaType("multipart/x-mixed-replace"); mediaType.setParameter("boundary", settings.boundary); string expires = DateTimeFormatter::format(Timestamp(0), // the beginning of time DateTimeFormat::HTTP_FORMAT); response.set("Cache-control","no-cache"); response.setContentType(mediaType); response.set("Expires",expires); response.set("Pragma","no-cache"); std::ostream& ostr = response.send(); // get output stream bool stopStreaming = false; while(ostr.good() && !stopStreaming) { if(!queue.empty()) { ofxIpVideoServerFramePtr frame = queue.pop(); if(frame != NULL) { ofBuffer* buffer = &frame.get()->buffer; ostr << settings.boundary; ostr << "\r\n"; ostr << "Content-Type: image/jpeg"; ostr << "\r\n"; ostr << "Content-Length: " << ofToString(buffer->size()); ostr << "\r\n"; ostr << "\r\n"; ostr << *buffer; } else { ofLogVerbose("ofxIpVideoServerRouteHandler::handleRequest") << "Null buffer."; } } else { ofLogVerbose("ofxIpVideoServerRouteHandler::handleRequest") << "Queue empty."; } Thread::sleep(50); } queue.setActive(false); // a desperate move ofLogNotice("ofxIpVideoServerRouteHandler::handleRequest") << "Client disconneted."; //sendErrorResponse(response); } }
//------------------------------------------------------------------------------ void ofxWebServerUploadRouteHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { if(isValidRequest(settings.route, request, response)) { HTMLForm form(request, request.stream(), *this); if(!settings.uploadRedirect.empty()) { response.redirect(settings.uploadRedirect); } else { response.setStatusAndReason(HTTPResponse::HTTP_OK); response.setContentLength(0); response.send(); } } else { return; // isValidRequest took care of the response } }
static void _read_query_from_client(EventLoop_t evloop , int fd , void* clientData , int mask) { AVOID_NOUSED(evloop); AVOID_NOUSED(mask); // AVOID_NOUSED(clientData); // Client_t* c = Map_GetPtr(g_executor.clients , &fd); Client_t* c = (Client_t*)clientData; int nread; char errmsg[MAX_NET_ERR_LEN]; StdMutex_Lock(c->oplock); nread = netRead(errmsg, c->fd, c->query_buffer + c->buffer_len, CLIENTS_BUFFER_LEN - c->buffer_len); if (nread == ERR_NETWORK_EXCEPTION) { //ERR HAPPEND StdMutex_Unlock(c->oplock); Log(LOG_ERROR,"Read query from client failed.reason:##%s##", errmsg); _release_client(fd, RELEASE_BY_ERROR); return; } else if (nread == 0) { //Client Close the connection Log(LOG_NOTICE,"[CLIENT_DISCONNECT][%s:%d]", c->addr, c->port); StdMutex_Unlock(c->oplock); _release_client(fd, RELEASE_BY_CLIENT); return; } c->buffer_len += nread; //try to extract as many package as possible char* iterator = c->query_buffer; while (1) { if (c->buffer_len < MSGHEAD_SIZE) { break; } if (!isValidRequest(iterator)) { //not a valid request, clear client Log(LOG_NOTICE,"invalid request received from [%s:%d], close the connection" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } Message_t new_msg; new_msg.msgid = *((int32_t*)iterator + 1); new_msg.pkg_len = *((int32_t*)iterator + 2); if (c->status == CLI_INIT && new_msg.msgid != MSG_EXCU_AUTH && new_msg.msgid != MSG_EXCU_PLUSE) { //client not login, kick out Log(LOG_NOTICE,"client [%s:%d] should get authentication first before anyother request" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } c->last_seen = time(NULL); if (new_msg.pkg_len + MSGHEAD_SIZE > c->buffer_len) { //not a complete package int offset = iterator - c->query_buffer; for (int pos = 0; pos != c->buffer_len; ++pos) { c->query_buffer[pos] = c->query_buffer[pos + offset]; } break; } //extract one package new_msg.sockfd = c->fd; uint8_t* pkg = ylmalloc(new_msg.pkg_len); memcpy(pkg , iterator + MSGHEAD_SIZE , new_msg.pkg_len); new_msg.package = pkg; uint8_t recv_buf = atomic_add_then_fetch((int*)&g_executor.cur_recv_buffer_ , 1)%CLIENT_RECV_QUEUES; Queue_Push(g_executor.recv_buffer_[recv_buf] , &new_msg); sem_post(&g_executor.recv_buffer_num_[recv_buf]); int msglen = new_msg.pkg_len + MSGHEAD_SIZE; c->buffer_len -= msglen; iterator += msglen; } StdMutex_Unlock(c->oplock); }