static void read_cb(struct ev_loop *loop, struct ev_io *watcher, int revents) { if(EV_ERROR & revents) { rdlog(LOG_ERR,"Read callback error: %s",mystrerror(errno,errbuf, ERROR_BUFFER_SIZE)); } struct connection_private *connection = (struct connection_private *) watcher->data; struct sockaddr_in6 saddr; #ifdef CONNECTION_PRIVATE_MAGIC assert(connection->magic == CONNECTION_PRIVATE_MAGIC); #endif char *buffer = calloc(READ_BUFFER_SIZE,sizeof(char)); const int recv_result = receive_from_socket(watcher->fd,&saddr,buffer,READ_BUFFER_SIZE); if(recv_result > 0){ process_data_received_from_socket(buffer,(size_t)recv_result,connection->client, connection->callback,connection->callback_opaque); }else if(recv_result < 0){ if(errno == EAGAIN){ rdbg("Socket not ready. re-trying"); free(buffer); return; }else{ rdlog(LOG_ERR,"Recv error: %s",mystrerror(errno,errbuf,ERROR_BUFFER_SIZE)); free(buffer); close_socket_and_stop_watcher(loop,watcher); return; } }else{ /* recv_result == 0 */ free(buffer); close_socket_and_stop_watcher(loop,watcher); return; } if(NULL!=global_config.response && !connection->first_response_sent){ int send_ret = 1; rdlog(LOG_DEBUG,"Sending first response..."); if(global_config.response_len == 0){ rdlog(LOG_ERR,"Can't send first response to %s: size of response == 0",connection->client); connection->first_response_sent = 1; } else { send_ret = send_to_socket(watcher->fd,global_config.response,(size_t)global_config.response_len-1); } if(send_ret <= 0){ rdlog(LOG_ERR,"Cannot send first response to %s socket: %s", connection->client, mystrerror(errno,errbuf,ERROR_BUFFER_SIZE)); close_socket_and_stop_watcher(loop,watcher); } rdlog(LOG_DEBUG,"first response ok"); connection->first_response_sent = 1; } }
int CHttpRequestHandler::send_from_socket_to_another(int from, int to) { char buffer[4*1024]; int size = receive_from_socket(from, buffer, sizeof(buffer), true); if(size==0) { //syslog(0,"send_from_socket_to_another(from=%d, to=%d): close socket", from, to); return -1; } else if(size<0 && errno!=EAGAIN) { //syslog(0,"send_from_socket_to_another(from=%d, to=%d): close socket %s", from, to, strerror(errno)); return -1; } else if(size>0) { return send_to_socket(to, buffer, size); } return 0; }
void CHttpRequestHandler::handle() { char buffer[1024]; size_t size; string line=""; char lastchar=0; clearParsetHeaderInfo(); while((size=receive_from_socket(socket, buffer, sizeof(buffer)))>0) { for(int i=0;i<size;i++) { if(this->getLine2(line, lastchar, buffer[i])) { if(line=="exit"||line=="quit"||line=="bye") { return; } else if(line.find("GET ")==0) { printf("Get=%s\n", line.c_str()); get_line = line; } else if(line.find("POST ")==0) { printf("Post=%s\n", line.c_str()); post_line = line; } else if(line.find("Content-Length: ")==0) { printf("%s\n", line.c_str()); if(sscanf(line.c_str() ,"%*s %d%*s", &content_length)) { printf("Content-Length parsed. length = %u\n", content_length); } } else if(line.find("Host: ")==0) { host=line.substr(6); printf("Host: %s\n", host.c_str()); } else if(line.find("Range: ")==0) { range=line.substr(7); printf("Range: %s\n", range.c_str()); } else if (line.empty()) //http header handled { printf("Handle http request\n"); if(content_length>0) { size_t content_received=0; char c_buf[1024]; while(content_received<content_length) { size_t s = receive_from_socket(socket, c_buf, sizeof(c_buf)); content_received+=s; if(s>0) { content<<string(c_buf, s); } else { printf("Connection lost during content fetching\n"); return; } } printf("Content received from client successfully\n"); } handleGetPost(); printf("Handle http request end\n"); clearParsetHeaderInfo(); printf("END"); return; } line=""; } } } return; }
bool CHttpRequestHandler::handleStreamFile(string file) { if(file.length()<2) return false; string link = CVdrLinks::instance()->getLink(file.substr(1)); if(link.empty()==false) { //string channel = file.substr(1,index-1); printf("VDR Stream: link %s\n", link.c_str()); if(link.empty()) return false; stringstream pid_file; pid_file<<"pid_"<<this; stringstream cmd; cmd<<"/bin/sh stream_script.sh "<<link<<" "<<pid_file.str()<<" "<<params["quality"]; printf("Open pipe\n"); FILE *pipef = popen(cmd.str().c_str(), "r"); int d = fileno(pipef); fcntl(d, F_SETFL, O_NONBLOCK); printf("pipe on auki \n"); char buffer[1024*16]; int loop=0; ssize_t min=99999, max=0, k=0,kt=0; ssize_t i=0; while(true) { ssize_t r = read(d, buffer, sizeof(buffer)); if (r == -1 && errno == EAGAIN) { loop++; usleep(1000); } else if (r > 0) { loop=0; if(send_to_socket(socket, buffer, r)<=0) break; if(min>r) min = r; if(max<r) max = r; i++; kt+=r; if(i==1000) { i=0; k=kt/1000; kt=0; } } else { break; } if(loop>10000) break; int size = receive_from_socket(socket, buffer, sizeof(buffer), true); if(size==0) { break; } else if(size<0 && errno!=EAGAIN) { break; } else if(size>0) { printf("Saatu jottain???\n"); } } printf("min %u, max%u, k=%u\n", (unsigned int)min,(unsigned int)max,(unsigned int)k); cmd.str(""); cmd<<"kill `cat "<<pid_file.str()<<"` ; rm "<<pid_file.str(); system(cmd.str().c_str()); pclose(pipef); //kill(pid, SIGKILL); //system(cmd.str().c_str()); printf("Pipe close\n"); return true; } return false; }