static HttpSession *httpserver_session_new(HttpServer *server,int fd) { HttpSession *session=NULL; session=malloc(sizeof(HttpSession)); bzero(session,sizeof(HttpSession) ); queue_init(&session->request.headers); session->fd=fd; session->server=server; session->request.buff=buff_new(1024,0); session->ev=event_fd_new( fd, EPOLLIN, httpsession_read, session); session->request.prase_status=HPARSE_FIRST_LINE; queue_insert_head(&server->sessions,&session->node); return session; }
static ex_t display_words(bfpath *bfp, int argc, char **argv, bool show_probability) { byte buf[BUFSIZE]; buff_t *buff = buff_new(buf, 0, BUFSIZE); const byte *word; const char *path = bfp->filepath; const char *head_format = !show_probability ? "%-30s %6s %6s\n" : "%-30s %6s %6s %6s\n"; const char *data_format = !show_probability ? "%-30s %6lu %6lu\n" : "%-30s %6lu %6lu %f\n"; void *dsh = NULL; /* initialize to silence bogus gcc warning */ void *dbe; int rv = 0; ex_t ec = EX_OK; dsv_t msgcnts; /* protect against broken stat(2) that succeeds for empty names */ if (path == NULL || *path == '\0') { fprintf(stderr, "Expecting non-empty directory or file name.\n"); return EX_ERROR; } dbe = ds_init(bfp); dsh = ds_open(dbe, bfp, DS_READ);; if (dsh == NULL) /* print error, cleanup, and exit */ ds_open_failure(bfp, dbe); if (DST_OK != ds_txn_begin(dsh)) { ds_close(dsh); ds_cleanup(dbe); fprintf(stderr, "Cannot begin transaction.\n"); return EX_ERROR; } if (show_probability) { ds_get_msgcounts(dsh, &msgcnts); robs = ROBS; robx = ROBX; } fprintf(fpo, head_format, "", "spam", "good", " Fisher"); while (argc >= 0) { dsv_t val; word_t *token; int rc; unsigned long spam_count; unsigned long good_count; double rob_prob = 0.0; if (argc == 0) { if (get_token(buff, stdin) != 0) break; token = &buff->t; } else { word = (const byte *) *argv++; if (--argc == 0) argc = -1; token = word_news((const char *)word); } rc = ds_read(dsh, token, &val); switch (rc) { case 0: spam_count = val.spamcount; good_count = val.goodcount; if (!show_probability) fprintf(fpo, data_format, token->u.text, spam_count, good_count); else { rob_prob = calc_prob(good_count, spam_count, msgcnts.goodcount, msgcnts.spamcount); fprintf(fpo, data_format, token->u.text, spam_count, good_count, rob_prob); } break; case 1: break; default: fprintf(stderr, "Cannot read from database.\n"); ec = EX_ERROR; goto finish; } if (token != &buff->t) word_free(token); } finish: if (DST_OK != rv ? ds_txn_abort(dsh) : ds_txn_commit(dsh)) { fprintf(stderr, "Cannot %s transaction.\n", rv ? "abort" : "commit"); ec = EX_ERROR; } ds_close(dsh); ds_cleanup(dbe); buff_free(buff); return ec; }
int request_file(HttpSession *session,char *rpath) { int fd; const char *mime=NULL; char *bname=NULL; char *full_fath=NULL; size_t path_len; unsigned long filesize=0; if(rpath==NULL) { path_len=strlen(session->request.path)+strlen(session->server->www_folder)+20; full_fath=malloc(path_len); sprintf(full_fath,"%s%s",session->server->www_folder,session->request.path); } else { full_fath=strdup(rpath); } //printf("mime:%s\n",mime); if(is_file_dir(full_fath) == 1 && enum_index(full_fath)==-1) { TraceErr("path %s has no inde.* ,return 404 !\n",full_fath); return 404; } TraceImport("request path:%s\n",full_fath); filesize=get_file_size((const char*)full_fath); if( filesize <= 0) { TraceErr("get_file_size %s fial,return 404 not found!\n",full_fath); return 404; } fd=open((const char *)full_fath,O_RDONLY|O_NONBLOCK); if(fd < 0 ) { TraceErr("open %s fial,return 404 not found!\n",full_fath); return 404; } TraceImport("open %s OK,size:%u !\n",full_fath,filesize); //path_len=get_file_size(full_fath); bname=basename(full_fath); mime=get_mime(strrchr(bname,'.')); if(mime!=NULL ) { session->response=buff_new(1500,0); buff_printf(session->response,"HTTP/1.0 200 OK\r\n"); buff_printf(session->response,"Server:whtc123\r\n"); buff_printf(session->response,"Content-type:%s\r\n",mime); buff_printf(session->response,"Content-length:%d; charset=GBK\r\n",filesize); buff_printf(session->response,"\r\n"); } else { } session->file_fd=fd; event_fd_setcallback(session->ev,httpsession_file_write); free(full_fath); return 200; }