博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
论肖战$\not=$孙笑川
阅读量:4173 次
发布时间:2019-05-26

本文共 8087 字,大约阅读时间需要 26 分钟。

证明:

以此文纪念我被爆破的粮仓

附录:此篇文章发表后一个月被举报了。我想问问肖战的粉丝:看看你们的所作所为,你们有什么资格举报?!

再附录:审核人员请看仔细,违规内容已经删除。

#include "csapp.h"void doit(int fd);void read_requesthdrs(rio_t *rp);int parse_uri(char *uri, char *filename, char *cgiargs);void serve_static(int fd, char *filename, int filesize);void get_filetype(char *filename, char *filetype);void serve_dynamic(int fd, char *filename, char *cgiargs);void clienterror(int fd, char *cause, char *errnum,    char *shortmsg, char *longmsg);void echo(int connfd);int main(int argc, char **argv){
int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; /* Check command line args */ if (argc != 2) {
fprintf(stderr, "usage: %s
\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); while (1) {
clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //line:netp:tiny:accept Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("Accepted connection from (%s, %s)\n", hostname, port); echo(connfd); Close(connfd); //line:netp:tiny:close }}void echo(int connfd) {
size_t n; char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
if (strcmp(buf, "\r\n") == 0) break; Rio_writen(connfd, buf, n); }}/* * doit - handle one HTTP request/response transaction */void doit(int fd){
int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio; /* Read request line and headers */ Rio_readinitb(&rio, fd); if (!Rio_readlineb(&rio, buf, MAXLINE)) //line:netp:doit:readrequest return; printf("%s", buf); sscanf(buf, "%s %s %s", method, uri, version); //line:netp:doit:parserequest if (strcasecmp(method, "GET")) {
//line:netp:doit:beginrequesterr clienterror(fd, method, "501", "Not Implemented", "Tiny does not implement this method"); return; } //line:netp:doit:endrequesterr read_requesthdrs(&rio); //line:netp:doit:readrequesthdrs /* Parse URI from GET request */ is_static = parse_uri(uri, filename, cgiargs); //line:netp:doit:staticcheck if (stat(filename, &sbuf) < 0) {
//line:netp:doit:beginnotfound clienterror(fd, filename, "404", "Not found", "Tiny couldn't find this file"); return; } //line:netp:doit:endnotfound if (is_static) {
/* Serve static content */ if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) {
//line:netp:doit:readable clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't read the file"); return; } serve_static(fd, filename, sbuf.st_size); //line:netp:doit:servestatic } else {
/* Serve dynamic content */ if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) {
//line:netp:doit:executable clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program"); return; } serve_dynamic(fd, filename, cgiargs); //line:netp:doit:servedynamic }}/* * read_requesthdrs - read HTTP request headers */void read_requesthdrs(rio_t *rp){
char buf[MAXLINE]; Rio_readlineb(rp, buf, MAXLINE); printf("%s", buf); while(strcmp(buf, "\r\n")) {
//line:netp:readhdrs:checkterm Rio_readlineb(rp, buf, MAXLINE); printf("%s", buf); } return;}/* * parse_uri - parse URI into filename and CGI args * return 0 if dynamic content, 1 if static */int parse_uri(char *uri, char *filename, char *cgiargs){
char *ptr; if (!strstr(uri, "cgi-bin")) {
/* Static content */ //line:netp:parseuri:isstatic strcpy(cgiargs, ""); //line:netp:parseuri:clearcgi strcpy(filename, "."); //line:netp:parseuri:beginconvert1 strcat(filename, uri); //line:netp:parseuri:endconvert1 if (uri[strlen(uri)-1] == '/') //line:netp:parseuri:slashcheck strcat(filename, "home.html"); //line:netp:parseuri:appenddefault return 1; } else {
/* Dynamic content */ //line:netp:parseuri:isdynamic ptr = index(uri, '?'); //line:netp:parseuri:beginextract if (ptr) {
strcpy(cgiargs, ptr+1); *ptr = '\0'; } else strcpy(cgiargs, ""); //line:netp:parseuri:endextract strcpy(filename, "."); //line:netp:parseuri:beginconvert2 strcat(filename, uri); //line:netp:parseuri:endconvert2 return 0; }}/* * serve_static - copy a file back to the client */void serve_static(int fd, char *filename, int filesize){
int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); //line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); //line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sConnection: close\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); //line:netp:servestatic:endserve printf("Response headers:\n"); printf("%s", buf); /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); //line:netp:servestatic:open srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);//line:netp:servestatic:mmap Close(srcfd); //line:netp:servestatic:close Rio_writen(fd, srcp, filesize); //line:netp:servestatic:write Munmap(srcp, filesize); //line:netp:servestatic:munmap}/* * get_filetype - derive file type from file name */void get_filetype(char *filename, char *filetype){
if (strstr(filename, ".html")) strcpy(filetype, "text/html"); else if (strstr(filename, ".gif")) strcpy(filetype, "image/gif"); else if (strstr(filename, ".png")) strcpy(filetype, "image/png"); else if (strstr(filename, ".jpg")) strcpy(filetype, "image/jpeg"); else strcpy(filetype, "text/plain");}/* * serve_dynamic - run a CGI program on behalf of the client */void serve_dynamic(int fd, char *filename, char *cgiargs){
char buf[MAXLINE], *emptylist[] = {
NULL }; /* Return first part of HTTP response */ sprintf(buf, "HTTP/1.0 200 OK\r\n"); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Server: Tiny Web Server\r\n"); Rio_writen(fd, buf, strlen(buf)); if (Fork() == 0) {
/* Child */ //line:netp:servedynamic:fork /* Real server would set all CGI vars here */ setenv("QUERY_STRING", cgiargs, 1); //line:netp:servedynamic:setenv Dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */ //line:netp:servedynamic:dup2 Execve(filename, emptylist, environ); /* Run CGI program */ //line:netp:servedynamic:execve } Wait(NULL); /* Parent waits for and reaps child */ //line:netp:servedynamic:wait}/* * clienterror - returns an error message to the client * id clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg){ char buf[MAXLINE], body[MAXBUF]; /* Build the HTTP response body */ sprintf(body, "
Tiny Error"); sprintf(body, "%s\r\n", body); sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg); sprintf(body, "%s

%s: %s\r\n", body, longmsg, cause); sprintf(body, "%s


The Tiny Web server\r\n", body); /* Print the HTTP response */ sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Content-type: text/html\r\n"); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body)); Rio_writen(fd, buf, strlen(buf)); Rio_writen(fd, body, strlen(body));}

转载地址:http://luwai.baihongyu.com/

你可能感兴趣的文章
页面静态化处理-必须利用URL重写规则
查看>>
[No00000A]计算机的存储单位
查看>>
NYOJ——301递推求值(矩阵快速幂)
查看>>
[洛谷P3384]【模板】树链剖分
查看>>
java的基础概念(1)
查看>>
卸载CentOS7-x64自带的OpenJDK并安装Sun的JDK7的方法
查看>>
[Node.js] 基于Socket.IO 的私聊
查看>>
poj 1287 Networking (最小生成树Kruskal算法)
查看>>
w3c 学习html DOM
查看>>
Socket连接、TCP、UDP、HTTP连接详解
查看>>
linux权限设置
查看>>
python 10 days
查看>>
解决tomcat启东时一闪而过的问题
查看>>
C# 参考之上下文关键字get、set、value、partial、where和yield
查看>>
c网购物车流程图
查看>>
油豆腐
查看>>
MySql排序,去重,表复制,数据导入导出
查看>>
[react native] Error loading page
查看>>
Win10更新补丁失败后出现无法更新正在撤销 解决办法
查看>>
Kindle一周使用感受
查看>>