main() { void** gc; errno = 0; if (-1 == mkdir(RUN_PATH, 00700)) { ASSERT(errno != EEXIST); errno = 0; } //printf("Content-type: text/plain\n\n"); /* int i = 0; char** lstEnv = getEnv(); for (; lstEnv[i]; ++i) { TRACE("%s\n", lstEnv[i]); } */ const char* http_host = getenv("HTTP_HOST"); ASSERT(http_host == NULL); char** lstArgs = lstSplitStr('.', http_host); ASSERT(lstArgs == NULL); int arg_c = lstSize(lstArgs); ASSERT(arg_c < 2); if (arg_c == 2) { const char* request_uri = getenv("REQUEST_URI"); ASSERT(request_uri == NULL); char* t = strchr(request_uri + 1, '/'); if (t == NULL) { Connect(atol(request_uri + 1)); } else { *t = '\0'; Respond(atol(request_uri + 1), atol(t + 1)); } } else { Pier(atol(lstArgs[0])); } mapFree(lstArgs); return 0; }
/* * This will handle connection for each client * */ void *connection_handler(void *socket_desc) { //Get the socket descriptor int sock = *(int*)socket_desc; int read_size; char client_message[2000]; while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) { Respond(sock, client_message, read_size); } if(read_size == 0) { puts("Client disconnected"); fflush(stdout); } return 0; }
void Loop(){ struct epoll_event *events; struct sockaddr_in srv; struct epoll_event ev; int listenfd; int epfd; int clifd; int i; int res; char buffer[1024]; int n; events = (struct epoll_event *)calloc(MAX_CLIENT, sizeof(struct epoll_event)); if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("error opening socket\n"); exit(1); } bzero(&srv, sizeof(srv)); srv.sin_family = AF_INET; srv.sin_addr.s_addr = INADDR_ANY; srv.sin_port = htons(PORT); if( bind(listenfd, (struct sockaddr *) &srv, sizeof(srv)) < 0) { perror("error binding to socket\n"); exit(1); } listen(listenfd, 1024); int reuse_addr = 1; /* Used so we can re-bind to our port */ /* So that we can re-bind to it without TIME_WAIT problems */ setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof (reuse_addr)); epfd = epoll_create(MAX_CLIENT); if(!epfd) { perror("epoll_create\n"); exit(1); } ev.events = EPOLLIN | EPOLLERR | EPOLLHUP; ev.data.fd = listenfd; if(epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev) < 0) { perror("epoll_ctl, failed to add listenfd\n"); exit(1); } for( ; ; ) { usleep(8000); res = epoll_wait(epfd, events, MAX_CLIENT, 0); for(i = 0; i < res; i++) { if(events[i].data.fd == listenfd) { clifd = accept(listenfd, NULL, NULL); if(clifd > 0) { printf("."); nonblock(clifd); ev.events = EPOLLIN | EPOLLET; ev.data.fd = clifd; if(epoll_ctl(epfd, EPOLL_CTL_ADD, clifd, &ev) < 0) { perror("epoll_ctl ADD\n"); exit(1); } } } else { /* Handle All this in the thread */ n = recv(events[i].data.fd, buffer, 1023, 0); if(n == 0) /* closed connection */ { epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL); } else if(n < 0) /* error, close connection */ { //epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL); close(events[i].data.fd); } else { /* got a request, process it. */ //send(events[i].data.fd, buffer, n, 0); //bzero(&buffer, n); //printf("%d data received: \n%s\n", events[i].data.fd, buffer); //send(events[i].data.fd, response, y, 0); //close(events[i].data.fd); Respond(events[i].data.fd, buffer, n); } } } } }
std::string WorkSpace::Respond(const std::string& query) { const char* cquery = query.c_str(); std::cmatch match; try { if ( std::regex_match( cquery, match, std::regex("\\s*") ) ) { return query; } else if ( std::regex_match( cquery, match, std::regex("\\s*check\\s+file\\s+(.+)\\s*") ) ) { if (access(std::string(match[1]).c_str(), F_OK ) == -1) { throw FIException(142142, "File with name " + std::string(match[1]) + " does not exits."); } if (CheckFile(match[1])) { return "File hasn't changed."; } else { return "File has changed."; } } else if ( std::regex_match( cquery, match, std::regex("\\s*add\\s+file\\s+(.+)\\s*") ) ) { if (access(std::string(match[1]).c_str(), F_OK ) == -1) { throw FIException(171142, "File with name " +std::string(match[1]) + " does not exits."); } pthread_mutex_lock(&m); AddFileToInotify(std::string(match[1])); // inotify_add_watch(inotify, std::string(match[1]).c_str(), IN_ALL_EVENTS); AddCheckSum(match[1]); pthread_mutex_unlock(&m); } else if ( std::regex_match( cquery, match, std::regex("\\s*delete\\s+file\\s+(.+)\\s*") ) ) { pthread_mutex_lock(&m); int wd = ind_by_files_table.Select(std::string(match[1])); logger << wd; ind_by_files_table.Delete(std::string(match[1])); files_by_ind_table.Delete(wd); checksum_table.Delete(std::string(match[1])); inotify_rm_watch(inotify, wd); pthread_mutex_unlock(&m); } else if ( std::regex_match( cquery, match, std::regex("\\s*update\\s+file\\s+(.+)\\s*") ) ) { if (access(std::string(match[1]).c_str(), F_OK ) == -1) { throw FIException(171142, "File with name " + std::string(match[1]) + " does not exits."); } AddCheckSum(match[1]); return "Ok"; } else if ( std::regex_match( cquery, match, std::regex("\\s*add\\s+files\\s+(.+)\\s*") ) ) { ExecuteHandler ex(" find / | grep -E \'" + std::string(match[1]) + "\'"); std::string file_name; std::string files; while (ex >> file_name) { files += file_name + " : " + Respond("add file " + file_name) + "\n"; } return files; } else if ( std::regex_match( cquery, match, std::regex("\\s*help\\s*") ) ) { return std::string("Queries:\n") +"\tadd file <filename>\n" + "\tdelete file <filename>\n" + "\tcheck file <filname>\n" + "\tupdate file <filename>\n" + "\tadd files <regexp>"; } else { return "Incorrent query."; } } catch (std::exception& e) {