登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
登录
注册
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
1
Star
0
Fork
0
20191205
/
zx代码
代码
Issues
19
Pull Requests
0
Wiki
统计
流水线
服务
JavaDoc
PHPDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
webserv.c
待办的
#I4KS5A
20191205
拥有者
创建于
2021-12-02 13:56
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> int main(int ac, char *av[]) { int sock, fd; FILE *fpin; char request[BUFSIZ]; if ( ac == 1 ){ fprintf(stderr,"usage: ws portnum\n"); exit(1); } sock = make_server_socket( atoi(av[1]) ); if ( sock == -1 ) exit(2); /* main loop here */ while(1){ /* take a call and buffer it */ fd = accept( sock, NULL, NULL ); fpin = fdopen(fd, "r" ); /* read request */ fgets(request,BUFSIZ,fpin); printf("got a call: request = %s", request); read_til_crnl(fpin); /* do what client asks */ process_rq(request, fd); fclose(fpin); } } /* ------------------------------------------------------ * read_til_crnl(FILE *) skip over all request info until a CRNL is seen ------------------------------------------------------ */ read_til_crnl(FILE *fp) { char buf[BUFSIZ]; while( fgets(buf,BUFSIZ,fp) != NULL && strcmp(buf,"\r\n") != 0 ) ; } /* ------------------------------------------------------ * process_rq( char *rq, int fd ) do what the request asks for and write reply to fd handles request in a new process rq is HTTP command: GET /foo/bar.html HTTP/1.0 ------------------------------------------------------ */ process_rq( char *rq, int fd ) { char cmd[BUFSIZ], arg[BUFSIZ]; /* create a new process and return if not the child */ if ( fork() != 0 ) return; strcpy(arg, "./"); /* precede args with ./ */ if ( sscanf(rq, "%s%s", cmd, arg+2) != 2 ) return; if ( strcmp(cmd,"GET") != 0 ) cannot_do(fd); else if ( not_exist( arg ) ) do_404(arg, fd ); else if ( isadir( arg ) ) do_ls( arg, fd ); else if ( ends_in_cgi( arg ) ) do_exec( arg, fd ); else do_cat( arg, fd ); } /* ------------------------------------------------------ * the reply header thing: all functions need one if content_type is NULL then don't send content type ------------------------------------------------------ */ header( FILE *fp, char *content_type ) { fprintf(fp, "HTTP/1.0 200 OK\r\n"); if ( content_type ) fprintf(fp, "Content-type: %s\r\n", content_type ); } /* ------------------------------------------------------ * simple functions first: cannot_do(fd) unimplemented HTTP command and do_404(item,fd) no such object ------------------------------------------------------ */ cannot_do(int fd) { FILE *fp = fdopen(fd,"w"); fprintf(fp, "HTTP/1.0 501 Not Implemented\r\n"); fprintf(fp, "Content-type: text/plain\r\n"); fprintf(fp, "\r\n"); fprintf(fp, "That command is not yet implemented\r\n"); fclose(fp); } do_404(char *item, int fd) { FILE *fp = fdopen(fd,"w"); fprintf(fp, "HTTP/1.0 404 Not Found\r\n"); fprintf(fp, "Content-type: text/plain\r\n"); fprintf(fp, "\r\n"); fprintf(fp, "The item you requested: %s\r\nis not found\r\n", item); fclose(fp); } /* ------------------------------------------------------ * the directory listing section isadir() uses stat, not_exist() uses stat do_ls runs ls. It should not ------------------------------------------------------ */ isadir(char *f) { struct stat info; return ( stat(f, &info) != -1 && S_ISDIR(info.st_mode) ); } not_exist(char *f) { struct stat info; return( stat(f,&info) == -1 ); } do_ls(char *dir, int fd) { FILE *fp ; fp = fdopen(fd,"w"); header(fp, "text/plain"); fprintf(fp,"\r\n"); fflush(fp); dup2(fd,1); dup2(fd,2); close(fd); execlp("ls","ls","-l",dir,NULL); perror(dir); exit(1); } /* ------------------------------------------------------ * the cgi stuff. function to check extension and one to run the program. ------------------------------------------------------ */ char * file_type(char *f) /* returns 'extension' of file */ { char *cp; if ( (cp = strrchr(f, '.' )) != NULL ) return cp+1; return ""; } ends_in_cgi(char *f) { return ( strcmp( file_type(f), "cgi" ) == 0 ); } do_exec( char *prog, int fd ) { FILE *fp ; fp = fdopen(fd,"w"); header(fp, NULL); fflush(fp); dup2(fd, 1); dup2(fd, 2); close(fd); execl(prog,prog,NULL); perror(prog); } /* ------------------------------------------------------ * do_cat(filename,fd) sends back contents after a header ------------------------------------------------------ */ do_cat(char *f, int fd) { char *extension = file_type(f); char *content = "text/plain"; FILE *fpsock, *fpfile; int c; if ( strcmp(extension,"html") == 0 ) content = "text/html"; else if ( strcmp(extension, "gif") == 0 ) content = "image/gif"; else if ( strcmp(extension, "jpg") == 0 ) content = "image/jpeg"; else if ( strcmp(extension, "jpeg") == 0 ) content = "image/jpeg"; fpsock = fdopen(fd, "w"); fpfile = fopen( f , "r"); if ( fpsock != NULL && fpfile != NULL ) { header( fpsock, content ); fprintf(fpsock, "\r\n"); while( (c = getc(fpfile) ) != EOF ) putc(c, fpsock); fclose(fpfile); fclose(fpsock); } exit(0); }
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> int main(int ac, char *av[]) { int sock, fd; FILE *fpin; char request[BUFSIZ]; if ( ac == 1 ){ fprintf(stderr,"usage: ws portnum\n"); exit(1); } sock = make_server_socket( atoi(av[1]) ); if ( sock == -1 ) exit(2); /* main loop here */ while(1){ /* take a call and buffer it */ fd = accept( sock, NULL, NULL ); fpin = fdopen(fd, "r" ); /* read request */ fgets(request,BUFSIZ,fpin); printf("got a call: request = %s", request); read_til_crnl(fpin); /* do what client asks */ process_rq(request, fd); fclose(fpin); } } /* ------------------------------------------------------ * read_til_crnl(FILE *) skip over all request info until a CRNL is seen ------------------------------------------------------ */ read_til_crnl(FILE *fp) { char buf[BUFSIZ]; while( fgets(buf,BUFSIZ,fp) != NULL && strcmp(buf,"\r\n") != 0 ) ; } /* ------------------------------------------------------ * process_rq( char *rq, int fd ) do what the request asks for and write reply to fd handles request in a new process rq is HTTP command: GET /foo/bar.html HTTP/1.0 ------------------------------------------------------ */ process_rq( char *rq, int fd ) { char cmd[BUFSIZ], arg[BUFSIZ]; /* create a new process and return if not the child */ if ( fork() != 0 ) return; strcpy(arg, "./"); /* precede args with ./ */ if ( sscanf(rq, "%s%s", cmd, arg+2) != 2 ) return; if ( strcmp(cmd,"GET") != 0 ) cannot_do(fd); else if ( not_exist( arg ) ) do_404(arg, fd ); else if ( isadir( arg ) ) do_ls( arg, fd ); else if ( ends_in_cgi( arg ) ) do_exec( arg, fd ); else do_cat( arg, fd ); } /* ------------------------------------------------------ * the reply header thing: all functions need one if content_type is NULL then don't send content type ------------------------------------------------------ */ header( FILE *fp, char *content_type ) { fprintf(fp, "HTTP/1.0 200 OK\r\n"); if ( content_type ) fprintf(fp, "Content-type: %s\r\n", content_type ); } /* ------------------------------------------------------ * simple functions first: cannot_do(fd) unimplemented HTTP command and do_404(item,fd) no such object ------------------------------------------------------ */ cannot_do(int fd) { FILE *fp = fdopen(fd,"w"); fprintf(fp, "HTTP/1.0 501 Not Implemented\r\n"); fprintf(fp, "Content-type: text/plain\r\n"); fprintf(fp, "\r\n"); fprintf(fp, "That command is not yet implemented\r\n"); fclose(fp); } do_404(char *item, int fd) { FILE *fp = fdopen(fd,"w"); fprintf(fp, "HTTP/1.0 404 Not Found\r\n"); fprintf(fp, "Content-type: text/plain\r\n"); fprintf(fp, "\r\n"); fprintf(fp, "The item you requested: %s\r\nis not found\r\n", item); fclose(fp); } /* ------------------------------------------------------ * the directory listing section isadir() uses stat, not_exist() uses stat do_ls runs ls. It should not ------------------------------------------------------ */ isadir(char *f) { struct stat info; return ( stat(f, &info) != -1 && S_ISDIR(info.st_mode) ); } not_exist(char *f) { struct stat info; return( stat(f,&info) == -1 ); } do_ls(char *dir, int fd) { FILE *fp ; fp = fdopen(fd,"w"); header(fp, "text/plain"); fprintf(fp,"\r\n"); fflush(fp); dup2(fd,1); dup2(fd,2); close(fd); execlp("ls","ls","-l",dir,NULL); perror(dir); exit(1); } /* ------------------------------------------------------ * the cgi stuff. function to check extension and one to run the program. ------------------------------------------------------ */ char * file_type(char *f) /* returns 'extension' of file */ { char *cp; if ( (cp = strrchr(f, '.' )) != NULL ) return cp+1; return ""; } ends_in_cgi(char *f) { return ( strcmp( file_type(f), "cgi" ) == 0 ); } do_exec( char *prog, int fd ) { FILE *fp ; fp = fdopen(fd,"w"); header(fp, NULL); fflush(fp); dup2(fd, 1); dup2(fd, 2); close(fd); execl(prog,prog,NULL); perror(prog); } /* ------------------------------------------------------ * do_cat(filename,fd) sends back contents after a header ------------------------------------------------------ */ do_cat(char *f, int fd) { char *extension = file_type(f); char *content = "text/plain"; FILE *fpsock, *fpfile; int c; if ( strcmp(extension,"html") == 0 ) content = "text/html"; else if ( strcmp(extension, "gif") == 0 ) content = "image/gif"; else if ( strcmp(extension, "jpg") == 0 ) content = "image/jpeg"; else if ( strcmp(extension, "jpeg") == 0 ) content = "image/jpeg"; fpsock = fdopen(fd, "w"); fpfile = fopen( f , "r"); if ( fpsock != NULL && fpfile != NULL ) { header( fpsock, content ); fprintf(fpsock, "\r\n"); while( (c = getc(fpfile) ) != EOF ) putc(c, fpsock); fclose(fpfile); fclose(fpsock); } exit(0); }
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
未关联
master
公文代码
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
1
https://gitee.com/two_thousand_and_thirteen/zx-code.git
git@gitee.com:two_thousand_and_thirteen/zx-code.git
two_thousand_and_thirteen
zx-code
zx代码
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册