// write SQInteger file_put_contents(HSQUIRRELVM v) { // function parameters const SQChar* filename; const SQChar* contents; // internal structures apr_file_t* file; apr_size_t nbytes; apr_status_t status; request_rec* r = get_request_rec(v); // for error messages char error[120]; char errorMessage[120]; ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "file_put_contents()"); // grab filename param if( sq_gettype(v, 2) != OT_STRING ) { return SQ_ERROR; } else if( SQ_FAILED(sq_getstring(v, 2, &filename)) ) { return SQ_ERROR; } // grab contents param if( sq_gettype(v, 3) != OT_STRING ) { return SQ_ERROR; } else if( SQ_FAILED(sq_getstring(v, 3, &contents)) ) { return SQ_ERROR; } nbytes = strlen(contents); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_put_contents('%s', '%s') %d", filename, contents, (int)nbytes); if( (status = apr_file_open(&file, filename, APR_WRITE|APR_CREATE, APR_OS_DEFAULT, r->pool)) != APR_SUCCESS ) { apr_strerror(status, error, sizeof error); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_put_contents() failed: %s", error); sprintf(errorMessage, "file_put_contents() failed: %s", error); errorfunc(v, errorMessage); sq_pushbool(v, SQFalse); } else if( (status = apr_file_write(file, contents, &nbytes)) != APR_SUCCESS ) { apr_strerror(status, error, sizeof error); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_put_contents() failed: %s", error); sprintf(errorMessage, "file_put_contents() failed: %s", error); errorfunc(v, errorMessage); sq_pushbool(v, SQFalse); } else { sq_pushinteger(v, nbytes); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_put_contents() wrote %d bytes", (int)nbytes); } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "file_put_contents() returning 1"); return 1; }
// read SQInteger file_get_contents(HSQUIRRELVM v) { // function parameters const SQChar* filename; char* contents; // internal structures apr_file_t* file; apr_finfo_t finfo; apr_size_t nbytes; apr_status_t status; request_rec* r = get_request_rec(v); // for error messages char error[120]; char errorMessage[120]; ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "file_get_contents()"); // grab filename param if( sq_gettype(v, 2) != OT_STRING ) { return SQ_ERROR; } else if( SQ_FAILED(sq_getstring(v, 2, &filename)) ) { return SQ_ERROR; } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_get_contents('%s')", filename); if( (status = apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, r->pool)) != APR_SUCCESS ) { apr_strerror(status, error, sizeof error); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_get_contents() failed: %s", error); sprintf(errorMessage, "file_get_contents() failed: %s", error); errorfunc(v, errorMessage); sq_pushbool(v, SQFalse); return 1; } // figure out file size and allocate enough room to read it all in status = apr_file_info_get(&finfo, APR_FINFO_NORM, file); nbytes = finfo.size; ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "file_get_contents() file is %d", (int)nbytes); contents = apr_palloc(r->pool, finfo.size); if( (status = apr_file_read(file, contents, &nbytes)) != APR_SUCCESS ) { apr_strerror(status, error, sizeof error); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_get_contents() failed: %s", error); sprintf(errorMessage, "file_get_contents() read %d bytes, but failed: %s", (int)nbytes, error); errorfunc(v, errorMessage); sq_pushbool(v, SQFalse); } else { contents[nbytes] = '\0'; sq_pushstring(v, contents, -1); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " file_get_contents() read %d bytes", (int)nbytes); } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "file_get_contents() returning 1"); return 1; }
int main(int argc,char **argv) { printf("Http server welcome you!\n"); tpool_create(10); if(1!=argc) getoption(argc,argv);//It's hard to learn how to use it if(NULL==_log) logfd=open(DEFAULTLOG,O_WRONLY | O_APPEND | O_CREAT); else logfd=open(_log,O_WRONLY | O_CREAT | O_APPEND); daytime(); int sockfd,sockfds; if(daemon_check) daemons(); ssl_init(ctx); signal(SIGPIPE,SIG_IGN); signal(SIGCHLD, SIG_IGN); sockfd=make_socket(sockfd); sockfds=make_socket_ssl(sockfds); if(sockfd<0||sockfds<0) errorfunc("sockfd error!"); int addrlen = 1; setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&addrlen,sizeof(addrlen));//set the port quickly reuse setsockopt(sockfds,SOL_SOCKET,SO_REUSEADDR,&addrlen,sizeof(addrlen)); struct epoll_event events[MAXEVENTS];//question int epollfd=epoll_create(MAXEVENTS); addfd(epollfd,sockfd,0); addfd(epollfd,sockfds,0); chdir("/home/wangyao/web"); while(1) { int ret=epoll_wait(epollfd,events,MAXEVENTS,-1);//epoll func should be use in here if(ret<0) errorfunc("epoll_wait error!"); lt(events,ret,epollfd,sockfd,sockfds); } close(sockfds); close(sockfd); close(epollfd); sleep(10); tpool_destroy(); SSL_CTX_free(ctx); exit(0); }
// unlink SQInteger sq_unlink(HSQUIRRELVM v) { const SQChar* filename; apr_status_t status; request_rec* r = get_request_rec(v); char errorMessage[120]; char error[120]; ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "unlink()"); // grab function param if( sq_gettype(v, 2) != OT_STRING ) { return SQ_ERROR; } if( SQ_FAILED(sq_getstring(v, 2, &filename)) ) { return SQ_ERROR; } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " unlink(%s)", filename); status = apr_file_remove(filename, r->pool); if( status ) { apr_strerror(status, error, sizeof error); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " unlink failed: %s", error); sprintf(errorMessage, "unlink failed: %s", error); errorfunc(v, errorMessage); sq_pushbool(v, SQFalse); } else { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, " unlink() worked"); sq_pushbool(v, SQTrue); } ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "unlink() returning 1"); return 1; }